-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfireworks-straight.js
112 lines (100 loc) · 3.08 KB
/
fireworks-straight.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const canvas = document.getElementById('fireworksCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let fireworks = [];
class Firework {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = Math.random() * 3 + 2;
this.speed = Math.random() * 3 + 2;
this.angle = Math.random() * Math.PI * 2;
this.color = `hsl(${Math.random() * 360}, 100%, 50%)`;
this.exploded = false;
this.particles = [];
}
update() {
if (!this.exploded) {
this.y -= this.speed;
if (this.y < canvas.height / 2) {
this.explode();
}
} else {
this.particles.forEach((particle) => {
particle.update();
});
}
}
explode() {
this.exploded = true;
const particleCount = Math.random() * 100 + 50;
for (let i = 0; i < particleCount; i++) {
this.particles.push(new Particle(this.x, this.y, this.color));
}
}
draw() {
if (!this.exploded) {
ctx.fillStyle = this.color;
ctx.shadowColor = this.color; // Set shadow color to the firework color
ctx.shadowBlur = 20; // Set the blur for the glow effect
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.shadowBlur = 0; // Reset shadow blur for particles
} else {
this.particles.forEach((particle) => {
particle.draw();
});
}
}
}
class Particle {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.size = Math.random() * 3 + 1;
this.speed = Math.random() * 3 + 1;
this.angle = Math.random() * Math.PI * 2;
this.color = color;
this.alpha = 1;
}
update() {
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed;
this.alpha -= 0.01;
}
draw() {
ctx.save();
ctx.globalAlpha = this.alpha;
ctx.fillStyle = this.color;
ctx.shadowColor = this.color; // Set shadow color to the particle color
ctx.shadowBlur = 10; // Set the blur for the glow effect
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
function createFirework() {
const x = Math.random() * canvas.width;
const y = canvas.height;
fireworks.push(new Firework(x, y));
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
fireworks.forEach((firework, index) => {
firework.update();
firework.draw();
if (firework.exploded && firework.particles.length === 0) {
fireworks.splice(index, 1);
}
});
requestAnimationFrame(animate);
}
// setInterval(createFirework, 1000);
createFirework();
setTimeout(createFirework, 2000);
setTimeout(createFirework, 4000);
setTimeout(() => { setInterval(createFirework, 3000); }, 5000);
animate();