Text Scramble Free React component
A headline that decrypts itself: every letter flickers through random symbols before it locks into place. It plays when it scrolls into view, on hover or on a loop, and screen readers always get the real text.
Get the code
npx shadcn@latest add https://alphenex.com/r/text-scramble.jsonWorks with any React project through the shadcn CLI. Prefer to paste it yourself? Copy the two files below into components/alphenex/. No Tailwind or animation library needed.
import TextScramble from '@/components/alphenex/text-scramble';
<TextScramble
as="h1"
text="Build something unforgettable"
trigger="loop"
/>'use client';
import { useCallback, useEffect, useRef } from 'react';
import './text-scramble.css';
const DEFAULT_GLYPHS = '!<>-_\\/[]{}=+*^?#%&@$01';
/**
* TextScramble
* Text that "decrypts" itself: each letter flickers through random symbols
* before it locks into place, left to right with a little randomness.
* Screen readers and search engines always get the real text.
*
* Free from Alphenex Lab: https://alphenex.com/lab/text-scramble (MIT)
*/
export default function TextScramble({
text = 'Build something unforgettable',
as: Tag = 'span',
duration = 1400, // ms for the whole reveal
glyphs = DEFAULT_GLYPHS,
trigger = 'view', // 'view' | 'hover' | 'loop'
loopDelay = 2200, // ms between runs when trigger is 'loop'
scrambleColor = 'currentColor',
className = '',
style,
...props
}) {
const rootRef = useRef(null);
const outRef = useRef(null);
const frameRef = useRef(0);
const run = useCallback(() => {
const out = outRef.current;
if (!out) return Promise.resolve();
cancelAnimationFrame(frameRef.current);
const chars = [...text];
// Each letter gets its own window: it scrambles from `start` and locks at `end`.
const plan = chars.map((char, i) => {
const start = (i / chars.length) * duration * 0.45 + Math.random() * duration * 0.15;
return { char, start, end: start + duration * 0.25 + Math.random() * duration * 0.3, glyph: '' };
});
const pick = () => glyphs[Math.floor(Math.random() * glyphs.length)];
const begin = performance.now();
return new Promise((resolve) => {
const draw = (now) => {
const t = now - begin;
const frag = document.createDocumentFragment();
let done = 0;
for (const item of plan) {
if (t >= item.end || item.char === ' ') {
frag.append(item.char);
done += 1;
} else {
if (!item.glyph || Math.random() < 0.28) item.glyph = pick();
const span = document.createElement('span');
span.className = t >= item.start ? 'alx-scramble__glyph' : 'alx-scramble__glyph is-waiting';
span.textContent = item.glyph;
frag.append(span);
}
}
out.replaceChildren(frag);
if (done < plan.length) frameRef.current = requestAnimationFrame(draw);
else resolve();
};
frameRef.current = requestAnimationFrame(draw);
});
}, [text, duration, glyphs]);
useEffect(() => {
const root = rootRef.current;
const out = outRef.current;
if (!root || !out) return undefined;
out.textContent = text;
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return undefined;
let alive = true;
let timer = 0;
const loop = async () => {
await run();
if (alive) timer = window.setTimeout(loop, loopDelay);
};
if (trigger === 'hover') {
const onEnter = () => run();
root.addEventListener('pointerenter', onEnter);
root.addEventListener('focus', onEnter);
return () => {
root.removeEventListener('pointerenter', onEnter);
root.removeEventListener('focus', onEnter);
cancelAnimationFrame(frameRef.current);
};
}
const observer = new IntersectionObserver(([entry]) => {
if (!entry.isIntersecting) return;
observer.disconnect();
if (trigger === 'loop') loop();
else run();
}, { threshold: 0.4 });
observer.observe(root);
return () => {
alive = false;
observer.disconnect();
window.clearTimeout(timer);
cancelAnimationFrame(frameRef.current);
};
}, [text, trigger, loopDelay, run]);
return (
<Tag
ref={rootRef}
className={`alx-scramble ${className}`}
aria-label={text}
tabIndex={trigger === 'hover' ? 0 : undefined}
style={{ '--alx-scramble-color': scrambleColor, ...style }}
{...props}
>
<span ref={outRef} aria-hidden="true">
{text}
</span>
</Tag>
);
}
/* TextScramble, Alphenex Lab (MIT) */
.alx-scramble {
display: inline-block;
white-space: pre-wrap;
font-variant-ligatures: none;
}
.alx-scramble__glyph {
color: var(--alx-scramble-color, currentColor);
opacity: 0.85;
}
/* Letters that haven't started yet stay faint, so the line keeps its shape. */
.alx-scramble__glyph.is-waiting {
opacity: 0.22;
}
Paste this into Claude, ChatGPT, Cursor, Lovable or v0 to build it from scratch.
Build a React "TextScramble" component with no libraries. It renders the given text, then plays a decrypt effect: each character gets its own time window spread left to right with some randomness; inside its window it shows random symbols from "!<>-_\/[]{}=+*^?#%&@$01" (changing about every fourth frame), then locks to the real character. Characters that have not started yet show a faint symbol so the line keeps its shape. Spaces never scramble. Draw with requestAnimationFrame by replacing the children of an inner span, not with React state. Props: text, as, duration (ms), glyphs, trigger ("view" uses IntersectionObserver once, "hover", "loop" with loopDelay), scrambleColor. Put the real text in aria-label and hide the animated span from screen readers. Skip the animation for prefers-reduced-motion.How the text scramble works
Every character gets a start and an end time, spread from left to right with a little randomness so the reveal ripples instead of marching. Between the two times the character shows a random symbol that changes every few frames. After its end time it shows the real letter.
Drawing happens on animation frames by swapping the children of one span, not through React state, so a long headline costs a single DOM update per frame and React re-renders nothing.
The real text sits in aria-label and in the server-rendered HTML, so search engines and screen readers read the final words, not the symbols. With reduced motion turned on, the text simply appears.
Frequently asked questions
Want motion like this on your website?
We design and build custom websites with motion, 3D and SEO built in. Websites from $500.