# scramble

Headings and labels decode into place: each letter cycles through random glyphs before it settles.

When the text scrolls into view, its letters appear one after another as random glyphs that flicker a few times a second and then lock into the real letters, left to right like a terminal decoding a message. Other motions show every letter scrambled at once and settle them in a random order, or add a brief red and cyan split and jitter, like a glitching screen. When the last letter settles the element is the real text again: selectable, crisp and styled by your CSS. With the hover trigger a label stays as it is and scrambles again each time the pointer or keyboard focus reaches it.

Module `text`, category Text. Import `@dioramajs/text/scramble`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/text/src/scramble.ts)

An incident tool headline whose letters flicker through random glyphs and lock in from left to right.

```html title="index.html"
<div class="sc-hero">
  <p class="sc-kicker">Incident response, automated</p>
  <h2 class="sc-headline" data-dio="scramble">Ship the fix before the page goes out.</h2>
</div>
```

## Usage

Put it on: a heading, a label, a link or a button with text. Not img, video, canvas, svg or form fields.

`start()` registers `scramble`, so the attribute alone puts it on an element.

```html
<h1 data-dio="scramble">Ship the fix before the page.</h1>
```

In page transitions: Leaving a page, the settled letters scramble again in reverse order and go; entering, they decode in order.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `motion` (rebuilds) | `data-dio-motion` | `'decode' \| 'random' \| 'glitch'` | `'decode'` | decode settles letters left to right, each scrambling in turn, for most headings; random scrambles every letter at once and settles them in a random order, for numbers and codes; glitch decodes with a red and cyan split and a jitter, for a louder tech look. |
| `by` (rebuilds) | `data-dio-by` | `'letter' \| 'word'` | `'letter'` | letter gives each letter its own turn; word lets the letters of a word settle together. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'scroll' \| 'hover'` | `'view'` | view plays when the text enters; scroll ties it to the scroll position, both ways; hover plays it each time the pointer or focus reaches it. |
| `chars` (rebuilds) | `data-dio-chars` | `string` | `'mixed'` | Glyph set: mixed, latin, upper, digits, hex, binary, symbols, or your own string. |
| `duration` | `data-dio-duration` | `number` 0 to 3 s | `0.6` | Time one letter scrambles before it settles. |
| `stagger` | `data-dio-stagger` | `number` 0 to 0.3 s | `0.03` | Delay between neighboring letters or words in their turn order. |
| `delay` | `data-dio-delay` | `number` 0 to 2 s | `0` | Wait after the trigger, for a cascade. |
| `cycle` | `data-dio-cycle` | `number` 0.02 to 0.3 s | `0.06` | Time each random glyph shows before the next one. |
| `threshold` | `data-dio-threshold` | `number` 0 to 1 | `0.2` | Share of the element height in view, above the bottom tenth, before it plays. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Picks the random glyphs and order; the same seed plays the same way. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `motion`: `decode`, `random`, `glitch`
  - `decode` settles letters left to right, each scrambling in turn, for most headings.
  - `random` scrambles every letter at once and settles them in a random order, for numbers and codes.
  - `glitch` decodes with a red and cyan split and a jitter, for a louder tech look.
- `by`: `letter`, `word`
  - `letter` gives each letter its own turn.
  - `word` lets the letters of a word settle together.
- `trigger`: `view`, `scroll`, `hover`
  - `view` plays when the text enters.
  - `scroll` ties it to the scroll position, both ways.
  - `hover` plays it each time the pointer or focus reaches it.

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`presence`](/docs/api/inputs#presence), [`progress`](/docs/api/inputs#progress), [`size`](/docs/api/inputs#size), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. Only while letters scramble, and with the scroll trigger while scrolling; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: the text is shown as it is, and the engine does not load for it.

Runs on every device. The same as with a mouse, except that the hover trigger needs keyboard focus on touch.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { scramble: { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 9.2 KB the first time a page uses it. GPU: Two instanced draws of a quad per letter, one or three texture reads per pixel. Memory: A canvas of the text at twice its CSS size with mip levels, and a small sheet of the glyph set, kept while the element lives.

- low: The text and the glyphs are rastered at 1 device pixel per CSS pixel instead of 2.

See [the quality plugin](/docs/effects/quality).

## Recipes

### Navigation links that decode on hover

With `data-dio-trigger="hover"` a link stays as plain text and scrambles each time the pointer or keyboard focus reaches it. Keep `duration` and `stagger` short so a short label settles before the visitor clicks. Hover the links below.

Four navigation links of a studio site that scramble when hovered or focused.

```html title="index.html"
<ul class="sc-nav">
  <li><a href="/work" data-dio="scramble" data-dio-trigger="hover" data-dio-stagger="0.02" data-dio-duration="0.3">Work</a></li>
  <li><a href="/services" data-dio="scramble" data-dio-trigger="hover" data-dio-stagger="0.02" data-dio-duration="0.3">Services</a></li>
  <li><a href="/studio" data-dio="scramble" data-dio-trigger="hover" data-dio-stagger="0.02" data-dio-duration="0.3">Studio</a></li>
  <li><a href="/contact" data-dio="scramble" data-dio-trigger="hover" data-dio-stagger="0.02" data-dio-duration="0.3">Contact</a></li>
