# dust

A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.

The text breaks into grains made of its own pixels, so the first grain to come loose looks exactly like the ink it leaves. A ragged edge sweeps across the heading from the side the wind blows to; behind it the grains lift off the page towards the viewer, tumble and catch a soft light from the upper left, and stream away along swirling paths, growing and blurring as they come closer and casting faint shadows on the page below. Other motions let the grains rise like ash or blow the heading apart into tiles that fly out of the page in every direction. Scrolling back gathers the sand into the heading again, and at rest the heading is plain DOM text.

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

A desert night walk headline that crumbles into lit sand, lifts off the page towards the viewer and drifts away.

```html title="index.html"
<div class="du-hero">
  <p class="du-kicker">Wadi Rum · Night walks</p>
  <h2 class="du-headline" data-dio="dust">Where the desert ends</h2>
  <p class="du-lede">Four hours on foot from the last road, with a Bedouin guide, mint tea in the dunes and the sky to yourself. Nightly from October to April.</p>
</div>
```

## Usage

Put it on: a large heading, a short line of display text or a dismissible message. Not img, video, canvas, svg or form fields. Text with text-shadow, a text stroke, background-clip: text or vertical writing stays as it is.

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

```html
<h1 data-dio="dust">The end of summer</h1>
```

- `data-dio-dust`: Set by the click trigger on its element: playing while the sand flies, then done for good.

In page transitions: Leaving a page, the heading blows away as sand; entering, the sand gathers back into the text.

- Event `dio:dust`, detail `null`: The click trigger has blown the text away; the element keeps its space, so remove or collapse it here.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `motion` (rebuilds) | `data-dio-motion` | `'wind' \| 'rise' \| 'explode'` | `'wind'` | wind blows the grains along angle in swirling streams, for most headings; rise lets them float up like ash; explode bursts the text into tiles that fly out of the page, front and back. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'scroll' \| 'click' \| 'view'` | `'scroll'` | scroll ties it to the heading leaving the top of the viewport, both ways; click plays it on a click or Enter, then leaves the element empty; view plays it once the heading scrolls out and gathers it back on return. |
| `density` (rebuilds) | `data-dio-density` | `number` 0.3 to 3 | `1` | Grains per area of the glyphs; 2 doubles them, up to the tier limit. At most low 0.6. |
| `dot` | `data-dio-dot` | `number` 0.6 to 6 px | `1.6` | Size of a grain in flight on the page plane; closer grains look larger. |
| `duration` | `data-dio-duration` | `number` 0.5 to 6 s | `2.2` | Time the click and view triggers take to blow the whole text away. |
| `angle` | `data-dio-angle` | `number` -180 to 180° | `15` | Where the wind blows, counterclockwise from the right; 90 blows straight up. |
| `turbulence` | `data-dio-turbulence` | `number` 0 to 3 | `1` | Strength of the swirls the grains ride; 0 lets them fly straight. |
| `depth` | `data-dio-depth` | `number` 0 to 2 | `1` | How far the grains travel towards the viewer and back; 0 keeps them flat. |
| `shadow` | `data-dio-shadow` | `number` 0 to 1 | `0.5` | Darkness of the shadows lifted grains cast on the page; 0 turns them off. At most low 0. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Picks the ragged edge, paths and sizes; the same seed blows the same way. |

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

### Variants

- `motion`: `wind`, `rise`, `explode`
  - `wind` blows the grains along angle in swirling streams, for most headings.
  - `rise` lets them float up like ash.
  - `explode` bursts the text into tiles that fly out of the page, front and back.
- `trigger`: `scroll`, `click`, `view`
  - `scroll` ties it to the heading leaving the top of the viewport, both ways.
  - `click` plays it on a click or Enter, then leaves the element empty.
  - `view` plays it once the heading scrolls out and gathers it back on return.

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`presence`](/docs/api/inputs#presence), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. While scrolling through the sweep with the scroll trigger, and while the sand flies with the click and view triggers; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The scroll and view triggers show the text as it is; the click trigger fades the text out in 0.4 s instead.

Runs on every device. The same as with a mouse; a tap plays the click trigger.

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

start({ effects: { dust: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 11.4 KB the first time a page uses it. GPU: Two instanced draws of up to 20,000 small quads, each tracing its path through the flow in five steps, and one instanced draw of a quad per line for the text that is left. Memory: A canvas of the text at twice its CSS size, a small map of when each grain comes loose, and 48 bytes per grain: about 1 MB at the high tier.

- low: A quarter of the grains, density at most 0.6, no shadows, and the text rastered at 1 device pixel per CSS pixel instead of 2.
- medium: About half of the grains.

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

## Recipes

### A notice the visitor dismisses

With `data-dio-trigger="click"` the text blows away once on a click, a tap or Enter, and the element is left empty, with `data-dio-dust="done"`. It still takes its space, so remove or collapse it on the `dio:dust` event. Put the close button inside the element, so the click on it plays the effect too. The dust carries only the text: fade the box around it with CSS while `data-dio-dust` is set. Click the notice below.

A saved draft notice that rises away as ash when it is clicked, then leaves the page.

```html title="index.html"
<div class="du-app">
  <p class="du-doc">Quarterly report · Draft 4</p>
  <div class="du-slot">
    <div class="du-toast">
      <div class="du-body" data-dio="dust" data-dio-trigger="click" data-dio-motion="rise">
        <p><span aria-hidden="true">✓</span> Draft saved. 3 changes synced to your team.</p>
        <button type="button" aria-label="Dismiss">×</button>
      </div>
    </div>
  </div>
  <button type="button" class="du-again" hidden>Show the notice again</button>
