-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticles.js
More file actions
298 lines (259 loc) · 9.66 KB
/
particles.js
File metadata and controls
298 lines (259 loc) · 9.66 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* particles.js — Interactive particle network background
* Particles float gently and connect with lines when close.
* Mouse interaction: particles near the cursor get attracted/repelled.
* Responds to theme changes (dark/light) automatically.
* Fully responsive — adjusts particle count on resize.
*/
(function () {
'use strict';
const canvas = document.getElementById('particle-canvas');
if (!canvas) return;
const ctx = canvas.getContext('2d');
// ---- Configuration ----
const CONFIG = {
// Particle density: 1 particle per N square pixels
densityDesktop: 12000,
densityMobile: 20000,
maxParticles: 80,
minParticles: 20,
// Connection
connectionDistance: 130,
// Mouse interaction radius
mouseRadius: 160,
// Particle properties
minRadius: 1.2,
maxRadius: 2.8,
minSpeed: 0.15,
maxSpeed: 0.45,
// Visual
particleOpacity: 0.5,
lineOpacity: 0.15,
// FPS limiter (60fps)
fpsInterval: 1000 / 60,
};
let particles = [];
let mouse = { x: -9999, y: -9999 };
let width, height;
let animationId;
let lastFrameTime = 0;
let prefersReducedMotion = false;
// ---- Detect reduced motion ----
const motionQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
prefersReducedMotion = motionQuery.matches;
/**
* Whether motion should be reduced: true when the OS requests it
* (prefers-reduced-motion) OR the in-app "Réduire les animations" setting
* is on (html.reduce-motion).
* @returns {boolean}
*/
function isMotionReduced() {
return prefersReducedMotion || document.documentElement.classList.contains('reduce-motion');
}
/**
* Start or stop the animation loop based on the current motion preference,
* clearing the canvas when motion is disabled.
*/
function applyMotionPreference() {
if (isMotionReduced()) {
cancelAnimationFrame(animationId);
if (width && height) ctx.clearRect(0, 0, width, height);
} else {
animate(0);
}
}
motionQuery.addEventListener('change', (e) => {
prefersReducedMotion = e.matches;
applyMotionPreference();
});
// Fired by app.js when the in-app toggle changes
window.addEventListener('admingo:motion-change', applyMotionPreference);
// ---- Get theme color ----
function getColor() {
const isDark = document.documentElement.classList.contains('dark');
// Use primary color variable, fallback to green
const style = getComputedStyle(document.documentElement);
const primary = style.getPropertyValue('--color-primary').trim();
if (primary) {
return {
particle: isDark
? `rgba(${primary}, ${CONFIG.particleOpacity * 0.7})`
: `rgba(${primary}, ${CONFIG.particleOpacity})`,
line: isDark
? `rgba(${primary}, ${CONFIG.lineOpacity * 0.6})`
: `rgba(${primary}, ${CONFIG.lineOpacity})`,
};
}
// Fallback
return {
particle: isDark ? 'rgba(88, 204, 2, 0.35)' : 'rgba(88, 204, 2, 0.5)',
line: isDark ? 'rgba(88, 204, 2, 0.09)' : 'rgba(88, 204, 2, 0.15)',
};
}
// ---- Particle class ----
class Particle {
constructor() {
this.reset();
}
reset() {
this.x = Math.random() * width;
this.y = Math.random() * height;
this.radius = CONFIG.minRadius + Math.random() * (CONFIG.maxRadius - CONFIG.minRadius);
const speed = CONFIG.minSpeed + Math.random() * (CONFIG.maxSpeed - CONFIG.minSpeed);
const angle = Math.random() * Math.PI * 2;
this.vx = Math.cos(angle) * speed;
this.vy = Math.sin(angle) * speed;
}
update() {
// Mouse attraction/repulsion
const dx = mouse.x - this.x;
const dy = mouse.y - this.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < CONFIG.mouseRadius && dist > 0) {
// Gentle attraction
const force = (CONFIG.mouseRadius - dist) / CONFIG.mouseRadius;
this.vx += (dx / dist) * force * 0.02;
this.vy += (dy / dist) * force * 0.02;
}
// Dampen speed to prevent runaway particles
const speed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
if (speed > CONFIG.maxSpeed * 2) {
this.vx *= 0.98;
this.vy *= 0.98;
}
this.x += this.vx;
this.y += this.vy;
// Wrap around edges with padding
const pad = 10;
if (this.x < -pad) this.x = width + pad;
if (this.x > width + pad) this.x = -pad;
if (this.y < -pad) this.y = height + pad;
if (this.y > height + pad) this.y = -pad;
}
draw(color) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.fill();
}
}
// ---- Calculate particle count based on screen ----
function getParticleCount() {
const area = width * height;
const isMobile = width < 768;
const density = isMobile ? CONFIG.densityMobile : CONFIG.densityDesktop;
const count = Math.floor(area / density);
return Math.max(CONFIG.minParticles, Math.min(CONFIG.maxParticles, count));
}
// ---- Resize handler ----
function resize() {
const dpr = window.devicePixelRatio || 1;
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width * dpr;
canvas.height = height * dpr;
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
ctx.scale(dpr, dpr);
// Adjust particle count
const targetCount = getParticleCount();
while (particles.length < targetCount) {
particles.push(new Particle());
}
while (particles.length > targetCount) {
particles.pop();
}
}
// ---- Draw connections between nearby particles ----
function drawConnections(colors) {
const maxDist = CONFIG.connectionDistance;
const maxDistSq = maxDist * maxDist;
for (let i = 0; i < particles.length; i++) {
for (let j = i + 1; j < particles.length; j++) {
const dx = particles[i].x - particles[j].x;
const dy = particles[i].y - particles[j].y;
const distSq = dx * dx + dy * dy;
if (distSq < maxDistSq) {
const dist = Math.sqrt(distSq);
const opacity = 1 - dist / maxDist;
ctx.beginPath();
ctx.moveTo(particles[i].x, particles[i].y);
ctx.lineTo(particles[j].x, particles[j].y);
ctx.strokeStyle = colors.line.replace(
/[\d.]+\)$/,
(parseFloat(colors.line.match(/[\d.]+\)$/)[0]) * opacity).toFixed(3) + ')'
);
ctx.lineWidth = 0.6;
ctx.stroke();
}
}
// Also draw lines from particles to mouse cursor
const dx = particles[i].x - mouse.x;
const dy = particles[i].y - mouse.y;
const distSq = dx * dx + dy * dy;
if (distSq < maxDistSq) {
const dist = Math.sqrt(distSq);
const opacity = 1 - dist / maxDist;
ctx.beginPath();
ctx.moveTo(particles[i].x, particles[i].y);
ctx.lineTo(mouse.x, mouse.y);
ctx.strokeStyle = colors.line.replace(
/[\d.]+\)$/,
(parseFloat(colors.line.match(/[\d.]+\)$/)[0]) * opacity * 1.5).toFixed(3) + ')'
);
ctx.lineWidth = 0.8;
ctx.stroke();
}
}
}
// ---- Animation loop ----
function animate(timestamp) {
if (isMotionReduced()) return;
animationId = requestAnimationFrame(animate);
// FPS limiter
const elapsed = timestamp - lastFrameTime;
if (elapsed < CONFIG.fpsInterval) return;
lastFrameTime = timestamp - (elapsed % CONFIG.fpsInterval);
ctx.clearRect(0, 0, width, height);
const colors = getColor();
// Update and draw particles
for (const p of particles) {
p.update();
p.draw(colors.particle);
}
// Draw connections
drawConnections(colors);
}
// ---- Event listeners ----
// Mouse
document.addEventListener('mousemove', (e) => {
mouse.x = e.clientX;
mouse.y = e.clientY;
}, { passive: true });
document.addEventListener('mouseleave', () => {
mouse.x = -9999;
mouse.y = -9999;
});
// Touch (mobile) — use first touch point
document.addEventListener('touchmove', (e) => {
if (e.touches.length > 0) {
mouse.x = e.touches[0].clientX;
mouse.y = e.touches[0].clientY;
}
}, { passive: true });
document.addEventListener('touchend', () => {
mouse.x = -9999;
mouse.y = -9999;
}, { passive: true });
// Resize (debounced)
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(resize, 150);
});
// ---- Init ----
resize();
if (!isMotionReduced()) {
animate(0);
}
})();