Canvas 2D and SVG animation techniques for web scenes
canvassvganimationjavascriptrequestanimationframeaccessibility
Patterns for building animated scenes in the browser with plain Canvas 2D, SVG and CSS, no game engine. Drawn from "The Big Top" circus page (July 2026) and the AnimateStudio SVG puppet engine (July 2026).
Canvas setup
- Cap the device pixel ratio at 2:
dpr = Math.min(devicePixelRatio, 2), size the backing store tocssSize * dpr, andctx.setTransform(dpr, 0, 0, dpr, 0, 0). Higher ratios cost a lot of fill rate for little visible gain. - Use separate canvases per layer: a scene canvas and a fixed full-screen overlay for particles (confetti, fireworks) with a higher z-index. Resize them together.
- Recompute layout every frame from the viewport (
geo()returning center, ring position, and a scale clamped to a sane range such asclamp(min(W,H)/760, 0.62, 1.25)), so resizing and rotation just work. - Use the
lightercomposite mode for light effects (spotlight beams, pools of light).
Structure
- Model each act or scene as an object with
update(dt, t)anddraw(ctx, t); the mainrequestAnimationFrameloop calls the active one. - Share drawing helpers (a parametric stick-figure
performer(ctx, x, y, scale, colors, pose)where pose holds limb angles) rather than hand-drawing each character. - Guard end-of-act transitions. End conditions usually stay true every frame, so an
if (done) finish()check fires dozens of times during the teardown delay (in the circus: dozens of applause sounds and confetti bursts). Set afinishingflag on the first call. - "Showbiz physics": for a catch that must always land (trapeze), compute the ballistic arc and lerp it toward the catcher's hand over the last part of the flight. It looks physical and never misses.
- Juggling cascade: with beat B, each ball's airtime is about 4B for a 5-ball pattern, and hand alternation follows the throw count parity.
SVG tricks
- Curved marquee text:
<textPath>on an arc. Decorations around it (chase bulbs) must sit on a concentric circle with the same center as the text arc, at a larger radius, over the same angular range. An ad-hoc formula put the bulbs below the text. - 2D cutout (puppet) animation: build characters as nested SVG
<g>groups, one per bone, each with atransform="rotate(angle, pivotX, pivotY)". Clips are keyframe lists per joint (keep them sorted by time and within the clip duration), interpolated in a rAF loop. A "parts library" maps character traits to part shapes, so a character is pure JSON data that can later become the on-disk format. - A camera can be just the SVG
viewBox, animated.
Accessibility and polish
- Honor
prefers-reduced-motionboth in CSS (media query) and in JS (areducedflag): static beams, fewer particles, no screen shake or bobbing. - A considered single dark theme is fine for a night scene; do not bolt on a light mode just because.
See also
- Procedural music and sound effects with the Web Audio API
- Zero-build web apps (vanilla JS, localStorage, single file)
- Arcade game design toolkit (loops, enemies, scoring, structure)