# voxel

A heading built from small lit 3D cubes that tumble in, bounce into place and settle into the text.

When the heading scrolls into view, small cubes in the colors of its letters fall in from above and behind the page, each turning on its own axis, and land in a wave from left to right with a hop and a squash, lower rows first, so the letters stack up like toy blocks. Each cube is lit from the upper left with a beveled edge, and the grooves between neighbors are darker. Once a column has landed the cubes flatten into the face of the text and the real text fades in over them, so the heading ends as selectable DOM text. Toy bricks with studs or flat retro voxels can stand in for the cubes; the voxels stay as they are, and cubes under the pointer pop out toward the viewer, lean out of the bump and spring back.

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

A toy store's headline in yellow cubes on a blue background that fall in from above, bounce into place and settle into the text.

```html title="index.html"
<section class="vx-shop">
  <p class="vx-kicker">Blockworks · toys, kits and bricks</p>
  <h2 class="vx-title" data-dio="voxel">Build something big</h2>
  <p class="vx-line">Free build table every Saturday from 10 a.m. Bring a friend, leave with a spaceship.</p>
</section>
```

## Usage

Put it on: a large heading or a short line of display text. 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 `voxel`, so the attribute alone puts it on an element.

```html
<h1 data-dio="voxel">Build it bold</h1>
```

In page transitions: Leaving a page, the heading turns back into cubes that bounce up and fly off behind the page; entering, they build it again.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'cubes' \| 'bricks' \| 'pixels'` | `'cubes'` | cubes are beveled blocks that settle into the text, for most headings; bricks are toy bricks two cells wide with mortar lines and studs; pixels are flat-faced voxels with crisp edges that stay as voxels, for a retro or game look. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'scroll' \| 'hover'` | `'view'` | view builds when the heading enters; scroll builds it toward the middle of the viewport and drops the cubes again as it leaves; hover shows voxels at once for the pointer to poke. |
| `cell` (rebuilds) | `data-dio-cell` | `number` 3 to 32 px | `8` | Size of one cube on the grid over the letters; larger reads chunkier. |
| `depth` | `data-dio-depth` | `number` 0 to 4 | `1` | Depth of a cube as a share of its width; 2 makes long blocks, 0 flat tiles. |
| `drop` | `data-dio-drop` | `number` 0 to 800 px | `240` | How far above the text the cubes start; 0 lets them pop up in place. |
| `duration` | `data-dio-duration` | `number` 0.3 to 3 s | `0.9` | Time one cube takes to fall, land and settle. |
| `stagger` | `data-dio-stagger` | `number` 0 to 0.5 s | `0.1` | Delay of the wave per em of text, left to right; 0 drops every column at once. |
| `bounce` | `data-dio-bounce` | `number` 0 to 2 | `1` | Height of the hop and the squash when a cube lands; 0 lands it dead. |
| `radius` | `data-dio-radius` | `number` 0 to 400 px | `120` | Reach of the pointer for voxels that stay; 0 turns the pop-out off. |
| `delay` | `data-dio-delay` | `number` 0 to 2 s | `0` | Wait after the trigger, for a cascade. |
| `threshold` | `data-dio-threshold` | `number` 0 to 1 | `0.2` | Share of the element height in view, above the bottom tenth, before it plays. |
| `studs` | `data-dio-studs` | `boolean` | `true` | Round studs on top of each brick in the bricks style. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Picks the starts and turns of the cubes; the same seed builds the same way. |

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

### Variants

- `style`: `cubes`, `bricks`, `pixels`
  - `cubes` are beveled blocks that settle into the text, for most headings.
  - `bricks` are toy bricks two cells wide with mortar lines and studs.
  - `pixels` are flat-faced voxels with crisp edges that stay as voxels, for a retro or game look.
