SVG Versus Canvas
Lines versus dots: what's a graphic designer to do? Here are some helpful suggestions on how you might choose to put illustrations on your webpage.
Join the DZone community and get the full member experience.Join For Free
suppose you want to draw something on your web page using browser-native technologies. it might be some kind of animated scene; it might be a technical diagram; it could be some kind of custom infographic. what should you use? as the title of the article suggests, there are a couple of obvious answers: a canvas element or a scalable vector graphic (svg). in some cases, either svg or canvas might work reasonably well, and you can choose whichever you prefer working with. in other cases, practical considerations will make it almost inevitable that you favor one over the other.
vectors versus pixels
as the name suggests, svg images are a vector image format. as the name also suggests, they can generally be scaled indefinitely without becoming blurry or pixelated. by contrast, an html canvas element is composed of pixels. if you blow-up or zoom in on an image drawn on a canvas element you'll be able to see pixelation.
but things are not quite as simple as just saying "use svg if you want a vector graphic and canvas if you want a raster graphic." firstly, an svg image can include one or more raster graphics, either through a data uri or by linking to a file. an embedded image won't be magically vectorized, so if you zoom in, your "vector graphic" will look pixelated. and while, ultimately, the appearance of a canvas element is just a collection of variously colored pixels, if you're drawing something from scratch programmatically, you can adapt what's drawn to the screen size. while you can change the colors of individual canvas pixels should you want to, much of the time you'll be drawing paths.
stateful versus stateless
svg is stateful. that is, once you've added elements—a line, a circle, a rectangle, etc.—you can go back and change them (move them around, change their size or color...). once you've drawn something on a canvas, by contrast, the only record you get for free is the color (and transparency) of the resultant pixels. if you want to change how the canvas looks, you may have to redraw the whole thing from scratch! in many cases, i think this fact is far more important when comparing svg to canvas than whether or not what you've actually drawn is a vector graphic. the next two subsections will explain why.
ease of event monitoring
with svg, it's really easy to associate graphical elements with events like click, mouseover, and touch. why? because the elements you add to your svg become part of the page. as a result, you can add event handlers in the same way you would to other dom elements like divs, imgs, spans, and sections. if you move an element around the event handler doesn't go away (unless you tell it to). with canvas, the only element you have on the page is the canvas element itself. the rectangle you drew isn't an element. there's no record you even drew a rectangle once it's been drawn unless you kept it yourself. all you have is some (presumably) colored pixels. and the only events you can fire are on the canvas element as a whole.
that's not to say that all is lost for canvas. you can keep your own records of where you placed graphic elements and use the coordinates associated with an event on the canvas to work out what a cursor or finger is over. it's just a lot more work if you don't have a good library to help you along.
coping with a large number of graphical elements
the fact that an svg image naturally creates a record in the dom of all the elements drawn, their positions and other attributes certainly seems to make it favorable for any kind of complex, interactive graphic. in many cases, this is true. but, svgs also have a big flaw: they create a record in the dom of all the elements drawn, their positions, and other attributes. if your svg contains a lot of elements, then so does your dom. and... this can make things slow. very slow. if you want to draw a couple of thousand elements in your svg, you might have problems. if you want to draw them and then move them around 60 times a second to create a smooth animation, you're in big trouble. canvas is almost certainly a better idea.
a third option
Published at DZone with permission of Tim Brock, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.