Canvas and SVG - which should I use when?
By Daniel Davis. Thursday, 10. December 2009, 02:05:00
What is SVG?
- Has been a W3C standard for several years.
- XML-based language using tags to create image objects.
- Resize as much as you want without losing quality.
- Every element is available within the SVG DOM so you can attach JavaScript events to them.
- Editing a single object or group of objects is easy.
- Slows down with a large number of objects.
- SVG is accessible - text is selectable just like regular text. Accessibility software needs to catch up, however.
What is canvas?
- At the time of writing, part of the HTML5 specification.
- Pixel-based images using JavaScript to edit pixels.
- Images become pixellated when enlarged.
- Everything is drawn with pixels. To change anything in the image you have to edit the pixels.
- Redrawing the pixels in the canvas area is fast.
- Slows down with a large drawing area.
- Canvas is not accessible (as of late 2009) - text is unreadable by e.g. screen readers and search engines.
So what are canvas and SVG best suited for?
Because of its speed of manipulation, canvas is best when fast rendering and redrawing is needed, e.g. animations and games, such as in this example: http://www.benjoffe.com/code/demos/canvascape/
SVG is better suited for large-scale rendering and interactivity, e.g. maps and user interfaces, such as in this slideshow demo: http://people.mozilla.com/~vladimir/demos/photos.svg
It should also be noted that it's not necessarily a choice of using one or the other. If required, there's a happy middle ground where SVG and canvas can be layered together to make the most of their respective strengths.
N.B. To work around Internet Explorer's lack of support, please look at explorercanvas for canvas capability and Raphael for cross-browser vector graphics.
Further reading: SVG vs. Canvas on Trivial Drawing Application







