Skip to content

Scramble Text

Scramble text effect: characters cycle through glyphs and lock in left to right. Hover re-scrambles, layout never shifts.

  • Text Effects
  • Cybersecurity
  • React only · 0 deps
  • 1 file
Full screen

Browser support · All evergreen browsers. Dim colour uses color-mix() with an opacity fallback.

Install. One command, or copy the file.

$npx shadcn@latest add https://freedesignmd.com/r/scramble-text.json
  • Byte-identical to the preview
  • No Tailwind or config needed
  • SSR-safe · keyboard accessible
  • Respects reduced motion & transparency
components/text/scramble-text.tsx
1/**
2 * ScrambleText, a decoding effect. Characters cycle through a glyph set and
3 * lock in left to right, each after a short, slightly random number of
4 * cycles. Hover (or focus) re-scrambles. Spaces and punctuation you choose
5 * stay put so the shape of the line reads from the first frame.
6 *
7 * Props
8 * - text: string Final text (also what screen readers get).
9 * - as?: keyof JSX intrinsic Element. Default "span".
10 * - glyphs?: string Scramble alphabet. Default A–Z 0–9 and symbols.
11 * - speed?: number Lock-in speed in characters per second. Default 28.
12 * - scramble?: number Cycles before the first char locks. Default 8.
13 * - trigger?: "view" | "mount" | "manual". Default "view".
14 * - hover?: boolean Re-scramble on hover / focus. Default true.
15 * - replay?: number | string Change to replay.
16 * - keep?: string Characters never scrambled. Default " .,:-/·".
17 * CSS vars: --fd-sc-dim (colour of unresolved glyphs, default 50% currentColor).
18 *
19 * Layout stays stable: every char cell is sized by its final glyph (a ghost
20 * layer holds the width), and scrambling glyphs are centred inside it.
21 * For best results use a monospace or tabular font.
22 * Accessibility: a visually hidden copy holds the final text; glyph cells are aria-hidden.
23 * Reduced motion shows the final text with no scrambling.
24 * Browser support: all evergreen browsers.
25 */
26import { useCallback, useEffect, useRef, useState } from "react";
27import type { CSSProperties, ElementType } from "react";
28
29export interface ScrambleTextProps {
30 text: string;
31 as?: ElementType;
32 glyphs?: string;
33 speed?: number;
34 scramble?: number;
35 trigger?: "view" | "mount" | "manual";
36 hover?: boolean;
37 replay?: number | string;
38 keep?: string;
39 className?: string;
40 style?: CSSProperties;
41}
42
43const DEFAULT_GLYPHS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#$%&*+=<>/\\[]{}";
44
45export function ScrambleText({
46 text,
47 as: Tag = "span",
48 glyphs = DEFAULT_GLYPHS,
49 speed = 28,
50 scramble = 8,
51 trigger = "view",
52 hover = true,
53 replay,
54 keep = " .,:-/·",
55 className,
56 style,
57}: ScrambleTextProps) {
58 const root = useRef<HTMLElement>(null);
59 const raf = useRef(0);
60 const running = useRef(false);
61
62 const run = useCallback(() => {
63 const el = root.current;
64 if (!el) return;
65 if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return;
66 const cells = Array.from(el.querySelectorAll<HTMLElement>(".fd-sc-live"));
67 const chars = Array.from(text);
68 const n = chars.length;
69 // each char locks at a time: left-to-right sweep + a bit of jitter
70 const per = 1000 / speed;
71 const lead = (scramble * 1000) / 30;
72 const lockAt = chars.map((_, i) => lead + i * per + Math.random() * per * 2.5);
73 const nextSwap = new Float32Array(n);
74 const locked = new Uint8Array(n);
75 const t0 = performance.now();
76 cancelAnimationFrame(raf.current);
77 running.current = true;
78 el.setAttribute("data-scrambling", "");
79 cells.forEach((c, i) => {
80 if (keep.includes(chars[i])) {
81 locked[i] = 1;
82 return;
83 }
84 c.textContent = glyphs[(Math.random() * glyphs.length) | 0];
85 c.setAttribute("data-s", "");
86 });
87 const tick = (t: number) => {
88 const e = t - t0;
89 let left = 0;
90 for (let i = 0; i < n; i++) {
91 if (locked[i]) continue;
92 const c = cells[i];
93 if (e >= lockAt[i]) {
94 locked[i] = 1;
95 c.textContent = chars[i];
96 c.removeAttribute("data-s");
97 continue;
98 }
99 left++;
100 // glyphs flicker at ~30 fps with per-cell phase, not every frame
101 if (e >= nextSwap[i]) {
102 c.textContent = glyphs[(Math.random() * glyphs.length) | 0];
103 nextSwap[i] = e + 26 + Math.random() * 40;
104 }
105 }
106 if (left > 0) raf.current = requestAnimationFrame(tick);
107 else {
108 running.current = false;
109 el.removeAttribute("data-scrambling");
110 }
111 };
112 raf.current = requestAnimationFrame(tick);
113 }, [text, glyphs, speed, scramble, keep]);
114
115 useEffect(() => {
116 const el = root.current;
117 if (!el) return;
118 if (trigger === "mount") run();
119 let io: IntersectionObserver | null = null;
120 if (trigger === "view") {
121 io = new IntersectionObserver(
122 (es) => {
123 if (es.some((x) => x.isIntersecting)) {
124 io?.disconnect();
125 run();
126 }
127 },
128 { threshold: 0.4 },
129 );
130 io.observe(el);
131 }
132 return () => {
133 io?.disconnect();
134 cancelAnimationFrame(raf.current);
135 };
136 }, [trigger, run]);
137
138 const first = useRef(true);
139 useEffect(() => {
140 if (first.current) {
141 first.current = false;
142 return;
143 }
144 run();
145 // eslint-disable-next-line react-hooks/exhaustive-deps
146 }, [replay]);
147
148 const onEnter = () => {
149 if (hover && !running.current) run();
150 };
151
152 return (
153 <Tag
154 ref={root}
155 className={`fd-sc${className ? " " + className : ""}`}
156 style={style}
157 onPointerEnter={onEnter}
158 onFocus={onEnter}
159 >
160 <style>{css}</style>
161 <span className="fd-sc-sr">{text}</span>
162 {Array.from(text).map((ch, i) => (
163 <span key={i} className="fd-sc-cell" aria-hidden="true">
164 <span className="fd-sc-ghost">{ch}</span>
165 <span className="fd-sc-live">{ch}</span>
166 </span>
167 ))}
168 </Tag>
169 );
170}
171
172const css = `
173.fd-sc,.fd-sc *{box-sizing:border-box}
174.fd-sc{--fd-sc-dim:color-mix(in srgb,currentColor 50%,transparent);margin:0;padding:0;font-variant-numeric:tabular-nums}
175.fd-sc-sr{position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);clip-path:inset(50%);white-space:nowrap;border:0}
176.fd-sc-cell{position:relative;display:inline-block;margin:0;padding:0;white-space:pre;vertical-align:baseline}
177.fd-sc-ghost{visibility:hidden;margin:0;padding:0}
178.fd-sc-live{position:absolute;left:50%;top:0;margin:0;padding:0;transform:translateX(-50%);white-space:pre}
179.fd-sc-live[data-s]{color:var(--fd-sc-dim)}
180@supports not (color:color-mix(in srgb,red,blue)){.fd-sc-live[data-s]{opacity:.5}}
181`;
182
183/* ───────────────────────────── demo ───────────────────────────── */
184
185export function Demo() {
186 const [n, setN] = useState(0);
187 return (
188 <div className="fd-scd">
189 <style>{demoCss}</style>
190 <p className="fd-scd-line">
191 <ScrambleText text="Decode the signal" trigger="mount" speed={16} scramble={10} replay={n} />
192 </p>
193 <button type="button" className="fd-scd-btn" onClick={() => setN((v) => v + 1)}>Replay</button>
194 </div>
195 );
196}
197
198const demoCss = `
199.fd-scd,.fd-scd *,.fd-scd *::before,.fd-scd *::after{box-sizing:border-box}
200.fd-scd{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:40px;min-height:100vh;width:100%;margin:0;padding:32px 20px;overflow:hidden;background:#f2f2f2;color:#1d1d1f;font:400 15px/1.4 ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif;letter-spacing:normal;-webkit-font-smoothing:antialiased}
201.fd-scd-line{margin:0;padding:0;max-width:100%;color:#1d1d1f;text-align:center;font:500 clamp(28px,7.2vw,72px)/1.1 ui-monospace,"SF Mono","JetBrains Mono",Menlo,monospace;letter-spacing:-.04em;--fd-sc-dim:rgba(29,29,31,.3)}
202.fd-scd-btn{appearance:none;-webkit-appearance:none;margin:0;height:34px;padding:0 16px;border:0;border-radius:9999px;background:rgba(0,0,0,.06);color:#3c3c43;font:500 13px/1 ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif;letter-spacing:normal;cursor:pointer;transition:background-color 200ms}
203.fd-scd-btn:hover{background:rgba(0,0,0,.1)}
204.fd-scd-btn:focus-visible{outline:2px solid rgba(10,132,255,.8);outline-offset:2px}
205`;

