Interactive Flower Explosion - MeggiTools
Run
Toggle Theme
Share Link
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Interactive Flower Explosion</title> <style> body { margin: 0; padding: 0; overflow: hidden; background: #111; } canvas { display: block; } </style> </head> <body> <canvas id="flowerCanvas"></canvas> <script> const canvas = document.getElementById('flowerCanvas'); const ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; class Flower { constructor(x, y, size, color, speedX, speedY) { this.x = x; this.y = y; this.size = size; this.color = color; this.speedX = speedX; this.speedY = speedY; this.opacity = 1; this.decay = Math.random() * 0.02 + 0.01; } draw() { ctx.save(); ctx.translate(this.x, this.y); ctx.globalAlpha = this.opacity; ctx.fillStyle = this.color; for (let i = 0; i < 8; i++) { ctx.beginPath(); ctx.arc(this.size, 0, this.size / 2, 0, Math.PI * 2); ctx.fill(); ctx.rotate(Math.PI / 4); } ctx.restore(); } update() { this.x += this.speedX; this.y += this.speedY; this.opacity -= this.decay; if (this.opacity <= 0) { this.opacity = 0; } this.draw(); } isFadedOut() { return this.opacity <= 0; } } const flowers = []; function createFlowerExplosion(x, y) { for (let i = 0; i < 30; i++) { const size = 10 + Math.random() * 20; const color = `hsl(${Math.random() * 360}, 100%, 50%)`; const speedX = Math.random() * 10 - 5; const speedY = Math.random() * 10 - 5; flowers.push(new Flower(x, y, size, color, speedX, speedY)); } } canvas.addEventListener('click', function(event) { createFlowerExplosion(event.clientX, event.clientY); }); function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (let i = 0; i < flowers.length; i++) { flowers[i].update(); if (flowers[i].isFadedOut()) { flowers.splice(i, 1); i--; } } requestAnimationFrame(animate); } animate(); </script> </body> </html>