Optimize HTML5 Canvas 2D drawing. Measure render time, then cut draw calls and state changes, cache static layers and text in offscreen canvases, cull off-screen objects, batch by image and style, use sprite atlases, avoid slow operations (shadowBlur, filters, getImageData, huge paths, sub-pixel text), size the canvas to devicePixelRatio sensibly, use layered canvases wisely, and know when to m...
Download skill (.zip) Files on GitHubCanvas performance
First measure the render time on its own (__perfMark in game-performance-profiling’s probe). If render is the
problem, work down this list. Background on drawing is in canvas-2d-rendering.
1. Draw less
- Cull: skip anything outside the camera rectangle (+ margin). Use the spatial grid to find visible objects in big worlds.
- Cache static layers: tilemaps, backgrounds and decorations drawn once into an offscreen canvas (or chunks of 512×512 for big maps),
then one
drawImageper chunk per frame. Redraw a chunk only when it changes. - Cache text: labels and HUD numbers that change rarely → draw them to a small canvas when the value changes.
- Particles: fewer, bigger particles look the same. Cap counts per effect, lower them on phones.
2. Change state less
- Sort or group draws by image, then by fillStyle / font / globalAlpha / composite op. Each change has a cost.
- One sprite atlas instead of many images: the same source image back to back is fastest.
setTransform(a, b, c, d, e, f)once per sprite instead ofsave → translate → rotate → scale → restore.- Set
fontonce per group of text (setting it is expensive; it parses a string).
3. Avoid the slow operations per frame
| Slow | Instead |
|---|---|
shadowBlur, ctx.filter |
Pre-rendered glow sprites; draw a blurred copy once and cache it |
getImageData / putImageData |
Logic on your own arrays; read pixels only once at load (masks, collision maps) |
Huge or complex paths, arc for 1,000 particles |
Small pre-drawn circle sprites via drawImage |
Text with strokeText + fillText every frame for many labels |
Cached label canvases |
drawImage scaling big images down every frame |
Pre-scale once to the drawn size |
Clearing with clearRect + then drawing a full background |
Just draw the background (it covers everything) |
| Many full-screen layered canvases with transparency | 1–2 canvases: static background layer + game layer |
4. Resolution
- Canvas backing size = CSS size ×
min(devicePixelRatio, 2). Huge canvases (4K at DPR 3) cost fill rate on phones. - Pixel art: render at the low native resolution into a small canvas, then scale up the whole canvas with
image-rendering: pixelated. That’s the fastest option of all. - Adaptive quality: drop to DPR 1 when frame time stays high (game-performance-optimization).
5. When Canvas 2D isn’t enough
More than ~1–2k moving sprites, many particles with blending, or shaders → PixiJS (pixijs-game-development) or Phaser (WebGL batching draws thousands of sprites in one call).
Prove it
Same heavy scene, render time before → after: “render 14.2 ms → 3.1 ms (tilemap cached in chunks, particles as sprites, culled).”