# shatter

A heading breaks into lit 3D shards that fly apart and tumble, then fly back together: on a click, on scroll, under the pointer or once in view.

The text box cracks into pieces along seeded fracture lines that follow the letters, and each piece is a real slab with the heading printed on its face. On a click the cracks run out from the point of impact in rings, the pieces nearest it burst out first, and every piece flies off with its own spin, seen through the page camera so the ones coming toward the viewer grow. When the text comes back (hold above 0) the pieces slow down and hang in the air close to the heading, so the break stays in its box; with hold 0 they arc over and fall away under gravity. Turning pieces catch the light: faces brighten and darken, the cut edges glint, the walls show their thickness, and lifted pieces cast soft shadows on the page. Glass breaks into thin clear panes with green edges and reflections, stone into thick chunks with rough walls and a puff of dust, paper into torn scraps with pale fibers that flutter down and show their blank backs. After a pause the pieces fly back along their own paths and lock into place.

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

A glass studio headline on a dark page that breaks into lit glass shards where it is clicked, which tumble away and fly back together.

```html title="index.html"
<div class="sh-hero">
  <p class="sh-kicker">Harbor Glass Studio · Open furnace weekend</p>
  <h2 class="sh-headline" data-dio="shatter">Fragile by design</h2>
  <p class="sh-lede">Two days of open furnaces, glassblowing demos and a seconds sale of studio pieces. Saturday and Sunday, 10 to 6, free entry.</p>
</div>
```

## Usage

Put it on: a large heading or 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 `shatter`, so the attribute alone puts it on an element.

```html
<h1 data-dio="shatter">Break the silence</h1>
```

- `data-dio-shatter`: Set by the click trigger on its element: broken while the pieces fly, gone once the text is gone for good.

In page transitions: The pieces fade with the page.

- Event `dio:shatter`, detail `null`: The click trigger with hold 0 has broken the text for good; the element keeps its space, so remove or collapse it here.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'glass' \| 'stone' \| 'paper'` | `'glass'` | glass is thin clear panes; stone is thick rough chunks with dust; paper is torn scraps. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'click' \| 'scroll' \| 'hover' \| 'view'` | `'click'` | click breaks where clicked; scroll as it leaves; hover lifts pieces; view flies it in. |
| `pieces` (rebuilds) | `data-dio-pieces` | `number` 6 to 140 | `40` | About how many pieces the text box is cut into; pieces with no ink are left out. At most low 24, medium 64. |
| `strength` | `data-dio-strength` | `number` 0 to 3 | `1` | Speed the pieces leave with, and how high they lift with the hover trigger. |
| `spin` | `data-dio-spin` | `number` 0 to 3 | `1` | How fast the pieces tumble; 0 keeps them flat to the page as they fly. |
| `gravity` | `data-dio-gravity` | `number` 0 to 3 | `1` | Pull that bends the paths down; 0 lets the pieces drift off in a straight line. |
| `depth` | `data-dio-depth` | `number` 0 to 3 | `1` | Thickness of a piece, times the thickness of its style; 0 makes flat pieces. |
| `hold` | `data-dio-hold` | `number` 0 to 10 s | `1.5` | Time the click trigger waits before the pieces fly back; 0 leaves the text gone. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Picks the fracture lines, paths and spins; the same seed breaks the same way. |

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

### Variants

- `style`: `glass`, `stone`, `paper`
  - `glass` is thin clear panes.
  - `stone` is thick rough chunks with dust.
  - `paper` is torn scraps.
- `trigger`: `click`, `scroll`, `hover`, `view`
  - `click` breaks where clicked.
  - `scroll` as it leaves.
  - `hover` lifts pieces.
  - `view` flies it in.

## Inputs and frames

