这是我改进的代码,发现粒子明显多了
- // Function to generate a random color
- function getRandomColor() {
- const letters = '0123456789ABCDEF';
- let color = '#';
- for (let i = 0; i < 6; i++) {
- color += letters[Math.floor(Math.random() * 16)];
- }
- return color;
- }
- // Function to create a single firework particle
- function createFirework(x, y, color) {
- const firework = document.createElement('div');
- firework.classList.add('firework');
- firework.style.left = x + 'px';
- firework.style.top = y + 'px';
- firework.style.backgroundColor = color;
- document.getElementById('fireworks-container').appendChild(firework);
- // Randomize the animation
- const scale = Math.random() * 0.5 + 0.5; // Size between 0.5 and 1
- const duration = Math.random() * 900 + 100; // Duration between 100ms and 1000ms
- const opacity = Math.random() * 0.5 + 0.5; // Opacity between 0.5 and 1
- firework.style.transform = `scale(${scale})`;
- firework.style.opacity = opacity;
- firework.style.transition = `all ${duration}ms`;
- // Apply animation
- setTimeout(() => {
- firework.style.transform = `translate(${(Math.random() - 0.5) * 200}px, ${(Math.random() - 0.5) * 200}px) scale(0)`;
- firework.style.opacity = 0;
- }, 10);
- // Remove firework after animation ends
- setTimeout(() => {
- firework.remove();
- }, duration);
- }
- // Function to launch multiple fireworks at a random position
- function launchFireworks() {
- const count = Math.random() * 20 + 30; // Launch between 30 and 50 particles
- for (let i = 0; i < count; i++) {
- const x = Math.random() * window.innerWidth;
- const y = Math.random() * window.innerHeight;
- const color = getRandomColor();
- createFirework(x, y, color);
- }
- }
- // Launch new fireworks every 500 milliseconds
- setInterval(launchFireworks, 500);
复制代码
|