Skip to main content
This example demonstrates how to create animations using the HTML5 Canvas API with Helios. Perfect for custom graphics, data visualizations, and high-performance animations.

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

1

Create the HTML canvas

Start with an HTML file containing a full-screen canvas element:
2

Initialize canvas and Helios

Set up the canvas context and initialize Helios:
3

Create the draw function

Implement your drawing logic based on the current frame:
4

Subscribe to state changes

Connect your draw function to Helios state updates:

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 currentFrame to calculate animation progress
  • Deterministic drawing: Same frame should always produce identical output
  • Canvas context: Store and reuse the 2D rendering context

Performance tips

  1. Clear efficiently: Only clear the portions of canvas that change
  2. Batch operations: Group similar drawing operations together
  3. Save/restore context: Use ctx.save() and ctx.restore() for transforms
  4. 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