requestAnimationFrame
The foundation of smooth JavaScript animations. It synchronizes with the browser’s refresh rate for optimal performance.Basic pattern
Complete example
Here’s a real implementation from the Animation Playground:Cleanup
Always clean up animation frames to prevent memory leaks:GSAP animations
GSAP (GreenSock Animation Platform) is a professional-grade animation library that makes complex animations simple and performant.Basic usage
Complete example
GSAP advantages
- Simple, intuitive API
- Excellent performance
- Rich easing options
- Timeline sequencing
- Plugin ecosystem
Canvas animations
Canvas provides a low-level API for drawing graphics and creating complex animations. Perfect for particle effects, games, and data visualizations.Particle system
Complete implementation
When to use JavaScript over CSS
Choose JavaScript animations when you need:- Dynamic values: Animation parameters that change based on user input or application state
- Complex timing: Sequenced animations or precise timing control
- User interaction: Animations that respond to mouse position, drag gestures, or scroll
- Conditional logic: Animations that branch based on conditions
- Physics simulation: Realistic motion with gravity, friction, or spring physics
- Canvas rendering: Particle systems, custom graphics, or data visualizations
Performance tips
- Use
requestAnimationFrameinstead ofsetIntervalorsetTimeout - Cache DOM queries outside the animation loop
- Use transforms for position and scale changes
- Debounce resize events when recalculating animation parameters
- Profile with browser DevTools to identify bottlenecks