-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirework Particles.html
More file actions
130 lines (116 loc) · 4.08 KB
/
Copy pathFirework Particles.html
File metadata and controls
130 lines (116 loc) · 4.08 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fireworks Trail with Counter</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
background-color: white;
}
canvas {
display: block;
}
#counter {
position: absolute;
top: 10px;
right: 20px;
font-size: 18px;
font-family: Arial, sans-serif;
color: black;
background: rgba(255, 255, 255, 0.7);
padding: 5px 10px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>
<canvas id="fireworksCanvas"></canvas>
<div id="counter">Particles: 0</div>
<script>
const canvas = document.getElementById('fireworksCanvas');
const ctx = canvas.getContext('2d');
const counter = document.getElementById('counter');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
const fireworks = [];
class Firework {
constructor(x, y) {
this.x = x;
this.y = y;
this.particles = [];
this.createParticles();
}
createParticles() {
const colors = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C', '#FF7F50', '#FFD700'];
const color = colors[Math.floor(Math.random() * colors.length)];
for (let i = 0; i < 25; i++) {
const angle = Math.random() * 2 * Math.PI;
const speed = Math.random() * 4 + 1;
const size = Math.random() * 3 + 2;
this.particles.push({
x: this.x,
y: this.y,
speedX: Math.cos(angle) * speed,
speedY: Math.sin(angle) * speed,
size: size,
alpha: 1,
color: color
});
}
}
update() {
this.particles.forEach(particle => {
particle.x += particle.speedX;
particle.y += particle.speedY;
particle.alpha -= 0.025;
});
this.particles = this.particles.filter(p => p.alpha > 0);
}
draw() {
this.particles.forEach(particle => {
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
ctx.fillStyle = `rgba(${hexToRgb(particle.color)}, ${particle.alpha})`;
ctx.shadowBlur = 25;
ctx.shadowColor = particle.color;
ctx.fill();
});
}
}
function hexToRgb(hex) {
const bigint = parseInt(hex.slice(1), 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return `${r}, ${g}, ${b}`;
}
canvas.addEventListener('mousemove', (e) => {
fireworks.push(new Firework(e.clientX, e.clientY));
});
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let particleCount = 0;
fireworks.forEach((firework, index) => {
firework.update();
firework.draw();
particleCount += firework.particles.length;
if (firework.particles.length === 0) {
fireworks.splice(index, 1);
}
});
counter.textContent = `Particles: ${particleCount}`;
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>