PLEXUS
Real-time particles
A slowly turning cloud of particles drifting on noise; neighbours link up with fading lines. Move the cursor to steer the rotation — nearby particles reach out to it.
Source
// Plexus — a slowly turning 3D cloud of particles drifting on smooth noise.
// Particles closer than LINK (in 3D) are joined by lines that fade with distance and depth.
// The cursor steers the rotation, and particles near it on screen link to the cursor.
// Colours follow the site's black/white theme.
const TAU = Math.PI * 2;
export function mount(canvas: HTMLCanvasElement, { thumb }: { thumb: boolean }) {
const ctx = canvas.getContext('2d');
if (!ctx) return;
const reduced = matchMedia('(prefers-reduced-motion: reduce)').matches;
const N = thumb ? 130 : 220; // particle count
const LINK = thumb ? 0.45 : 0.38; // max 3D distance for a line (cloud radius ≈ 1)
const CURSOR_LINK = 110; // px: particles this close to the cursor link to it
const MAX_PULL = 0.5; // the closest particles move this fraction of the way toward the cursor
const FOV = 2.6; // perspective strength
// ---- particles: positions in a unit sphere + per-particle noise phases ----
const P = Array.from({ length: N }, () => {
// uniform point in a sphere
const u = Math.random(), v = Math.random(), r = Math.cbrt(Math.random());
const th = u * TAU, ph = Math.acos(2 * v - 1);
return {
x: r * Math.sin(ph) * Math.cos(th), y: r * Math.sin(ph) * Math.sin(th), z: r * Math.cos(ph),
a: Math.random() * TAU, b: Math.random() * TAU, c: Math.random() * TAU, f: 0.25 + Math.random() * 0.35,
sx: 0, sy: 0, sz: 0, s: 0, // projected screen x/y, depth, scale
pull: 0, dx: 0, dy: 0, // eased pull toward the cursor and final drawn position
};
});
// ---- sizing ----
let w = 0, h = 0, R = 0;
const resize = () => {
const dpr = Math.min(devicePixelRatio || 1, thumb ? 1.5 : 2);
w = canvas.clientWidth; h = canvas.clientHeight;
canvas.width = Math.max(1, Math.round(w * dpr));
canvas.height = Math.max(1, Math.round(h * dpr));
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
R = Math.min(w, h) * 0.42;
};
new ResizeObserver(resize).observe(canvas);
resize();
// ---- theme ----
let rgb = '232,232,232';
const readTheme = () => {
const light = document.documentElement.dataset.theme === 'light';
rgb = light ? '20,20,20' : '232,232,232';
};
readTheme();
new MutationObserver(readTheme).observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
// ---- cursor: steers rotation, links nearby particles ----
let mouse: { x: number; y: number } | null = null;
let lastMouse = { x: 0, y: 0 }; // where pulled particles relax back from
let targetYaw = 0, targetPitch = 0, yaw = 0, pitch = 0;
if (!thumb) {
canvas.addEventListener('pointermove', (e) => {
const b = canvas.getBoundingClientRect();
mouse = { x: e.clientX - b.left, y: e.clientY - b.top };
lastMouse = mouse;
targetYaw = (mouse.x / w - 0.5) * 1.2;
targetPitch = (mouse.y / h - 0.5) * 0.8;
});
canvas.addEventListener('pointerleave', () => { mouse = null; targetYaw = 0; targetPitch = 0; });
}
// ---- loop (only while visible) ----
let visible = false, raf = 0, last = performance.now(), spin = 0;
const speed = reduced ? 0.15 : 1;
const frame = (now: number) => {
const dt = Math.min((now - last) / 1000, 0.05) * speed;
last = now;
const t = now / 1000 * speed;
spin += dt * 0.12;
const k = 1 - Math.exp(-dt * 3);
yaw += (targetYaw - yaw) * k;
pitch += (targetPitch - pitch) * k;
const cy = Math.cos(spin + yaw), sy = Math.sin(spin + yaw);
const cp = Math.cos(0.35 + pitch), sp = Math.sin(0.35 + pitch);
const cx0 = w / 2, cy0 = h / 2;
const kPull = 1 - Math.exp(-dt / speed * 5);
// drift on smooth noise, keep inside the sphere with a soft pull
for (const p of P) {
p.x += Math.sin(t * p.f + p.a) * 0.0009 * (dt * 60);
p.y += Math.sin(t * p.f * 1.3 + p.b) * 0.0009 * (dt * 60);
p.z += Math.sin(t * p.f * 0.8 + p.c) * 0.0009 * (dt * 60);
const d = Math.hypot(p.x, p.y, p.z);
if (d > 1) { p.x /= d; p.y /= d; p.z /= d; }
// rotate (yaw around Y, then pitch around X) and project
const x1 = p.x * cy - p.z * sy;
const z1 = p.x * sy + p.z * cy;
const y2 = p.y * cp - z1 * sp;
const z2 = p.y * sp + z1 * cp;
const s = FOV / (FOV + z2);
p.sx = cx0 + x1 * s * R;
p.sy = cy0 + y2 * s * R;
p.sz = z2;
p.s = s;
// Pull toward the cursor: up to MAX_PULL of the distance for the closest,
// fading smoothly to nothing at CURSOR_LINK. Eased so it never snaps.
let target = 0;
if (mouse) {
const d = Math.hypot(p.sx - mouse.x, p.sy - mouse.y);
if (d < CURSOR_LINK) {
const u = 1 - d / CURSOR_LINK;
target = MAX_PULL * u * u * (3 - 2 * u);
}
}
p.pull += (target - p.pull) * kPull;
p.dx = p.sx + (lastMouse.x - p.sx) * p.pull;
p.dy = p.sy + (lastMouse.y - p.sy) * p.pull;
}
ctx.clearRect(0, 0, w, h);
// lines between neighbours — bucketed by alpha so we stroke only a few paths
const BUCKETS = 6;
const paths: Path2D[] = Array.from({ length: BUCKETS }, () => new Path2D());
const L2 = LINK * LINK;
for (let i = 0; i < N; i++) {
const a = P[i];
for (let j = i + 1; j < N; j++) {
const b = P[j];
const dx = a.x - b.x, dy = a.y - b.y, dz = a.z - b.z;
const d2 = dx * dx + dy * dy + dz * dz;
if (d2 > L2) continue;
const near = 1 - Math.sqrt(d2) / LINK; // 1 close … 0 at LINK
const depth = 0.35 + 0.65 * (1 - (a.sz + b.sz + 2) / 4); // front brighter
const alpha = near * depth;
const bi = Math.min(BUCKETS - 1, Math.floor(alpha * BUCKETS));
paths[bi].moveTo(a.dx, a.dy);
paths[bi].lineTo(b.dx, b.dy);
}
}
ctx.lineWidth = thumb ? 0.6 : 0.7;
for (let bi = 0; bi < BUCKETS; bi++) {
ctx.strokeStyle = `rgba(${rgb},${((bi + 0.5) / BUCKETS) * 0.55})`;
ctx.stroke(paths[bi]);
}
// cursor links
if (mouse) {
ctx.lineWidth = 0.8;
for (const p of P) {
const d = Math.hypot(p.sx - mouse.x, p.sy - mouse.y);
if (d > CURSOR_LINK) continue;
ctx.strokeStyle = `rgba(${rgb},${(1 - d / CURSOR_LINK) * 0.8})`;
ctx.beginPath();
ctx.moveTo(mouse.x, mouse.y);
ctx.lineTo(p.dx, p.dy);
ctx.stroke();
}
}
// particles: bigger and brighter in front
for (const p of P) {
const front = 1 - (p.sz + 1) / 2;
ctx.fillStyle = `rgba(${rgb},${0.35 + 0.65 * front})`;
ctx.beginPath();
ctx.arc(p.dx, p.dy, (thumb ? 1 : 1.3) * p.s * (0.7 + front), 0, TAU);
ctx.fill();
}
raf = visible ? requestAnimationFrame(frame) : 0;
};
new IntersectionObserver(([entry]) => {
visible = entry.isIntersecting;
if (visible && !raf) { last = performance.now(); raf = requestAnimationFrame(frame); }
}).observe(canvas);
}