</ul>
```

### A number that settles in a random order

`motion="random"` shows every letter scrambled at once and locks them in a random order. With `chars="digits"` it suits a figure on a fintech or crypto page, such as a balance or a block height.

```html
<p
  class="stat"
  data-dio="scramble"
  data-dio-motion="random"
  data-dio-chars="digits"
  data-dio-duration="0.4"
>$4,812,330</p>
```

### A louder headline for a launch

`motion="glitch"` adds a red and cyan split and a small jitter while letters scramble, and lets the split fade out as each one settles. Moving by word with a longer stagger keeps it readable.

```html
<h2
  data-dio="scramble"
  data-dio-motion="glitch"
  data-dio-by="word"
  data-dio-stagger="0.12"
>Zero trust. Zero downtime.</h2>
```

## Notes

`chars` takes a named set, `mixed`, `latin`, `upper`, `digits`, `hex`, `binary` or `symbols`, or any string of your own, such as `chars="01"` or `chars="▖▗▘▙▚▛▜▝▞▟"`. The random glyphs use the font and color of the element, and each one is narrowed a little to fit the slot of the letter it stands for, so the line does not shift while it plays.

The element keeps its text as you wrote it, with no span per letter, so screen readers, search engines and find in page see the real words. While letters scramble a CSS filter hides the DOM text without removing it, and when the last one settles the element is real, selectable text again. Under reduced motion the effect is off and the text shows at once.

Text on screen when the page opens is hidden until the effect is ready and then plays, as with `text-reveal`. In the scroll and hover modes it stays as it is instead.

The canvas cannot draw `background-clip: text`, `text-shadow`, `-webkit-text-stroke`, underlines or vertical writing modes the way the browser does. An element with any of the first four keeps its text as it is, and a warning in development names the reason; an underline is hidden while the letters scramble.

Keep it to headings, labels and short lines: over 1500 letters it stays as plain text. `motion`, `by`, `trigger`, `chars` and `seed` rebuild the effect when they change.

## Related

- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [counter](/docs/effects/counter.md): Stats count up to their value on real 3D mechanics: odometer drums, slot machine reels or flip cards.
- [dust](/docs/effects/dust.md): A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.
- [emboss](/docs/effects/emboss.md): Letters pressed into or raised out of paper, leather, brushed metal, stone or plaster, lit by a light that follows the pointer.
- [extrude](/docs/effects/extrude.md): A headline becomes a solid 3D block: beveled letters with lit side walls, seen in perspective, that turn toward the pointer.
- [fire](/docs/effects/fire.md): The letters burn: flames rise from their tops, sparks drift up in depth and the firelight flickers on the page.
- [hologram](/docs/effects/hologram.md): A heading becomes a 3D hologram: projected from a glowing emitter, built of slices in depth, crossed by scan lines.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
