Specification
- Catalogue
- No. 108 of 110
- Section
- Loading and progress
- Interaction
- Loading, Autoplay
- Element
- Loader
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
A sphere of fine dotted stripes that turns while a dent wanders over it, pressing the dots inwards: a thinking indicator for an assistant or a long task.
It says "working" without a spinner's urgency, and it reads at any size from a 20px badge to a hero tile.
850 dots sit on 34 meridians, drawn on a 2D canvas with depth: far dots are smaller and fainter. The sphere turns at 0.55 radians a second, tilted 0.32 radians towards the viewer. The dent's rim carries a three-lobed wave, so its edge ripples as it moves. Speed eases in and out over about a quarter of a second, so it never starts or stops dead, and it holds its frame when it stops. On a card it thinks only while hovered; on the entry page it thinks for eight seconds and rests until replayed. Under reduced motion it shows one still frame.
Changed from the library: one state instead of nine, dots in the site's text colour, no cursor gravity.
Code
React and plain CSS"use client";
import { useEffect, useRef, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { useReducedMotion } from "@/lib/client/hooks";
import styles from "./thinking-orb.module.css";
/** Logical size of the drawing; the stage scales it. */
const SIZE = 140;
const RADIUS = 58;
/** Dots sit on meridians, close together, so the sphere reads as fine vertical stripes. */
const MERIDIANS = 34;
const PER_MERIDIAN = 26;
/** On the entry page the orb thinks for this long, then settles until replayed. */
const DETAIL_RUN_MS = 8000;
/** The frame the orb rests on: the dent faces down and slightly forward. */
const REST_TIME = 2.6;
type Dot = { x: number; y: number; z: number };
const UNIT_DOTS: Dot[] = (() => {
const dots: Dot[] = [];
for (let m = 0; m < MERIDIANS; m++) {
const lon = (m / MERIDIANS) * Math.PI * 2;
for (let k = 1; k < PER_MERIDIAN; k++) {
const lat = -Math.PI / 2 + (k / PER_MERIDIAN) * Math.PI;
dots.push({ x: Math.cos(lat) * Math.cos(lon), y: Math.sin(lat), z: Math.cos(lat) * Math.sin(lon) });
}
}
return dots;
})();
const smooth = (edge0: number, edge1: number, x: number) => {
const t = Math.min(1, Math.max(0, (x - edge0) / (edge1 - edge0)));
return t * t * (3 - 2 * t);
};
/**
* One frame of the orb at time t (seconds of thinking). A dent travels over
* the sphere and presses its dots inwards, with a slow wave along the rim of
* the dent; the whole sphere turns and is drawn with depth (far dots smaller
* and fainter).
*/
function drawOrb(ctx: CanvasRenderingContext2D, t: number, ink: string, pixelRatio: number) {
ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
ctx.clearRect(0, 0, SIZE, SIZE);
ctx.fillStyle = ink;
// Where the dent points: a slow wander, mostly downwards and towards the viewer.
const dx = Math.sin(t * 0.7) * 0.55;
const dy = 0.62 + Math.sin(t * 0.43) * 0.2;
const dz = 0.55 + Math.cos(t * 0.7) * 0.35;
const dl = Math.hypot(dx, dy, dz);
const dir = { x: dx / dl, y: dy / dl, z: dz / dl };
const yaw = t * 0.55;
const cosY = Math.cos(yaw);
const sinY = Math.sin(yaw);
const tilt = 0.32;
const cosT = Math.cos(tilt);
const sinT = Math.sin(tilt);
const centre = SIZE / 2;
for (const p of UNIT_DOTS) {
// Turn the sphere first, then measure the dent in view space so it stays put while the dots slide through it.
const rx = p.x * cosY - p.z * sinY;
const rz = p.x * sinY + p.z * cosY;
const ry = p.y * cosT - rz * sinT;
const vz = p.y * sinT + rz * cosT;
const along = rx * dir.x + ry * dir.y + vz * dir.z;
const rim = 0.18 + 0.08 * Math.sin(Math.atan2(ry, rx) * 3 + t * 2.1);
const press = smooth(rim, 0.95, along) * 0.92;
const r = 1 - press;
const x = rx * r;
const y = ry * r;
const z = vz * r;
const depth = (z + 1) / 2;
const perspective = 1 + z * 0.08;
const size = (0.55 + depth * 0.75) * perspective;
// Dots pressed deep into the dent crowd together; fade them so the dent reads as a hollow, not a bright knot.
ctx.globalAlpha = (0.12 + Math.pow(depth, 1.6) * 0.88) * (1 - press * 0.75);
ctx.beginPath();
ctx.arc(centre + x * RADIUS * perspective, centre + y * RADIUS * perspective, size, 0, Math.PI * 2);
ctx.fill();
}
ctx.globalAlpha = 1;
}
export default function ThinkingOrb({ active, context }: LivePreviewProps) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const reduce = useReducedMotion();
const [run, setRun] = useState(0);
const [detailRunning, setDetailRunning] = useState(true);
const playing = !reduce && (context === "card" ? active : detailRunning);
// The entry page runs for a while, then rests until Replay.
useEffect(() => {
if (context !== "detail" || !detailRunning) return;
const id = window.setTimeout(() => setDetailRunning(false), DETAIL_RUN_MS);
return () => window.clearTimeout(id);
}, [context, detailRunning, run]);
// Clock and speed survive between plays, so the orb picks up where it stopped.
const clock = useRef({ t: REST_TIME, speed: 0 });
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("Thinking orb: the 2D canvas is not available.");
// Drawn at up to four times its logical size: the stage scales the canvas up to 1.6 times on large frames.
const pixelRatio = Math.min(4, (window.devicePixelRatio || 1) * 2);
canvas.width = SIZE * pixelRatio;
canvas.height = SIZE * pixelRatio;
const ink = () => getComputedStyle(canvas).color;
let frame = 0;
let last = performance.now();
const tick = (now: number) => {
const state = clock.current;
const dt = Math.min(0.05, (now - last) / 1000);
last = now;
// Ease into and out of thinking rather than starting or stopping dead.
const target = playing ? 1 : 0;
state.speed += (target - state.speed) * Math.min(1, dt * 4);
state.t += dt * state.speed;
drawOrb(ctx, state.t, ink(), pixelRatio);
if (playing || state.speed > 0.002) frame = requestAnimationFrame(tick);
};
if (reduce) {
clock.current = { t: REST_TIME, speed: 0 };
drawOrb(ctx, REST_TIME, ink(), pixelRatio);
} else {
frame = requestAnimationFrame(tick);
}
// A theme switch changes the ink: redraw the resting frame in the new colour.
const redraw = () => drawOrb(ctx, clock.current.t, ink(), pixelRatio);
const themeObserver = new MutationObserver(redraw);
themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
const scheme = window.matchMedia("(prefers-color-scheme: dark)");
scheme.addEventListener("change", redraw);
return () => {
cancelAnimationFrame(frame);
themeObserver.disconnect();
scheme.removeEventListener("change", redraw);
};
}, [playing, reduce]);
return (
<div className={styles.root}>
<canvas ref={canvasRef} className={styles.orb} width={SIZE} height={SIZE} role="img" aria-label="A sphere of dots, thinking" />
{context === "detail" && (
<div className={styles.bar}>
<p className={styles.status} aria-live="polite">
{playing ? "Thinking" : "Resting"}
</p>
<button
type="button"
className={styles.replay}
onClick={() => {
setRun((r) => r + 1);
setDetailRunning(true);
}}
>
Replay
</button>
</div>
)}
</div>
);
}