-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (83 loc) · 2.31 KB
/
script.js
File metadata and controls
89 lines (83 loc) · 2.31 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
// Confetti effect
const btn = document.getElementById('surpriseBtn');
const canvas = document.getElementById('confetti');
const ctx = canvas.getContext('2d');
function resizeCanvas() {
canvas.width = canvas.parentElement.offsetWidth;
canvas.height = canvas.parentElement.offsetHeight;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
function randomColor() {
const colors = ['#f14a95', '#fff6ea', '#ffb5a7', '#ffd6e0', '#f9c6c9', '#fbb1bd', '#ffe0f7'];
return colors[Math.floor(Math.random() * colors.length)];
}
function makeConfetti(n) {
let particles = [];
for (let i = 0; i < n; i++) {
particles.push({
x: Math.random() * canvas.width,
y: -10,
r: Math.random() * 5 + 6,
d: Math.random() * 55 + 12,
color: randomColor(),
tilt: Math.random() * 10 - 5,
tiltAngle: 0,
tiltAngleIncremental: (Math.random() * 0.07) + 0.05
});
}
return particles;
}
function animateConfetti(particles) {
let angle = 0;
function draw() {
ctx.clearRect(0,0,canvas.width,canvas.height);
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
ctx.beginPath();
ctx.lineWidth = p.r;
ctx.strokeStyle = p.color;
ctx.moveTo(p.x + p.tilt + p.r / 3, p.y);
ctx.lineTo(p.x + p.tilt, p.y + p.tilt + p.r / 5);
ctx.stroke();
}
update();
}
function update() {
angle += 0.01;
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.y += (Math.cos(angle + p.d) + 3 + p.r / 2) / 2;
p.x += Math.sin(angle);
p.tiltAngle += p.tiltAngleIncremental;
p.tilt = Math.sin(p.tiltAngle - i) * 15;
if (p.y > canvas.height) {
particles[i] = {
...p,
x: Math.random() * canvas.width,
y: -10,
r: p.r,
d: p.d,
color: randomColor(),
tilt: Math.random() * 10 - 5,
tiltAngle: 0,
tiltAngleIncremental: p.tiltAngleIncremental
};
}
}
}
let times = 0;
let animation = setInterval(()=> {
draw();
times++;
if (times > 100) {
clearInterval(animation);
ctx.clearRect(0,0,canvas.width,canvas.height);
}
}, 16);
}
btn.onclick = function() {
animateConfetti(makeConfetti(130));
btn.textContent = "🎉 Love You! 🎉";
btn.disabled = true;
};