</div>
```

```ts title="script.ts"
// When the notice has blown away the page removes it; a button brings a fresh one back.
export default function mount(example: HTMLElement): () => void {
  const slot = example.querySelector('.du-slot')
  const again = example.querySelector<HTMLButtonElement>('.du-again')
  const fresh = slot?.querySelector('.du-toast')?.cloneNode(true) as
    | Element
    | undefined
  if (!slot || !again || !fresh) return () => {}
  const gone = (e: Event) => {
    ;(e.target as Element).closest('.du-toast')?.remove()
    again.hidden = false
    again.focus()
  }
  const restore = () => {
    const toast = fresh.cloneNode(true) as Element
    for (const el of toast.querySelectorAll(
      '[data-dio-state], [data-dio-dust]',
    )) {
      el.removeAttribute('data-dio-state')
      el.removeAttribute('data-dio-dust')
    }
    slot.append(toast)
    again.hidden = true
  }
  slot.addEventListener('dio:dust', gone)
  again.addEventListener('click', restore)
  return () => {
    slot.removeEventListener('dio:dust', gone)
    again.removeEventListener('click', restore)
  }
}
```

```css
.toast:has([data-dio-dust]) {
  background-color: transparent;
  box-shadow: none;
  transition: background-color 0.6s 0.3s, box-shadow 0.6s 0.3s;
}
```

### A heading that shatters once it scrolls out

With `data-dio-trigger="view"` the heading plays on its own clock once it has moved into the top of the window, whatever the scroll speed, and gathers back when it returns. `explode` breaks it into tiles that tumble out of the page, towards the viewer and behind it.

```html
<h2 data-dio="dust" data-dio-trigger="view" data-dio-motion="explode" data-dio-duration="1.6">Sold out</h2>
```

### Ash rising off a closing chapter

`rise` lets the grains float straight up like ash, and an `angle` of 90 starts the edge at the top of the text, so the grains never cross the lines still standing. Without shadows the ash reads lighter on a dark page.

```html
<h2 data-dio="dust" data-dio-motion="rise" data-dio-angle="90" data-dio-shadow="0">The last summer</h2>
```

## Notes

The grains are pieces of the heading’s own text, drawn the way the browser laid it out: each one carries its cell of the glyph pixels, so the edge where the text comes loose looks like the ink it leaves, in every color and weight the heading has. A grain that comes loose first tumbles as a flake, then rounds off into sand of the `dot` size, lifts off the page towards the viewer and rides a swirling flow in the direction of the wind. Grains in front of the page are seen through the page camera, larger and slightly out of focus, those behind it smaller and dimmer, and a soft light from the upper left shades and glints them as they turn. Lifted grains cast faint shadows on the page below.

The edge sweeps from the side the wind blows to, so the sand always flies over the empty page, never across the text that is left. With the scroll trigger the sweep starts when the heading’s top rises past 30% of the window height, or with the first scroll for a heading that starts above that line, and ends when its middle reaches the top; it runs both ways.

The number of grains follows the area of the glyphs: about 10,000 for a 120 px heading of four words, at most 20,000 on fast devices, fewer on slower ones. `density` raises or lowers it within that limit. Under reduced motion the scroll and view triggers keep the text as it is, and the click trigger fades it out instead. Text with `text-shadow`, `-webkit-text-stroke`, `background-clip: text` or a vertical writing mode stays as it is, with a warning in development. `motion`, `trigger`, `density` 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.
- [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.
- [inflate](/docs/effects/inflate.md): Letters puff up into round 3D shapes, like balloons or soft clay, that dent and wobble under the pointer.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
