-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (78 loc) · 2.28 KB
/
script.js
File metadata and controls
90 lines (78 loc) · 2.28 KB
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
const canvas = document.getElementById("fireworksCanvas");
const ctx = canvas.getContext("2d");
const audio = document.getElementById("birthdaySong");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function startCelebration() {
showFireworks();
audio.play(); // Memulai pemutaran lagu ulang tahun
setInterval(showFireworks, 80);
}
class Particle {
constructor(x, y, color, speedX, speedY) {
this.x = x;
this.y = y;
this.color = color;
this.radius = Math.random() * 3 + 1;
this.alpha = 1;
this.speedX = speedX;
this.speedY = speedY;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
this.alpha -= 0.01;
this.radius *= 0.96;
}
draw() {
ctx.save();
ctx.globalAlpha = this.alpha;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
class Firework {
constructor(x, y) {
this.x = x;
this.y = y;
this.particles = [];
this.createParticles();
}
createParticles() {
const colors = ["#ff5757", "#ffae42", "#42aaff", "#42ff72", "#ff42d9"];
for (let i = 0; i < 80; i++) {
const angle = Math.random() * Math.PI * 2;
const speed = Math.random() * 5 + 2;
const speedX = Math.cos(angle) * speed;
const speedY = Math.sin(angle) * speed;
const color = colors[Math.floor(Math.random() * colors.length)];
this.particles.push(new Particle(this.x, this.y, color, speedX, speedY));
}
}
update() {
this.particles = this.particles.filter((particle) => particle.alpha > 0.1);
this.particles.forEach((particle) => particle.update());
}
draw() {
this.particles.forEach((particle) => particle.draw());
}
}
let fireworks = [];
function showFireworks() {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height * 0.5;
fireworks.push(new Firework(x, y));
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
fireworks = fireworks.filter((firework) => firework.particles.length > 0);
fireworks.forEach((firework) => {
firework.update();
firework.draw();
});
requestAnimationFrame(animate);
}
animate();