Frontend / Canvas vs SVG

Canvas vs SVG

Two ways to draw in a browser: describe shapes, or paint pixels. Draw one circle both ways, then move it, zoom it, click it and multiply it to see where each one wins.

Beginner7 steps~6 min
SVG — shapes kept as elements
Canvas — a grid of pixels your code paints
Code — what you write for each
// THE SHORT VERSION

In 30 seconds

SVG describes a picture as shapes. Each circle or line is an element in the page, which the browser remembers, redraws, scales sharply and lets you click.

Canvas is a rectangle of pixels that your code paints. Once a shape is drawn, only its coloured pixels remain; the browser knows nothing about it.

So SVG is convenient and sharp but gets heavy with thousands of shapes. Canvas handles huge numbers of things, but moving, clicking and describing them is your code’s job.

Canvas vs SVG, step by step

STEPS01 / 07

The canvas here is the 2D kind; WebGL and WebGPU canvases also end in pixels. Real canvas edges are slightly softened (anti-aliased). Where SVG starts to slow depends on the device and on what moves.

STEP 01 / 07 Two ways to draw a circle
THE MATH
<circle cx="50" cy="50" r="30"/> ctx.arc(50, 50, 30, 0, 2 * Math.PI)
  1. Step 1: Two ways to draw a circle. The browser offers two drawing tools. With SVG you write an element that describes the shape. With canvas you run commands that paint it. Asked for a circle of radius 30 in the middle of a 100 by 100 area, both put the same picture on screen.
  2. Step 2: A shape, or just pixels. SVG keeps the circle as an element in the page’s DOM, with its centre and radius. The canvas keeps no circle at all. Once the drawing command has run, all that remains is a grid of pixels, four numbers each: red, green, blue and opacity.
  3. Step 3: Changing the picture. To move the circle in SVG, change one attribute; the browser knows what the scene contains and repaints it. On a canvas the old pixels stay until you erase them, and the browser cannot redraw what it never remembered, so your code clears the area and draws every shape again.
  4. Step 4: Zooming in. Enlarge both 16 times. SVG is stored as geometry, so the browser recomputes the edge at the new size and it stays sharp. The canvas holds a fixed 100 by 100 pixels; enlarging only stretches them into visible blocks. To get a sharp canvas you must give it more pixels and draw again.
  5. Step 5: Clicks and screen readers. An SVG circle is an element, so it can have its own click listener, hover style and a title a screen reader will read. A canvas is one element whatever it shows. A click only reports x and y; your code must work out what was hit, and must describe the picture for people who cannot see it.
  6. Step 6: Ten thousand dots. Now draw 10 000 dots. In SVG that is 10 000 elements for the browser to style, lay out and keep in memory, and pages slow down as the count climbs into the thousands. The canvas is still one element with the same 40 000 bytes of pixels. Its cost is the drawing your code does each frame.
  7. Step 7: Which one to pick. Pick SVG when the picture is made of a modest number of shapes that must stay sharp, respond to the user or be read aloud: icons, charts, diagrams. Pick canvas when there are very many things on screen or you need the pixels themselves: games, particle effects, photo filters. Many pages use both.
VALUES SO FAR
circler 30 at (50,50)input
SVG····model
canvas····model
to move····update
zoom ×16····scaling
hit test····canvas
SVG····cost
canvas····fixed
pick by····rule

Spotted a mistake on this page?