-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (49 loc) · 1.38 KB
/
script.js
File metadata and controls
65 lines (49 loc) · 1.38 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
const canvas = document.getElementById('bgCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particles = [];
function initParticles() {
particles = [];
for (let i = 0; i < 120; i++) {
particles.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: Math.random() * 2,
speedX: (Math.random() - 0.5) * 0.6,
speedY: (Math.random() - 0.5) * 0.6,
});
}
}
initParticles();
function animateParticles() {
ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#00ffff";
particles.forEach(p => {
p.x += p.speedX;
p.y += p.speedY;
if (p.x < 0 || p.x > canvas.width) p.speedX *= -1;
if (p.y < 0 || p.y > canvas.height) p.speedY *= -1;
ctx.beginPath();
ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
ctx.fill();
});
requestAnimationFrame(animateParticles);
}
animateParticles();
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
initParticles();
});
window.addEventListener('mousemove', e => {
particles.push({
x: e.x,
y: e.y,
size: Math.random() * 3,
speedX: (Math.random() - 0.5) * 1.5,
speedY: (Math.random() - 0.5) * 1.5,
});
if (particles.length > 150) particles.splice(0, 1);
});