Build a React 19 + TypeScript component called DitherCursor: a cursor effect from Alert's home page. A trail of ordered-dither pixels follows the pointer, keeps its momentum and fades, behind a short headline.
Constraints: one .tsx file starting with "use client", imports from react only. Put the CSS in a template string rendered as <style href="play-dither-cursor" precedence="play">, with every class prefixed dc-. The root fills its parent (position relative, width and height 100%, overflow hidden, isolation isolate), paints #f9f9f9 and sets touch-action: none. Props with defaults: ditherSize 4, radius 0.12, exponent 2, decay 0.022, momentum 0.9, and colors, a list of { name, color }.
Layers, back to front:
1. A canvas (aria-hidden) at dither-pixel resolution, ceil(root size / ditherSize), scaled back up in CSS with image-rendering: pixelated. Size it with a ResizeObserver on the root; reallocate the fields when the grid changes.
2. A hairline grid: 111px cells, 1px #ececec lines, a 2px #dadada dot per cell, masked out behind the copy.
3. Centred copy: "Every move you make", a line break, then "leaves a trail" in the accent. Inter 500, clamp(24px, 7cqi, 60px) (the root is an inline-size container), line height 1.08, letter spacing -.02em, ink #262626. Under it a 13px hint in rgba(38,38,38,.56): "Move your cursor", or "Drag your finger" when (hover: none) or (pointer: coarse) matches.
4. A white pill 24px from the bottom holding 32px round buttons with 16px color dots, in a group labelled "Accent color": Red #ef3b2d, Blue #2774ae, Maroon #8c1515, Orange #bf5700, Green #007030, Purple #57068c. Each has the color name as aria-label and title, plus aria-pressed; the picked one gets a 2px white ring and a 1.5px ring in its color. Picking sets the accent, and the accent text transitions over .9s with cubic-bezier(.3,.7,.2,1).
Simulation, a fluid with no pressure solve:
- Grid: one cell per 2x2 dither pixels, ceil(columns / 2) + 1 wide, likewise tall. Fields D, U, V plus swap buffers (Float32Array).
- Pointer in cells, relative to the root's bounding rect. Blob radius R in px = clamp(radius x height, min(80, 0.15 x width), 180).
- Smoothed velocity: v += (delta / frames - v) x 0.5, where frames = clamp(elapsed ms / 16.7, 0.5, 2). Cap speed at 2.6 cells per frame, then multiply by momentum.
- Splat along the segment from the last point in steps of R / 4, each with amount 0.55 x max(0.35, step length / (R / 4)). Density inside R gains amount x min(1, (1 - r / R) x 1.7) ^ exponent, capped at 1.8. Velocity: inside a disc of radius 1.6R centred 0.5R ahead along the motion, blend U and V toward the splat velocity by min(1, (1 - r / 1.6R) x 1.6) x 0.7.
- Each frame (dt in 60fps frames, at most 3): for every cell, step back by (U, V) x dt and sample D, U and V there bilinearly, reading zero outside the grid. New density is that minus decay x dt, floored at 0. New velocity is that times 0.95 ^ dt. Swap buffers.
- Draw: each dither pixel samples D at its centre, takes min(0.3, d) and shows the accent where that beats its 8x8 Bayer threshold, (value + 0.5) / 64; otherwise transparent. Write through a Uint32Array view of an ImageData and putImageData.
- Read the accent from the accent text's computed color, cached for 30ms, so the trail cross-fades with the text.
- Stop requestAnimationFrame when no density is left (zero the velocity), off screen (IntersectionObserver) or in a hidden tab. Restart on the next input.
Input: pointer events on the root. Mouse and pen paint on move. Touch and pen start a fresh stroke on pointerdown; pointerup, pointercancel and pointerleave end it.
Under prefers-reduced-motion, attach nothing, leave the canvas empty and hide the hint. Remove all listeners and observers on unmount.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334"use client";
import { useEffect, useRef, useState, type CSSProperties } from "react";
type Swatch = { name: string; color: string };
type Props = {
ditherSize?: number;
radius?: number;
exponent?: number;
decay?: number;
momentum?: number;
colors?: Swatch[];
};
const SWATCHES: Swatch[] = [
{ name: "Red", color: "#ef3b2d" },
{ name: "Blue", color: "#2774ae" },
{ name: "Maroon", color: "#8c1515" },
{ name: "Orange", color: "#bf5700" },
{ name: "Green", color: "#007030" },
{ name: "Purple", color: "#57068c" },
];
const BAYER = [
0, 32, 8, 40, 2, 34, 10, 42, 48, 16, 56, 24, 50, 18, 58, 26, 12, 44, 4, 36, 14, 46, 6, 38, 60, 28, 52, 20, 62, 30, 54, 22,
3, 35, 11, 43, 1, 33, 9, 41, 51, 19, 59, 27, 49, 17, 57, 25, 15, 47, 7, 39, 13, 45, 5, 37, 63, 31, 55, 23, 61, 29, 53, 21,
].map((v) => (v + 0.5) / 64);
const SIM = 2;
const PEAK = 0.3;
const FILL = 0.55;
const CAP = 1.8;
const FRICTION = 0.95;
const MAX_V = 2.6;
const FLOW = 1.6;
const LEAD = 0.5;
const CORE = 1.7;
let colorKey = "", colorVal = 0;
function packed(c: string) {
if (c === colorKey) return colorVal;
let r = 0, g = 0, b = 0;
const m = c.match(/rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/);
if (m) [r, g, b] = [m[1], m[2], m[3]].map((n) => Math.round(+n));
else {
let h = c.replace("#", "");
if (h.length === 3) h = h.replace(/./g, "$&$&");
const n = parseInt(h.slice(0, 6), 16) || 0;
r = n >> 16; g = (n >> 8) & 255; b = n & 255;
}
colorKey = c;
colorVal = ((255 << 24) | (b << 16) | (g << 8) | r) >>> 0;
return colorVal;
}
const css = `
.dc{--dc-bg:#f9f9f9;--dc-ink:#262626;--dc-muted:rgba(38,38,38,.56);--dc-line:#ececec;--dc-ease:cubic-bezier(.3,.7,.2,1);container:dc/inline-size;position:relative;width:100%;height:100%;overflow:hidden;isolation:isolate;background:var(--dc-bg);color:var(--dc-ink);font-family:var(--font-inter,Inter),system-ui,sans-serif;-webkit-font-smoothing:antialiased;touch-action:none;-webkit-tap-highlight-color:transparent}
.dc *,.dc *::before,.dc *::after{box-sizing:border-box}
.dc ::selection{background:color-mix(in srgb,var(--dc-accent) 20%,#f9f9f9);color:var(--dc-accent)}
.dc-canvas{position:absolute;left:0;top:0;z-index:0;display:block;image-rendering:pixelated;pointer-events:none}
/* Alert's hairline grid with a dot in each cell, drawn over the trail, faded out behind the copy */
.dc-grid{position:absolute;inset:0;z-index:1;pointer-events:none;background-image:radial-gradient(circle,#dadada 0 1px,transparent 1.5px),linear-gradient(to right,var(--dc-line) 1px,transparent 1px),linear-gradient(to bottom,var(--dc-line) 1px,transparent 1px);background-size:111px 111px;background-position:center;-webkit-mask-image:radial-gradient(closest-side at 50% 45%,transparent 50%,#000 100%);mask-image:radial-gradient(closest-side at 50% 45%,transparent 50%,#000 100%)}
.dc-copy{position:absolute;inset:0;z-index:2;display:flex;flex-direction:column;align-items:center;justify-content:center;padding:24px 24px 96px;text-align:center;pointer-events:none;user-select:none;-webkit-user-select:none}
.dc-h{margin:0;font-size:clamp(24px,7cqi,60px);line-height:1.08;font-weight:500;letter-spacing:-.02em;color:var(--dc-ink);text-wrap:balance}
.dc-hot{color:var(--dc-accent);transition:color .9s var(--dc-ease)}
.dc-hint{margin:18px 0 0;font-size:13px;line-height:1.4;color:var(--dc-muted)}
.dc-touch{display:none}
@media (hover:none),(pointer:coarse){.dc-mouse{display:none}.dc-touch{display:inline}}
.dc-sw{position:absolute;left:50%;bottom:24px;z-index:3;display:flex;gap:2px;padding:4px;border-radius:999px;background:#fff;box-shadow:0 1px 2px rgba(0,0,0,.08),0 0 0 .5px rgba(0,0,0,.05);transform:translateX(-50%)}
.dc-sw button{display:grid;place-items:center;width:32px;height:32px;padding:0;border:0;border-radius:50%;background:none;cursor:pointer}
.dc-sw button::before{content:"";width:16px;height:16px;border-radius:50%;background:var(--dc-c);box-shadow:inset 0 0 0 1px rgba(0,0,0,.08);transition:transform .25s var(--dc-ease),box-shadow .25s var(--dc-ease)}
.dc-sw button[aria-pressed="true"]::before{box-shadow:0 0 0 2px #fff,0 0 0 3.5px var(--dc-c)}
.dc-sw button:focus-visible{outline:2px solid var(--dc-ink);outline-offset:0}
@media (hover:hover){.dc-sw button:hover::before{transform:scale(1.12)}}
@media (prefers-reduced-motion:reduce){
.dc-hint{display:none}
.dc *,.dc *::before,.dc *::after{transition:none!important}
}
`;
export default function DitherCursor({ ditherSize = 4, radius = 0.12, exponent = 2, decay = 0.022, momentum = 0.9, colors = SWATCHES }: Props) {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const hotRef = useRef<HTMLSpanElement>(null);
const [pick, setPick] = useState(0);
const accent = colors[pick]?.color ?? colors[0]?.color ?? "#ef3b2d";
const opts = useRef({ radius, exponent, decay, momentum, accent });
useEffect(() => { opts.current = { radius, exponent, decay, momentum, accent }; });
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
const ctx = canvas?.getContext("2d");
if (!root || !canvas || !ctx) return;
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
const ds = Math.max(1, Math.round(ditherSize));
const cell = ds * SIM;
let W = 0, H = 0, gw = 0, gh = 0, sw = 0, sh = 0;
let D = new Float32Array(0), U = D, V = D, D2 = D, U2 = D, V2 = D;
let img = ctx.createImageData(1, 1), px = new Uint32Array(img.data.buffer);
let raf = 0, last = 0, live = false, onScreen = true, tabShown = !document.hidden;
let has = false, mx = 0, my = 0, mt = 0, vx = 0, vy = 0;
let colAt = 0, col = "";
const size = () => {
W = root.clientWidth; H = root.clientHeight;
const nw = Math.max(1, Math.ceil(W / ds)), nh = Math.max(1, Math.ceil(H / ds));
if (nw === gw && nh === gh) return;
gw = nw; gh = nh;
sw = Math.ceil(gw / SIM) + 1; sh = Math.ceil(gh / SIM) + 1;
canvas.width = gw; canvas.height = gh;
canvas.style.width = `${gw * ds}px`; canvas.style.height = `${gh * ds}px`;
const n = sw * sh;
D = new Float32Array(n); U = new Float32Array(n); V = new Float32Array(n);
D2 = new Float32Array(n); U2 = new Float32Array(n); V2 = new Float32Array(n);
img = ctx.createImageData(gw, gh);
px = new Uint32Array(img.data.buffer);
};
const color = () => {
const now = performance.now();
if (!col || now - colAt > 30) {
colAt = now;
col = (hotRef.current && getComputedStyle(hotRef.current).color) || opts.current.accent;
}
return col;
};
const at = (f: Float32Array, x: number, y: number) => {
const x0 = Math.floor(x), y0 = Math.floor(y), fx = x - x0, fy = y - y0;
const inX0 = x0 >= 0 && x0 < sw, inX1 = x0 + 1 >= 0 && x0 + 1 < sw;
const r0 = y0 >= 0 && y0 < sh ? y0 * sw : -1, r1 = y0 + 1 >= 0 && y0 + 1 < sh ? (y0 + 1) * sw : -1;
const a = r0 >= 0 && inX0 ? f[r0 + x0] : 0, b = r0 >= 0 && inX1 ? f[r0 + x0 + 1] : 0;
const c = r1 >= 0 && inX0 ? f[r1 + x0] : 0, d = r1 >= 0 && inX1 ? f[r1 + x0 + 1] : 0;
return (a + (b - a) * fx) * (1 - fy) + (c + (d - c) * fx) * fy;
};
const splat = (cx: number, cy: number, R: number, svx: number, svy: number, amt: number) => {
const e = opts.current.exponent;
const sp = Math.hypot(svx, svy) || 1;
const vcx = cx + (svx / sp) * R * LEAD, vcy = cy + (svy / sp) * R * LEAD;
const Rv = R * FLOW, reach = R * LEAD + Rv;
const x0 = Math.max(0, Math.floor(cx - reach)), x1 = Math.min(sw - 1, Math.ceil(cx + reach));
const y0 = Math.max(0, Math.floor(cy - reach)), y1 = Math.min(sh - 1, Math.ceil(cy + reach));
for (let y = y0; y <= y1; y++) {
for (let x = x0; x <= x1; x++) {
const i = y * sw + x;
const rv = Math.hypot(x - vcx, y - vcy);
if (rv < Rv) {
const w = Math.min(1, (1 - rv / Rv) * 1.6) * 0.7;
U[i] += (svx - U[i]) * w;
V[i] += (svy - V[i]) * w;
}
const r = Math.hypot(x - cx, y - cy);
if (r >= R) continue;
const s = Math.pow(Math.min(1, (1 - r / R) * CORE), e);
D[i] = Math.min(CAP, D[i] + amt * s);
}
}
};
const frame = (now: number) => {
const dt = last ? Math.min(3, (now - last) / (1000 / 60)) : 1;
last = now;
const fade = opts.current.decay * dt, keep = Math.pow(FRICTION, dt);
live = false;
for (let y = 0; y < sh; y++) {
for (let x = 0; x < sw; x++) {
const i = y * sw + x;
const fx = x - U[i] * dt, fy = y - V[i] * dt;
const d = at(D, fx, fy) - fade;
D2[i] = d > 0 ? d : 0;
U2[i] = at(U, fx, fy) * keep;
V2[i] = at(V, fx, fy) * keep;
if (d > 0) live = true;
}
}
[D, D2] = [D2, D]; [U, U2] = [U2, U]; [V, V2] = [V2, V];
const on = packed(color());
for (let y = 0; y < gh; y++) {
const row = (y & 7) << 3, fy = (y + 0.5) / SIM - 0.5;
for (let x = 0; x < gw; x++) {
px[y * gw + x] = Math.min(PEAK, at(D, (x + 0.5) / SIM - 0.5, fy)) > BAYER[row | (x & 7)] ? on : 0;
}
}
ctx.putImageData(img, 0, 0);
if (live && onScreen && tabShown) raf = requestAnimationFrame(frame);
else {
raf = 0; last = 0;
if (!live) { U.fill(0); V.fill(0); }
}
};
const wake = () => { if (!raf && onScreen && tabShown) raf = requestAnimationFrame(frame); };
const sleep = () => { cancelAnimationFrame(raf); raf = 0; last = 0; };
const move = (e: PointerEvent) => {
if (!W || !H) return;
const rect = root.getBoundingClientRect();
const cx = (e.clientX - rect.left) / cell, cy = (e.clientY - rect.top) / cell;
const { radius: r, momentum: m } = opts.current;
const R = Math.min(180, Math.max(Math.min(80, W * 0.15), r * H)) / cell;
if (!has) { has = true; mx = cx; my = cy; mt = e.timeStamp; }
const dx = cx - mx, dy = cy - my;
const frames = Math.min(2, Math.max(0.5, (e.timeStamp - mt) / (1000 / 60)));
vx += (dx / frames - vx) * 0.5; vy += (dy / frames - vy) * 0.5;
const sp = Math.hypot(vx, vy), cap = sp > MAX_V ? MAX_V / sp : 1;
const step = R / 4, dist = Math.hypot(dx, dy);
const n = Math.max(1, Math.ceil(dist / step));
const amt = FILL * Math.max(0.35, dist / n / step);
for (let s = 1; s <= n; s++) splat(mx + (dx * s) / n, my + (dy * s) / n, R, vx * cap * m, vy * cap * m, amt);
mx = cx; my = cy; mt = e.timeStamp;
live = true;
wake();
};
const lift = () => { has = false; vx = vy = 0; };
const down = (e: PointerEvent) => { if (e.pointerType !== "mouse") { lift(); move(e); } };
const up = (e: PointerEvent) => { if (e.pointerType !== "mouse") lift(); };
const onVisibility = () => {
tabShown = !document.hidden;
if (!tabShown) sleep();
else if (live) wake();
};
size();
const ro = new ResizeObserver(size);
ro.observe(root);
const io = new IntersectionObserver(([en]) => {
onScreen = en.isIntersecting;
if (!onScreen) sleep();
else if (live) wake();
});
io.observe(root);
root.addEventListener("pointermove", move, { passive: true });
root.addEventListener("pointerdown", down, { passive: true });
root.addEventListener("pointerup", up);
root.addEventListener("pointercancel", lift);
root.addEventListener("pointerleave", lift);
document.addEventListener("visibilitychange", onVisibility);
return () => {
sleep();
ro.disconnect();
io.disconnect();
root.removeEventListener("pointermove", move);
root.removeEventListener("pointerdown", down);
root.removeEventListener("pointerup", up);
root.removeEventListener("pointercancel", lift);
root.removeEventListener("pointerleave", lift);
document.removeEventListener("visibilitychange", onVisibility);
};
}, [ditherSize]);
return (
<div className="dc" ref={rootRef} style={{ "--dc-accent": accent } as CSSProperties}>
<style href="play-dither-cursor" precedence="play">{css}</style>
<canvas ref={canvasRef} className="dc-canvas" aria-hidden="true" />
<div className="dc-grid" aria-hidden="true" />
<div className="dc-copy">
<p className="dc-h">Every move you make<br /><span className="dc-hot" ref={hotRef}>leaves a trail</span></p>
<p className="dc-hint"><span className="dc-mouse">Move your cursor</span><span className="dc-touch">Drag your finger</span></p>
</div>
{colors.length > 1 && (
<div className="dc-sw" role="group" aria-label="Accent color">
{colors.map((s, i) => (
<button
key={s.name}
type="button"
aria-label={s.name}
title={s.name}
aria-pressed={i === pick}
style={{ "--dc-c": s.color } as CSSProperties}
onClick={() => setPick(i)}
/>
))}
</div>
)}
</div>
);
}