Snap

Snap(…)

Creates a drawing surface or wraps existing SVG element.

Parameters

  1. width number string width of surface
  2. height number string height of surface

or

Parameters

  1. DOM SVGElement element to be wrapped into Snap structure

or

Parameters

  1. array array array of elements (will return set of elements)

or

Parameters

  1. query string CSS query selector

Returns: object Element

Snap.format(token, json)

Replaces construction of type {<name>} to the corresponding argument

Parameters

  1. token string string to format
  2. json object object which properties are used as a replacement

Returns: string formatted string

Usage

// this draws a rectangular shape equivalent to "M10,20h40v50h-40z"
paper.path(Snap.format("M{x},{y}h{dim.width}v{dim.height}h{dim['negative width']}z", {
    x: 10,
    y: 20,
    dim: {
        width: 40,
        height: 50,
        "negative width": -40
    }
}));

Snap.rad(deg)

Transform angle to radians

Parameters

  1. deg number angle in degrees

Returns: number angle in radians

Snap.deg(rad)

Transform angle to degrees

Parameters

  1. rad number angle in radians

Returns: number angle in degrees

Snap.sin(angle)

Equivalent to Math.sin() only works with degrees, not radians.

Parameters

  1. angle number angle in degrees

Returns: number sin

Snap.tan(angle)

Equivalent to Math.tan() only works with degrees, not radians.

Parameters

  1. angle number angle in degrees

Returns: number tan

Snap.cos(angle)

Equivalent to Math.cos() only works with degrees, not radians.

Parameters

  1. angle number angle in degrees

Returns: number cos

Snap.asin(num)

Equivalent to Math.asin() only works with degrees, not radians.

Parameters

  1. num number value

Returns: number asin in degrees

Snap.acos(num)

Equivalent to Math.acos() only works with degrees, not radians.

Parameters

  1. num number value

Returns: number acos in degrees

Snap.atan(num)

Equivalent to Math.atan() only works with degrees, not radians.

Parameters

  1. num number value

Returns: number atan in degrees

Snap.atan2(num)

Equivalent to Math.atan2() only works with degrees, not radians.

Parameters

  1. num number value

Returns: number atan2 in degrees

Snap.angle(x1, y1, x2, y2, [x3], [y3])

Returns an angle between two or three points

Parameters

Parameters

  1. x1 number x coord of first point
  2. y1 number y coord of first point
  3. x2 number x coord of second point
  4. y2 number y coord of second point
  5. x3 number x coord of third point
  6. y3 number y coord of third point

Returns: number angle in degrees

Snap.len(x1, y1, x2, y2)

Returns distance between two points

Parameters

Parameters

  1. x1 number x coord of first point
  2. y1 number y coord of first point
  3. x2 number x coord of second point
  4. y2 number y coord of second point

Returns: number distance

Snap.len2(x1, y1, x2, y2)

Returns squared distance between two points

Parameters

Parameters

  1. x1 number x coord of first point
  2. y1 number y coord of first point
  3. x2 number x coord of second point
  4. y2 number y coord of second point

Returns: number distance

Snap.closestPoint(path, x, y)

Returns closest point to a given one on a given path.

Parameters

Parameters

  1. path Element path element
  2. x number x coord of a point
  3. y number y coord of a point

Returns: object in format

Snap.is(o, type)

Handy replacement for the typeof operator

Parameters

  1. o any object or primitive
  2. type string name of the type, e.g., string, function, number, etc.

Returns: boolean true if given value is of given type

Snap.snapTo(values, value, [tolerance])

Snaps given value to given grid

Parameters

  1. values array number given array of values or step of the grid
  2. value number value to adjust
  3. tolerance number maximum distance to the target value that would trigger the snap. Default is 10.

Returns: number adjusted value

Snap.getRGB(color)

Parses color string as RGB object

Parameters

  1. color string color string in one of the following formats:
  • Color name (red, green, cornflowerblue, etc)
  • #••• — shortened HTML color: (#000, #fc0, etc.)
  • #•••••• — full length HTML color: (#000000, #bd2300)
  • rgb(•••, •••, •••) — red, green and blue channels values: (rgb(200, 100, 0))
  • rgba(•••, •••, •••, •••) — also with opacity
  • rgb(•••%, •••%, •••%) — same as above, but in %: (rgb(100%, 175%, 0%))
  • rgba(•••%, •••%, •••%, •••%) — also with opacity
  • hsb(•••, •••, •••) — hue, saturation and brightness values: (hsb(0.5, 0.25, 1))
  • hsba(•••, •••, •••, •••) — also with opacity
  • hsb(•••%, •••%, •••%) — same as above, but in %
  • hsba(•••%, •••%, •••%, •••%) — also with opacity
  • hsl(•••, •••, •••) — hue, saturation and luminosity values: (hsb(0.5, 0.25, 0.5))
  • hsla(•••, •••, •••, •••) — also with opacity
  • hsl(•••%, •••%, •••%) — same as above, but in %
  • hsla(•••%, •••%, •••%, •••%) — also with opacity

