Explore creative coding with interactive Canvas animations. Build particle systems, wave animations, and visual effects from scratch.
Create mesmerizing particle effects with Canvas API
Interactive Particle System! Move your mouse to emit particles. Click anywhere for a particle burst! Adjust controls to create different effects.
1// Basic Canvas Particle System2 3const canvas = document.getElementById('canvas');4const ctx = canvas.getContext('2d');5const particles = [];6 7class Particle {8 constructor(x, y) {9 this.x = x;10 this.y = y;11 this.vx = (Math.random() - 0.5) * 4;12 this.vy = (Math.random() - 0.5) * 4;13 this.radius = 2 + Math.random() * 3;14 this.color = `hsl(${Math.random() * 360}, 100%, 50%)`;15 this.life = 1.0;16 }17 18 update() {19 this.x += this.vx;20 this.y += this.vy;21 this.life -= 0.01; // Fade out over time22 }23 24 draw(ctx) {25 ctx.beginPath();26 ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);27 ctx.fillStyle = this.color;28 ctx.globalAlpha = this.life;29 ctx.fill();30 ctx.globalAlpha = 1;31 }32}33 34function animate() {35 // Clear with trail effect36 ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';37 ctx.fillRect(0, 0, canvas.width, canvas.height);38 39 // Update and draw particles40 particles.forEach((particle, index) => {41 particle.update();42 particle.draw(ctx);43 44 // Remove dead particles45 if (particle.life <= 0) {46 particles.splice(index, 1);47 }48 });49 50 requestAnimationFrame(animate);51}52 53// Spawn particles on click54canvas.addEventListener('click', (e) => {55 for (let i = 0; i < 20; i++) {56 particles.push(new Particle(e.offsetX, e.offsetY));57 }58});59 60animate();Canvas particle systems create visual effects using hundreds or thousands of small objects. **Key concepts:** - **requestAnimationFrame**: Smooth 60fps animations - **Particle lifecycle**: Spawn → Update → Draw → Destroy - **Physics simulation**: Velocity, gravity, collisions - **Visual effects**: Trails, fading, color schemes **Performance tips:** - Limit particle count (100-500 is sweet spot) - Remove dead particles to prevent memory leaks - Use object pooling for better performance - Clear canvas efficiently (fillRect with alpha vs clearRect)
HTML5 Canvas is a 2D drawing API that lets you create graphics, animations, and interactive visualizations using JavaScript.
Perfect for games, data visualizations, generative art, particle effects, and any graphics that need pixel-level control.
Use requestAnimationFrame, limit particle counts, avoid unnecessary clears, and consider offscreen canvases for complex scenes.