# split-flap

A departure board: each character flips through a drum of letters to its own, on view and whenever the site changes the text.

Every letter sits on a dark tile split across the middle. When the board comes into view the top half of each tile falls over its hinge again and again, showing the next letter of the drum, until it lands on the right one; letters further along the drum take longer, so the line settles unevenly like a real station board. When your code changes the text, only the characters that changed flip to their new letters. The plain style flips the letters without tiles and hands back to the real text at rest.

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

A departures board of three flights whose statuses flip to new values every few seconds.

```html title="index.html"
<div class="sf-board">
  <p class="sf-head">Departures</p>
  <ol class="sf-rows">
    <li><span>LISBON</span><span>14:05</span><span class="sf-status" data-dio="split-flap" data-dio-trigger="change" aria-live="polite">ON TIME</span></li>
    <li><span>OSLO</span><span>14:20</span><span class="sf-status" data-dio="split-flap" data-dio-trigger="change" aria-live="polite">DELAYED</span></li>
    <li><span>VALENCIA</span><span>14:45</span><span class="sf-status" data-dio="split-flap" data-dio-trigger="change" aria-live="polite">BOARDING</span></li>
  </ol>
</div>
```

## Usage

Put it on: a short line of text: a destination, a status, a price, a time, a menu item. Not img, video, canvas, svg or form fields.

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

```html
<p class="board" data-dio="split-flap">LISBON 14:05 BOARDING</p>
```

In page transitions: The copy fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'tiles' \| 'plain'` | `'tiles'` | tiles draws each character on a split tile in the tile color, also at rest; plain flips the letters alone and shows the real text at rest. |
| `motion` (rebuilds) | `data-dio-motion` | `'drum' \| 'random'` | `'drum'` | drum walks the drum in order, so each letter takes as many flips as it is far from its target, up to flips; random shows flips random faces on every letter, so they land together. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'change'` | `'view'` | view flips in from blank when the text enters, then on every change; change shows the text as it is and flips only when it changes. |
| `chars` (rebuilds) | `data-dio-chars` | `string` | `'board'` | Characters on the drum: board (A to Z, digits and . , : - / &), letters, digits, time, or your own string in order. Characters not on it are dropped in at the end of the flips. |
| `duration` | `data-dio-duration` | `number` 0 to 0.3 s | `0.07` | Time of one flip; 0 swaps the letters without flipping. |
| `flips` | `data-dio-flips` | `number` 0 to 40 | `10` | Most flips per character; 0 or 1 flips straight to the new letter. |
| `stagger` | `data-dio-stagger` | `number` 0 to 0.3 s | `0.03` | Delay between neighboring characters that flip. |
| `delay` | `data-dio-delay` | `number` 0 to 2 s | `0` | Wait after the text enters, 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. |
| `tile` | `data-dio-tile` | `color` | `'#1c1c1f'` | Color of the tiles; the letters take the color of the text. |
| `gap` | `data-dio-gap` | `number` 0 to 0.5 em | `0.08` | Space between neighboring tiles. |
| `shade` | `data-dio-shade` | `number` 0 to 1 | `0.5` | Shadow on a turning flap, the slit and the lighter top half; 0 is flat. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Picks the random faces; the same seed plays the same way. |

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

### Variants

- `style`: `tiles`, `plain`
  - `tiles` draws each character on a split tile in the tile color, also at rest.
  - `plain` flips the letters alone and shows the real text at rest.
- `motion`: `drum`, `random`
  - `drum` walks the drum in order, so each letter takes as many flips as it is far from its target, up to flips.
  - `random` shows flips random faces on every letter, so they land together.
- `trigger`: `view`, `change`
  - `view` flips in from blank when the text enters, then on every change.
  - `change` shows the text as it is and flips only when it changes.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size). Draws only while something on it moves, then stops. Only while flaps turn; 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: the tiles stay, and new text shows at once without flips. The plain style is the text as it is.

Runs on every device. The same as with a mouse.

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

start({ effects: { 'split-flap': { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 9.2 KB the first time a page uses it. GPU: One instanced draw of three quads per character, one texture read per pixel. Memory: A small sheet of the drum and the letters of the text, kept while the element lives.

- low: The letters are drawn at 1 device pixel per CSS pixel instead of 2.

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

## Recipes

### A status your code changes

Write the new text into the element and the board flips to it: the effect watches the text, so there is no extra call. Only the characters that differ turn, and the DOM always holds the current text, so screen readers, search and copy see it. Add `aria-live="polite"` if a screen reader should announce each change; it then reads the new status once, not every flip.

```html
<span
  class="status"
  data-dio="split-flap"
  data-dio-trigger="change"
  aria-live="polite"
>ON TIME</span>
```

```ts
document.querySelector('.status').textContent = 'BOARDING'
```

`data-dio-trigger="change"` shows the text at once and flips only on changes. The default `view` also flips the board in from blank tiles the first time it comes into view.

### A countdown that ticks every second

For a clock, a launch countdown or a sale timer, use the `time` drum with `data-dio-flips="1"`: each changed digit turns once, straight to the next value, the way a flip clock does.

```html
<time
  class="clock"
  data-dio="split-flap"
  data-dio-trigger="change"
  data-dio-chars="time"
  data-dio-flips="1"
>02:14:09</time>
```

### Menu boards and prices without tiles

`data-dio-style="plain"` flips the letters alone, in your own font and color, and shows the real text at rest. With `motion="random"` every letter shows a few random faces and they all land together.

```html
<h2
  data-dio="split-flap"
  data-dio-style="plain"
  data-dio-motion="random"
  data-dio-flips="6"
>Today: ramen</h2>
```

## Notes

The tiles look best in a monospaced font with some `letter-spacing` and a `line-height` of 1.3 or more, so neighboring tiles and lines do not touch. Each tile is centered on its letter; a space stays a gap. The letters take the color of the text and the tiles take `tile`; `shade="0"` gives flat tiles, `gap="0"` tiles that touch.

`chars` sets the drum, the order a flap walks through: `board` (the letters A to Z, digits and `. , : - / &`), `letters`, `digits`, `time`, or your own string. Lowercase letters stop at the place of their capital, and a character not on the drum drops in at the end. A letter takes as many flips as it is far from its target along the drum, at most `flips`, so a line settles unevenly like a real board. `duration="0"` swaps the letters without turning.

With the tiles the board stays drawn at rest, and the DOM text under it stays in the page for screen readers and selection. Under reduced motion the tiles stay and new text shows at once; the plain style is the text as it is. `style`, `motion`, `trigger`, `chars` and `seed` rebuild the effect when they change; the other params change live.

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