# text-reveal

Headings rise, slide up, fade, pop or type into place letter by letter, word by word or line by line.

When the text scrolls into view, each letter, word or line rises from a little below its baseline, standing up from a backward tilt and coming into focus as it fades in, one after another in reading order; other motions slide each one up out of its line, fade it in, pop it in or type it. When the last one lands the element is the real text again: selectable, crisp and styled by your CSS. Text on screen when the page opens is hidden until the effect is ready and then plays the same way; if that takes over 3 seconds it shows as it is. With the scroll trigger such text stays as it is until scrolling reaches it.

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

A roastery headline that rises into place letter by letter, tipped back and blurred at first.

```html title="index.html"
<div class="tr-hero">
  <p class="tr-kicker">Northside Roasters</p>
  <h2 class="tr-headline" data-dio="text-reveal">Roasted on Tuesday, at your door by Friday.</h2>
</div>
```

## Usage

Put it on: any element with text: a heading, a paragraph, a link. Not img, video, canvas, svg or form fields. Any font-stretch, font-variation-settings and font-feature-settings draw as on the page when the font comes from a readable @font-face (same origin, or a stylesheet served with CORS such as Google Fonts); otherwise such text stays as it is, as does text with text-shadow, a text stroke, background-clip: text or vertical writing.

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

```html
<h1 data-dio="text-reveal">Silence, shaped.</h1>
```

In page transitions: Leaving a page, the units go again in reverse order; entering, they rise in order. A flight to a text element lands on the DOM text.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `by` (rebuilds) | `data-dio-by` | `'letter' \| 'word' \| 'line'` | `'letter'` | Unit that moves as one; over 1500 letters it moves by words, over 1500 words by lines. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'scroll'` | `'view'` | view plays when the text enters; scroll ties it to the scroll position, both ways. |
| `motion` (rebuilds) | `data-dio-motion` | `'rise' \| 'mask' \| 'fade' \| 'pop' \| 'type'` | `'rise'` | rise lifts each unit from below its baseline, tipped back and blurred, for most headings; mask slides it up out of its own line as if from behind an edge, the editorial look; fade fades it in place, coming into focus by blur, for body text; pop grows it from its middle with a slight overshoot, for a playful brand; type shows it at once when its turn comes, like a typewriter (set stagger to the time per unit). |
| `duration` | `data-dio-duration` | `number` 0.2 to 3 s | `0.9` | Time one unit takes to land; type ignores it. |
| `stagger` | `data-dio-stagger` | `number` 0 to 0.3 s | `0.03` | Delay between neighboring units in reading order. |
| `delay` | `data-dio-delay` | `number` 0 to 2 s | `0` | Wait after the trigger, for a cascade. |
| `rise` | `data-dio-rise` | `number` 0 to 2 em | `0.6` | How far below its place a unit starts, with rise. |
| `tilt` | `data-dio-tilt` | `number` 0 to 90° | `35` | How far a unit starts tipped back around its baseline, with rise. |
| `blur` | `data-dio-blur` | `number` 0 to 1 | `0.5` | How blurred a unit starts with rise and fade; 0 for sharp. At most low 0. |
| `threshold` | `data-dio-threshold` | `number` 0 to 1 | `0.2` | Share of the element height that must be in view, above the bottom tenth of the viewport, before it plays. |

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

### Variants

- `motion`: `rise`, `mask`, `fade`, `pop`, `type`
  - `rise` lifts each unit from below its baseline, tipped back and blurred, for most headings.
  - `mask` slides it up out of its own line as if from behind an edge, the editorial look.
  - `fade` fades it in place, coming into focus by blur, for body text.
  - `pop` grows it from its middle with a slight overshoot, for a playful brand.
  - `type` shows it at once when its turn comes, like a typewriter (set stagger to the time per unit).
- `by`: `letter`, `word`, `line`
- `trigger`: `view`, `scroll`
  - `view` plays when the text enters.
  - `scroll` ties it to the scroll position, both ways.

## 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 units move, 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: it follows the scroll.

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

start({ effects: { 'text-reveal': { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 7.6 KB the first time a page uses it. GPU: One instanced draw of a quad per unit, one texture read per pixel. Memory: A canvas of the text at twice its CSS size with mip levels, kept while the element lives: about 2.5 MB for a 1200 x 200 heading.

- low: No blur, and the text is rastered at 1 device pixel per CSS pixel instead of 2.

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

## Recipes

### A hero headline and its subline

Let the headline’s words slide up out of a mask, then fade the subline in line by line. A `delay` on the subline makes the two read as one entrance instead of two animations that start together.

A finance app headline whose words slide up out of a mask, then a subline that fades in line by line.

```html title="index.html"
<header class="tr-launch">
  <h2 class="tr-launch-title" data-dio="text-reveal" data-dio-by="word" data-dio-motion="mask" data-dio-stagger="0.08">Close the books in a day, not a week.</h2>
  <p class="tr-launch-sub" data-dio="text-reveal" data-dio-by="line" data-dio-motion="fade" data-dio-delay="0.5" data-dio-stagger="0.15">Ledgerline matches every bank line to its invoice as it arrives, so month-end is a review instead of a hunt.</p>
