HTML5 Canvas versus SVG for interactive charts and graphical models
Some links and notes:
- sitepoint: Canvas vs SVG: How to Choose the Right Format
- MSDN Blogs > IEBlog> Thoughts on when to use Canvas and SVG
- dev.opera.org: SVG or Canvas? Choosting between the two
- Graphic JavaScript Tree with Layout (implements layout algorithm from scratch)
- Sencha Touch Charts describing why they went with Canvas not SVG
- stackoverflow posts:
- What is the difference between SVG and HTML5 Canvas?
- HTML5 Canvas vs SVG vs div
- Suggestions: Canvas or SVG?
- Interactive directed graphs with SVG and Javascript
- Why do we need the HTML5 Canvas element when the same can be achieved through SVG?
- Library for Canvas / SVG web-based tree graphs with layout algorithm?
- JavaScript charting library? - recommends SVG over Canvas; and Raphael
- jQuery SVG vs. Raphael - vast majority of support is for Raphael
Toolkits and Frameworks
- JavaScript InfoVis Toolkit
- Paper.js - Vector Graphics Scripting based on Canvas
- Raphaël - based on SVG. Links to tutorials in this SO thread. Makes animation and interaction easy. Good support. MIT License. Draggable graphical model is one of the examples. Also see this SO post on using dynamic data with Raphael. Some libraries built on Raphaël:
- Really good comparison between Raphaël, Paper.js and Processing
- ExtJS: very, very powerful JavaScript framework for building web app UIs; with a good charting module
- Dojo (another powerful JavaScript web app framework) has http://livedocs.dojotoolkit.org/dojox/chartingdojox.charting
Tutorials and demos
- HTML5 graph slider using canvas and jQuery
- 2D function glotter using canvas (full screen; zoom and pan)
- HTML5 Canvas Graphic Solutions every web developer must know
Current conclusions
I should use Raphaël and one of its graphing add-ons. I need to do more than just plot a standard line chart; I want lots of interaction.
Canvas is a lower level graphics API than SVG. Canvas thinks in terms of individual pixels whilst SVG thinks in terms of vector objects. Canvas is good for high performance 2D graphics like games; but SVG is good for applications which draw objects like boxes, lines etc. My only slight concern about using SVG is that I may run into performance issues if I want to build a full-screen waveform viewer. Also,pre-Honeycomb Android doesn't support SVG (but does support Canvas). But I don't care about pre-Honeycomb Android for my specific application.
Comments
Matan (not verified)
Sat, 25/08/2012 - 12:22
Permalink
I beg to slightly defer. It
I beg to slightly defer. It may seem it matters most how you would use SVG or Canvas (i.e. mainly what library or libraries you'd use for driving them). Directly working with SVG means crafting SVG paths yourself, or relying on a library that won't always satisfy your needs. Canvas has libraries driving it, which would draw objects for you just as using SVG would. Your code, or the library you use for driving Canvas, should, per my understanding, be able to remember objects and hence you would have the same level of object oriented approach as supplied by SVG. Using Canvas, you will not be bound to the specs of SVG in your abstractions. You will not have to produce SVG elements and mix css and code as you would in SVG. You'd just draw what you want to draw. You will lose the ability to debug the elements directly in the browser (in case you found that helpful with SVG).
So I'd say SVG v.s. Canvas is a draw. As time goes on, we can assume libraries driving Canvas will improve and multiply, until the next standard superseding both SVG and Canvas may get invented, who knows. Canvas is newer, so maybe SVG is more stable in browsers than Canvas is. Choosing SVG would be slightly more conservative then, but it seems no browser can afford crashing its Canvas developers too much.
The event model in SVG is the same as with html elements, while in Canvas there's more than one approach, I'd look at the event models as a key facet to consider.
All I can say about using libraries for driving SVG is, well, that it's not that cool and you spend time fiddling with the library rather than pursuing your design of what your application should be actually doing. Look at Canvas driving libraries for juxtaposing with SVG, not at bare Canvas... I would explore
KineticJS as a Canvas driving example.
Of course, if you're looking for ready made visualizations for which you'll have partial control of customizing, then SVG based libraries may have the upper hand as they've had more time to hone and grow those visualizations. d3.js being a case in point.
Jack
Sat, 25/08/2012 - 12:50
Permalink
Hi Matan,
Add new comment