Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diagrams in SVG versus HTML5 Canvas

Tags:

html

canvas

svg

I want to start a project where I need to draw diagrams consisting of rounded rectangles connected with lines and a JavaScript action when I click some elements. This needs to work in all modern browsers.

Both SVG and HTML5 Canvas seem to be able to do this so I wonder what would be best.

Also I don't want to reinvent the wheel, so if there are libraries that do such things I would like to know; I took a look at Raphaël and some other JavaScript drawing libraries but they don't give all the functionality I need. In Google's API there is such a tool but it is very limited.

like image 445
peter Avatar asked Apr 25 '12 18:04

peter


People also ask

What is the main difference between drawing with SVG versus the canvas element?

SVG gives better performance with smaller number of objects or larger surface. Canvas gives better performance with smaller surface or larger number of objects. SVG is vector based and composed of shapes. Canvas is raster based and composed of pixel.

How would you compare HTML Canvas and SVG?

Differences Between SVG and Canvas Canvas draws 2D graphics, on the fly (with a JavaScript). SVG is XML based, which means that every element is available within the SVG DOM. You can attach JavaScript event handlers for an element. In SVG, each drawn shape is remembered as an object.

What is faster SVG or Canvas?

And SVG is faster when rendering really large objects, but slower when rendering many objects. A game would probably be faster in Canvas. A huge map program would probably be faster in SVG.

Is SVG slower than canvas?

SVG becomes slow rendering if it is complex because anything that uses the Document object model (DOM) at great extent will become slow. Canvas provides the high-performance element best suited for rendering faster graphics like image editing, an application that requires pixel manipulation.


1 Answers

Use SVG because—as a retained-mode drawing API—you can attach event listeners directly to specific elements, and change properties of specific elements and have the page magically update. Further, as a vector-based format, it is resolution-independent.

HTML5 Canvas, by comparison, is a non-retained-mode (aka immediate-mode) drawing API; every pixel you draw is blended with all other pixels on the canvas, with no concept of the original shape. Further, as a raster-based format, you would need to do some extra work to get the drawing commands to adjust for different display sizes.

In general, you should use Canvas if and only if you need:

  1. Direct setting of pixels (e.g. blurs, sparkly effects), or
  2. Direct getting of pixels (e.g. reading a user's drawing to save as a PNG, sampling portions of the image to detect visual overlaps), or
  3. massive number of 'objects' that won't move much or track individual events (SVG can be slow to redraw with thousands of objects).

Note also that you don't have to choose only one or the other. You can draw SVG onto canvas. You can include bitmaps (images) in SVG. You can even include HTML5 Canvas in SVG via <foreignElement>. You can have a single HTML page with multiple layered canvases and SVG elements with transparent backgrounds, intermingling the output of each.

like image 170
Phrogz Avatar answered Oct 05 '22 16:10

Phrogz