Paper

Paper.el(name, attr)

Creates an element on paper with a given name and no attributes

Parameters

  1. name string tag name
  2. attr object attributes

Returns: Element the current element

Usage

var c = paper.circle(10, 10, 10); // is the same as...
var c = paper.el("circle").attr({
    cx: 10,
    cy: 10,
    r: 10
});
// and the same as
var c = paper.el("circle", {
    cx: 10,
    cy: 10,
    r: 10
});

Paper.rect(x, y, width, height, [rx], [ry])

Draws a rectangle

Parameters

  1. x number x coordinate of the top left corner
  2. y number y coordinate of the top left corner
  3. width number width
  4. height number height
  5. rx number horizontal radius for rounded corners, default is 0
  6. ry number vertical radius for rounded corners, default is rx or 0

Returns: object the rect element

Usage

// regular rectangle
var c = paper.rect(10, 10, 50, 50);
// rectangle with rounded corners
var c = paper.rect(40, 40, 50, 50, 10);

Paper.circle(x, y, r)

Draws a circle

Parameters

  1. x number x coordinate of the centre
  2. y number y coordinate of the centre
  3. r number radius

Returns: object the circle element

Usage

var c = paper.circle(50, 50, 40);

Paper.image(src, x, y, width, height)

Places an image on the surface

Parameters

  1. src string URI of the source image
  2. x number x offset position
  3. y number y offset position
  4. width number width of the image
  5. height number height of the image

Returns: object the image element

or

Returns: object Snap element object with type image

Usage

var c = paper.image("apple.png", 10, 10, 80, 80);

Paper.ellipse(x, y, rx, ry)

Draws an ellipse

Parameters

  1. x number x coordinate of the centre
  2. y number y coordinate of the centre
  3. rx number horizontal radius
  4. ry number vertical radius

Returns: object the ellipse element

Usage

var c = paper.ellipse(50, 50, 40, 20);

Paper.path([pathString])

Creates a <path> element using the given string as the path's definition

Parameters

  1. pathString string path string in SVG format

Path string consists of one-letter commands, followed by comma seprarated arguments in numerical form. Example:

"M10,20L30,40"

This example features two commands: M, with arguments (10, 20) and L with arguments (30, 40). Uppercase letter commands express coordinates in absolute terms, while lowercase commands express them in relative terms from the most recently declared coordinates.

Here is short list of commands available, for more details see SVG path string format or article about path strings at MDN.

