1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
const canvas = document.getElementById('fireworksCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particles = [];
function randomColor() {
const colors = ['#ff5e57', '#ffbc42', '#4bd3a1', '#32a0f6', '#f542bc'];
return colors[Math.floor(Math.random() * colors.length)];
}
class Particle {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.size = Math.random() * 5 + 1;
this.speedX = (Math.random() - 0.5) * 6;
this.speedY = (Math.random() - 0.5) * 6;
this.color = color;
this.life = 100;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
this.life -= 2;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
}
isAlive() {
return this.life > 0;
}
}
function createFirework(x, y) {
const color = randomColor();
for (let i = 0; i < 100; i++) {
particles.push(new Particle(x, y, color));
}
}
function animateFireworks() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles = particles.filter(p => p.isAlive());
particles.forEach(particle => {
particle.update();
particle.draw();
});
if (particles.length === 0) {
createFirework(canvas.width / 2, canvas.height / 2);
}
requestAnimationFrame(animateFireworks);
}
animateFireworks();
|