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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- Event bubbling and capturingA click travels down to its target and back up, calling listeners in order. Beginner
- Reflow vs repaintWhy changing a width costs the browser more than changing a colour. Intermediate
- GPU compositingHow browsers split a page into layers and let the graphics chip move them. Advanced
Spotted a mistake on this page?