Text scramble
Headlines decode themselves from random glyphs into words, letter by letter, when they scroll into view or get hovered.
"use client";
import { useEffect, useRef, type ElementType } from "react";
import "./scramble-text.css";
const GLYPHS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#%&*+=<>/";
type ScrambleTextProps = {
text: string;
as?: ElementType;
/** Milliseconds each letter keeps scrambling before it locks in. */
speed?: number;
className?: string;
};
/**
* ScrambleText
* Letters start as random glyphs and lock into place from left to right.
* Runs when the element enters the viewport and again on hover / tap.
* Screen readers always get the real text (a hidden copy), never the noise.
*/
export function ScrambleText({
text,
as: Tag = "span",
speed = 32,
className = "",
}: ScrambleTextProps) {
const ref = useRef<HTMLElement>(null);
const frame = useRef(0);
useEffect(() => {
const el = ref.current;
if (!el) return;
const reduce = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const play = () => {
if (reduce) return;
cancelAnimationFrame(frame.current);
const start = performance.now();
const tick = (now: number) => {
const locked = Math.floor((now - start) / speed); // how many letters are final
let out = "";
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (ch === " " || i < locked) out += ch;
else out += GLYPHS[Math.floor(Math.random() * GLYPHS.length)];
}
el.textContent = out;
if (locked < text.length) frame.current = requestAnimationFrame(tick);
else el.textContent = text;
};
frame.current = requestAnimationFrame(tick);
};
// Play once when it scrolls into view.
const io = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
play();
io.disconnect();
}
},
{ threshold: 0.6 },
);
io.observe(el);
el.addEventListener("pointerenter", play);
return () => {
io.disconnect();
el.removeEventListener("pointerenter", play);
cancelAnimationFrame(frame.current);
};
}, [text, speed]);
return (
<Tag className={`gu-scramble ${className}`}>
<span className="gu-scramble__sr">{text}</span>
<span ref={ref} aria-hidden="true">
{text}
</span>
</Tag>
);
}
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: Text scramble
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 text effect where each letter starts as a random glyph and locks into the real letter from left to right, like a code being decoded. Use it on headlines, labels or buttons.
## Behavior and motion
- Glyph set: A–Z, 0–9 and # % & * + = < > /.
- On play: every animation frame, compute locked = floor(elapsed / speed) (speed default 32ms per letter). Letters before `locked` and spaces show their real character; the rest show a random glyph. Stop when all letters are locked.
- Plays once when the element is 60% visible (IntersectionObserver), and again on every pointerenter (mouse hover or tap on touch screens).
- Cancel any running animation before restarting.
## Look
- Inherits font and color from the parent. Works best with a monospace or tabular font; set font-variant-numeric: tabular-nums and white-space: pre-wrap.
- Demo styling: monospace, weight 800, clamp(1.6rem, 5vw, 3rem), color #ff5a1f, subtitle at 70% opacity.
## Accessibility
- A visually hidden copy of the real text is read by screen readers; the animated span is aria-hidden, so screen readers never hear random characters.
- prefers-reduced-motion: never scramble; show the final text.
## Props
text (string), as (element/tag, default span), speed (ms per letter), className.
## 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.
### ScrambleText.tsx
```tsx
"use client";
import { useEffect, useRef, type ElementType } from "react";
import "./scramble-text.css";
const GLYPHS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#%&*+=<>/";
type ScrambleTextProps = {
text: string;
as?: ElementType;
/** Milliseconds each letter keeps scrambling before it locks in. */
speed?: number;
className?: string;
};
/**
* ScrambleText
* Letters start as random glyphs and lock into place from left to right.
* Runs when the element enters the viewport and again on hover / tap.
* Screen readers always get the real text (a hidden copy), never the noise.
*/
export function ScrambleText({
text,
as: Tag = "span",
speed = 32,
className = "",
}: ScrambleTextProps) {
const ref = useRef<HTMLElement>(null);
const frame = useRef(0);
useEffect(() => {
const el = ref.current;
if (!el) return;
const reduce = window.matchMedia(
"(prefers-reduced-motion: reduce)",
).matches;
const play = () => {
if (reduce) return;
cancelAnimationFrame(frame.current);
const start = performance.now();
const tick = (now: number) => {
const locked = Math.floor((now - start) / speed); // how many letters are final
let out = "";
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (ch === " " || i < locked) out += ch;
else out += GLYPHS[Math.floor(Math.random() * GLYPHS.length)];
}
el.textContent = out;
if (locked < text.length) frame.current = requestAnimationFrame(tick);
else el.textContent = text;
};
frame.current = requestAnimationFrame(tick);
};
// Play once when it scrolls into view.
const io = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
play();
io.disconnect();
}
},
{ threshold: 0.6 },
);
io.observe(el);
el.addEventListener("pointerenter", play);
return () => {
io.disconnect();
el.removeEventListener("pointerenter", play);
cancelAnimationFrame(frame.current);
};
}, [text, speed]);
return (
<Tag className={`gu-scramble ${className}`}>
<span className="gu-scramble__sr">{text}</span>
<span ref={ref} aria-hidden="true">
{text}
</span>
</Tag>
);
}
```
### scramble-text.css
```css
.gu-scramble {
display: inline-block;
/* Same-width digits and letters stop the line from jumping while scrambling. */
font-variant-numeric: tabular-nums;
white-space: pre-wrap;
cursor: default;
}
/* Real text for screen readers, visually hidden. */
.gu-scramble__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
white-space: nowrap;
}
```
You might also want
Next piece: Odometer counterMarker highlight
Animations, beginner
Split text reveal
Animations, intermediate, needs motion
Infinite marquee
Animations, beginner