particles connections pointer background ambient network
Particle Field
particle-field.js
/**
* Particle Field — Floating particles with connection lines on pointer proximity.
* Category: backgrounds | Animate: true | Interactive: true | Complexity: low
*/
export default function particleField(ctx, state) {
const COUNT = 80;
const MAX_DIST = 120;
const SPEED = 0.3;
const PARTICLE_R = 1.5;
const LINE_ALPHA = 0.15;
let particles = [];
function seed() {
particles = Array.from({ length: COUNT }, () => ({
x: Math.random() * state.width,
y: Math.random() * state.height,
vx: (Math.random() - 0.5) * SPEED,
vy: (Math.random() - 0.5) * SPEED,
}));
}
seed();
return {
resize() {
seed();
},
frame({ width, height, pointer, reducedMotion }) {
for (const p of particles) {
if (!reducedMotion) {
p.x += p.vx;
p.y += p.vy;
}
if (p.x < 0) p.x = width;
if (p.x > width) p.x = 0;
if (p.y < 0) p.y = height;
if (p.y > height) p.y = 0;
}
// Connection lines
ctx.strokeStyle = `rgba(148, 163, 184, ${LINE_ALPHA})`;
ctx.lineWidth = 0.5;
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 dist = Math.sqrt(dx * dx + dy * dy);
if (dist < MAX_DIST) {
ctx.globalAlpha = (1 - dist / MAX_DIST) * LINE_ALPHA;
ctx.beginPath();
ctx.moveTo(particles[i].x, particles[i].y);
ctx.lineTo(particles[j].x, particles[j].y);
ctx.stroke();
}
}
}
// Pointer connections
if (pointer.inside) {
ctx.strokeStyle = 'rgba(99, 102, 241, 0.25)';
for (const p of particles) {
const dx = p.x - pointer.x;
const dy = p.y - pointer.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < MAX_DIST * 1.5) {
ctx.globalAlpha = (1 - dist / (MAX_DIST * 1.5)) * 0.3;
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(pointer.x, pointer.y);
ctx.stroke();
}
}
}
// Dots
ctx.globalAlpha = 1;
ctx.fillStyle = 'rgba(148, 163, 184, 0.6)';
for (const p of particles) {
ctx.beginPath();
ctx.arc(p.x, p.y, PARTICLE_R, 0, Math.PI * 2);
ctx.fill();
}
},
};
}
embed.html
<canvas id="canvas-effect" style="width:100%;height:400px;display:block" aria-hidden="true"></canvas>
<script>
/**
* mountCanvas — Minimal runtime for Canvas effects.
*
* Handles HiDPI scaling, ResizeObserver, RAF loop, pointer tracking,
* reduced-motion, and cleanup. The effect only implements draw logic.
*
* @param {HTMLCanvasElement} canvas
* @param {(ctx: CanvasRenderingContext2D, state: object) => object} createEffect
* @param {object} [options]
* @returns {() => void} destroy function
*/
function mountCanvas(canvas, createEffect, options = {}) {
const ctx = canvas.getContext('2d', { alpha: true });
if (!ctx) throw new Error('2D context not available');
const opts = {
animate: true,
clear: true,
maxDpr: 2,
pauseWhenHidden: true,
respectReducedMotion: true,
...options,
};
const reducedMotion =
opts.respectReducedMotion && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const state = {
canvas,
ctx,
width: 0,
height: 0,
pixelWidth: 0,
pixelHeight: 0,
dpr: 1,
time: 0,
delta: 0,
frame: 0,
playing: false,
reducedMotion,
pointer: { x: 0, y: 0, inside: false, down: false },
};
let rafId = 0;
let lastTime = 0;
let disposed = false;
function measure() {
const rect = canvas.getBoundingClientRect();
const w = Math.max(1, rect.width);
const h = Math.max(1, rect.height);
const dpr = Math.min(window.devicePixelRatio || 1, opts.maxDpr);
const pw = Math.round(w * dpr);
const ph = Math.round(h * dpr);
if (canvas.width !== pw || canvas.height !== ph) {
canvas.width = pw;
canvas.height = ph;
}
state.width = w;
state.height = h;
state.pixelWidth = pw;
state.pixelHeight = ph;
state.dpr = dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
function clear() {
ctx.clearRect(0, 0, state.width, state.height);
}
const effect = createEffect(ctx, state) || {};
function onResize() {
measure();
effect.resize?.(state);
if (!opts.animate || reducedMotion) {
if (opts.clear) clear();
effect.frame?.(state);
}
}
function loop(now) {
if (disposed) return;
if (opts.pauseWhenHidden && document.hidden) {
rafId = requestAnimationFrame(loop);
return;
}
state.delta = lastTime ? (now - lastTime) / 1000 : 0;
state.time = now / 1000;
state.frame += 1;
lastTime = now;
if (opts.clear) clear();
effect.frame?.(state);
rafId = requestAnimationFrame(loop);
}
function onPointerMove(e) {
const rect = canvas.getBoundingClientRect();
state.pointer.x = e.clientX - rect.left;
state.pointer.y = e.clientY - rect.top;
if (!opts.animate || reducedMotion) {
if (opts.clear) clear();
effect.frame?.(state);
}
}
function onPointerEnter() {
state.pointer.inside = true;
}
function onPointerLeave() {
state.pointer.inside = false;
state.pointer.down = false;
}
function onPointerDown() {
state.pointer.down = true;
}
function onPointerUp() {
state.pointer.down = false;
}
const ro = new ResizeObserver(onResize);
ro.observe(canvas);
canvas.addEventListener('pointermove', onPointerMove);
canvas.addEventListener('pointerenter', onPointerEnter);
canvas.addEventListener('pointerleave', onPointerLeave);
canvas.addEventListener('pointerdown', onPointerDown);
window.addEventListener('pointerup', onPointerUp);
measure();
if (opts.animate && !reducedMotion) {
state.playing = true;
rafId = requestAnimationFrame(loop);
} else {
effect.frame?.(state);
}
return function destroy() {
disposed = true;
state.playing = false;
cancelAnimationFrame(rafId);
ro.disconnect();
canvas.removeEventListener('pointermove', onPointerMove);
canvas.removeEventListener('pointerenter', onPointerEnter);
canvas.removeEventListener('pointerleave', onPointerLeave);
canvas.removeEventListener('pointerdown', onPointerDown);
window.removeEventListener('pointerup', onPointerUp);
effect.destroy?.(state);
};
}
/**
* Particle Field — Floating particles with connection lines on pointer proximity.
* Category: backgrounds | Animate: true | Interactive: true | Complexity: low
*/
const __effect = function particleField(ctx, state) {
const COUNT = 80;
const MAX_DIST = 120;
const SPEED = 0.3;
const PARTICLE_R = 1.5;
const LINE_ALPHA = 0.15;
let particles = [];
function seed() {
particles = Array.from({ length: COUNT }, () => ({
x: Math.random() * state.width,
y: Math.random() * state.height,
vx: (Math.random() - 0.5) * SPEED,
vy: (Math.random() - 0.5) * SPEED,
}));
}
seed();
return {
resize() {
seed();
},
frame({ width, height, pointer, reducedMotion }) {
for (const p of particles) {
if (!reducedMotion) {
p.x += p.vx;
p.y += p.vy;
}
if (p.x < 0) p.x = width;
if (p.x > width) p.x = 0;
if (p.y < 0) p.y = height;
if (p.y > height) p.y = 0;
}
// Connection lines
ctx.strokeStyle = `rgba(148, 163, 184, ${LINE_ALPHA})`;
ctx.lineWidth = 0.5;
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 dist = Math.sqrt(dx * dx + dy * dy);
if (dist < MAX_DIST) {
ctx.globalAlpha = (1 - dist / MAX_DIST) * LINE_ALPHA;
ctx.beginPath();
ctx.moveTo(particles[i].x, particles[i].y);
ctx.lineTo(particles[j].x, particles[j].y);
ctx.stroke();
}
}
}
// Pointer connections
if (pointer.inside) {
ctx.strokeStyle = 'rgba(99, 102, 241, 0.25)';
for (const p of particles) {
const dx = p.x - pointer.x;
const dy = p.y - pointer.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < MAX_DIST * 1.5) {
ctx.globalAlpha = (1 - dist / (MAX_DIST * 1.5)) * 0.3;
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(pointer.x, pointer.y);
ctx.stroke();
}
}
}
// Dots
ctx.globalAlpha = 1;
ctx.fillStyle = 'rgba(148, 163, 184, 0.6)';
for (const p of particles) {
ctx.beginPath();
ctx.arc(p.x, p.y, PARTICLE_R, 0, Math.PI * 2);
ctx.fill();
}
},
};
}
const canvas = document.getElementById('canvas-effect');
mountCanvas(canvas, __effect, { animate: true, clear: true });
</script> Details
Animated Interactive Reduced Motion aria-hidden
particlesconnectionspointerbackgroundambientnetwork
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| COUNT | number | 80 | Number of particles |
| MAX_DIST | number | 120 | Connection line threshold in CSS pixels |
| SPEED | number | 0.3 | Particle drift speed |
Particles drift across the canvas and form connection lines when within proximity. Moving the pointer over the canvas creates additional connections in accent color. On prefers-reduced-motion: reduce, particles freeze in place but connections still render.