Overview
The canvas animation example shows how to:- Set up a full-screen canvas element
- Respond to Helios state changes
- Draw frame-synchronized graphics
- Handle canvas resizing
Setup
How it works
State subscription
Helios uses a subscription model to notify your code when the timeline state changes. Each time Helios updates (during playback or seeking), your draw function is called with the current frame number.Frame-based rendering
Canvas animations in Helios are frame-based, not time-based. This ensures:- Deterministic rendering - same frame always produces same output
- Perfect seeking - any frame can be rendered independently
- Export reliability - rendered videos are frame-perfect
Canvas resizing
Proper canvas sizing is critical for quality output. The example handles window resize events and updates the canvas dimensions accordingly:Key concepts
- Subscribe pattern: Listen to state changes instead of using requestAnimationFrame
- Frame numbers: Use
currentFrameto calculate animation progress - Deterministic drawing: Same frame should always produce identical output
- Canvas context: Store and reuse the 2D rendering context
Performance tips
- Clear efficiently: Only clear the portions of canvas that change
- Batch operations: Group similar drawing operations together
- Save/restore context: Use
ctx.save()andctx.restore()for transforms - Avoid allocations: Reuse objects instead of creating new ones each frame
Complete example
View the complete source code in the simple-canvas-animation directory.Next steps
- Explore Three.js integration for 3D graphics
- Try PixiJS for high-performance 2D rendering
- Learn about P5.js integration for creative coding