- `trigger`: `view`, `scroll`, `hover`
  - `view` builds when the heading enters.
  - `scroll` builds it toward the middle of the viewport and drops the cubes again as it leaves.
  - `hover` shows voxels at once for the pointer to poke.

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`presence`](/docs/api/inputs#presence), [`progress`](/docs/api/inputs#progress), [`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. Only while cubes fall and settle, while scrolling with the scroll trigger and while popped voxels spring back; 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. Static: cubes and bricks show the text as it is; pixels show still voxels without the build or the pop-out.

Runs on every device. The same as with a mouse; voxels that stay pop out under a finger while it presses on the heading.

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

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

## Cost and quality

Loads 12.1 KB the first time a page uses it. GPU: One instanced draw of up to 9,000 lit boxes, one quad per letter for the crisp text, and for voxels that stay one quad per cube for the shadows. Memory: A canvas of the text at twice its CSS size, and about 100 bytes per cube: under 1 MB at the high tier.

- low: At most 2,250 cubes (the grid widens past that), and the text rastered at 1 device pixel per CSS pixel instead of 2.
- medium: At most about 5,000 cubes.

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

## Recipes

### Retro voxels for a game launch

`style="pixels"` draws flat-faced voxels with crisp edges and no bevel, and keeps them: once the heading is built it stays as voxels instead of turning back into text. A larger `depth` makes long blocks whose sides show in perspective, more so away from the middle of the screen. Voxels that stay pop out toward the viewer under the pointer, lean out of the bump, cast a soft shadow on the cubes around them and spring back when it moves on. Scroll down to this example to see it build, then move the pointer across it.

A dark purple game teaser with the words Press start in green voxels that pop out under the pointer.

```html title="index.html"
<section class="vx-arcade">
  <p class="vx-arcade-kicker">Night Owl Games · out Friday on every console</p>
  <h2 class="vx-arcade-title" data-dio="voxel" data-dio-style="pixels" data-dio-cell="6" data-dio-depth="2" data-dio-radius="140">Press start</h2>
</section>
```

`radius` sets how far the pointer reaches, in CSS pixels; 0 turns the pop-out off. The text stays real for screen readers, search and selection, but it shows as voxels, so keep this style for a short display line.

### Toy bricks for a kids’ brand

`style="bricks"` pairs neighboring cells into bricks two cells wide, laid in a running bond like a wall, with dark mortar lines between them and a round stud on top of each cell. Bricks read best with a larger `cell`, so the studs have room; `studs="false"` leaves plain bricks.

```html
<h1 data-dio="voxel" data-dio-style="bricks" data-dio-cell="12" data-dio-bounce="1.5">Toy box</h1>
```

### A heading that builds as you scroll

With `data-dio-trigger="scroll"` the cubes fall into place as the heading rises toward the middle of the viewport, the text holds there as plain DOM text, and as it moves on toward the top it turns back into cubes that drop away down the page. Scrolling back plays it in reverse. A higher `drop` makes the cubes fall from further up.

```html
<h2 data-dio="voxel" data-dio-trigger="scroll" data-dio-drop="400">Step by step</h2>
```

### Blocks to poke in a navigation or a hero

`data-dio-trigger="hover"` skips the build and shows the heading as cubes from the start, for the pointer to poke. Under reduced motion it stays plain text, unless the style is pixels.

```html
<h2 data-dio="voxel" data-dio-trigger="hover" data-dio-radius="160">Play with us</h2>
```

## Notes

The cubes are real boxes in the 3D space of the page, seen through the same camera, one instanced draw for all of them. Each samples the color of the text under its cell of the grid, so mixed colors inside the heading carry over. They are lit by a fixed key light from the upper left: cubes get a beveled edge that catches the light, bricks a gloss on their studs, and the sides of a cube are darker where a neighbor sits beside it and toward the page, a cheap kind of ambient occlusion between neighbors.

Each cube starts above and behind the page, tumbles around its own axis on the way down, lands with a hop and a squash, and wobbles still. The wave runs from left to right with lower rows first, so the letters stack up. Once a column has landed the cubes flatten into the face of the text and the real text fades in over them; when the last column is in the element is plain, selectable DOM text. Text on screen when the page opens is hidden until the effect is ready and then builds, as with `assemble`.

`cell` sets the size of the grid in CSS pixels: 8 suits headings of 80 to 160 px, 5 or 6 smaller ones, and 12 or more gives chunky toy blocks. The grid widens when a heading would need more than 9,000 cubes, fewer on slower devices. Frames run only while cubes move; at rest the effect draws nothing, voxels that stay included. Under reduced motion cubes and bricks show the text as it is, and pixels show still voxels. 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`, `mode`, `cell` 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)
