rotator
Words take turns in a headline on the faces of a real 3D cube, drum, split flap or folding strip.
Northbeam · Design studio, Lisbon
We build websites apps brands launches
people remember.
A team of nine working with founders from the first sketch to launch day, and the long months after it.
Live preview needs WebGPU or WebGL 2. A design studio's hero headline whose last word rolls on a lit 3D cube through websites, apps, brands and launches.
This example failed to draw.
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. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add rotator<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>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.rt-word', 'rotator', { style: 'cube', axis: 'x', interval: 2.2, duration: 0.8, depth: 1, shade: 1,})import { start } from '@dioramajs/diorama'import rotator from '@dioramajs/text/rotator'
const app = start()app.attach('.rt-word', rotator({ style: 'cube', axis: 'x', interval: 2.2, duration: 0.8, depth: 1, shade: 1,}))Markup it reads
:scope > *inside: Each child element with text is one word, in order; the first shows before the first turn.
Page transitions
Between turns the word leaves and enters with the page; a turn under way fades with its copy. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
stylerebuildsdata-dio-style | 'cube' | 'drum' | 'flip' | 'fold' | 'cube' |
|
triggerrebuildsdata-dio-trigger | 'interval' | 'scroll' | 'hover' | 'interval' |
|
axisrebuildsdata-dio-axis | 'x' | 'y' | 'x' |
|
intervaldata-dio-interval | number0.5 to 8 s | 2.2 | Time each word stays before the next turn, with trigger interval. |
durationdata-dio-duration | number0.2 to 2 s | 0.8 | Time one turn takes. |
depthdata-dio-depth | number0 to 3 | 1 | Strength of the perspective; 0 turns the shape flat, without perspective. |
shadedata-dio-shade | number0 to 1 | 1 | How much the lamp darkens and lights the turning faces; 0 keeps the text color. |
pausedata-dio-pause | boolean | true | Holds the timer while the pointer is over the line or focus is in it. |
loopdata-dio-loop | boolean | true | Starts over after the last word; false stops on it. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads 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. How a frame is decided.
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.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { rotator: { reducedMotion: 'off' } } })Cost and quality
- Download
- 10.3 KB min+gzip 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.
- Frames
- Idle at rest
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | The words are rastered at 1 device pixel per CSS pixel instead of 2. |
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.
Night train EN 471 · Platform 4
Next stop Berlin Prague Vienna Budapest
Departs 21:42 · On time
Live preview needs WebGPU or WebGL 2. A night train's departure line that flips through Berlin, Prague, Vienna and Budapest in yellow capitals on a dark board.
This example failed to draw.
<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.
<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.
<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.
[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.