Dot ripple field
A field of dots that acts like water: they swell away from your cursor like a lens, and a click drops a ripple that lifts and lights them as it travels.
"use client";
import { useEffect, useRef } from "react";
import "./dot-ripple.css";
type Wave = { x: number; y: number; t: number; strong: boolean };
/**
* DotRipple: a field of dots that behaves like the surface of water.
* - Dots swell and lean away from the pointer like a lens moving over them.
* - Click or tap to drop a stone: a ring travels outward, lifting and colouring the dots it passes.
* - When nobody touches it, a gentle ripple starts by itself every few seconds.
*/
export function DotRipple({
theme = "dark",
gap = 22,
label = "Touch the surface",
}: {
theme?: "light" | "dark";
gap?: number;
label?: string;
}) {
const canvas = useRef<HTMLCanvasElement>(null);
const drop = useRef<(x?: number, y?: number) => void>(() => {});
useEffect(() => {
const cv = canvas.current!;
const ctx = cv.getContext("2d")!;
const reduce = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const ink = theme === "dark" ? [243, 238, 227] : [14, 14, 16];
let w = 0;
let h = 0;
let dpr = 1;
let raf = 0;
const pointer = { x: -999, y: -999, k: 0, on: false };
const waves: Wave[] = [];
let idle = 0;
let seed = 7;
const rand = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const fit = () => {
const b = cv.getBoundingClientRect();
dpr = Math.min(2, window.devicePixelRatio || 1);
w = b.width;
h = b.height;
cv.width = Math.round(w * dpr);
cv.height = Math.round(h * dpr);
};
fit();
const ro = new ResizeObserver(fit);
ro.observe(cv);
drop.current = (x = w / 2, y = h / 2) => {
// With reduced motion we draw one still frame with the ring already part-way out.
waves.push({
x,
y,
t: performance.now() - (reduce ? 450 : 0),
strong: true,
});
idle = performance.now();
if (reduce) draw(performance.now());
};
const draw = (now: number) => {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, w, h);
pointer.k += ((pointer.on ? 1 : 0) - pointer.k) * 0.12;
while (waves.length && now - waves[0].t > 2600) waves.shift();
const cols = Math.ceil(w / gap) + 1;
const rows = Math.ceil(h / gap) + 1;
const ox = (w - (cols - 1) * gap) / 2;
const oy = (h - (rows - 1) * gap) / 2;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
let x = ox + c * gap;
let y = oy + r * gap;
let size = 1.3;
let heat = 0;
// The pointer: a lens that pushes dots outward and makes them bigger.
const dx = x - pointer.x;
const dy = y - pointer.y;
const d = Math.hypot(dx, dy);
if (d < 120 && pointer.k > 0.01) {
const f = (1 - d / 120) ** 2 * pointer.k;
x += (dx / (d || 1)) * f * 14;
y += (dy / (d || 1)) * f * 14;
size += f * 2.6;
}
// Each wave is a travelling ring with a fading tail.
for (const wv of waves) {
const age = (now - wv.t) / 1000;
const radius = age * 340;
const wd = Math.hypot(ox + c * gap - wv.x, oy + r * gap - wv.y);
const off = wd - radius;
if (off > -90 && off < 30) {
const fade = Math.max(0, 1 - age / 2.6) * (wv.strong ? 1 : 0.6);
const amp =
Math.sin((off / 90) * Math.PI * 2) *
Math.exp(-((off + 20) ** 2) / 1600);
size += Math.max(0, amp) * 4.6 * fade;
y -= amp * 10 * fade;
heat = Math.max(heat, Math.min(1, Math.max(0, amp) * fade * 1.5));
}
}
const rC = Math.round(ink[0] + (255 - ink[0]) * heat);
const gC = Math.round(ink[1] + (90 - ink[1]) * heat);
const bC = Math.round(ink[2] + (31 - ink[2]) * heat);
ctx.fillStyle = `rgba(${rC},${gC},${bC},${0.32 + Math.min(0.68, heat + (size - 1.3) * 0.25)})`;
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.fill();
}
}
};
const loop = (now: number) => {
if (now - idle > 4200 && !pointer.on) {
waves.push({
x: w * (0.2 + rand() * 0.6),
y: h * (0.25 + rand() * 0.5),
t: now,
strong: false,
});
idle = now;
}
draw(now);
raf = requestAnimationFrame(loop);
};
const local = (e: PointerEvent) => {
const b = cv.getBoundingClientRect();
return { x: e.clientX - b.left, y: e.clientY - b.top };
};
const move = (e: PointerEvent) => {
const p = local(e);
pointer.x = p.x;
pointer.y = p.y;
pointer.on = true;
idle = performance.now();
};
const leave = () => (pointer.on = false);
const down = (e: PointerEvent) => {
const p = local(e);
drop.current(p.x, p.y);
};
cv.addEventListener("pointermove", move);
cv.addEventListener("pointerleave", leave);
cv.addEventListener("pointerdown", down);
if (reduce) draw(performance.now());
else {
idle = performance.now() - 3600;
raf = requestAnimationFrame(loop);
}
return () => {
cancelAnimationFrame(raf);
ro.disconnect();
cv.removeEventListener("pointermove", move);
cv.removeEventListener("pointerleave", leave);
cv.removeEventListener("pointerdown", down);
};
}, [theme, gap]);
return (
<div className="gu-ripple" data-theme={theme}>
<canvas ref={canvas} aria-hidden="true" />
<div className="gu-ripple__ui">
<p>{label}</p>
<button type="button" onClick={() => drop.current()}>
Drop a ripple
</button>
</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Dot ripple field
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A canvas panel full of small dots that behaves like the surface of water. The cursor is a lens that pushes dots outward and makes them bigger; a click drops a stone and a ring wave travels outward, lifting dots and tinting them orange as it passes. With no one touching it, a softer ripple starts by itself every 4.2 seconds.
## Layout and look
- Panel max 760px wide, 380px tall, radius 28px. Dark: #0e0e10 with a faint orange glow rising from the bottom; light: #f3eee3 with the same glow.
- Dots on a centred grid every 22px, radius 1.3px, cream (#f3eee3) on dark or ink (#0e0e10) on light, at 32% opacity; wave dots grow up to +4.6px and lift up to 10px.
- Bottom row: a 14px label at 70% opacity on the left ("Touch the surface"), a 44px pill outline button on the right ("Drop a ripple") that fills with #ff5a1f on hover.
## Behavior and motion
1. Canvas at devicePixelRatio (max 2), resized with a ResizeObserver; drawn every frame with requestAnimationFrame.
2. Lens: within 120px of the pointer, each dot moves away by up to 14px and grows by up to 2.6px, with a (1 − d/120)² falloff; the lens strength eases in and out (factor 0.12 per frame) when the pointer enters or leaves.
3. Waves: radius grows at 340px/s and lives 2.6s. For each dot, offset = distance − radius; between −90 and +30 the dot gets amp = sin(offset/90·2π)·exp(−(offset+20)²/1600). Positive amp grows it (×4.6), all amp lifts it (y − amp·10), and the colour mixes toward #ff5a1f. Waves fade linearly over their life; automatic ones are at 55% strength and start at seeded random points in the middle area.
4. The button drops a ripple from the centre.
## Accessibility
- The canvas is aria-hidden (decorative); the button is a real, keyboard-focusable way to start a ripple, with a visible focus ring.
- prefers-reduced-motion: no animation loop and no automatic ripples; a tap or the button draws a single still frame.
## Props
theme ("light" | "dark"), gap (dot spacing, default 22), label (text in the corner).
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### DotRipple.tsx
```tsx
"use client";
import { useEffect, useRef } from "react";
import "./dot-ripple.css";
type Wave = { x: number; y: number; t: number; strong: boolean };
/**
* DotRipple: a field of dots that behaves like the surface of water.
* - Dots swell and lean away from the pointer like a lens moving over them.
* - Click or tap to drop a stone: a ring travels outward, lifting and colouring the dots it passes.
* - When nobody touches it, a gentle ripple starts by itself every few seconds.
*/
export function DotRipple({
theme = "dark",
gap = 22,
label = "Touch the surface",
}: {
theme?: "light" | "dark";
gap?: number;
label?: string;
}) {
const canvas = useRef<HTMLCanvasElement>(null);
const drop = useRef<(x?: number, y?: number) => void>(() => {});
useEffect(() => {
const cv = canvas.current!;
const ctx = cv.getContext("2d")!;
const reduce = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const ink = theme === "dark" ? [243, 238, 227] : [14, 14, 16];
let w = 0;
let h = 0;
let dpr = 1;
let raf = 0;
const pointer = { x: -999, y: -999, k: 0, on: false };
const waves: Wave[] = [];
let idle = 0;
let seed = 7;
const rand = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const fit = () => {
const b = cv.getBoundingClientRect();
dpr = Math.min(2, window.devicePixelRatio || 1);
w = b.width;
h = b.height;
cv.width = Math.round(w * dpr);
cv.height = Math.round(h * dpr);
};
fit();
const ro = new ResizeObserver(fit);
ro.observe(cv);
drop.current = (x = w / 2, y = h / 2) => {
// With reduced motion we draw one still frame with the ring already part-way out.
waves.push({
x,
y,
t: performance.now() - (reduce ? 450 : 0),
strong: true,
});
idle = performance.now();
if (reduce) draw(performance.now());
};
const draw = (now: number) => {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, w, h);
pointer.k += ((pointer.on ? 1 : 0) - pointer.k) * 0.12;
while (waves.length && now - waves[0].t > 2600) waves.shift();
const cols = Math.ceil(w / gap) + 1;
const rows = Math.ceil(h / gap) + 1;
const ox = (w - (cols - 1) * gap) / 2;
const oy = (h - (rows - 1) * gap) / 2;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
let x = ox + c * gap;
let y = oy + r * gap;
let size = 1.3;
let heat = 0;
// The pointer: a lens that pushes dots outward and makes them bigger.
const dx = x - pointer.x;
const dy = y - pointer.y;
const d = Math.hypot(dx, dy);
if (d < 120 && pointer.k > 0.01) {
const f = (1 - d / 120) ** 2 * pointer.k;
x += (dx / (d || 1)) * f * 14;
y += (dy / (d || 1)) * f * 14;
size += f * 2.6;
}
// Each wave is a travelling ring with a fading tail.
for (const wv of waves) {
const age = (now - wv.t) / 1000;
const radius = age * 340;
const wd = Math.hypot(ox + c * gap - wv.x, oy + r * gap - wv.y);
const off = wd - radius;
if (off > -90 && off < 30) {
const fade = Math.max(0, 1 - age / 2.6) * (wv.strong ? 1 : 0.6);
const amp =
Math.sin((off / 90) * Math.PI * 2) *
Math.exp(-((off + 20) ** 2) / 1600);
size += Math.max(0, amp) * 4.6 * fade;
y -= amp * 10 * fade;
heat = Math.max(heat, Math.min(1, Math.max(0, amp) * fade * 1.5));
}
}
const rC = Math.round(ink[0] + (255 - ink[0]) * heat);
const gC = Math.round(ink[1] + (90 - ink[1]) * heat);
const bC = Math.round(ink[2] + (31 - ink[2]) * heat);
ctx.fillStyle = `rgba(${rC},${gC},${bC},${0.32 + Math.min(0.68, heat + (size - 1.3) * 0.25)})`;
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.fill();
}
}
};
const loop = (now: number) => {
if (now - idle > 4200 && !pointer.on) {
waves.push({
x: w * (0.2 + rand() * 0.6),
y: h * (0.25 + rand() * 0.5),
t: now,
strong: false,
});
idle = now;
}
draw(now);
raf = requestAnimationFrame(loop);
};
const local = (e: PointerEvent) => {
const b = cv.getBoundingClientRect();
return { x: e.clientX - b.left, y: e.clientY - b.top };
};
const move = (e: PointerEvent) => {
const p = local(e);
pointer.x = p.x;
pointer.y = p.y;
pointer.on = true;
idle = performance.now();
};
const leave = () => (pointer.on = false);
const down = (e: PointerEvent) => {
const p = local(e);
drop.current(p.x, p.y);
};
cv.addEventListener("pointermove", move);
cv.addEventListener("pointerleave", leave);
cv.addEventListener("pointerdown", down);
if (reduce) draw(performance.now());
else {
idle = performance.now() - 3600;
raf = requestAnimationFrame(loop);
}
return () => {
cancelAnimationFrame(raf);
ro.disconnect();
cv.removeEventListener("pointermove", move);
cv.removeEventListener("pointerleave", leave);
cv.removeEventListener("pointerdown", down);
};
}, [theme, gap]);
return (
<div className="gu-ripple" data-theme={theme}>
<canvas ref={canvas} aria-hidden="true" />
<div className="gu-ripple__ui">
<p>{label}</p>
<button type="button" onClick={() => drop.current()}>
Drop a ripple
</button>
</div>
</div>
);
}
```
### dot-ripple.css
```css
.gu-ripple {
position: relative;
width: min(100%, 760px);
height: 380px;
overflow: hidden;
border-radius: 28px;
background:
radial-gradient(
70% 90% at 50% 110%,
rgb(255 90 31 / 0.16),
transparent 70%
),
#0e0e10;
color: #f3eee3;
font-family: ui-sans-serif, system-ui, sans-serif;
}
.gu-ripple[data-theme="light"] {
background:
radial-gradient(
70% 90% at 50% 110%,
rgb(255 90 31 / 0.14),
transparent 70%
),
#f3eee3;
color: #0e0e10;
}
.gu-ripple canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
cursor: crosshair;
touch-action: manipulation;
}
.gu-ripple__ui {
position: absolute;
left: 22px;
right: 22px;
bottom: 20px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.gu-ripple__ui p {
margin: 0;
font-size: 14px;
font-weight: 600;
opacity: 0.7;
}
.gu-ripple__ui button {
pointer-events: auto;
min-height: 44px;
padding: 0 18px;
border: 1.5px solid currentColor;
border-radius: 999px;
background: transparent;
color: inherit;
font-weight: 700;
font-size: 14px;
line-height: 1;
font-family: inherit;
cursor: pointer;
transition:
background-color 0.3s,
color 0.3s,
scale 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.gu-ripple__ui button:hover {
background: #ff5a1f;
border-color: #ff5a1f;
color: #0e0e10;
}
.gu-ripple__ui button:active {
scale: 0.95;
}
.gu-ripple__ui button:focus-visible {
outline: 3px solid #ff5a1f;
outline-offset: 3px;
}
```
You might also want
Next piece: How it works, sticky scenesConfetti send button
Animations, beginner
Cursor trail
Animations, intermediate
Letter scatter headline
Animations, beginner