83

I have tried to find a proper SVG library for modern browsers. My goal is to decide, what library is suitable for creating simple online SVG editor with eg. text and path editing and clipping text with paths.

I found two libraries, which may be suitable: Snap.svg and Svg.js.


SNAP.SVG

Github: https://github.com/adobe-webplatform/Snap.svg
Source code lines: 6925 Github Stars: 3445
Doc: http://snapsvg.io/docs/
Getting started: http://snapsvg.io/start/
Starter example (JSBIN)

var draw = Snap(800, 600);

// create image
var image = draw.image('/images/shade.jpg', 
                       0, -150, 600, 600);

// create text
var text = draw.text(0,120, 'SNAP.SVG');

text.attr({
  fontFamily: 'Source Sans Pro',
  fontSize: 120,
  textAnchor: 'left'
});

// clip image with text
image.attr('clip-path', text);

SVG.JS

Github: https://github.com/svgdotjs/svg.js
Source code lines: 3604 Github Stars: 1905
Doc: https://svgdotjs.github.io/

Starter example (JSBIN):

var draw = SVG('drawing');

// create image
var image = draw.image('/images/shade.jpg');
image.size(600, 600).y(-150);

// create text
var text = draw.text('SVG.JS').move(300, 0);
text.font({
  family: 'Source Sans Pro',
  size: 180,
  anchor: 'middle',
  leading: '1em'
});

// clip image with text
image.clipWith(text);

Usage seems to be rather similar.

What are the main differences between these two libraries?

wout
  • 2,477
  • 2
  • 21
  • 32
Timo Kähkönen
  • 11,962
  • 9
  • 71
  • 112
  • 15
    It might be an opinion-based question, but it's an excellent one, and the opinions of people who actually used the libraries can be a huge time saver for those who are trying to decide where to invest their time. Besides, most answers on SO are opinion-based. – isapir Feb 27 '17 at 23:43
  • 2
    @Igal, conversely, the time "wasted" by unclosed opinion-based questions is ... negligible. _At least_ 56 people thought this question was indeed worthwhile. – KlaymenDK May 08 '17 at 17:52
  • 1
    @KlaymenDK I think that thousands of people found the question useful since it was viewed 19,257 times as of yet. Most people simply don't bother to upvote a question or answers even if they find it useful. – isapir May 08 '17 at 20:13

3 Answers3

85

I am the creator of SVG.js (so I'm biased too :). You'll have to try them both and see what suits you best. With SVG.js I try to keep the syntax more javascript based so everything is more dynamic, whereas Snap often uses a string based syntax. This makes the resulting code often more human readable in SVG.js, which I obviously prefer. Let's take a gradient as an example.

SNAP.SVG:

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

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

SVG.JS:

var g = draw.gradient('linear', function(stop) {
  stop.at(0, '#000')
  stop.at(0.25, '#f00')
  stop.at(1, '#fff')
})

draw.circle(80).center(50,50).fill(g)

The Snap.svg syntax is a bit more concise, the SVG.js code is more readable. So it's really a matter of taste.

Generally SVG.js is much more Object Oriented. Everything is a class, even down to numbers and colors and are therefore extendible. Because of the OO structure it is extremely easy to write plugins and extend existing objects on any level.

wout
  • 2,477
  • 2
  • 21
  • 32
  • 11
    SVG.js seems to have a very encouraged and community oriented creator - might be a plus point too! :) I also prefer the chaining-way of coding more than jQuery "string-based" schema. – Florian Loch Feb 20 '14 at 22:36
  • @wout: With svg.js in an SVG file, is it possible to capture the document element instead of inserting extra elements? – Williham Totland Aug 22 '14 at 18:16
  • 2
    @wout: Awesome work! I started with snap.svg a week ago, but I was already having some doubts. Reading through svg.js documention I'm discovering things that I found missing from Snap, like relative move. And I see things like viewbox, rbox, the Use class, defs, more easing functions and filters (with the plugins), looping animations. Overall it seems more modular and open. Also the documentation on svg.js seems more elaborate and complete. I just hope svg.js performs as good as snap.svg, since snap is targeting newer browsers specifically. Any thoughts on this? Have people done benchmarks? – Thijs Koerselman Oct 28 '14 at 21:41
  • 1
    @0x80 svg.js easily outperforms snap.svg. A simple `attr()` test on jsperf shows that it's at least 5 times as fast: http://jsperf.com/raphael-vs-snapsvg-attr/3 . Also take into account that svg.js is only half the size of snap and as you stated yourself, is more feature rich. – wout Oct 30 '14 at 09:38
  • Wow that is significant! I'm going to do more tests myself, but this is really something I should look into. Do you think a simple attr test is fairly representational for animation performance too? The lib size doesn't matter to me, I'm solely interested in performance. What makes me suspicious is that Raphael outperforms Snap even though the latter is focused on modern browsers. It doesn't make much sense to me. – Thijs Koerselman Oct 31 '14 at 13:36
  • 3
    Look at it this way, every manipulation of an element is done through the `attr()` method. This method is the center of change so if it's slow, the overall performance hit is exponential. As for Raphael vs Snap, both libraries are form the same author. Snap is basically Raphael 3.0 and if you look at the code you see a lot of similarities. It baffles me why Snap performs so bad, especially with Adobe backing it. – wout Oct 31 '14 at 16:38
18

I originally tried Snap as it had a nice website and seemly good documentation. After a few issues that I couldn't explain, I decided to try SVG.js. I can't pinpoint why, but SVG.js seems easier to write; more intuitive. I'm not saying that Snap is bad, but it doesn't fit my style, and the documentation was a little sparse in content.

Nate
  • 2,035
  • 8
  • 23
  • 33
  • 4
    The documentation of Snap.svg has been a pain spot for me as well. It's autogenerated, leaving often crucial things to be guessed by the reader. Also, not advancing from 0.3.0 (which seems useful - found only one bug so far) may mean lack of momentum. – akauppi Apr 04 '15 at 20:20
  • I can't get svg.js to work. Anyone have a working example? – David Spector Feb 01 '19 at 21:46
14

I'm not sure you are going to get an unbiased answer, as most people will have experience of one or the other.

As both are essentially interfaces to the underlying SVG spec, you should be able to do most things with either, so I wouldn't worry too much about picking either. Solutions will be similar, rather than seeing differences.

I've more experience with Snap (so biased), but looking at the docs, my impressions would be that svg.js seems to have a bit more sugar to some aspects like animations and text, whereas maybe Snap has a bit more towards things like Matrices (which I've found very useful as I struggle with those sometimes), and seem to support a few extra things like touch elements (I suspect they are available in both somehow, and partly dependent on browser support though, but things like touch support may become increasingly important with svg).

Ultimately, I'd just get coding in one or the other and not worry about it. I think if you understand SVG you will be able to swap between them relatively easy if you ever need as well.

Ian
  • 13,724
  • 4
  • 52
  • 75