The JavaScript library for modern SVG graphics.

Overview

Snap.svg · Build Status CDNJS GitHub Tag License

A JavaScript SVG library for the modern web. Learn more at snapsvg.io.

Follow us on Twitter.

Install

Learn

Use

In your HTML file, load simply by:

<script src="snap.svg-min.js"></script>

No other scripts are needed. Both the minified and uncompressed (for development) versions are in the /dist folder.

webpack

To load with webpack 2.x and 3.x, install Imports Loader (npm i -D imports-loader), and add the following to your webpack config:

module: {
  rules: [
    {
      test: require.resolve('snapsvg/dist/snap.svg.js'),
      use: 'imports-loader?this=>window,fix=>module.exports=0',
    },
  ],
},
resolve: {
  alias: {
    snapsvg: 'snapsvg/dist/snap.svg.js',
  },
},

Then, in any module you’d like to require Snap, use:

import Snap from 'snapsvg';

Build

Build Status

Snap.svg uses Grunt to build.

  • Open the terminal from the Snap.svg directory:
cd Snap.svg
  • Install its command line interface (CLI) globally:
npm install -g grunt-cli

*You might need to use sudo npm, depending on your configuration.

  • Install dependencies with npm:
npm install

*Snap.svg uses Grunt 0.4.0. You might want to read more on their website if you haven’t upgraded since a lot has changed.

  • To build the files run
grunt
  • The results will be built into the dist folder.
  • Alternatively type grunt watch to have the build run automatically when you make changes to source files.

Testing

Tests are located in test folder. To run tests, simply open test.html in there. Automatic tests use PhantomJS to scrap this file, so you can use it as a reference.

Alternatively, install PhantomJS and run command

grunt test

Contribute

git checkout -b my_branch
  • Add your changes.
  • Check that tests are passing
  • Commit your changes:
git commit -am "Added some awesome stuff"
  • Push your branch:
git push origin my_branch

Note: Pull requests to other branches than dev or without filled CLA wouldn’t be accepted.

