Canvas Animations
Bring your canvas to life with smooth animations, particle systems, sprite animations, and interactive effects using requestAnimationFrame and advanced animation techniques.
How do you animate on an HTML canvas?
Use a loop driven by requestAnimationFrame: on each frame, clear the canvas (clearRect), update your objects' positions, and draw them again. requestAnimationFrame syncs with the display's refresh rate and pauses in background tabs. Use the timestamp it passes in to keep motion speed independent of frame rate.
let x = 0, last = 0;
function frame(t) {
const dt = (t - last) / 1000; last = t;
x += 120 * dt; // 120px per second
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillRect(x % canvas.width, 50, 40, 40);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);Animation Fundamentals
Canvas animations work by rapidly clearing and redrawing the canvas content. The key to smooth animation is using requestAnimationFrame() which synchronizes with the browser's refresh rate for optimal performance.
Animation Loop
Clear, update, draw, repeat
Performance
60 FPS smooth motion
Optimization
Efficient rendering
Animation Techniques
Basic Animations
- • Position changes (movement)
- • Rotation and scaling
- • Color and opacity fades
- • Shape morphing
Physics Simulation
- • Gravity and friction
- • Collision detection
- • Bouncing and elasticity
- • Particle systems
Advanced Effects
- • Easing functions
- • Sprite sheet animations
- • Trail effects
- • Interactive animations
Optimization
- • Delta time calculations
- • Object pooling
- • Culling off-screen objects
- • Reduce drawing operations
Complete Animation Examples
Interactive examples including basic animation loops, physics simulations, particle systems, sprite animations, wave effects, mouse interactions, and easing functions.
Code Editor
<canvas id="c" width="320" height="180"></canvas>
<button id="toggle">Pause</button>Preview
Performance Optimization
Animation Loop Optimization
- • Use requestAnimationFrame() instead of setInterval
- • Implement delta time for frame-rate independence
- • Clear only dirty regions, not entire canvas
- • Limit the number of animated objects
- • Use object pooling for particles
Drawing Optimization
- • Batch similar drawing operations
- • Avoid changing context properties frequently
- • Use integer coordinates when possible
- • Pre-calculate expensive operations
- • Use layered canvases for static elements
Animation Performance Checklist
- Monitor frame rate (aim for 60 FPS)
- Profile animation performance
- Test on mobile devices
- Implement animation controls
- Handle visibility changes
- Use GPU acceleration when possible
Canvas Animations — step by step
Set up the animation loop with requestAnimationFrame
Use requestAnimationFrame(callback) instead of setInterval for smooth animations. It syncs with the display refresh rate, pauses when the tab is hidden, and gives better performance than timer-based loops.
🎯 Quick Check
What should you do at the start of each canvas animation frame?
Canvas Animations: summary
Key points
- Don't use
setIntervalfor animation;requestAnimationFrameis smoother and battery-friendly. - Redraw only what changed, or use several layered canvases, to keep heavy scenes fast.
- Respect
prefers-reduced-motionfor decorative animation.
Frequently asked questions
›Why is my canvas animation leaving trails?
You're not clearing the previous frame. Call ctx.clearRect(0, 0, width, height) at the start of each frame.