Number 105: Sphere of dots that parts for the pointer
Specification
- Catalogue
- No. 105 of 110
- Section
- Page sections
- Interaction
- Hover, Press
- Element
- Background
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
About six hundred dots on a hexagonal grid inside a circle, smaller toward the rim so the disc reads as a sphere. The pointer pushes a hole through them that closes behind it, and a press scatters the whole sphere before it gathers itself again.
Each dot is held to its home by a spring and slowed by friction, and the pointer adds a push that falls off with the square of the distance inside 48px. The drawing loop runs only while a dot is still moving or the pointer is over it, then stops, so a still sphere costs nothing. The dots take the page's ink colour and redraw when the theme changes. Under reduced motion the sphere stays still.
Code
React and plain CSS"use client";
import { useEffect, useRef } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { useReducedMotion } from "@/lib/client/hooks";
import styles from "./particle-sphere.module.css";
type Dot = { hx: number; hy: number; x: number; y: number; vx: number; vy: number; r: number };
const SIZE = 280;
const RADIUS = 122;
const GAP = 8.6;
/** The pointer clears a hole this wide. */
const REACH = 48;
const PUSH = 20;
/** Spring home and friction, per sixtieth of a second. */
const HOME = 0.06;
const FRICTION = 0.84;
/** Dots on a hexagonal grid inside a circle, smaller toward the rim so the disc reads as a sphere. */
function makeDots(): Dot[] {
const dots: Dot[] = [];
const rowH = GAP * 0.866;
for (let row = -Math.ceil(RADIUS / rowH); row <= Math.ceil(RADIUS / rowH); row++) {
const y = row * rowH;
const shift = row % 2 === 0 ? 0 : GAP / 2;
for (let x = -RADIUS - shift; x <= RADIUS; x += GAP) {
const px = x + shift;
const d = Math.hypot(px, y);
if (d > RADIUS) continue;
const depth = Math.sqrt(1 - (d / RADIUS) ** 2);
dots.push({ hx: px, hy: y, x: px, y, vx: 0, vy: 0, r: 0.55 + 1.15 * depth });
}
}
return dots;
}
export default function ParticleSphere({ active, context }: LivePreviewProps) {
const card = context === "card";
const reduced = useReducedMotion();
const canvasRef = useRef<HTMLCanvasElement>(null);
const dots = useRef<Dot[]>(makeDots());
const pointer = useRef<{ x: number; y: number } | null>(null);
const raf = useRef(0);
const running = useRef(false);
const draw = () => {
const canvas = canvasRef.current;
const ctx = canvas?.getContext("2d");
if (!canvas || !ctx) return;
const ratio = canvas.width / SIZE;
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
ctx.clearRect(0, 0, SIZE, SIZE);
ctx.fillStyle = getComputedStyle(canvas).color;
ctx.beginPath();
for (const d of dots.current) {
ctx.moveTo(SIZE / 2 + d.x + d.r, SIZE / 2 + d.y);
ctx.arc(SIZE / 2 + d.x, SIZE / 2 + d.y, d.r, 0, Math.PI * 2);
}
ctx.fill();
};
/** One physics step; returns true while anything is still moving. */
const step = (dt: number): boolean => {
let moving = false;
const p = pointer.current;
for (const d of dots.current) {
if (p) {
const dx = d.x - p.x;
const dy = d.y - p.y;
const dist = Math.hypot(dx, dy) || 1;
if (dist < REACH) {
const f = (1 - dist / REACH) ** 2 * PUSH * 0.12;
d.vx += (dx / dist) * f * dt;
d.vy += (dy / dist) * f * dt;
}
}
d.vx += (d.hx - d.x) * HOME * dt;
d.vy += (d.hy - d.y) * HOME * dt;
const keep = Math.pow(FRICTION, dt);
d.vx *= keep;
d.vy *= keep;
d.x += d.vx * dt;
d.y += d.vy * dt;
if (Math.abs(d.vx) + Math.abs(d.vy) > 0.02 || Math.abs(d.x - d.hx) + Math.abs(d.y - d.hy) > 0.05) moving = true;
}
return moving;
};
/** Runs the loop until every dot is home and the pointer has gone; then it stops. */
const wake = () => {
if (running.current || reduced) return;
running.current = true;
let last = performance.now();
const frame = (now: number) => {
const dt = Math.min(2.5, (now - last) / (1000 / 60));
last = now;
const moving = step(dt);
draw();
if (moving || pointer.current) raf.current = requestAnimationFrame(frame);
else running.current = false;
};
raf.current = requestAnimationFrame(frame);
};
const burst = (at: { x: number; y: number }) => {
if (reduced) return;
for (const d of dots.current) {
const dx = d.x - at.x;
const dy = d.y - at.y;
const dist = Math.hypot(dx, dy) || 1;
const kick = 9 * (1 - Math.min(1, dist / (RADIUS * 2))) + Math.random() * 3;
d.vx += (dx / dist) * kick + (Math.random() - 0.5) * 2;
d.vy += (dy / dist) * kick + (Math.random() - 0.5) * 2;
}
wake();
};
// Size the bitmap for the screen it lands on, and redraw when the theme changes the ink.
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ratio = Math.min(3, (window.devicePixelRatio || 1) * 1.6);
canvas.width = Math.round(SIZE * ratio);
canvas.height = Math.round(SIZE * ratio);
draw();
const scheme = window.matchMedia("(prefers-color-scheme: dark)");
const observer = new MutationObserver(() => requestAnimationFrame(draw));
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
scheme.addEventListener("change", draw);
return () => {
observer.disconnect();
scheme.removeEventListener("change", draw);
cancelAnimationFrame(raf.current);
running.current = false;
};
}, []);
// The card demo: a pointer circles inside the sphere, then a click scatters it.
const demo = card && active;
useEffect(() => {
if (!demo) {
pointer.current = null;
return;
}
let t = 0;
let timer = 0;
const orbit = () => {
t += 1;
const a = t / 9;
pointer.current = t < 70 ? { x: Math.cos(a) * 52, y: Math.sin(a * 1.3) * 46 } : null;
wake();
if (t === 74) burst({ x: 0, y: 10 });
timer = window.setTimeout(orbit, t >= 74 ? 2200 : 30);
if (t >= 74) t = 0;
};
timer = window.setTimeout(orbit, 300);
return () => {
window.clearTimeout(timer);
pointer.current = null;
};
// wake and burst read refs only.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [demo]);
function at(event: { clientX: number; clientY: number }) {
const canvas = canvasRef.current;
if (!canvas) return null;
const rect = canvas.getBoundingClientRect();
const scale = rect.width / SIZE;
return { x: (event.clientX - rect.left) / scale - SIZE / 2, y: (event.clientY - rect.top) / scale - SIZE / 2 };
}
return (
<div className={styles.area}>
<button
type="button"
className={styles.sphere}
tabIndex={card ? -1 : 0}
aria-label="Dot sphere. Point to part the dots; press to scatter them"
onPointerMove={(e) => {
if (card || e.pointerType !== "mouse") return;
pointer.current = at(e);
wake();
}}
onPointerLeave={() => {
pointer.current = null;
}}
onClick={(e) => {
if (card) return;
const point = e.detail === 0 ? { x: 0, y: 0 } : at(e);
burst(point ?? { x: 0, y: 0 });
}}
>
<canvas ref={canvasRef} className={styles.canvas} width={SIZE} height={SIZE} aria-hidden="true" />
</button>
</div>
);
}