Letter scatter headline
A huge headline whose letters lean, lift and turn away from your cursor, glow orange up close, and blow apart on click before gathering back letter by letter.
"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type PointerEvent,
} from "react";
import "./letter-scatter.css";
/**
* LetterScatter: a headline whose letters shy away from the cursor.
* - Letters near the pointer lean away, lift and turn, then spring home when it leaves.
* - Click (or press the button) to blow the whole word apart; it gathers again letter by letter.
*/
export function LetterScatter({
text = "Move gently",
theme = "dark",
radius = 140,
}: {
text?: string;
theme?: "light" | "dark";
radius?: number;
}) {
const box = useRef<HTMLHeadingElement>(null);
const centers = useRef<{ x: number; y: number }[]>([]);
const [burst, setBurst] = useState(0);
const measure = () => {
const spans = box.current?.querySelectorAll<HTMLElement>(".gu-scatter__ch");
if (!spans) return;
centers.current = [...spans].map((s) => ({
x: s.offsetLeft + s.offsetWidth / 2,
y: s.offsetTop + s.offsetHeight / 2,
}));
};
useEffect(() => {
measure();
const ro = new ResizeObserver(measure);
if (box.current) ro.observe(box.current);
return () => ro.disconnect();
}, [text]);
const move = (e: PointerEvent<HTMLHeadingElement>) => {
const el = box.current;
if (!el) return;
const b = el.getBoundingClientRect();
const k = el.offsetWidth / b.width || 1;
const px = (e.clientX - b.left) * k;
const py = (e.clientY - b.top) * k;
el.querySelectorAll<HTMLElement>(".gu-scatter__ch").forEach((s, i) => {
const c = centers.current[i];
if (!c) return;
const dx = c.x - px;
const dy = c.y - py;
const d = Math.hypot(dx, dy);
const f = Math.max(0, 1 - d / radius) ** 1.6;
s.style.setProperty("--x", `${(dx / (d || 1)) * f * 46}px`);
s.style.setProperty("--y", `${(dy / (d || 1)) * f * 46 - f * 18}px`);
s.style.setProperty("--r", `${(dx / (d || 1)) * f * 28}deg`);
s.style.setProperty("--f", f.toFixed(3));
});
};
const leave = () => {
box.current
?.querySelectorAll<HTMLElement>(".gu-scatter__ch")
.forEach((s) => {
s.style.setProperty("--x", "0px");
s.style.setProperty("--y", "0px");
s.style.setProperty("--r", "0deg");
s.style.setProperty("--f", "0");
});
};
// Every letter gets its own fixed flight path for the burst, so it looks wild but repeats the same way.
const chars = text.split("");
const n = chars.length;
return (
<div className="gu-scatter" data-theme={theme}>
<h2
ref={box}
className="gu-scatter__word"
aria-label={text}
onPointerMove={move}
onPointerLeave={leave}
onPointerUp={(e) => e.pointerType !== "mouse" && leave()}
onClick={() => setBurst((b) => b + 1)}
data-burst={burst === 0 ? undefined : burst % 2}
>
{chars.map((ch, i) => {
const a = (i * 137.5 * Math.PI) / 180;
const dist = 160 + ((i * 53) % 120);
return (
<span
key={i}
className="gu-scatter__ch"
aria-hidden="true"
data-space={ch === " "}
style={
{
"--i": i,
"--mid": Math.abs(i - (n - 1) / 2),
"--bx": `${Math.cos(a) * dist}px`,
"--by": `${Math.sin(a) * dist * 0.7}px`,
"--br": `${((i * 97) % 360) - 180}deg`,
} as CSSProperties
}
>
<span>{ch === " " ? "\u00a0" : ch}</span>
</span>
);
})}
</h2>
<button type="button" onClick={() => setBurst((b) => b + 1)}>
Scatter the letters
</button>
</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: Letter scatter headline
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 giant headline where every letter is shy: letters near the cursor lean away, lift, turn and glow orange, then spring home. Clicking the word (or the button under it) blows all letters out on their own paths and gathers them back, middle letters first, with a small bounce.
## Layout and look
- Centred, max width 860px. Headline clamp(48px, 11vw, 128px), weight 900, letter-spacing −0.05em, line-height 0.95. Cream #f3eee3 on dark, ink #0e0e10 on light; hot colour #ff5a1f.
- Below it a 44px pill outline button "Scatter the letters" that fills orange on hover.
## Behavior and motion
1. Split the text into one span per character (spaces become non-breaking). Measure each letter's centre with offsetLeft/offsetTop (remeasure on resize).
2. On pointer move, for each letter: f = max(0, 1 − distance/140)^1.6. Push it away by f·46px along the line from the pointer, lift it by an extra f·18px, rotate by up to 28° depending on side, scale the inner span by 1 + f·0.18, and mix its colour toward orange by f. CSS transitions (700ms, spring cubic-bezier(0.34,1.56,0.64,1)) smooth it all.
3. On leave everything returns to zero.
4. Burst: each letter gets a fixed flight target (golden-angle direction, distance 160–280px, rotation from its index). Keyframe: fly out by 28% with a slight blur and orange, back to −6px at 70%, +3px at 84%, rest at 100%. Duration 1.5s, delay = distance from the middle × 30ms. Two identical keyframes alternate so each click replays.
## Accessibility
- The heading has aria-label with the full text; letter spans are aria-hidden so screen readers read a word, not letters.
- The button gives keyboard users the burst.
- prefers-reduced-motion: no movement or burst, only the warm colour under the pointer.
## Props
text, theme ("light" | "dark"), radius (reach of the cursor in px, default 140).
## 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.
### LetterScatter.tsx
```tsx
"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type PointerEvent,
} from "react";
import "./letter-scatter.css";
/**
* LetterScatter: a headline whose letters shy away from the cursor.
* - Letters near the pointer lean away, lift and turn, then spring home when it leaves.
* - Click (or press the button) to blow the whole word apart; it gathers again letter by letter.
*/
export function LetterScatter({
text = "Move gently",
theme = "dark",
radius = 140,
}: {
text?: string;
theme?: "light" | "dark";
radius?: number;
}) {
const box = useRef<HTMLHeadingElement>(null);
const centers = useRef<{ x: number; y: number }[]>([]);
const [burst, setBurst] = useState(0);
const measure = () => {
const spans = box.current?.querySelectorAll<HTMLElement>(".gu-scatter__ch");
if (!spans) return;
centers.current = [...spans].map((s) => ({
x: s.offsetLeft + s.offsetWidth / 2,
y: s.offsetTop + s.offsetHeight / 2,
}));
};
useEffect(() => {
measure();
const ro = new ResizeObserver(measure);
if (box.current) ro.observe(box.current);
return () => ro.disconnect();
}, [text]);
const move = (e: PointerEvent<HTMLHeadingElement>) => {
const el = box.current;
if (!el) return;
const b = el.getBoundingClientRect();
const k = el.offsetWidth / b.width || 1;
const px = (e.clientX - b.left) * k;
const py = (e.clientY - b.top) * k;
el.querySelectorAll<HTMLElement>(".gu-scatter__ch").forEach((s, i) => {
const c = centers.current[i];
if (!c) return;
const dx = c.x - px;
const dy = c.y - py;
const d = Math.hypot(dx, dy);
const f = Math.max(0, 1 - d / radius) ** 1.6;
s.style.setProperty("--x", `${(dx / (d || 1)) * f * 46}px`);
s.style.setProperty("--y", `${(dy / (d || 1)) * f * 46 - f * 18}px`);
s.style.setProperty("--r", `${(dx / (d || 1)) * f * 28}deg`);
s.style.setProperty("--f", f.toFixed(3));
});
};
const leave = () => {
box.current
?.querySelectorAll<HTMLElement>(".gu-scatter__ch")
.forEach((s) => {
s.style.setProperty("--x", "0px");
s.style.setProperty("--y", "0px");
s.style.setProperty("--r", "0deg");
s.style.setProperty("--f", "0");
});
};
// Every letter gets its own fixed flight path for the burst, so it looks wild but repeats the same way.
const chars = text.split("");
const n = chars.length;
return (
<div className="gu-scatter" data-theme={theme}>
<h2
ref={box}
className="gu-scatter__word"
aria-label={text}
onPointerMove={move}
onPointerLeave={leave}
onPointerUp={(e) => e.pointerType !== "mouse" && leave()}
onClick={() => setBurst((b) => b + 1)}
data-burst={burst === 0 ? undefined : burst % 2}
>
{chars.map((ch, i) => {
const a = (i * 137.5 * Math.PI) / 180;
const dist = 160 + ((i * 53) % 120);
return (
<span
key={i}
className="gu-scatter__ch"
aria-hidden="true"
data-space={ch === " "}
style={
{
"--i": i,
"--mid": Math.abs(i - (n - 1) / 2),
"--bx": `${Math.cos(a) * dist}px`,
"--by": `${Math.sin(a) * dist * 0.7}px`,
"--br": `${((i * 97) % 360) - 180}deg`,
} as CSSProperties
}
>
<span>{ch === " " ? "\u00a0" : ch}</span>
</span>
);
})}
</h2>
<button type="button" onClick={() => setBurst((b) => b + 1)}>
Scatter the letters
</button>
</div>
);
}
```
### letter-scatter.css
```css
.gu-scatter {
--spring: cubic-bezier(0.34, 1.56, 0.64, 1);
display: grid;
justify-items: center;
gap: 28px;
width: min(100%, 860px);
padding: 48px 20px;
color: #f3eee3;
font-family: ui-sans-serif, system-ui, sans-serif;
}
.gu-scatter[data-theme="light"] {
color: #0e0e10;
}
.gu-scatter__word {
position: relative;
margin: 0;
padding: 24px 10px;
font-size: clamp(48px, 11vw, 128px);
font-weight: 900;
letter-spacing: -0.05em;
line-height: 0.95;
text-align: center;
cursor: pointer;
user-select: none;
touch-action: none;
}
.gu-scatter__ch {
--x: 0px;
--y: 0px;
--r: 0deg;
--f: 0;
display: inline-block;
translate: var(--x) var(--y);
rotate: var(--r);
transition:
translate 0.7s var(--spring),
rotate 0.7s var(--spring),
color 0.4s;
will-change: translate;
}
/* The closer the cursor, the hotter the letter. */
.gu-scatter__ch > span {
display: inline-block;
color: color-mix(in oklab, currentColor, #ff5a1f calc(var(--f) * 100%));
scale: calc(1 + var(--f) * 0.18);
transition: scale 0.6s var(--spring);
}
/* The burst: every letter flies out on its own path and gathers back, the middle ones first. */
.gu-scatter__word[data-burst="0"] .gu-scatter__ch > span {
animation: gu-scatter-burst-a 1.5s calc(var(--mid) * 30ms)
cubic-bezier(0.2, 0.7, 0.2, 1) backwards;
}
.gu-scatter__word[data-burst="1"] .gu-scatter__ch > span {
animation: gu-scatter-burst-b 1.5s calc(var(--mid) * 30ms)
cubic-bezier(0.2, 0.7, 0.2, 1) backwards;
}
@keyframes gu-scatter-burst-a {
0% {
translate: 0 0;
rotate: 0deg;
}
28% {
translate: var(--bx) var(--by);
rotate: var(--br);
opacity: 0.85;
filter: blur(1px);
color: #ff5a1f;
}
70% {
translate: 0 -6px;
rotate: 0deg;
filter: blur(0);
}
84% {
translate: 0 3px;
}
}
@keyframes gu-scatter-burst-b {
0% {
translate: 0 0;
rotate: 0deg;
}
28% {
translate: var(--bx) var(--by);
rotate: var(--br);
opacity: 0.85;
filter: blur(1px);
color: #ff5a1f;
}
70% {
translate: 0 -6px;
rotate: 0deg;
filter: blur(0);
}
84% {
translate: 0 3px;
}
}
.gu-scatter button {
min-height: 44px;
padding: 0 20px;
border: 1.5px solid currentColor;
border-radius: 999px;
background: none;
color: inherit;
font-weight: 700;
font-size: 14px;
line-height: 1;
font-family: inherit;
cursor: pointer;
transition:
background-color 0.3s,
color 0.3s,
border-color 0.3s;
}
.gu-scatter button:hover {
background: #ff5a1f;
border-color: #ff5a1f;
color: #0e0e10;
}
.gu-scatter button:focus-visible {
outline: 3px solid #ff5a1f;
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.gu-scatter__ch {
translate: none;
rotate: none;
transition: none;
}
.gu-scatter__ch > span {
scale: 1;
animation: none !important;
transition: color 0.3s;
}
}
```
You might also want
Next piece: Liquid glass controlsCursor trail
Animations, intermediate
Dot ripple field
Animations, intermediate
Infinite marquee
Animations, beginner