# rotator

Words take turns in a headline on the faces of a real 3D cube, drum, split flap or folding strip.

The word in the headline sits on a face of a solid shape. Every few seconds the shape turns in perspective to show the next word: a cube rolls up, a drum spins with the words above and below curving away, a split flap falls over its hinge, or a strip folds back into the page while the next one unfolds. Faces darken as they turn away from a lamp above the text and catch a glint as they pass it, and the line around the word eases to the new width. Between turns the word is plain text.

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

A design studio's hero headline whose last word rolls on a lit 3D cube through websites, apps, brands and launches.

```html title="index.html"
<section class="rt-hero">
  <p class="rt-kicker">Northbeam · Design studio, Lisbon</p>
  <h2 class="rt-title">We build <span class="rt-word" data-dio="rotator"><span>websites</span> <span>apps</span> <span>brands</span> <span>launches</span></span><br />people remember.</h2>
  <p class="rt-line">A team of nine working with founders from the first sketch to launch day, and the long months after it.</p>
</section>
```

## Usage

Put it on: an inline element of a heading or paragraph whose children are the words, one element each, such as spans. Not img, video, canvas, svg or form fields.

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

```html
<h1>We build <span data-dio="rotator"><span>websites</span> <span>apps</span> <span>brands</span></span></h1>
```

- `:scope > *` inside: Each child element with text is one word, in order; the first shows before the first turn.

In page transitions: Between turns the word leaves and enters with the page; a turn under way fades with its copy.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'cube' \| 'drum' \| 'flip' \| 'fold'` | `'cube'` | cube rolls a box; drum spins a wheel; flip drops a split flap; fold folds a paper strip. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'interval' \| 'scroll' \| 'hover'` | `'interval'` | interval turns on a timer; scroll scrubs the words with the scroll; hover turns on hover. |
| `axis` (rebuilds) | `data-dio-axis` | `'x' \| 'y'` | `'x'` | x turns the words up and over; y turns them sideways, along the line. |
| `interval` | `data-dio-interval` | `number` 0.5 to 8 s | `2.2` | Time each word stays before the next turn, with trigger interval. |
| `duration` | `data-dio-duration` | `number` 0.2 to 2 s | `0.8` | Time one turn takes. |
| `depth` | `data-dio-depth` | `number` 0 to 3 | `1` | Strength of the perspective; 0 turns the shape flat, without perspective. |
| `shade` | `data-dio-shade` | `number` 0 to 1 | `1` | How much the lamp darkens and lights the turning faces; 0 keeps the text color. |
| `pause` | `data-dio-pause` | `boolean` | `true` | Holds the timer while the pointer is over the line or focus is in it. |
| `loop` | `data-dio-loop` | `boolean` | `true` | Starts over after the last word; false stops on it. |

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

### Variants

- `style`: `cube`, `drum`, `flip`, `fold`
  - `cube` rolls a box.
  - `drum` spins a wheel.
  - `flip` drops a split flap.
  - `fold` folds a paper strip.
- `trigger`: `interval`, `scroll`, `hover`
  - `interval` turns on a timer.
  - `scroll` scrubs the words with the scroll.
  - `hover` turns on hover.
- `axis`: `x`, `y`
  - `x` turns the words up and over.
  - `y` turns them sideways, along the line.

## Inputs and frames

Reads [`size`](/docs/api/inputs#size). Draws only while something on it moves, then stops. Only while a word turns, and with trigger scroll while scrolling through a turn; none between turns: a timer wakes the next one. 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 words still change, but fade into each other in place over a third of a second, without turning.

Runs on every device. The same as with a mouse; with trigger hover a tap turns to the next word, and nothing pauses.

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

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

## Cost and quality

Loads 10.3 KB the first time a page uses it. GPU: One instanced draw of two to six textured quads while a word turns; nothing between turns. Memory: A small atlas of the words of the current turn at twice their CSS size, replaced each turn.

- low: The words are rastered at 1 device pixel per CSS pixel instead of 2.

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

## Recipes

### A departures board

`style="flip"` turns each word on a split flap: the top half of the word falls over a hinge, shows the bottom half of the next word on its back and lands with a small bounce, the way a station board clacks. A shorter `interval` suits it. It reads best in capitals and a bold, narrow font, on an event, travel or launch page.

A night train's departure line that flips through Berlin, Prague, Vienna and Budapest in yellow capitals on a dark board.

```html title="index.html"
<section class="rt-board">
  <p class="rt-label">Night train EN 471 · Platform 4</p>
  <h2 class="rt-stop">Next stop <span data-dio="rotator" data-dio-style="flip" data-dio-interval="1.8"><span>Berlin</span> <span>Prague</span> <span>Vienna</span> <span>Budapest</span></span></h2>
  <p class="rt-time">Departs 21:42 · On time</p>
</section>
```

### Words turned by the scroll

With `data-dio-trigger="scroll"` the words follow the scroll position both ways and hold still between turns. In a section pinned with `position: sticky`, or one marked with `data-dio-section`, the words spread over the scroll through that section; anywhere else over the way the line travels up the viewport. A drum suits it: the words above and below curve away like a reel behind a window.

```html
<section class="story" data-dio-section="">
  <h2 class="sticky">Made for <span data-dio="rotator" data-dio-style="drum" data-dio-trigger="scroll"><span>founders</span> <span>teams</span> <span>agencies</span> <span>everyone</span></span></h2>
</section>
```

### A button that answers the pointer

With `data-dio-trigger="hover"` the label turns once each time the pointer or keyboard focus reaches the button, and on a tap on a phone. `axis="y"` turns it sideways, along the line.

```html
<a class="cta" href="/signup">Start <span data-dio="rotator" data-dio-trigger="hover" data-dio-style="flip" data-dio-axis="y"><span>free</span> <span>today</span></span></a>
```

## Notes

Put the effect on an inline element whose children are the words, one element each, usually spans, with a space between them. The effect shows the current word and keeps the others in the page for screen readers, which read every word, so write them in an order that makes sense read aloud. Before the effect runs, and on a device without WebGL, the browser would show all the words in a row: the rule below hides all but the first the same way.

```css
[data-dio="rotator"] > * + * {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
```

The words are drawn on a real 3D shape only while they turn: a cube or drum whose axis lies behind the page, a flap on a hinge or a strip folding into the page, seen in perspective from in front of the word. A lamp above and to the left lights the faces by their angle, so a face turning away darkens and one tilting up catches a glint; `shade="0"` keeps the text color. Between turns the word is real, selectable text, and the effect draws no frames: a timer wakes the next turn, and it waits while the pointer is over the line or focus is in it. Under reduced motion the words still change, but fade into each other in place.

While a word turns, the line around it eases to the width of the next one, so a centered heading stays centered and the text after the word moves along. The rotator is set to `display: inline-block` and `white-space: nowrap` while it runs, so a phrase of two words never breaks mid-turn. The element itself is hidden behind its copy while a word turns, its own background too: put a highlight or a pill behind the word on a parent.

The canvas cannot draw `background-clip: text`, `text-shadow`, `-webkit-text-stroke` or vertical writing modes the way the browser does. Words with any of them still change on the timer, without turning, and a warning in development names the reason. `style`, `trigger` and `axis` rebuild the effect when they change and start over from the first word.

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