-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
121 lines (107 loc) · 4.07 KB
/
script.js
File metadata and controls
121 lines (107 loc) · 4.07 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
// script.js for Open Source Weekend
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('particle-canvas');
const ctx = canvas.getContext('2d');
let particles = [];
const particleCount = 100;
const maxDistance = 150;
const colors = ['#00fff7', '#a259f7', '#ffffff'];
let mouse = { x: null, y: null };
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
class Particle {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.vx = (Math.random() - 0.5) * 1.2;
this.vy = (Math.random() - 0.5) * 1.2;
this.size = Math.random() * 2.5 + 1.5;
this.baseColor = colors[Math.floor(Math.random() * colors.length)];
this.color = this.baseColor;
}
update() {
this.x += this.vx;
this.y += this.vy;
// Bounce off edges
if (this.x < 0 || this.x > canvas.width) this.vx *= -1;
if (this.y < 0 || this.y > canvas.height) this.vy *= -1;
// Mouse interaction: attract/repel
if (mouse.x && mouse.y) {
const dx = this.x - mouse.x;
const dy = this.y - mouse.y;
const dist = Math.sqrt(dx*dx + dy*dy);
if (dist < 120) {
this.x += dx / dist * 0.7;
this.y += dy / dist * 0.7;
}
}
}
draw() {
const gradient = ctx.createRadialGradient(this.x, this.y, 0.1, this.x, this.y, this.size*2);
gradient.addColorStop(0, this.baseColor);
gradient.addColorStop(0.7, this.baseColor + 'cc');
gradient.addColorStop(1, 'rgba(11,12,16,0.0)');
ctx.globalAlpha = 0.7;
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.globalAlpha = 1;
}
}
function initParticles() {
particles = [];
for (let i = 0; i < particleCount; i++) {
particles.push(new Particle());
}
}
function connectParticles() {
for (let i = 0; i < particles.length; i++) {
for (let j = i + 1; j < particles.length; j++) {
const dist = Math.hypot(particles[i].x - particles[j].x, particles[i].y - particles[j].y);
if (dist < maxDistance) {
// Neon gradient line color
const grad = ctx.createLinearGradient(particles[i].x, particles[i].y, particles[j].x, particles[j].y);
grad.addColorStop(0, particles[i].baseColor);
grad.addColorStop(1, particles[j].baseColor);
ctx.strokeStyle = grad;
ctx.globalAlpha = 0.12 + 0.25 * (1 - dist / maxDistance);
ctx.shadowColor = grad;
ctx.shadowBlur = 8;
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.moveTo(particles[i].x, particles[i].y);
ctx.lineTo(particles[j].x, particles[j].y);
ctx.stroke();
ctx.shadowBlur = 0;
ctx.globalAlpha = 1;
}
}
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#0b0c10';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (const particle of particles) {
particle.update();
particle.draw();
}
connectParticles();
requestAnimationFrame(animate);
}
window.addEventListener('resize', resizeCanvas);
window.addEventListener('mousemove', (e) => {
mouse.x = e.clientX;
mouse.y = e.clientY;
});
window.addEventListener('mouseout', () => {
mouse.x = null;
mouse.y = null;
});
resizeCanvas();
initParticles();
animate();
});