Note that % can be used any time: rgb(20%, 255, 50%).

Returns: object RGB object in the following format:

  1. {
    1. r number red,
    2. g number green,
    3. b number blue,
    4. hex string color in HTML/CSS format: #••••••,
    5. error boolean true if string can't be parsed
  2. }

Snap.hsb(h, s, b)

Converts HSB values to a hex representation of the color

Parameters

  1. h number hue
  2. s number saturation
  3. b number value or brightness

Returns: string hex representation of the color

Snap.hsl(h, s, l)

Converts HSL values to a hex representation of the color

Parameters

  1. h number hue
  2. s number saturation
  3. l number luminosity

Returns: string hex representation of the color

Snap.rgb(r, g, b)

Converts RGB values to a hex representation of the color

Parameters

  1. r number red
  2. g number green
  3. b number blue

Returns: string hex representation of the color

Snap.color(clr)

Parses the color string and returns an object featuring the color's component values

Parameters

  1. clr string color string in one of the supported formats (see Snap.getRGB)

Returns: object Combined RGB/HSB object in the following format:

  1. {
    1. r number red,
    2. g number green,
    3. b number blue,
    4. hex string color in HTML/CSS format: #••••••,
    5. error boolean true if string can't be parsed,
    6. h number hue,
    7. s number saturation,
    8. v number value (brightness),
    9. l number lightness
  2. }

Snap.hsb2rgb(h, s, v)

Converts HSB values to an RGB object

Parameters

  1. h number hue
  2. s number saturation
  3. v number value or brightness

Returns: object RGB object in the following format:

  1. {
    1. r number red,
    2. g number green,
    3. b number blue,
    4. hex string color in HTML/CSS format: #••••••
  2. }

Snap.hsl2rgb(h, s, l)

Converts HSL values to an RGB object

Parameters

  1. h number hue
  2. s number saturation
  3. l number luminosity

Returns: object RGB object in the following format:

  1. {
    1. r number red,
    2. g number green,
    3. b number blue,
    4. hex string color in HTML/CSS format: #••••••
  2. }

Snap.rgb2hsb(r, g, b)

Converts RGB values to an HSB object

Parameters

  1. r number red
  2. g number green
  3. b number blue

Returns: object HSB object in the following format:

  1. {
    1. h number hue,
    2. s number saturation,
    3. b number brightness
  2. }

Snap.rgb2hsl(r, g, b)

Converts RGB values to an HSL object

Parameters

  1. r number red
  2. g number green
  3. b number blue

Returns: object HSL object in the following format:

  1. {
    1. h number hue,
    2. s number saturation,
    3. l number luminosity
  2. }

Snap.parsePathString(pathString)

Utility method Parses given path string into an array of arrays of path segments

Parameters

  1. pathString string array path string or array of segments (in the last case it is returned straight away)

Returns: array array of segments

Snap.parseTransformString(TString)

Utility method Parses given transform string into an array of transformations

Parameters

  1. TString string array transform string or array of transformations (in the last case it is returned straight away)

Returns: array array of transformations

Snap.select(query)

Wraps a DOM element specified by CSS selector as Element

Parameters

  1. query string CSS selector of the element

Returns: Element the current element

Snap.selectAll(query)

Wraps DOM elements specified by CSS selector as set or array of Element

Parameters

  1. query string CSS selector of the element

Returns: Element the current element

Snap.parse(svg)

Parses SVG fragment and converts it into a Fragment

Parameters

  1. svg string SVG string

Returns: Fragment the Fragment

Snap.fragment(varargs)

Creates a DOM fragment from a given list of elements or strings

Parameters

  1. varargs SVG string

Returns: Fragment the Fragment

Snap.ajax(…)

Simple implementation of Ajax

Parameters

  1. url string URL
  2. postData object string data for post request
  3. callback function callback
  4. scope object scope of callback

or

Parameters

  1. url string URL
  2. callback function callback
  3. scope object scope of callback

Returns: XMLHttpRequest the XMLHttpRequest object, just in case

Snap.load(url, callback, [scope])

Loads external SVG file as a Fragment (see Snap.ajax for more advanced AJAX)

Parameters

  1. url string URL
  2. callback function callback
  3. scope object scope of callback

Snap.getElementByPoint(x, y)

Returns you topmost element under given point.

Returns: object Snap element object

Parameters

  1. x number x coordinate from the top left corner of the window
  2. y number y coordinate from the top left corner of the window

Usage

Snap.getElementByPoint(mouseX, mouseY).attr({stroke: "#f00"});

Snap.plugin(f)

