Twinkling Stars Chandelier - 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>Twinkling Stars Chandelier</title> <style> body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #1e1e1e; } canvas { background-color: #3f3f3f; } </style> </head> <body> <canvas id="twinklingStarsCanvas"></canvas> <script> const canvas = document.getElementById('twinklingStarsCanvas'); const ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; const chandelier = { lines: [], numberOfLines: 20, maxLength: 200, swingSpeed: 0.03, maxSwingAngle: 0.25 }; for (let i = 0; i < chandelier.numberOfLines; i++) { chandelier.lines.push({ x: canvas.width / 2 - chandelier.numberOfLines * 10 + i * 20, y: 100, length: Math.random() * chandelier.maxLength, swingAngle: Math.random() * Math.PI * 2, swingSpeed: chandelier.swingSpeed + Math.random() * 0.02 }); } function drawChandelier() { ctx.clearRect(0, 0, canvas.width, canvas.height); chandelier.lines.forEach(line => { const swing = Math.sin(line.swingAngle) * chandelier.maxSwingAngle; const endX = line.x + line.length * Math.sin(swing); const endY = line.y + line.length * Math.cos(swing); ctx.strokeStyle = '#fff'; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(line.x, line.y); ctx.lineTo(endX, endY); ctx.stroke(); drawLightBulb(endX, endY); }); } function drawLightBulb(x, y) { const twinkleFactor = Math.random() * 0.5 + 0.5; // Twinkle effect ctx.fillStyle = `rgba(255, 255, 0, ${twinkleFactor})`; ctx.beginPath(); ctx.arc(x, y, 12, 0, Math.PI * 2); ctx.fill(); } function updateChandelier() { chandelier.lines.forEach(line => { line.swingAngle += line.swingSpeed; if (line.swingAngle > Math.PI * 2) { line.swingAngle = 0; } }); } function animate() { updateChandelier(); drawChandelier(); requestAnimationFrame(animate); } animate(); </script> </body> </html>