SVG vs Canvas: retained shapes or drawn pixels

Compare SVG and the HTML canvas: retained DOM shapes versus immediate pixel drawing, with code, trade-offs and clear advice on which to pick.

SVG and the HTML <canvas> element both draw 2D graphics in the browser, but they work in opposite ways. SVG describes a scene as a tree of elements. Canvas gives you a bitmap and a set of JavaScript drawing calls. Most of the trade-offs between them follow from that single difference.

How each one works

With SVG, every shape is an element in the document. You declare it, and the browser keeps track of it and redraws it when needed. You can change an attribute later and the picture updates.

<svg viewBox="0 0 200 120" xmlns="http://www.w3.org/2000/svg">
  <circle id="dot" cx="60" cy="60" r="40" fill="#1f3fe0"/>
</svg>

With canvas, you get a rectangle of pixels and a context object. MDN’s basic example gets a 2D context and paints a rectangle:

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "green";
ctx.fillRect(10, 10, 150, 100);

After fillRect runs, the canvas holds colored pixels and nothing else. As MDN’s overview implies, it keeps no record of the shapes you drew, so it cannot tell you what objects are on it. To change the picture you clear the canvas and draw again. Developers often describe this as immediate mode, and SVG as retained mode.

What the retained model gives you

Because SVG shapes are DOM elements, a lot comes for free:

  • Events. You can attach a click or hover handler to a single shape. With canvas, you must work out which shape sits under the pointer yourself.
  • CSS. Inline SVG shapes can be styled with selectors, custom properties and transitions.
  • Inspection. Open the browser’s developer tools and select a shape. You can edit its attributes live.
  • Text and structure. Titles, descriptions, ARIA attributes and real text can be attached to shapes. See SVG accessibility.
  • Scaling. Vector output stays sharp when zoomed or printed.

Here is a tiny interactive-looking picture built with no script at all. The hover color is a CSS rule inside the SVG. In the playground, point at the circle.

Doing the same in canvas takes a mouse listener, a hit test, a state variable and a redraw.

What canvas gives you

Canvas is a bitmap, so its cost depends on the size of the bitmap and the work you do per frame, not on how many objects you have conceptually. MDN lists typical uses as animation, game graphics, data visualization, photo manipulation and real-time video processing.

Canvas is the better choice when:

  • You manipulate pixels. Reading and writing pixel data, applying custom image processing, or compositing video frames are canvas jobs.
  • You draw very many things per frame. A particle effect or a large scatter plot with an enormous number of points can be cheaper to paint into a bitmap than to hold as a huge number of DOM nodes. This is a general trade-off that depends on the browser and device, so measure with your own data before you decide.
  • You do not need the objects afterward. If a picture is purely output, retaining the scene has no value.

If you need still more throughput or 3D, MDN points to WebGL, which also uses a <canvas> element. See SVG vs WebGL.

Resolution and zoom

SVG output is resolution independent. Canvas output is not. MDN notes that canvas pixels do not scale like vectors, so content becomes blurry when zoomed and enlarged pixels show. A canvas is usually sized for the device’s pixel density by hand, and it must be redrawn when its size changes. SVG just redraws at the new size.

Accessibility

This is a strong reason to prefer SVG for charts and diagrams. MDN’s canvas guidance says drawn objects are not exposed to assistive technology the way semantic HTML is, that text drawn on a canvas can be hard to read under magnification, and that a canvas should be treated like an image: do not use it as the only container for significant content, and provide accessible markup alongside it.

With SVG, labels are real text and can be read by a screen reader when you mark the graphic up correctly. Good markup is still your responsibility, but the structure is available to build on.

Code size and tooling

Canvas has no built-in scene, so non-trivial projects tend to adopt a library. MDN lists Fabric.js, Konva.js and p5.js as libraries that simplify common tasks. SVG has its own ecosystem, and you can also skip libraries entirely for small graphics because the markup is the scene. For a view of working with SVG from script, see SVG with JavaScript.

Combining them

The two work together. A chart might draw its axes, labels and legend in SVG, where text and accessibility matter, and render a dense point cloud into a canvas layered beneath it. Layering lets each technology do the job it is good at.

Here is a case where SVG is clearly the better fit: a diagram with labeled, grouped parts.

When to choose which

Choose SVG for icons, logos, diagrams, charts with a modest number of marks, maps with clickable regions, and any graphic that must be accessible, styleable or printed sharply.

Choose canvas for pixel processing, games, real-time visualizations with very large amounts of data, and effects that are recomputed every frame.

Do not choose canvas just because it feels more like programming, and do not choose SVG just because the output looks crisp. Ask what the graphic has to do. If users need to select, inspect or read parts of it, keep the parts as elements. If it is a fast-changing field of pixels, draw it as pixels.

When you are unsure, start with SVG. It is easy to build, easy to debug, and you can move the heaviest layer to canvas later if measurements show a problem.

Sources

Checked against these references on 2026-11-21.