HTML5 Canvas or SVG: Choose Wisely
In 2004, the introduction of HTML5 also brought us the JavaScript based drawing method Canvas. The vector-based SVG format has been a thing since 2001. However, it only became truly successful over the past few years, in parallel to HTML5. Both formats have their advantages and disadvantages. But which of the two is the better choice for which situation? Where are their strengths and weak points?
JavaScript vs. XML-Syntax
The biggest difference between HTML5 Canvas and SVG is the markup, or rather, the coding. Ultimately, only a single element ("<canvas>") is being marked up via HTML5. The entire content of the canvas is realized via JavaScript. According methods allow you to draw and combine shapes like rectangles, circles, but also complex polygons and arcs. JavaScript is also used for the looks (fill color, as well as stroke and stroke color).var canvas = document.getElementById("canvas"); var drawing = canvas.getContext("2d"); drawing.moveTo(0, 0); drawing.lineTo(200, 200); drawing.lineWidth = 2; drawing.strokeStyle = "red"; drawing.stroke();In the example, a red line is drawn from the position "0, 0" to "200, 200". Via SVG, the same line would be drawn using an according SVG element.
<line x1="0" y1="0" x2="200" y2="200" stroke-width="2" stroke="red" />Although the result is identical in both cases, you can tell that the SVG variant is significantly shorter, and, due to the XML syntax, the markup should be a lot more intuitive for most people. Another advantage of the SVG format is that the design can also be done via CSS. Here, individual shapes can be designed uniformly via ID or category. So, when it comes to the markup, the SVG format is ahead. It saves space and has a simpler syntax. With HTML5 Canvas, the cards are stacked against those that generally want to provide all of their content without JavaScript as it is.
Pixel vs. Vectors
Another significant difference is the way the formats display content. While HTML5 Canvas is a pixel based format (a PNG image is created within the browser), the SVG format is vector based. Of course, HTML5 Canvas also allows you to create graphics for high-resolution displays. But, at the latest, after zooming in, the Canvas drawings become pixelated, while SVG drawings keep their acuity. Once again, the SVG format stays in the lead - especially since you can even integrate bitmaps within an SVG document.Interactions and Effects
As the SVG format also supports CSS, all stylesheets options are available to you. Aside from simple designs, it is also possible to implement interactive effects. This way, you get to apply hover effects to individual SVG shapes, highlighting them, changing their color, or enhancing them when hovering over them with the mouse. This allows for animations as well, thanks to the "transition" feature. Since the SVG format supports links, you can add references to shapes, creating links from individual shapes. On top of that, users get to address and manipulate SVG elements via JavaScript.document.getElementsByTagName("rect")[0].setAttribute("width", "200");In the example, an SVG rectangle is being manipulated using JavaScript.