More Text Effects. Same file contract.

Split Text, text preview: Split text animation for React: split a string into characters, words or lines and reveal it with rise, wave,…

Split Text

Split text animation for React: split a string into characters, words or lines and reveal it with rise, wave, blur or typewriter.

Text Morph, text preview: Text morph animation: cycle through words with a letter-level morph where shared letters glide into place and…

Text Morph

Text morph animation: cycle through words with a letter-level morph where shared letters glide into place and the rest blur away.

Text Repel, text preview: Text repel effect: a headline whose letters drift away from or toward your cursor and spring home, each on…

Text Repel

Text repel effect: a headline whose letters drift away from or toward your cursor and spring home, each on its own spring.

Questions about Scramble Text

Run npx shadcn@latest add https://freedesignmd.com/r/scramble-text.json, or open the page and copy the file into your project. It has zero dependencies besides React. What you copy is the exact file that renders the live demo.

Scramble text effect: characters cycle through glyphs and lock in left to right. Hover re-scrambles, layout never shifts. It is a free, MIT-licensed React text effects component from the freedesignmd library.

Yes. It is free and MIT-licensed, use, modify and ship it in personal and commercial projects without attribution.

All evergreen browsers. Dim colour uses color-mix() with an opacity fallback.

Yes. It is a plain React component with its own styles, so it runs in Next.js, Vite, Remix and Lovable projects, and it needs no Tailwind setup. It has zero dependencies besides React.