CommandNameParameters
Mmoveto(x y)+
Zclosepath(none)
Llineto(x y)+
Hhorizontal linetox+
Vvertical linetoy+
Ccurveto(x1 y1 x2 y2 x y)+
Ssmooth curveto(x2 y2 x y)+
Qquadratic Bézier curveto(x1 y1 x y)+
Tsmooth quadratic Bézier curveto(x y)+
Aelliptical arc(rx ry x-axis-rotation large-arc-flag sweep-flag x y)+
RCatmull-Rom curveto*x1 y1 (x y)+

  • Catmull-Rom curveto is a not standard SVG command and added to make life easier.
  • Note: there is a special case when a path consists of only three commands: M10,10R…z. In this case the path connects back to its starting point.

    Usage

    var c = paper.path("M10 10L90 90");
    // draw a diagonal line:
    // move to 10,10, line to 90,90

    Paper.g([varargs])

    Creates a group element

    Parameters

    1. varargs elements to nest within the group

    Returns: object the g element

    Usage

    var c1 = paper.circle(),
        c2 = paper.rect(),
        g = paper.g(c2, c1); // note that the order of elements is different

    or

    var c1 = paper.circle(),
        c2 = paper.rect(),
        g = paper.g();
    g.add(c2, c1);

    Paper.svg(x, y, width, height, vbx, vby, vbw, vbh)

    Creates a nested SVG element.

    Parameters

    1. x number optional X of the element
    2. y number optional Y of the element
    3. width number optional width of the element
    4. height number optional height of the element
    5. vbx number optional viewbox X
    6. vby number optional viewbox Y
    7. vbw number optional viewbox width
    8. vbh number optional viewbox height

    Returns: object the svg element

    Paper.mask()

    Equivalent in behaviour to Paper.g, except it’s a mask.

    Returns: object the mask element

    Paper.ptrn(x, y, width, height, vbx, vby, vbw, vbh)

    Equivalent in behaviour to Paper.g, except it’s a pattern.

    Parameters

    1. x number optional X of the element
    2. y number optional Y of the element
    3. width number optional width of the element
    4. height number optional height of the element
    5. vbx number optional viewbox X
    6. vby number optional viewbox Y
    7. vbw number optional viewbox width
    8. vbh number optional viewbox height

    Returns: object the pattern element

    Paper.use(…)

    Creates a <use> element.

    Parameters

    1. id string optional id of element to link

    or

    Parameters

    1. id Element optional element to link

    Returns: object the use element

    Paper.symbol(vbx, vby, vbw, vbh)

    Creates a <symbol> element.

    Parameters

    1. vbx number optional viewbox X
    2. vby number optional viewbox Y
    3. vbw number optional viewbox width
    4. vbh number optional viewbox height

    Returns: object the symbol element

    Paper.text(x, y, text)

    Draws a text string

    Parameters

    1. x number x coordinate position
    2. y number y coordinate position
    3. text string array The text string to draw or array of strings to nest within separate <tspan> elements

    Returns: object the text element

    Usage

    var t1 = paper.text(50, 50, "Snap");
    var t2 = paper.text(50, 50, ["S","n","a","p"]);
    // Text path usage
    t1.attr({textpath: "M10,10L100,100"});
    // or
    var pth = paper.path("M10,10L100,100");
    t1.attr({textpath: pth});

    Paper.line(x1, y1, x2, y2)

    Draws a line

    Parameters

    1. x1 number x coordinate position of the start
    2. y1 number y coordinate position of the start
    3. x2 number x coordinate position of the end
    4. y2 number y coordinate position of the end

    Returns: object the line element

    Usage

    var t1 = paper.line(50, 50, 100, 100);

    Paper.polyline(…)

    Draws a polyline

    Parameters

    1. points array array of points

    or

    Parameters

    1. varargs points

    Returns: object the polyline element

    Usage

    var p1 = paper.polyline([10, 10, 100, 100]);
    var p2 = paper.polyline(10, 10, 100, 100);

    Paper.gradient(gradient)

    Creates a gradient element

    Parameters

    1. gradient string gradient descriptor

    Gradient Descriptor

    The gradient descriptor is an expression formatted as follows: <type>(<coords>)<colors>. The <type> can be either linear or radial. The uppercase L or R letters indicate absolute coordinates offset from the SVG surface. Lowercase l or r letters indicate coordinates calculated relative to the element to which the gradient is applied. Coordinates specify a linear gradient vector as x1, y1, x2, y2, or a radial gradient as cx, cy, r and optional fx, fy specifying a focal point away from the center of the circle. Specify <colors> as a list of dash-separated CSS color values. Each color may be followed by a custom offset value, separated with a colon character.

    Examples

    Linear gradient, relative from top-left corner to bottom-right corner, from black through red to white:

    var g = paper.gradient("l(0, 0, 1, 1)#000-#f00-#fff");

    Linear gradient, absolute from (0, 0) to (100, 100), from black through red at 25% to white:

    var g = paper.gradient("L(0, 0, 100, 100)#000-#f00:25-#fff");

    Radial gradient, relative from the center of the element with radius half the width, from black to white:

    var g = paper.gradient("r(0.5, 0.5, 0.5)#000-#fff");

    To apply the gradient:

    paper.circle(50, 50, 40).attr({
        fill: g
    });

    Returns: object the gradient element

    Paper.toString()

    Returns SVG code for the Paper

    Returns: string SVG code for the Paper

    Paper.toDataURL()

    Returns SVG code for the Paper as Data URI string.

    Returns: string Data URI string

    Paper.clear()

    Removes all child nodes of the paper, except <defs>.

    Paper.filter(filstr)

    Creates a <filter> element

    Parameters

    1. filstr string SVG fragment of filter provided as a string

    Returns: object Element

    Note: It is recommended to use filters embedded into the page inside an empty SVG element.

    Usage

    var f = paper.filter(''),
        c = paper.circle(10, 10, 10).attr({
            filter: f
        });