Scramble Text

Letters near the pointer flicker into coloured symbols of similar width, then glide back into place.

Preview

Setup

Install with the shadcn CLI

$npx shadcn@latest add https://kinetikui.singhgourav.site/r/scramble-text.json

One command adds every file, the npm packages and any Kinetik components it builds on. Needs a components.json; the CLI offers to create one if your project has none.

Or install manually

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge
  2. 2

    Copy lib/utils.ts from the Code tab. Skip if you already have cn().

  3. 3

    Create a components/ui/scramble-text/ folder and copy every file from the Code tab into it. Each file does one job, so you can read or change a piece without the rest.

  4. 4

    Import and render:

    Example
    import { ScrambleText } from "@/components/ui/scramble-text"
    
    <ScrambleText as="h1" boot>
      Build beautiful interfaces.
    </ScrambleText>

No dependencies: it is plain React, DOM and a canvas for measuring.

Usage

Pass a string as children. Hovering scrambles the letters around the pointer.

Example
import { ScrambleText } from "@/components/ui/scramble-text"

export function Hero() {
  return (
    <ScrambleText as="h1" boot className="text-6xl font-semibold">
      Build beautiful interfaces.
    </ScrambleText>
  )
}

Variants

variant picks what letters turn into:

  • symbols: coloured symbols picked by width; neighbours glide out of the way
  • emoji: emoji shrunk to fit each letter's box, so nothing around them moves
  • dither: the real letter breaks up into a pixel dither pattern (a CSS mask) and fills back in to solid. The letter never changes, so nothing reflows
Example
<ScrambleText variant="emoji">Hover me</ScrambleText>
<ScrambleText variant="dither" boot>Loading the future</ScrambleText>

Boot reveal

Set boot to reveal the text on mount. Every letter shows up in reading order as a flickering symbol, then settles on its real letter. The reveal starts fast and slows down, and it always takes duration no matter how long the text is.

Example
<ScrambleText as="h1" boot duration={1000}>Hello there.</ScrambleText>

Line breaks

Each word is locked to the width of its real letters, so a scrambled symbol never pushes a word onto another line and text-balance keeps the same layout. To fix the lines yourself, put a newline in the text. It renders as a <br>.

Example
<ScrambleText as="h1" className="whitespace-nowrap">
  {"Most interfaces feel like\nnobody made them."}
</ScrambleText>

How it works

  • Each letter is its own box, rendered by React, so the server and client markup match
  • Separate boxes lose the font's kerning, so each pair is measured on a canvas and the difference is put back as margin-right in em
  • A letter picks from the 5 symbols closest to its own width, and picks any symbol 1 time in 5
  • When a box changes width, the boxes after it are pushed back to where they were, then glide home (FLIP)
  • Each change waits a random 0–70 ms (0–150 ms when settling), so letters never change on the same frame
  • Only the markup and measuring load with the page. The animation engine is a separate chunk, loaded on mount for boot and otherwise when the browser is idle. With reduced motion it is never loaded

Files

  • index.tsx: the component; renders the letter boxes and lazy-loads the engine
  • engine.ts: scramble, release, boot reveal and pointer zones
  • glide.ts: the FLIP glide loop
  • measure.ts: kerning and width locks, measured on a canvas
  • variants.ts: the symbol, emoji and dither sets
  • dither.ts: the Bayer dither masks
  • types.ts: shared types

Props

PropTypeDefaultNotes
childrenstring—The text. A newline is a fixed line break.
asElementType"span"Element to render, e.g. h1 or p.
variant"symbols" | "emoji" | "dither""symbols"What letters turn into.
bootbooleanfalseReveal the text with a scramble on mount.
durationnumber1000Length of the boot reveal in ms.
colorsstring[]Palette / text colourSymbol colours. Symbols default to a palette, dither to the text colour. Emoji ignore it.
classNamestring—Applied to the root element.

Accessibility

  • Screen readers get one clean copy of the text; the letter boxes are hidden from them
  • Copying the text selects it once, not letter by letter
  • With reduced motion turned on, the text shows straight away and never scrambles
  • Touch input does not scramble, so tapping a link stays readable