</header>
```

```html title="panel.html"
<button type="button" data-replay>Replay</button>
<p>The subline starts 0.5 s after the headline.</p>
```

```ts title="script.ts"
import { onScreen, running } from '../../replay.ts'
import { play, settle } from '../_play.ts'

// Plays the headline and its subline again on Replay, and once if they were on screen at load.
export default function mount(example: HTMLElement): () => void {
  const texts = [...example.querySelectorAll('.ex-root [data-dio]')]
  const button =
    example.querySelector<HTMLButtonElement>('[data-replay]')
  const again = () => {
    for (const text of texts) play(text)
  }
  button?.addEventListener('click', again)
  const first = texts[0]
  if (first && onScreen(first))
    void Promise.all(texts.map((t) => running(t, 'text-reveal'))).then(
      (ok) => ok.every(Boolean) && again(),
    )
  return () => {
    button?.removeEventListener('click', again)
    for (const text of texts) settle(text)
  }
}
```

### Section headings that follow the scroll

With `data-dio-trigger="scroll"` a heading plays as it rises from the bottom of the viewport to the middle, and plays backward when you scroll up. Use it for the headings between the sections of a long landing page, where each one marks a step of the story. Scroll past this example to see it.

A section heading whose words come in as it rises up the page and go back as it sinks.

```html title="index.html"
<section class="tr-steps">
  <h2 class="tr-steps-title" data-dio="text-reveal" data-dio-by="word" data-dio-trigger="scroll" data-dio-stagger="0.2">How a bag gets to you</h2>
  <ol>
    <li>We roast on Tuesday morning.</li>
    <li>It rests for a day and ships on Wednesday.</li>
    <li>It reaches you by Friday.</li>
  </ol>
</section>
```

```html title="panel.html"
<p>Scroll the page: the words come in as the heading rises to the middle of the viewport and go back as it sinks.</p>
```

A longer `stagger`, 0.2 s here, spreads the words along the scroll; with the default they arrive almost together. `threshold` sets how much of the heading must be in view before it starts. A heading that is on screen when the page opens stays as it is until scrolling would have played it.

### An editorial masthead

Lines that slide up out of their own band read like type being set. Break the lines yourself with `<br>` so each one is a phrase, and give them a longer stagger than letters need.

```html
<h1
  class="masthead"
  data-dio="text-reveal"
  data-dio-by="line"
  data-dio-motion="mask"
  data-dio-stagger="0.12"
>
  The slow issue:<br />six makers who refuse to hurry
</h1>
```

### A command that types itself

`motion="type"` shows each letter at once when its turn comes, so `stagger` alone sets the pace. On a developer site it makes the install line the first thing the reader watches happen.

An install command typed one letter at a time.

```html title="index.html"
<div class="tr-term">
  <p class="tr-term-label">Get started</p>
  <p class="tr-term-line"><span aria-hidden="true">$</span> <code data-dio="text-reveal" data-dio-motion="type" data-dio-stagger="0.05">npm install @dioramajs/diorama</code></p>
</div>
```

```html title="panel.html"
<button type="button" data-replay>Replay</button>
<p>One letter every 0.05 s.</p>
```

```ts title="script.ts"
import { onScreen, running } from '../../replay.ts'
import { play, settle } from '../_play.ts'

// Types the command again on Replay, and once if it was on screen when the page opened.
export default function mount(example: HTMLElement): () => void {
  const command = example.querySelector('.ex-root [data-dio]')
  const button =
    example.querySelector<HTMLButtonElement>('[data-replay]')
  if (!command) return () => {}
  const again = () => play(command)
  button?.addEventListener('click', again)
  if (onScreen(command))
    void running(command, 'text-reveal').then((ok) => ok && again())
  return () => {
    button?.removeEventListener('click', again)
    settle(command)
  }
}
```

## Notes

The element keeps its text as you wrote it, with no span per letter. Screen readers read words rather than single letters, search engines index the heading, and find in page works. While units move, a CSS filter hides the DOM text without removing it, so assistive technology still reads it, and once the last unit lands the heading 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. If the engine takes over 3 seconds to get there, the text shows as it is instead of waiting longer.

The canvas cannot draw some text the way the browser does: `background-clip: text`, `text-shadow`, `-webkit-text-stroke` and vertical writing modes. An element with any of them keeps its text as it is, and a warning in development names the reason.

Keep it to headings, short paragraphs and labels. Over 1500 letters it moves by words, over 1500 words by lines, and each element keeps a raster of its text for as long as it lives.

`by`, `trigger` and `motion` rebuild the effect when they change. Set them in the markup rather than switching them while the text is on screen.

## 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)
