dots grid pointer proximity magnetic interactive
Proximity Dots
proximity-dots.js
/**
* Proximity Dots — Grid of dots that scale up and glow near the pointer.
* Category: interactive | Animate: false | Interactive: true | Complexity: low
*/
export default function proximityDots(ctx, state) {
const GAP = 28;
const BASE_R = 1.2;
const MAX_R = 4;
const RANGE = 100;
return {
frame({ width, height, pointer }) {
const cols = Math.ceil(width / GAP);
const rows = Math.ceil(height / GAP);
const offsetX = (width - (cols - 1) * GAP) / 2;
const offsetY = (height - (rows - 1) * GAP) / 2;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = offsetX + col * GAP;
const y = offsetY + row * GAP;
let r = BASE_R;
let alpha = 0.15;
if (pointer.inside) {
const dx = x - pointer.x;
const dy = y - pointer.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < RANGE) {
const t = 1 - dist / RANGE;
r = BASE_R + (MAX_R - BASE_R) * t * t;
alpha = 0.15 + 0.6 * t * t;
}
}
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fillStyle = `rgba(99, 102, 241, ${alpha})`;
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);
};
}
/**
* Proximity Dots — Grid of dots that scale up and glow near the pointer.
* Category: interactive | Animate: false | Interactive: true | Complexity: low
*/
const __effect = function proximityDots(ctx, state) {
const GAP = 28;
const BASE_R = 1.2;
const MAX_R = 4;
const RANGE = 100;
return {
frame({ width, height, pointer }) {
const cols = Math.ceil(width / GAP);
const rows = Math.ceil(height / GAP);
const offsetX = (width - (cols - 1) * GAP) / 2;
const offsetY = (height - (rows - 1) * GAP) / 2;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = offsetX + col * GAP;
const y = offsetY + row * GAP;
let r = BASE_R;
let alpha = 0.15;
if (pointer.inside) {
const dx = x - pointer.x;
const dy = y - pointer.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < RANGE) {
const t = 1 - dist / RANGE;
r = BASE_R + (MAX_R - BASE_R) * t * t;
alpha = 0.15 + 0.6 * t * t;
}
}
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fillStyle = `rgba(99, 102, 241, ${alpha})`;
ctx.fill();
}
}
},
};
}
const canvas = document.getElementById('canvas-effect');
mountCanvas(canvas, __effect, { animate: false, clear: true });
</script> Details
Interactive aria-hidden
dotsgridpointerproximitymagneticinteractive
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| GAP | number | 28 | Grid spacing in CSS pixels |
| RANGE | number | 100 | Pointer influence radius |
| MAX_R | number | 4 | Maximum dot radius on proximity |
A uniform dot grid where dots near the pointer enlarge and brighten. Uses animate: false — no RAF loop, only redraws when the pointer moves or the canvas resizes. Zero CPU when idle.