Connect with us


Canvas vs SVG: Selecting the Proper Device for the Job – SitePoint

HTML5 Canvas and SVG are each standards-based HTML5 applied sciences that you need to use to create wonderful graphics and visible experiences. The query I’m asking on this article is the next: Ought to it matter which one you employ in your undertaking? In different phrases, are there any use circumstances for preferring HTML5 Canvas over SVG?

First, let’s spend a number of phrases introducing HTML5 Canvas and SVG.

What Is HTML5 Canvas?

Right here’s how the WHATWG specification introduces the canvas aspect:

The canvas aspect offers scripts with a resolution-dependent bitmap canvas, which can be utilized for rendering graphs, recreation graphics, artwork, or different visible photographs on the fly.

In different phrases, the <canvas> tag exposes a floor the place you may create and manipulate rasterized photographs pixel by pixel utilizing a JavaScript programmable interface.

Right here’s a fundamental code pattern:

See the Pen
Fundamental Canvas Form Demo
by SitePoint (@SitePoint)
on CodePen.


<canvas id="myCanvas" width="800" peak="800"></canvas>

The JavaScript:

const canvas = doc.getElementById('myCanvas');
const context = canvas.getContext('second');
context.fillStyle = '#c00';
context.fillRect(10, 10, 100, 100);

You may benefit from the HTML5 Canvas API strategies and properties by getting a reference to the 2D context object. Within the instance above, I’ve drawn a easy crimson sq., 100 x 100 pixels in measurement, positioned 10px from the left and 10px from the highest of the <canvas> drawing floor.

Being resolution-dependent, photographs you create on <canvas> could lose high quality when enlarged or displayed on Retina Shows.

Drawing easy shapes is simply the tip of the iceberg. The HTML5 Canvas API permits you to draw arcs, paths, textual content, gradients, and so forth. It’s also possible to manipulate your photographs pixel by pixel. This implies you can exchange one shade with one other in sure areas of the graphics, you may animate your drawings, and even draw a video onto the canvas and alter its look.

What Is SVG?

SVG stands for Scalable Vector Graphics. Based on the specification:

SVG is a language for describing two-dimensional graphics. As a standalone format or when combined with different XML, it makes use of the XML syntax. When combined with HTML5, it makes use of the HTML5 syntax. …

SVG drawings could be interactive and dynamic. Animations could be outlined and triggered both declaratively (i.e., by embedding SVG animation components in SVG content material) or through scripting.

SVG is an XML file format designed to create vector graphics. Being scalable has the benefit of letting you enhance or lower a vector picture whereas sustaining its crispness and top quality. (This may’t be executed with HTML5 Canvas-generated photographs.)

Right here’s the identical crimson sq. (beforehand created with HTML5 Canvas) this time drawn utilizing SVG:

See the Pen
Fundamental SVG Form
by SitePoint (@SitePoint)
on CodePen.

<svg xmlns="" viewbox="0 0 600 600">
  <desc>Purple rectangle form</desc>
  <rect x="10" y="10" width="100" peak="100" fill="#c00"  />  

You are able to do with SVG many of the stuff you are able to do with Canvas — comparable to drawing shapes and paths, gradients, patterns, animation, and so forth. Nevertheless, these two applied sciences work in basically other ways. Not like a Canvas-based graphic, SVG has a DOM, and as such each CSS and JavaScript have entry to it. For instance, you may change the appear and feel of an SVG graphic utilizing CSS, animate its nodes with CSS or JavaScript, make any of its components reply to a mouse or a keyboard occasion the identical as a <div>. As it is going to grow to be clearer within the following sections, this distinction performs a major half whenever you want to choose between Canvas and SVG to your subsequent undertaking.

It’s essential to tell apart between rapid mode and retained mode. HTML5 Canvas is an instance of the previous, SVG of the latter.

Instant mode implies that, as soon as your drawing is on the canvas, the canvas stops preserving monitor of it. In different phrases you, because the developer, you could work out the instructions to attract objects, create and preserve the mannequin or scene of what the ultimate output ought to appear like, and specify what must be up to date. The browser’s Graphics API merely communicates your drawing instructions to the browser, which then executes them.

