Spaces:
Sleeping
Sleeping
Update genesis/static/animation.js
Browse files- genesis/static/animation.js +52 -25
genesis/static/animation.js
CHANGED
@@ -1,43 +1,70 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
2 |
const ctx = canvas.getContext('2d');
|
3 |
-
canvas.width = window.innerWidth;
|
4 |
-
canvas.height = window.innerHeight;
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
let particles = [];
|
|
|
7 |
|
8 |
-
for (let i = 0; i <
|
9 |
particles.push({
|
10 |
x: Math.random() * canvas.width,
|
11 |
y: Math.random() * canvas.height,
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
});
|
16 |
}
|
17 |
|
18 |
-
|
|
|
19 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
ctx.beginPath();
|
23 |
-
ctx.arc(p.x, p.y, p.
|
|
|
24 |
ctx.fill();
|
25 |
-
});
|
26 |
-
}
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
});
|
35 |
-
}
|
36 |
|
37 |
-
|
38 |
-
draw();
|
39 |
-
update();
|
40 |
-
requestAnimationFrame(loop);
|
41 |
}
|
42 |
|
43 |
-
|
|
|
1 |
+
// ===== MOLECULAR PARTICLE ANIMATION =====
|
2 |
+
|
3 |
+
// Create a canvas background
|
4 |
+
const canvas = document.createElement('canvas');
|
5 |
+
canvas.id = 'moleculeCanvas';
|
6 |
+
document.body.appendChild(canvas);
|
7 |
const ctx = canvas.getContext('2d');
|
|
|
|
|
8 |
|
9 |
+
// Resize canvas to full window
|
10 |
+
function resizeCanvas() {
|
11 |
+
canvas.width = window.innerWidth;
|
12 |
+
canvas.height = window.innerHeight;
|
13 |
+
}
|
14 |
+
window.addEventListener('resize', resizeCanvas);
|
15 |
+
resizeCanvas();
|
16 |
+
|
17 |
+
// Create particles
|
18 |
let particles = [];
|
19 |
+
const numParticles = 80;
|
20 |
|
21 |
+
for (let i = 0; i < numParticles; i++) {
|
22 |
particles.push({
|
23 |
x: Math.random() * canvas.width,
|
24 |
y: Math.random() * canvas.height,
|
25 |
+
radius: Math.random() * 4 + 1,
|
26 |
+
color: Math.random() > 0.5 ? '#00ff88' : '#ffaa00',
|
27 |
+
speedX: (Math.random() - 0.5) * 1.5,
|
28 |
+
speedY: (Math.random() - 0.5) * 1.5
|
29 |
});
|
30 |
}
|
31 |
|
32 |
+
// Animation loop
|
33 |
+
function animate() {
|
34 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
35 |
+
|
36 |
+
// Draw and move particles
|
37 |
+
particles.forEach((p, index) => {
|
38 |
+
p.x += p.speedX;
|
39 |
+
p.y += p.speedY;
|
40 |
+
|
41 |
+
// Bounce off edges
|
42 |
+
if (p.x < 0 || p.x > canvas.width) p.speedX *= -1;
|
43 |
+
if (p.y < 0 || p.y > canvas.height) p.speedY *= -1;
|
44 |
+
|
45 |
+
// Draw particle
|
46 |
ctx.beginPath();
|
47 |
+
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
|
48 |
+
ctx.fillStyle = p.color;
|
49 |
ctx.fill();
|
|
|
|
|
50 |
|
51 |
+
// Connect lines if particles are close
|
52 |
+
for (let j = index + 1; j < particles.length; j++) {
|
53 |
+
let dx = particles[j].x - p.x;
|
54 |
+
let dy = particles[j].y - p.y;
|
55 |
+
let distance = Math.sqrt(dx * dx + dy * dy);
|
56 |
+
if (distance < 120) {
|
57 |
+
ctx.beginPath();
|
58 |
+
ctx.moveTo(p.x, p.y);
|
59 |
+
ctx.lineTo(particles[j].x, particles[j].y);
|
60 |
+
ctx.strokeStyle = p.color === '#00ff88' ? '#00ff88aa' : '#ffaa00aa';
|
61 |
+
ctx.lineWidth = 0.5;
|
62 |
+
ctx.stroke();
|
63 |
+
}
|
64 |
+
}
|
65 |
});
|
|
|
66 |
|
67 |
+
requestAnimationFrame(animate);
|
|
|
|
|
|
|
68 |
}
|
69 |
|
70 |
+
animate();
|