Lesson 9 of 9 · 27m
Choosing SVG, Canvas, WebGL, or the DOM
Make pragmatic rendering decisions using object lifetime, interaction, accessibility, motion, text, asset type, team cost, and failure modes.
The worst graphics decision is the one made from fashion. Teams pick canvas because it sounds fast, SVG because it sounds semantic, or WebGL because a demo looked impressive. Six months later they are rebuilding hit testing, accessibility, text layout, and responsive scaling that another technology already provided.
Compare scene memory, not slogans
| Technology | Scene memory | Best at | Main cost |
|---|---|---|---|
| HTML/CSS | DOM tree | text and controls | layout under heavy mutation |
| SVG | vector scene graph | diagrams and charts | DOM overhead at scale |
| Canvas 2D | none | bitmaps and dynamic plots | manual interaction/semantics |
| WebGL | none | parallel geometry and shaders | GPU complexity |
"SVG is slow" and "canvas is fast" are not engineering statements. Ask what work each frame requires.
Object lifetime is the first filter
Long-lived, individually interactive, semantically meaningful objects favor SVG or DOM. A network diagram has nodes users click, label, focus, drag, and inspect.
Ephemeral or numerous primitives — particles, waveform samples, rain, spectral bars, dense scatter points — favor canvas or WebGL. Nobody needs fifty thousand focusable DOM nodes representing noise.
< 500 simple interactive objects SVG often fine
500-10,000 dynamic primitives canvas often fine
10,000+ moving primitives/effects WebGL enters the conversationThese are not hard limits. Profile real shape complexity.
Text is the hidden deal-breaker
Canvas and WebGL do not provide normal text layout. Wrapping, font fallback, bidi shaping, selection, spellcheck, accessibility, and localization are not free. If the product is mostly labeled visual content, keep text in HTML/SVG or use a mature rendering library with a real text pipeline.
Interaction cost is implementation cost
SVG gives pointer events per element. Canvas requires picking. WebGL requires picking plus GPU-aware architecture. Ask whether every object needs keyboard focus, whether users select text, whether semantic relationships matter, and whether hit regions are irregular shapes or simple spatial cells.
Hybrids are normal production architecture
A dashboard might use HTML for controls and tables, SVG for small diagrams, canvas for dense time series, and WebGL for a heatmap. Keep projection, theme tokens, resize handling, and interaction state outside the renderer so one layer can be replaced independently.
Libraries are legitimate architecture. D3 transforms data into DOM/SVG/canvas, PixiJS is a mature 2D WebGL renderer, and Three.js abstracts 3D scenes. Using one is not cheating; building a half-tested scene graph inside product code is often the larger indulgence.
Field notes
- Decide who owns accessibility before choosing canvas. "Later" usually means never.
- Static export requirements favor SVG because vector output survives resizing and print.
- Full-screen animated backgrounds carry battery cost; reduced-motion and low-power behavior belong in the design.
- If WebGL is proposed for a business dashboard, require evidence: measured object counts, frame cost, or effects impossible in simpler renderers.
- The best migration is often hybridization: move only the bottleneck layer while preserving DOM controls and semantic fallbacks.
Was this lesson helpful?