Let you write plugins. You pass in a function with five arguments, like this:

Snap.plugin(function (Snap, Element, Paper, global, Fragment) {
    Snap.newmethod = function () {};
    Element.prototype.newmethod = function () {};
    Paper.prototype.newmethod = function () {};
});

Inside the function you have access to all main objects (and their prototypes). This allow you to extend anything you want.

Parameters

  1. f function your plugin body

Snap.animation(attr, duration, [easing], [callback])

Creates an animation object

Parameters

  1. attr object attributes of final destination
  2. duration number duration of the animation, in milliseconds
  3. easing function one of easing functions of mina or custom one
  4. callback function callback function that fires when animation ends

Returns: object animation object

Snap.animate(from, to, setter, duration, [easing], [callback])

Runs generic animation of one number into another with a caring function

Parameters

  1. from number array number or array of numbers
  2. to number array number or array of numbers
  3. setter function caring function that accepts one number argument
  4. duration number duration, in milliseconds
  5. easing function easing function from mina or custom
  6. callback function callback function to execute when animation ends

Returns: object animation object in mina format

  1. {
    1. id string animation id, consider it read-only,
    2. duration function gets or sets the duration of the animation,
    3. easing function easing,
    4. speed function gets or sets the speed of the animation,
    5. status function gets or sets the status of the animation,
    6. stop function stops the animation
  2. }
var rect = Snap().rect(0, 0, 10, 10);
Snap.animate(0, 10, function (val) {
    rect.attr({
        x: val
    });
}, 1000);
// in given context is equivalent to
rect.animate({x: 10}, 1000);

Snap.Matrix()

Matrix constructor, extend on your own risk. To create matrices use Snap.matrix.

Snap.matrix(…)

Utility method Returns a matrix based on the given parameters

Parameters

  1. a number
  2. b number
  3. c number
  4. d number
  5. e number
  6. f number

or

Parameters

  1. svgMatrix SVGMatrix

Returns: object Matrix

Snap.filter.blur(x, [y])

Returns an SVG markup string for the blur filter

Parameters

  1. x number amount of horizontal blur, in pixels
  2. y number amount of vertical blur, in pixels

Returns: string filter representation

Usage

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

Snap.filter.shadow(…)

Returns an SVG markup string for the shadow filter

Parameters

  1. dx number horizontal shift of the shadow, in pixels
  2. dy number vertical shift of the shadow, in pixels
  3. blur number amount of blur
  4. color string color of the shadow
  5. opacity number 0..1 opacity of the shadow

or

Parameters

  1. dx number horizontal shift of the shadow, in pixels
  2. dy number vertical shift of the shadow, in pixels
  3. color string color of the shadow
  4. opacity number 0..1 opacity of the shadow

which makes blur default to 4. Or

Parameters

  1. dx number horizontal shift of the shadow, in pixels
  2. dy number vertical shift of the shadow, in pixels
  3. opacity number 0..1 opacity of the shadow

Returns: string filter representation

Usage

var f = paper.filter(Snap.filter.shadow(0, 2, 3)),
    c = paper.circle(10, 10, 10).attr({
        filter: f
    });

Snap.filter.grayscale(amount)

Returns an SVG markup string for the grayscale filter

Parameters

  1. amount number amount of filter (0..1)

Returns: string filter representation

Snap.filter.sepia(amount)

Returns an SVG markup string for the sepia filter

Parameters

  1. amount number amount of filter (0..1)

Returns: string filter representation

Snap.filter.saturate(amount)

Returns an SVG markup string for the saturate filter

Parameters

  1. amount number amount of filter (0..1)

Returns: string filter representation

Snap.filter.hueRotate(angle)

Returns an SVG markup string for the hue-rotate filter

Parameters

  1. angle number angle of rotation

Returns: string filter representation

Snap.filter.invert(amount)

Returns an SVG markup string for the invert filter

Parameters

  1. amount number amount of filter (0..1)

Returns: string filter representation

Snap.filter.brightness(amount)

Returns an SVG markup string for the brightness filter

Parameters

  1. amount number amount of filter (0..1)

Returns: string filter representation

Snap.filter.contrast(amount)

Returns an SVG markup string for the contrast filter

Parameters

  1. amount number amount of filter (0..1)

Returns: string filter representation

Snap.path.getTotalLength(path)

Returns the length of the given path in pixels

Parameters

  1. path string SVG path string

Returns: number length

Snap.path.getPointAtLength(path, length)

Returns the coordinates of the point located at the given length along the given path

Parameters

  1. path string SVG path string
  2. length number length, in pixels, from the start of the path, excluding non-rendering jumps

Returns: object representation of the point:

  1. {
    1. x: number x coordinate,
    2. y: number y coordinate,
    3. alpha: number angle of derivative
  2. }

Snap.path.getSubpath(path, from, to)

