Repository navigation
Expand file tree
/
Copy pathdot-grid.tsx
More file actions
290 lines (264 loc) · 9.13 KB
/
Copy pathdot-grid.tsx
File metadata and controls
290 lines (264 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"use client";
import { useEffect, useRef } from "react";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { cn } from "@/lib/cn";
const SPACING = 16;
const DOT = 1.5;
// Dots within this radius feel the cursor; falloff is quadratic to the edge.
const FIELD = 88;
// Far enough to read as a push, small enough that neighbours never overlap.
const PUSH = 7;
const GROW = 1.3;
// Slightly underdamped (ratio about 0.7), so dots spring back with a hint of
// give instead of sliding home.
const STIFFNESS = 170;
const DAMPING = 18;
// A click ring crosses the 480px grid in about 0.9s.
const RING_SPEED = 620;
const RING_WIDTH = 22;
const RING_PUSH = 9;
const REST = 0.01;
type Ring = { x: number; y: number; born: number };
export function DotGrid({ className }: { className?: string }) {
const reduce = useReducedMotion() ?? false;
const wrapRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const wrap = wrapRef.current;
const canvas = canvasRef.current;
const ctx = canvas?.getContext("2d");
if (!wrap || !canvas || !ctx) return;
let width = 0;
let height = 0;
let count = 0;
// Flat typed arrays keep the per-frame loop allocation free.
let baseX = new Float32Array(0);
let baseY = new Float32Array(0);
let offX = new Float32Array(0);
let offY = new Float32Array(0);
let velX = new Float32Array(0);
let velY = new Float32Array(0);
let size = new Float32Array(0);
let velSize = new Float32Array(0);
let pointer: { x: number; y: number } | null = null;
let rings: Ring[] = [];
let frameId = 0;
let last = 0;
let visible = true;
let dim = "";
let lit = "";
const readColors = () => {
// Computed colors resolve light-dark(), which a raw custom property doesn't.
dim = getComputedStyle(canvas).color;
lit = getComputedStyle(wrap).color;
};
const layout = () => {
const dpr = window.devicePixelRatio || 1;
width = wrap.clientWidth;
height = wrap.clientHeight;
canvas.width = Math.round(width * dpr);
canvas.height = Math.round(height * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
const cols = Math.floor(width / SPACING);
const rows = Math.floor(height / SPACING);
// Centre the lattice so the margins match on every side.
const startX = (width - (cols - 1) * SPACING) / 2;
const startY = (height - (rows - 1) * SPACING) / 2;
count = cols * rows;
baseX = new Float32Array(count);
baseY = new Float32Array(count);
offX = new Float32Array(count);
offY = new Float32Array(count);
velX = new Float32Array(count);
velY = new Float32Array(count);
size = new Float32Array(count);
velSize = new Float32Array(count);
for (let r = 0, i = 0; r < rows; r++) {
for (let c = 0; c < cols; c++, i++) {
baseX[i] = startX + c * SPACING;
baseY[i] = startY + r * SPACING;
}
}
};
const draw = () => {
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = dim;
// Resting dots sit back as texture; the lit pass supplies the contrast.
ctx.globalAlpha = 0.5;
ctx.beginPath();
for (let i = 0; i < count; i++) {
const x = baseX[i] + offX[i];
const y = baseY[i] + offY[i];
const r = DOT * (1 + (reduce ? 0 : GROW) * size[i]);
ctx.moveTo(x + r, y);
ctx.arc(x, y, r, 0, Math.PI * 2);
}
ctx.fill();
// Only the few energised dots get a second, brighter pass.
ctx.fillStyle = lit;
for (let i = 0; i < count; i++) {
const s = size[i];
if (s < 0.02) continue;
const x = baseX[i] + offX[i];
const y = baseY[i] + offY[i];
const r = DOT * (1 + (reduce ? 0 : GROW) * s);
ctx.globalAlpha = Math.min(1, s);
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fill();
}
ctx.globalAlpha = 1;
};
const step = (now: number) => {
// Clamped so a dropped frame or a background tab can't explode the spring.
const dt = Math.min((now - last) / 1000, 1 / 30);
last = now;
const t = now / 1000;
rings = rings.filter(
(ring) => (t - ring.born) * RING_SPEED < Math.hypot(width, height) + RING_WIDTH,
);
let moving = rings.length > 0;
for (let i = 0; i < count; i++) {
let targetX = 0;
let targetY = 0;
let targetSize = 0;
if (pointer) {
const dx = baseX[i] - pointer.x;
const dy = baseY[i] - pointer.y;
const d = Math.hypot(dx, dy);
if (d < FIELD) {
const f = (1 - d / FIELD) ** 2;
targetSize = f;
if (!reduce && d > 0.001) {
targetX = (dx / d) * PUSH * f;
targetY = (dy / d) * PUSH * f;
}
}
}
for (const ring of rings) {
const dx = baseX[i] - ring.x;
const dy = baseY[i] - ring.y;
const d = Math.hypot(dx, dy);
const radius = (t - ring.born) * RING_SPEED;
const band = Math.exp(-(((d - radius) / RING_WIDTH) ** 2));
// The ring weakens as it spreads, like a real shockwave.
const fade = Math.max(0, 1 - radius / Math.hypot(width, height));
const f = band * fade;
if (f < 0.01 || d < 0.001) continue;
targetX += (dx / d) * RING_PUSH * f;
targetY += (dy / d) * RING_PUSH * f;
targetSize = Math.max(targetSize, f);
}
velX[i] += (STIFFNESS * (targetX - offX[i]) - DAMPING * velX[i]) * dt;
velY[i] += (STIFFNESS * (targetY - offY[i]) - DAMPING * velY[i]) * dt;
velSize[i] += (STIFFNESS * (targetSize - size[i]) - DAMPING * velSize[i]) * dt;
offX[i] += velX[i] * dt;
offY[i] += velY[i] * dt;
size[i] = Math.max(0, size[i] + velSize[i] * dt);
// Settled means at its target, not at home: a resting cursor costs nothing.
if (
Math.abs(targetX - offX[i]) > REST ||
Math.abs(targetY - offY[i]) > REST ||
Math.abs(targetSize - size[i]) > REST ||
Math.abs(velX[i]) > REST ||
Math.abs(velY[i]) > REST ||
Math.abs(velSize[i]) > REST
) {
moving = true;
}
}
draw();
frameId = moving && visible ? requestAnimationFrame(step) : 0;
};
// Sleeps when everything settles; any input wakes it.
const wake = () => {
if (frameId || !visible) return;
last = performance.now();
frameId = requestAnimationFrame(step);
};
const local = (e: PointerEvent) => {
const box = canvas.getBoundingClientRect();
return { x: e.clientX - box.left, y: e.clientY - box.top };
};
const onMove = (e: PointerEvent) => {
if (e.pointerType === "touch") return;
pointer = local(e);
wake();
};
const onLeave = () => {
pointer = null;
wake();
};
const onDown = (e: PointerEvent) => {
if (reduce || e.button !== 0) return;
// Three rings at once is already a lot; older ones make way.
rings = [...rings.slice(-2), { ...local(e), born: performance.now() / 1000 }];
wake();
};
readColors();
layout();
draw();
const resize = new ResizeObserver(() => {
layout();
draw();
});
resize.observe(wrap);
const repaint = () =>
requestAnimationFrame(() => {
readColors();
if (!frameId) draw();
});
const theme = new MutationObserver(repaint);
theme.observe(document.documentElement, {
attributes: true,
attributeFilter: ["data-theme"],
});
const scheme = window.matchMedia("(prefers-color-scheme: dark)");
scheme.addEventListener("change", repaint);
// Offscreen, the loop pauses entirely.
const seen = new IntersectionObserver(([entry]) => {
visible = entry.isIntersecting;
if (visible) wake();
else if (frameId) {
cancelAnimationFrame(frameId);
frameId = 0;
}
});
seen.observe(wrap);
canvas.addEventListener("pointermove", onMove);
canvas.addEventListener("pointerleave", onLeave);
canvas.addEventListener("pointerdown", onDown);
return () => {
cancelAnimationFrame(frameId);
resize.disconnect();
theme.disconnect();
seen.disconnect();
scheme.removeEventListener("change", repaint);
canvas.removeEventListener("pointermove", onMove);
canvas.removeEventListener("pointerleave", onLeave);
canvas.removeEventListener("pointerdown", onDown);
};
}, [reduce]);
return (
<div
ref={wrapRef}
className={cn(
"relative aspect-[3/2] w-[min(480px,100%)] overflow-hidden rounded-2xl bg-surface text-foreground shadow-raised",
className,
)}
>
<canvas
ref={canvasRef}
aria-hidden
className="absolute inset-0 size-full touch-manipulation text-muted"
/>
<p className="sr-only">
A decorative grid of dots that swell and move away from the pointer.
Clicking sends a ring outward through the grid.
</p>
</div>
);
}
export default function DotGridDemo() {
return <DotGrid />;
}