Reads [`presence`](/docs/api/inputs#presence), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size). Draws only while something on it moves, then stops. While the pieces fly, fly back or settle, and while scrolling through the break with the scroll trigger; 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 click trigger fades the text out instead, and back in after hold; the other triggers keep the text as it is.

Runs on every device. A tap breaks it with the click trigger; with the hover trigger a finger on the text lifts the pieces under it.

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

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

## Cost and quality

Loads 16.0 KB the first time a page uses it. GPU: One draw of every piece as a prism of a few dozen triangles, back to front, with a short march through stone; one draw of their shadows and one of up to 420 dust motes. Memory: A canvas of the text at twice its CSS size and two small float textures of the pieces: a few MB for a large heading.

- low: At most 24 pieces, a shorter march through stone, no dust, and the text rastered at 1 device pixel per CSS pixel instead of 2.
- medium: At most 64 pieces and a shorter march through stone.

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

## Recipes

### Cracks under the pointer

With `data-dio-trigger="hover"` the heading stays whole, but the pieces near the pointer lift toward the viewer and tilt, and a ripple of cracks runs out across the line from where the pointer came in. When the pointer leaves, the pieces settle back and the heading is plain text again. `strength` sets how high they lift and `spin` how far they tilt. On a touch screen a finger on the heading lifts the pieces under it. Move the pointer over the heading below.

A stoneware shop banner on a cream page whose heading cracks into stone pieces that lift under the pointer.

```html title="index.html"
<div class="sh-shop">
  <p class="sh-shop-tag">Hand-thrown stoneware · ships in recycled crates</p>
  <h2 class="sh-shop-title" data-dio="shatter" data-dio-trigger="hover" data-dio-style="stone">Handle with care</h2>
  <p class="sh-shop-note">Every bowl is packed by the potter who made it, so it reaches you in one piece.</p>
</div>
```

### A notice that breaks for good

With `data-dio-hold="0"` the click trigger never brings the text back: the pieces fall away, the element is left empty with `data-dio-shatter="gone"`, and a `dio:shatter` event fires. The element still takes its space, so remove or collapse it then. Give a clickable element `role="button"` and `tabindex="0"`, so Enter breaks it too.

```html
<p class="notice" role="button" tabindex="0" data-dio="shatter" data-dio-style="stone" data-dio-hold="0">Offer expired. Click to dismiss.</p>
```

```js
document.querySelector('.notice').addEventListener('dio:shatter', (e) => e.target.remove())
```

### A chapter heading that crumbles as it scrolls out

With `data-dio-trigger="scroll"` the heading is whole while it sits in the viewport, and breaks apart as it rises past the top, scrubbed by the scroll in both directions. A heading that scrolls in from the bottom flies together on its way up. Stone suits a heavy, solid typeface.

```html
<h2 data-dio="shatter" data-dio-style="stone" data-dio-trigger="scroll" data-dio-pieces="30">Built to last</h2>
```

`data-dio-trigger="view"` plays the opposite once: the pieces fly in from around the page and lock together as the heading comes into view, a good opener for a launch or a portfolio.

## Notes

The pieces are cut from the heading’s own text, drawn the way the browser laid it out, so every piece carries its part of the letters in their font, color and weight. The cuts are seeded Voronoi cells that crowd onto the letters; cells with no ink are left out. A click cuts the text again around the point of impact, with small pieces in rings close to it and larger ones farther out, like a struck pane, and the crack runs out from there so the far pieces let go a moment later.

Each piece is a real slab seen through the page camera: glass is a thin clear pane with the text printed on its face, green along its edges where the light travels through more glass, with reflections and bright glints on the cut edges as it turns. Stone is a thick chunk whose walls follow the letters, rough and matte, and it breaks with a puff of dust. Paper is a sheet with torn, fibrous edges that bends and rocks as it flutters down and shows its blank back. Lifted pieces cast soft shadows on the page. `depth` scales the thickness; 0 gives flat pieces.

`pieces` sets about how many pieces the text box is cut into: 40 suits a heading of two or three words, more makes finer shards. The drawing order follows the distance to the camera every frame, so pieces that fly toward the viewer pass in front of the others. Nothing is drawn at rest: the heading is plain DOM text before the break and once the pieces are back, and with the hover trigger once they have settled.

Under reduced motion the click trigger fades the text out, and back in after `hold`; the other triggers keep the text as it is. 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`, `pieces` 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)