Returns the subpath of a given path between given start and end lengths

Parameters

  1. path string SVG path string
  2. from number length, in pixels, from the start of the path to the start of the segment
  3. to number length, in pixels, from the start of the path to the end of the segment

Returns: string path string definition for the segment

Snap.path.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t)

Utility method Finds dot coordinates on the given cubic beziér curve at the given t

Parameters

  1. p1x number x of the first point of the curve
  2. p1y number y of the first point of the curve
  3. c1x number x of the first anchor of the curve
  4. c1y number y of the first anchor of the curve
  5. c2x number x of the second anchor of the curve
  6. c2y number y of the second anchor of the curve
  7. p2x number x of the second point of the curve
  8. p2y number y of the second point of the curve
  9. t number position on the curve (0..1)

Returns: object point information in format:

  1. {
    1. x: number x coordinate of the point,
    2. y: number y coordinate of the point,
    3. m: {
      1. x: number x coordinate of the left anchor,
      2. y: number y coordinate of the left anchor
    4. },
    5. n: {
      1. x: number x coordinate of the right anchor,
      2. y: number y coordinate of the right anchor
    6. },
    7. start: {
      1. x: number x coordinate of the start of the curve,
      2. y: number y coordinate of the start of the curve
    8. },
    9. end: {
      1. x: number x coordinate of the end of the curve,
      2. y: number y coordinate of the end of the curve
    10. },
    11. alpha: number angle of the curve derivative at the point
  2. }

Snap.path.bezierBBox(…)

Utility method Returns the bounding box of a given cubic beziér curve

Parameters

  1. p1x number x of the first point of the curve
  2. p1y number y of the first point of the curve
  3. c1x number x of the first anchor of the curve
  4. c1y number y of the first anchor of the curve
  5. c2x number x of the second anchor of the curve
  6. c2y number y of the second anchor of the curve
  7. p2x number x of the second point of the curve
  8. p2y number y of the second point of the curve

or

Parameters

  1. bez array array of six points for beziér curve

Returns: object bounding box

  1. {
    1. x: number x coordinate of the left top point of the box,
    2. y: number y coordinate of the left top point of the box,
    3. x2: number x coordinate of the right bottom point of the box,
    4. y2: number y coordinate of the right bottom point of the box,
    5. width: number width of the box,
    6. height: number height of the box
  2. }

Snap.path.isPointInsideBBox(bbox, x, y)

Utility method Returns true if given point is inside bounding box

Parameters

  1. bbox string bounding box
  2. x string x coordinate of the point
  3. y string y coordinate of the point

Returns: boolean true if point is inside

Snap.path.isBBoxIntersect(bbox1, bbox2)

Utility method Returns true if two bounding boxes intersect

Parameters

  1. bbox1 string first bounding box
  2. bbox2 string second bounding box

Returns: boolean true if bounding boxes intersect

Snap.path.intersection(path1, path2)

Utility method Finds intersections of two paths

Parameters

  1. path1 string path string
  2. path2 string path string

Returns: array dots of intersection

  1. [
  2. {
    1. x: number x coordinate of the point,
    2. y: number y coordinate of the point,
    3. t1: number t value for segment of path1,
    4. t2: number t value for segment of path2,
    5. segment1: number order number for segment of path1,
    6. segment2: number order number for segment of path2,
    7. bez1: array eight coordinates representing beziér curve for the segment of path1,
    8. bez2: array eight coordinates representing beziér curve for the segment of path2
  3. }
  4. ]

Snap.path.isPointInside(path, x, y)

Utility method Returns true if given point is inside a given closed path.

Note: fill mode doesn’t affect the result of this method.

Parameters

  1. path string path string
  2. x number x of the point
  3. y number y of the point

Returns: boolean true if point is inside the path

Snap.path.getBBox(path)

Utility method Returns the bounding box of a given path

Parameters

  1. path string path string

Returns: object bounding box

  1. {
    1. x: number x coordinate of the left top point of the box,
    2. y: number y coordinate of the left top point of the box,
    3. x2: number x coordinate of the right bottom point of the box,
    4. y2: number y coordinate of the right bottom point of the box,
    5. width: number width of the box,
    6. height: number height of the box
  2. }

Snap.path.toRelative(path)

Utility method Converts path coordinates into relative values

Parameters

  1. path string path string

Returns: array path string

Snap.path.toAbsolute(path)

Utility method Converts path coordinates into absolute values

Parameters

  1. path string path string

Returns: array path string

Snap.path.toCubic(pathString)

Utility method Converts path to a new path where all segments are cubic beziér curves

Parameters

  1. pathString string array path string or array of segments

Returns: array array of segments

Snap.path.map(path, matrix)

Transform the path string with the given matrix

Parameters

  1. path string path string
  2. matrix object see Matrix

Returns: string transformed path string