Diorama

Made for a big screen.

This site is one 3D scene, built for a computer: a wide window and a mouse. Open dioramajs.dev on a desktop or a laptop to see it.This site is one 3D scene, built for a wide window. Make this one wider, or open dioramajs.dev on a larger screen.

GitHub

DioramaDocs

flipbook

Galleries and viewers

@dioramajs/media/flipbookSource

The page images of a container become a book whose pages bend as they turn.

View as Markdown
On this page

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.

Pages show as a spread of two, or one on a narrow box. Closed on its cover the book stands in the middle of the box; as the cover turns the book slides so the open spread is centered on its spine, and closed on the back cover it stands in the middle again. A turning page bends around a fold from the spine, the fold is shaded and a shadow falls on the page below. A page follows the finger or the mouse from a corner or an edge and, let go, finishes the turn or falls back; a click on a closed book opens it and a click on a half of a spread turns forward or back, and with tabindex on the container the arrows, PageUp, PageDown, Home and End turn too. The images stay in the page in order, so screen readers read them; give the text as HTML or PDF as well, pictures of pages are neither searched nor translated.
Cover

Click a page, drag its corner or focus the book and use the arrow keys.

Params of flipbook
motion

curl bends the page around a fold like paper.

mode

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.

Terminal window
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.

ParamTypeDefaultDescription
indexdata-dio-indexnumber0 to 200First page of the spread; a fraction holds a frame of a turn.
itemsrebuildsdata-dio-itemsstring'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-coverbooleantruetrue 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'
  • curl bends the page around a fold like paper.
  • flat swings it stiff about the spine like a board book or a layflat album.
  • fade crossfades the spreads over duration without a page in the air.
durationdata-dio-durationnumber0.2 to 2 s0.9Length of a turn by click or key.
curldata-dio-curlnumber0 to 10.6Tightness of the page bend; curl motion only.
shadedata-dio-shadenumber0 to 10.5Darkness of the fold and the cast shadow.
clickdata-dio-clickbooleantrueA click on a closed book opens it; on a half of a spread it turns forward or back.
swipedata-dio-swipebooleantrueDragging and swiping turn pages.
resolutionrebuildsdata-dio-resolutionnumber512 to 4096 px1600Cap 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.

TierWhat changes
lowPages decode at 1024 px and bend on a 16 × 4 grid.
mediumPages 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.

Cystoseira granulata: white branching fronds on deep blue Cystoseira foeniculacea: fine feathery fronds on deep blue Cystoseira fibrosa: long fronds with small round bladders on deep blue Ectocarpus granulosus: a small dense tuft on deep blue

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.

Params of flipbook
<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.