flipbook
The page images of a container become a book whose pages bend as they turn.
Live preview needs WebGPU or WebGL 2. A six-page dinner menu as a book with a cover; a click on a page turns it and the paper bends around a fold.
This example failed to draw.
Click a page, drag its corner or focus the book and use the arrow keys.
Usage
Put it on a container of page images. start() registers flipbook, 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 flipbook<section class="menu" data-dio="flipbook" tabindex="0" aria-label="Dinner menu, autumn. Click a page or use the arrow keys to turn it." style="touch-action: pan-y"> <img src="/media/menu/01.webp" width="1200" height="1600" /> <img src="/media/menu/02.webp" width="1200" height="1600" /> <img src="/media/menu/03.webp" width="1200" height="1600" /> <img src="/media/menu/04.webp" width="1200" height="1600" /> <img src="/media/menu/05.webp" width="1200" height="1600" /> <img src="/media/menu/06.webp" width="1200" height="1600" /></section>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.menu', 'flipbook', { motion: 'curl', mode: 'auto', cover: true, duration: 0.9, curl: 0.6, shade: 0.5, click: true, swipe: true,})import { start } from '@dioramajs/diorama'import flipbook from '@dioramajs/media/flipbook'
const app = start()app.attach('.menu', flipbook({ motion: 'curl', mode: 'auto', cover: true, duration: 0.9, curl: 0.6, shade: 0.5, click: true, swipe: true,}))Events
It dispatches these on its element; they bubble.
dio:index,detail: { index: number }. A spread is reached; index is its first page, for a counter such as "Pages 2–3 of 12".
Page transitions
Fades with the page transition; a keyed flight carries the book. 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 |
|---|---|---|---|
indexdata-dio-index | number0 to 20 | 0 | First page of the spread; a fraction holds a frame of a turn. |
itemsrebuildsdata-dio-items | string | 'img' | Selector of the page images. |
moderebuildsdata-dio-mode | 'auto' | 'double' | 'single' | 'auto' | auto picks two pages on wide boxes and one on narrow ones. |
coverdata-dio-cover | boolean | true | true treats the first page as a cover: the book starts closed in the middle of the box and opens to spreads centered on the spine; with an even number of pages the last is the back cover. false shows a spread from the start. |
motionrebuildsdata-dio-motion | 'curl' | 'flat' | 'fade' | 'curl' |
|
durationdata-dio-duration | number0.2 to 2 s | 0.9 | Length of a turn by click or key. |
curldata-dio-curl | number0 to 1 | 0.6 | Tightness of the page bend; curl motion only. |
shadedata-dio-shade | number0 to 1 | 0.5 | Darkness of the fold and the cast shadow. |
clickdata-dio-click | boolean | true | A click on a closed book opens it; on a half of a spread it turns forward or back. |
swipedata-dio-swipe | boolean | true | Dragging and swiping turn pages. |
resolutionrebuildsdata-dio-resolution | number512 to 4096 px | 1600 | Cap on the long side of a page when decoding.At most low 1024, medium 1280 by quality tier. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads height, opacity, reducedMotion, size and width
. Draws only while something on it moves, then stops. Only while a page moves; none at rest. 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. Turns are a 0.25 s crossfade without bending, and the book moves to the middle at once.
Runs on every device. A tap on a half turns the page; dragging needs touch-action: pan-y on the container.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { flipbook: { reducedMotion: 'off' } } })Cost and quality
- Download
- 7.5 KB min+gzip the first time a page uses it
- GPU
- Two still pages and one or two turning pages on a bend grid.
- Memory
- Up to six pages as textures, about 9.6 MB each at 1600 px.
- 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 | Pages decode at 1024 px and bend on a 16 × 4 grid. |
medium | Pages decode at 1280 px and bend on a 24 × 6 grid. |
Recipes
A photo album with stiff pages
The flat motion swings each page about the spine without bending it, like a board book or a layflat album. With cover set to false the first two pictures open as a spread instead of a closed book on its cover.
Live preview needs WebGPU or WebGL 2. Four cyanotype plates of seaweed as an album; each page swings flat about the spine.
This example failed to draw.
<section class="album" data-dio="flipbook" data-dio-motion="flat" data-dio-cover="false" tabindex="0" aria-label="Photographs of British Algae, four plates. Click a page or use the arrow keys to turn it."> <img src="/media/algae/plate-1.avif" width="1000" height="1250" /> <img src="/media/algae/plate-2.avif" width="1000" height="1250" /> <img src="/media/algae/plate-3.avif" width="1000" height="1250" /> <img src="/media/algae/plate-4.avif" width="1000" height="1250" /></section>A page counter and a text version
Pictures of pages are neither searched nor translated, and a screen reader reads only their alt text. Put the menu as text next to the book, and a counter that follows dio:index, whose index is the first page of the spread on screen.
<section class="menu" data-dio="flipbook" tabindex="0" aria-label="Dinner menu. Click a page or use the arrow keys to turn it." style="touch-action: pan-y"> <img src="/media/menu/01.webp" /> <img src="/media/menu/02.webp" /> <img src="/media/menu/03.webp" /></section><p> <output class="pages" aria-live="polite">Cover</output> · <a href="/menu.pdf">The menu as PDF</a></p>For a book that shows two pages, the counter reads the spread from its first page.
const menu = document.querySelector('.menu')const pages = document.querySelector('.pages')const n = menu.querySelectorAll('img').length
menu.addEventListener('dio:index', (e) => { const first = e.detail.index + 1 const last = Math.min(first + 1, n) pages.textContent = first === 1 ? 'Cover' : first === last ? `Page ${first} of ${n}` : `Pages ${first} and ${last} of ${n}`})Jump to a section
Set index to a page and the book turns to the spread that holds it, so a row of links works as a table of contents. Pages count from 0: in the menu above, page 3 is the mains.
<nav class="sections" aria-label="Menu sections"> <button type="button" data-page="1">To begin</button> <button type="button" data-page="3">Mains</button> <button type="button" data-page="5">Wine</button></nav>const menu = document.querySelector('.menu')
document.querySelector('.sections').addEventListener('click', (e) => { const page = e.target.closest('button')?.dataset.page if (page) app.effects.get(menu, 'flipbook')?.set({ index: Number(page) })})Notes
The images stay in the page in their order, so a screen reader reads every page’s alt text, and without WebGL the pages are the strip you styled. With tabindex="0" on the container the book takes focus and turns with the arrow keys, PageUp, PageDown, Home and End. Make the container a <section> with an aria-label that names the book and says how to turn it.
With cover, the default, the book starts closed: the cover stands alone in the middle of the box, and a click anywhere on it opens the book. As the cover turns, the book slides so the open spread is centered on its spine. With an even number of pages the last one is the back cover, and the book closed on it stands in the middle again. Under reduced motion the book moves at once and the pages crossfade.
Give every page the same proportions: the book takes them from the first page and fits the spread inside the container’s box. On a box narrower than one and a half pages, mode="auto" shows one page at a time.
Up to six pages are textures at a time, decoded at most at resolution on the long side. Pages of 1200 to 1600 px are sharp in a book that fills a laptop screen.
mode, motion, items and resolution rebuild the effect when they change. Set them in the markup rather than switching them while a reader turns pages.