example-react
React Composition Patterns
Integrate Helios into React components using hooks for state management and Refs for canvas access.
Quick Start
1. Create useVideoFrame Hook
This hook subscribes to Helios and returns the current frame, triggering re-renders.
// hooks/useVideoFrame.js
import { useState, useEffect } from 'react';
export function useVideoFrame(helios) {
const [frame, setFrame] = useState(helios.currentFrame.peek());
useEffect(() => {
// Subscribe to updates
const unsubscribe = helios.subscribe((state) => {
setFrame(state.currentFrame);
});
return unsubscribe;
}, [helios]);
return frame;
}
2. Create Composition Component
// App.jsx
import React, { useRef, useEffect } from 'react';
import { Helios } from '@helios-project/core';
import { useVideoFrame } from './hooks/useVideoFrame';
// Initialize Singleton (outside component to persist across re-renders)
const helios = new Helios({
duration: 10,
fps: 30
});
helios.bindToDocumentTimeline();
// Expose for Renderer/Player
if (typeof window !== 'undefined') window.helios = helios;
export default function App() {
const canvasRef = useRef(null);
const frame = useVideoFrame(helios);
useEffect(() => {
const ctx = canvasRef.current?.getContext('2d');
if (!ctx) return;
const { width, height } = canvasRef.current;
// Clear
ctx.clearRect(0, 0, width, height);
// Draw based on frame
const progress = frame / (helios.duration * helios.fps);
ctx.fillStyle = '#61dafb';
ctx.fillRect(progress * width, height / 2 - 50, 100, 100);
}, [frame]); // Re-run draw when frame changes
return <canvas ref={canvasRef} width={1920} height={1080} />;
}
Optimization
For complex scenes, avoid React state updates for every frame (which triggers full component re-render). Instead, use a useRef to hold the frame and an animation loop, or update the canvas imperatively inside subscribe.
Imperative Pattern (High Performance)
useEffect(() => {
const ctx = canvasRef.current.getContext('2d');
const unsubscribe = helios.subscribe((state) => {
// Draw directly without triggering React render
drawScene(ctx, state.currentFrame);
});
return unsubscribe;
}, []);
Source Files
- Example:
examples/react-canvas-animation/
More from bintzgavin/helios-skills
motion-design-rules
Motion design framework for programmatic video. Defines anti-slideshow architecture, visual layering, physics-based easing, choreography rules, and quality validation. Reference this skill from any guided video skill.
6helios-renderer
Renderer API for generating video/image output from Helios compositions. Use when you need to programmatically render a composition to a file using Node.js.
5guided-product-demo
End-to-end guided workflow for creating a product demo or showcase video. Extracts brand identity from the repo, generates a polished soundtrack, and produces a feature-focused motion.dev composition rendered via Helios CLI. Use when making product demos, feature showcases, or UI walkthroughs.
5guided-promo-video
End-to-end guided workflow for creating a promotional hype video. Extracts brand identity from the repo, generates beat-synced music, and produces a high-energy motion.dev composition rendered via Helios CLI. Use when making brand-aligned promo or hype videos.
5render-video
Workflow for rendering a Helios composition to a video file. Use when you need to automate video production or export a high-quality animation.
4helios-core
Core API for Helios video engine. Use when creating compositions, managing timeline state, controlling playback, or subscribing to frame updates. Covers Helios class instantiation, signals, animation helpers, and DOM synchronization.
4