SVG makes use of the retained method, the place you merely concern your drawing directions of what you wish to show on the display screen, and the browser’s Graphics API creates an in-memory mannequin or scene of the ultimate output and interprets it into drawing instructions to your browser.

Being an instantaneous graphics system, Canvas hasn’t obtained a DOM, or Doc Object Mannequin. With Canvas, you draw your pixels and the system forgets all about them, thereby slicing down on the additional reminiscence wanted to take care of an inner mannequin of your drawing. With SVG, every object you draw will get added to the browser’s inner mannequin, which makes your life as a developer considerably simpler, however at some prices by way of efficiency.

On the idea of the excellence between rapid and retained mode, in addition to different particular traits of Canvas and SVG respectively, it’s attainable to stipulate some circumstances the place utilizing one expertise over the opposite may higher serve your undertaking’s targets.

HTML5 Canvas: Professionals and Cons

The HTML5 Canvas specification clearly recommends that authors ought to not use the <canvas> aspect the place they produce other extra appropriate means obtainable to them.

For example, for a graphically wealthy <header> aspect, the very best instruments are HTML and CSS, not <canvas> (and neither is SVG, by the best way). Dr Summary, creator and founding father of the Zim JS canvas framework, confirms this view as he writes:

The DOM and the DOM frameworks are good at displaying data and particularly textual content data. Compared, the canvas is an image. Textual content could be added to the canvas and it may be made responsive, however the textual content can’t be readily chosen or searched as on the DOM. Lengthy scrolling pages of textual content or textual content and pictures is a wonderful use of the DOM. The DOM is nice for social media websites, boards, purchasing, and all the knowledge apps we’re used to. — “When to Use a JavaScript Canvas Library or Framework

So, that’s a clear-cut instance of when to not use <canvas>. However when is <canvas> a very good possibility?

This tweet by Sarah Drasner summarizes some main execs and cons of canvas options and capabilities, which will help us to work out some use circumstances for this highly effective expertise:

You may view the picture from the tweet right here.

What HTML5 Canvas Can Be Nice For

Alvin Wan has benchmarked Canvas and SVG by way of efficiency each in relation to the variety of objects being drawn and in relation to the dimensions of the objects or the canvas itself. He states his outcomes as follows:

In sum, the overhead of DOM rendering is extra poignant when juggling tons of if not hundreds of objects; on this state of affairs, Canvas is the clear winner. Nevertheless, each the canvas and SVG are invariant to object sizes. Given the ultimate tally, the canvas gives a transparent win in efficiency.

Drawing on what we learn about Canvas, particularly its glorious efficiency at drawing a number of objects, listed below are some attainable situations the place it may be acceptable, and even preferable to SVG.

Video games and Generative Artwork

For graphics-intensive, extremely interactive video games, in addition to for generative artwork, Canvas is mostly the best way to go.

Ray Tracing

Ray tracing is a way for creating 3D graphics.

Ray tracing can be utilized to hydrate a picture by tracing the trail of sunshine by pixels in a picture airplane and simulating the results of its encounters with digital objects. … The results achieved by ray tracing, … vary from … creating life like photographs from in any other case easy vector graphics to making use of photo-like filters to take away red-eye. — SVG vs Canvas: how to decide on, Microsoft Docs.

For those who’re curious, right here’s a raytracer utility in motion by Mark Webster.

Canvas Raytracer by Mark Webster

Nevertheless, though HTML5 Canvas is certainly higher suited to the duty than SVG may ever be, it doesn’t essentially observe that ray tracing is finest executed on a <canvas> aspect. The truth is, the pressure on the CPU might be fairly appreciable, to the purpose that your browser may cease responding.

Drawing a Important Variety of Objects on a Small Floor

One other instance is a state of affairs the place your utility wants to attract a major variety of objects on a comparatively small floor — comparable to non-interactive real-time information visualizations like graphical representations of climate patterns.