Comments
  • Memory leaks

    Memory leaks

    Hi, first off thanks for the hard work on snap.

    I found that my animations were leaking memory (and found that the problem could be reproduced by debugging the demos > snap-mascot page) and did some digging. Turned out that the issue was with the following handlers:

    eve.once("mina.finish." + anim.id, function () {
        delete el.anims[anim.id];
        callback && callback.call(el);
    });
    eve.once("mina.stop." + anim.id, function () {
        delete el.anims[anim.id];
    });
    

    When the mina.stop event triggers the handler for the mina.finish event is not deleted and vice versa. The handlers remain in memory until the application is terminated since they are referenced by eve's events property.

    I fixed the issue by removing mina.stop altogether and triggering mina.finish in stopit. There is still a less major memory leak issue involving the handlers but I haven't fixed it yet.

    Hope this helps someone, cheers.

    opened by JeremyTCD 26
  • Fix CommonJS Bundle; add manual webpack verification

    Fix CommonJS Bundle; add manual webpack verification

    This PR creates a separate bundle: dist/snap.svg-commonjs-min.js that does not embed the 'eve' dependency. Additionally, the eve dependency is passed to the plugins via the Snap._ object, which was already used to pass around jQuery.

    Fixes #447, #341

    opened by darthtrevino 22
  • Invalid value for <polygon> attribute points...

    Invalid value for attribute points...

    It seems the newest version 0.4.1 doesn't allow to animate a polygon's "points" attribute.

    var s = Snap(400, 620);
    var pol = s.polygon([0, 464, 0, 464, 111.6, 464, 282.5, 464, 457.4, 464, 613.4, 464, 762.3, 464, 912.3, 464, 1440.1, 464, 0, 464]);
    pol.animate(
      { points: [0, 464, 0, 367, 111.6, 323.3, 282.5, 373, 457.4, 423.8, 613.4, 464, 762.3, 464, 912.3, 464, 1440.1, 464, 0, 464] },
      1000)
    

    It errors with 'Error: Invalid value for attribute points="0"'

    I got it working with version 0.3.0. So some change must have broken this behavior

    opened by azevedo 21
  • Element.animate [ method ] treats percentage values as pixel values

    Element.animate [ method ] treats percentage values as pixel values

    https://jsfiddle.net/sjhcnuus/ In the above example I would expect rectangle2 to move above rectangle1 and completely cover it yet all of the attr values fed into rectangle2.animate({}) seem to be converted into pixel based values.

    P.S. Snap.svg is awesome keep up the great work

    opened by n-a-m-e 17
  • Adding a drag handler overwrites the default behavior?

    Adding a drag handler overwrites the default behavior?

    I was trying to attach an onmove event listener to the drag method and expected it to behave like a normal JS event listener, but it seems that when I pass a listener function, the normal drag behavior is overwritten and the element doesn't move anymore.

    This fiddle shows what I mean: http://jsfiddle.net/XLe6N/

    The black circle doesn't react to mouse events anymore.

    Are we supposed to move the elements ourselves when attaching an onmove listener?

    opened by janmonschke 17
  • consider a package manager

    consider a package manager

    npm / component / bower or something so that the little chunks are useful to people (and to keep yourself sane), then produce a snap build from those. Otherwise looks good! :D

    opened by tj 17
  • Add Browserify / CommonJS support

    Add Browserify / CommonJS support

    This makes it possible to require Snap.svg with Browserify and modifies package.json to point towards the correct module entry point instead of the Gruntfile.

    Supporting AMD is great and all, but it's only a few more lines to support CommonJS style module systems when you're already using the factory pattern. Plus, npm is already being used to install eve, so I didn't even have to modify dependencies to get things working. Edit: looks like eve was only included as a devDependency, so that needed to be changed, too.

    Thanks in advance!

    opened by wookiehangover 15
  • Issue snap 0.5  Elem.attr(

    Issue snap 0.5 Elem.attr("fill")

    I get this error with the following code (with snap.svg.js 0.5):

    var s = Snap("#svg1"); 
    var r = s.rect(100,100,100,100).attr({ stroke: '#123456', 'strokeWidth': 20, fill: 'red', 'opacity': 1 });
    
    console.log(r.attr("fill"));
    
    snap.svg.js:4123 Uncaught TypeError: Cannot read property '2' of null
        at Function.Snap.deurl (snap.svg.js:4123)
        at Element.<anonymous> (snap.svg.js:4511)
        at eve (snap.svg.js:111)
        at Element.attr (snap.svg.js:2276)
        at app.js:4
    Snap.deurl @ snap.svg.js:4123
    (anonymous) @ snap.svg.js:4511
    eve @ snap.svg.js:111
    Element.attr @ snap.svg.js:2276
    (anonymous) @ app.js:4
    

    Can someone confirm?

    thank you

    opened by pfx81 13
  • Clone() produces TypeError: Constructor URL requires 'new'

    Clone() produces TypeError: Constructor URL requires 'new'

    Snap Snap.svg 0.4.1

    When loading an external SVG and cloning it, to iterate a series of buttons for a menu. Chrome (Mac) produces the failure:

    Uncaught TypeError: Failed to construct 'URL': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

    firefox: TypeError: Constructor URL requires 'new' at line 3136

    which is in function fixids:

      attr[name] = URL(id);
    

    When I make that: attr[name] = new URL(id);

    Firebug produces TypeError: radialGradientSiap76j711b is not a valid URL.

    Loading and then displaying this file works as expected.

    opened by bakajin 12
  • .drag() with viewBox set

    .drag() with viewBox set

    I'm using the default Element.drag() handler with a viewBox applied and it doesn't scale the mouse delta's accordingly, so the drag effect is amplified by 2-3x what it should be, is there an easy way to take the viewBox scaling factor into account?

    opened by Siyfion 12
  • how to limit the draggable area in snap.svg?

    how to limit the draggable area in snap.svg?


    I am working on a puzzle game and I use snap.svg and javascript to move the elements of the game . But the problem is that any user can drag the element of svg anywhere on the screen, including out of the droppable area. I want to prevent it , I found some resources on internet about snap.svg but it doesn't like i want

    I share the code here : https://jsfiddle.net/j058aoyn/2/

    my script :

           var s = Snap("#mysvg");
            var dragging = false;
            var tmp = null
            
            Snap.load("puzzle.svg", onSVGLoaded)
            
            function onSVGLoaded(frag) {
                s.append(frag);
                s.mouseover(clickDrag)
            }
            
            function clickDrag(e) {
                tmp = e.target.parentNode.getAttribute('index');
                if (tmp != null) {
                    var t = Snap(e.target);
                    t.drag();
                }
                if (t !== undefined && (t.data('dragEl') === undefined) && (dragging === false)) {
                    t.drag(dragMove, dragStart, dragStop).data('dragEl', t);
                }
            }
            
            function dragStart(x, y, e) {
                this.data('targetPiece', e.target);
            }
            
            function dragMove(dx, dy, x, y, e) {
                if (tmp !== this.data('targetPiece')) {
                    return
                }
                // Check the event isn't coming from a different state
                this.data('dragEl').attr({cx: x - 10, cy: y - 10})
                dragging = true;
            }
            
            function dragStop() {
            }
    
    opened by kevinG73 11
  • Bug on Getting Started page

    Bug on Getting Started page

    Screen Shot 2022-06-30 at 4 33 28 PM

    Stepping through the getting started example, the steps work up until step 14. When you're on step 13 and you click the play button to advance to step 14, this error shows up in the debug console:

    Screen Shot 2022-06-30 at 4 22 54 PM

    The page I am talking about is the one that shows up at this url:

    Screen Shot 2022-06-30 at 4 28 37 PM

    snapsvg.io/start/

    opened by JimiHFord 0
  • Animated clock that worked for version 0.3.0 is failing with version 0.5.1

    Animated clock that worked for version 0.3.0 is failing with version 0.5.1

    The clock works OK for 0.3 and its URL is: https://oreillymedia.github.io/svg-essentials-examples/ch14/snap_animated_clock.svg

    I copied the source to my own directory, and used it with snap.svg-min.js version 0.5.1. The animation failed: the minuteHand and hourHand are working OK, but the secondHand is not moving. I also saw this failure manifesting itself differently: the minuteHand was getting detached from the clock and shifting away.

    opened by yashchie 1
Owner
Adobe Web Platform
Adobe Web Platform
A lightweight JavaScript graphics library with the intuitive API, based on SVG/VML technology.

GraphicsJS GraphicsJS is a lightweight JavaScript graphics library with the intuitive API, based on SVG/VML technology. Overview Quick Start Articles

AnyChart 937 Feb 5, 2021
Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser

Fabric.js Fabric.js is a framework that makes it easy to work with HTML5 canvas element. It is an interactive object model on top of canvas element. I

Fabric.js 23.6k Jan 3, 2023
Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser

Fabric.js Fabric.js is a framework that makes it easy to work with HTML5 canvas element. It is an interactive object model on top of canvas element. I

Fabric.js 23.6k Jan 3, 2023
React + Canvas = Love. JavaScript library for drawing complex canvas graphics using React.

React Konva React Konva is a JavaScript library for drawing complex canvas graphics using React. It provides declarative and reactive bindings to the

konva 4.9k Jan 9, 2023
A library optimized for concise and principled data graphics and layouts.

MetricsGraphics is a library built for visualizing and laying out time-series data. At around 15kB (gzipped), it provides a simple way to produce comm

Metrics Graphics 7.5k Dec 22, 2022
A library optimized for concise and principled data graphics and layouts.

MetricsGraphics is a library built for visualizing and laying out time-series data. At around 15kB (gzipped), it provides a simple way to produce comm

Metrics Graphics 7.5k Dec 22, 2022
The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey

Paper.js - The Swiss Army Knife of Vector Graphics Scripting If you want to work with Paper.js, simply download the latest "stable" version from http:

Paper.js 13.5k Dec 30, 2022
Simple, responsive, modern SVG Charts with zero dependencies

Frappe Charts GitHub-inspired modern, intuitive and responsive charts with zero dependencies Explore Demos » Edit at CodePen » Contents Installation U

Frappe 14.6k Jan 4, 2023
Create graphics with a hand-drawn, sketchy, appearance

Rough.js Rough.js is a small (<9 kB) graphics library that lets you draw in a sketchy, hand-drawn-like, style. The library defines primitives to draw

Rough 17.8k Dec 30, 2022
A cross platform high-performance graphics system.

spritejs.org Spritejs is a cross platform high-performance graphics system, which can render graphics on web, node, desktop applications and mini-prog

null 5.1k Dec 24, 2022
The lightweight library for manipulating and animating SVG

SVG.js A lightweight library for manipulating and animating SVG, without any dependencies. SVG.js is licensed under the terms of the MIT License. Inst

SVG.js 10k Dec 25, 2022
The lightweight library for manipulating and animating SVG

SVG.js A lightweight library for manipulating and animating SVG, without any dependencies. SVG.js is licensed under the terms of the MIT License. Inst

SVG.js 10k Jan 3, 2023
Easy-to-use js library for building graphs using svg.

LineChart Easy-to-use js library for building graphs using svg. Examples How to use Just add linechart.js from 'src' directory to your project. And ad

Korpusov Maxim 8 Nov 21, 2022
📊 Interactive JavaScript Charts built on SVG

A modern JavaScript charting library to build interactive charts and visualizations with simple API. Our Partner ApexCharts is now a partner of Fusion

ApexCharts 12.1k Jan 3, 2023
JavaScript SVG parser and renderer on Canvas

canvg JavaScript SVG parser and renderer on Canvas. It takes the URL to the SVG file or the text of the SVG file, parses it in JavaScript and renders

null 3.3k Jan 4, 2023
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:

D3: Data-Driven Documents D3 (or D3.js) is a JavaScript library for visualizing data using web standards. D3 helps you bring data to life using SVG, C

D3 103.8k Jan 3, 2023
Progressive pie, donut, bar and line charts

Peity Peity (sounds like deity) is a jQuery plugin that converts an element's content into a mini <svg> pie, donut, line or bar chart. Basic Usage HTM

Ben Pickles 4.2k Jan 1, 2023
Beautiful React SVG maps with d3-geo and topojson using a declarative api.

react-simple-maps Create beautiful SVG maps in react with d3-geo and topojson using a declarative api. Read the docs, or check out the examples. Why R

z creative labs 2.7k Dec 29, 2022