Bash.tv → GitHub → itch.io

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 GitHub

Canvas 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

2. Change state less

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

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).”