# physics

Playful gravity type: the letters of a heading become thick lit 3D tiles that fall, bounce, tumble and pile up, can be thrown with the pointer, and stand back up.

Each letter turns into a solid tile cut in the shape of its glyph, as thick as the depth says, with beveled edges and walls that catch the light. When the heading falls, the tiles drop to the bottom of their container, bounce, tip over and tumble about all three axes, seen through the page camera, and come to rest in a pile, lying on their faces or leaning on each other, each casting a soft shadow on the page and the floor. The drop style collapses the heading where it stands, scatter bursts the letters outward so they settle across the floor, and stack drops them in from above one by one into their places with a bounce. A loose letter can be grabbed, dragged and thrown. On demand the letters fly back up and the heading is plain text again.

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

A toy fair banner whose red heading drops into a pile of thick 3D letter tiles at the bottom of the banner, ready to be thrown about.

```html title="index.html"
<div class="ph-stage">
  <p class="ph-kicker">Toy Fair 2027 · Hall 4 · Stand B12</p>
  <h2 class="ph-headline" data-dio="physics" data-dio-trigger="click" data-dio-container=".ph-stage" tabindex="0">Play it again</h2>
  <p class="ph-lede">Wooden blocks, marble runs and stacking games, made in Porto and tested by the toughest critics we know.</p>
  <button class="ph-reset" type="button" data-dio-reset="">Stand it up</button>
</div>
```

## Usage

Put it on: a large heading or a short line of display text, in a container with room below it. Not img, video, canvas, svg or form fields.

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

```html
<section class="hero"><h1 data-dio="physics" data-dio-container=".hero">Gravity wins</h1><button data-dio-reset="">Reset</button></section>
```

- `data-dio-physics`: Set on the heading while letters are off their places: moving while any moves, piled once all are still.

- `data-dio-reset`: A click on any element with it stands the letters back up; a selector as its value limits it to matching headings.

In page transitions: Fades with the page.

- Event `dio:physics`, detail `{ state: 'moving' | 'piled' | 'upright' }`: The letters changed state: moving while they fall or fly, piled once still, upright once back as text.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'drop' \| 'scatter' \| 'stack'` | `'drop'` | drop collapses the heading into a pile; scatter bursts the letters apart; stack drops them in one by one. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'click' \| 'scroll' \| 'hover'` | `'view'` | view falls once in view; click toggles on a click; scroll falls when scrolled past; hover knocks letters. |
| `gravity` | `data-dio-gravity` | `number` 0 to 3 | `1` | Pull of gravity, 1 is about earth for the page; 0 lets the letters float. |
| `bounce` | `data-dio-bounce` | `number` 0 to 1 | `0.35` | How much of its speed a letter keeps when it hits the floor or another letter. |
| `friction` | `data-dio-friction` | `number` 0 to 1.5 | `0.5` | Grip between letters and the floor; 0 lets them slide like ice. |
| `depth` | `data-dio-depth` | `number` 0 to 1 em | `0.25` | Thickness of a tile in em of its font size; 0 draws flat letters. |
| `throw` | `data-dio-throw` | `number` 0 to 3 | `1` | Strength of a throw with the pointer; 0 turns grabbing and knocking off. |
| `container` (rebuilds) | `data-dio-container` | `string` | `''` | Selector of an ancestor whose box the letters fall in; empty keeps the heading’s own box. |

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

### Variants

- `style`: `drop`, `scatter`, `stack`
  - `drop` collapses the heading into a pile.
  - `scatter` bursts the letters apart.
  - `stack` drops them in one by one.
- `trigger`: `view`, `click`, `scroll`, `hover`
  - `view` falls once in view.
  - `click` toggles on a click.
  - `scroll` falls when scrolled past.
  - `hover` knocks letters.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`presence`](/docs/api/inputs#presence), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. While letters fall, fly, hop or are dragged; none once the pile has settled or the heading stands upright. 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 heading stays upright as plain text, and the engine does not load for it.

Runs on every device. A finger drags and throws a loose letter without scrolling the page; in the hover trigger a tap knocks a letter off.

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

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

## Cost and quality

Loads 18.1 KB the first time a page uses it. GPU: One instanced draw of a box per letter, marched to the glyph in up to 24 texture reads per pixel, and two instanced draws of shadows. Memory: An atlas of the letters at twice their CSS size with room for the shadows, and a relief map of the same size.

- low: The letters are rastered at 1 device pixel per CSS pixel instead of 2 and the march through each tile takes 8 steps instead of 24.
- medium: The march through each tile takes 14 steps instead of 24.

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

## Recipes

### A hero heading that falls once in view

The default `view` trigger lets the heading fall a moment after it comes into view. Point `data-dio-container` at the hero, so the letters pile up at its bottom edge rather than inside the heading’s own line, and give the visitor a way back with a button that carries `data-dio-reset`.

```html
<section class="hero">
  <h1 data-dio="physics" data-dio-container=".hero">Gravity wins</h1>
  <button data-dio-reset="">Reset</button>
</section>
```

While letters are off their places the heading carries `data-dio-physics="moving"` or `"piled"`, so CSS can show the button only then: `.hero:has([data-dio-physics]) button { visibility: visible }`.

### Letters that stack in one by one

`data-dio-style="stack"` drops each letter in from above into its own place, one after another, with a bounce and a wobble. On the first screen the heading stays hidden until the letters land, so it never shows as flat text first.

```html
<h2 data-dio="physics" data-dio-style="stack">Built to stack</h2>
```

### A word mark you can knock apart

With `data-dio-trigger="hover"` the letters the pointer touches fall off, and the rest stay in place. When the pointer leaves the container and the pile is still, the letters fly back. On a touch screen a tap knocks a letter off.

```html
<header><p class="mark" data-dio="physics" data-dio-trigger="hover" data-dio-container="header">loose</p></header>
```

`data-dio-trigger="scroll"` lets the heading fall as it scrolls past the upper third of the viewport and stands it back up when it comes back, and from code `element.dispatchEvent(new Event('dio:reset'))` stands it up at any time. A `dio:physics` event reports each change with `detail.state` set to `moving`, `piled` or `upright`.

## Notes

Each letter is a real 3D tile cut in the shape of its glyph from the heading’s own text, as the browser laid it out, so it keeps its font, color and weight. The tiles are boxes in a small rigid-body simulation written for this effect: gravity, the walls of the container, contacts between tiles with friction and restitution, all on a fixed step, and each tile goes to sleep once it lies still. In the drawing every tile is marched from its box to the glyph, so its walls are the letter’s own outline seen from any angle, lit by their normals, with a bevel round the face, and each casts a soft shadow on the page behind it and on the floor below.

The container is the heading’s own box unless `data-dio-container` names an ancestor; the room also reaches a little toward the viewer, so letters can lie flat and lean on each other in depth. A loose letter can be grabbed and thrown: `throw` scales the speed it leaves the pointer with, and 0 turns the pointer off. While a letter is held the page does not scroll under a finger.

Nothing is drawn once everything is still: the pile stays where it lies without frames, and once the letters are back in their places the heading is plain DOM text again. Under reduced motion the effect is off and the heading stays upright as text. Text with `text-shadow`, `-webkit-text-stroke`, `background-clip: text` or a vertical writing mode stays as it is, with a warning in development. `style`, `trigger` and `container` 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)