Graphical representation of weather patterns with HTML5 Canvas

The above picture is from this MSDN article, which was a part of my analysis for this piece.

Pixel Substitute in Movies

As demonstrated on this HTML5 Physician article, one other instance the place Canvas can be acceptable is when changing a video background shade with a unique shade, one other scene, or picture.

Replacing pixels in a video to convert it to grayscale on the fly using HTML5 Canvas

What HTML5 Canvas Isn’t So Nice For

Then again, there are a variety of circumstances the place Canvas may not be your best option in comparison with SVG.


Most situations the place scalability is a plus are going to be higher served utilizing SVGs fairly than Canvas. Excessive-fidelity, complicated graphics like constructing and engineering diagrams, organizational charts, organic diagrams, and so forth., are examples of this.

When drawn utilizing SVG, enlarging the photographs or printing them preserves all the small print to a excessive degree of high quality. It’s also possible to generate these paperwork from a database, which makes the XML format of SVG extremely suited to the duty.

Additionally, these graphics are sometimes interactive. Take into consideration seat maps whenever you’re reserving your airplane ticket on-line for example, which makes them a fantastic use case for a retained graphics system like SVG.

That stated, with the assistance of some nice libraries like CreateJS and Zim (which extends CreateJS), builders can comparatively shortly combine mouse occasions, hit checks, multitouch gestures, drag and drop capabilities, controls, and extra, to their Canvas-based creations.


Though there are steps you may take to make a Canvas graphic extra accessible — and a very good Canvas library like Zim might be useful right here to hurry up the method — canvas doesn’t shine in terms of accessibility. What you draw on the canvas floor is only a bunch of pixels, which might’t be learn or interpreted by assistive applied sciences or search engine bots. That is one other space the place SVG is preferable: SVG is simply XML, which makes it readable by each people and machines.

No Reliance on JavaScript

For those who don’t wish to use JavaScript in your utility, then Canvas isn’t your finest buddy. The truth is, the one approach you may work with the <canvas> aspect is with JavaScript. Conversely, you may draw SVG graphics utilizing a normal vector modifying program like Adobe Illustrator or Inkscape, and you need to use pure CSS to manage their look and carry out eye-catching, delicate animations and microinteractions.

Combining HTML5 Canvas and SVG for Superior Eventualities

There are circumstances the place your utility can get the very best of each worlds by combining HTML5 Canvas and SVG. For example, a Canvas-based recreation may implement sprites from SVG photographs generated by a vector modifying program, to benefit from the scalability and lowered obtain measurement in comparison with a PNG picture. Or a paint program may have its consumer interface designed utilizing SVG, with an embedded <canvas>aspect, which might be used for drawing.

Lastly, a strong library like Paper.js makes it attainable to script vector graphics on prime of HTML5 Canvas, thereby providing devs a handy approach of working with each applied sciences.


On this article, I’ve explored some key options of each HTML5 Canvas and SVG that will help you determine which expertise may be most suited suited to specific duties.

What’s the reply?

Chris Coyier agrees with Benjamin De Cock, as the latter tweets:

Dr Summary gives a lengthy record of issues which are finest constructed utilizing Canvas, together with interactive logos and promoting, interactive infographics, e-learning apps, and far more.

In my opinion, there aren’t any laborious and quick guidelines for when it’s finest to make use of Canvas as an alternative of SVG. The excellence between rapid and retained mode factors to HTML5 Canvas as being the undisputed winner in terms of constructing issues like graphic-intensive video games, and to SVG as being preferable for issues like flat photographs, icons and UI components. Nevertheless, in between there’s room for HTML5 Canvas to develop its attain, particularly contemplating what the varied highly effective Canvas libraries may deliver to the desk. Not solely do they make it simpler to work with each applied sciences in the identical graphic work, however in addition they make it in order that some hard-to-implement options in a local Canvas-based undertaking — like interactive controls and occasions, responsiveness, accessibility options, and so forth — may now be at most builders’ fingertips, which leaves the door open to the potential for ever extra fascinating makes use of of HTML5 Canvas.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *