# flipbook

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

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.

Module `media`, category Galleries and viewers. Import `@dioramajs/media/flipbook`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/media/src/flipbook.ts)

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.

```html title="index.html"
<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" alt="Dinner, autumn on the hill. Served on the terrace from half past six." width="1200" height="1600" />
  <img src="/media/menu/02.webp" alt="To begin: Calimyrna figs, sea urchin on toast, little gem, smoked trout rillettes." width="1200" height="1600" />
  <img src="/media/menu/03.webp" alt="From the garden: avocado, roasted squash, heirloom beans, charred leeks." width="1200" height="1600" />
  <img src="/media/menu/04.webp" alt="Mains: duck breast, halibut, tagliatelle, rib of beef for two." width="1200" height="1600" />
  <img src="/media/menu/05.webp" alt="To finish: persimmon pudding, olive oil cake, dark chocolate, cheese from the Central Coast." width="1200" height="1600" />
  <img src="/media/menu/06.webp" alt="From the cellar: sauvignon blanc, chardonnay, pinot noir and syrah by the glass." width="1200" height="1600" />
</section>
```

## Usage

Put it on: a container of page images.

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

```html
<div class="album" data-dio="flipbook" data-dio-motion="flat" data-dio-cover="false" tabindex="0" role="region" aria-label="Wedding album"><img src="/album/01.webp" alt="The ceremony" /><img src="/album/02.webp" alt="The garden" /></div>
```

In page transitions: Fades with the page transition; a keyed flight carries the book.

- Event `dio:index`, detail `{ index: number }`: A spread is reached; index is its first page, for a counter such as "Pages 2–3 of 12".

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `index` | `data-dio-index` | `number` 0 to 20 | `0` | First page of the spread; a fraction holds a frame of a turn. |
| `items` (rebuilds) | `data-dio-items` | `string` | `'img'` | Selector of the page images. |
| `mode` (rebuilds) | `data-dio-mode` | `'auto' \| 'double' \| 'single'` | `'auto'` | auto picks two pages on wide boxes and one on narrow ones. |
| `cover` | `data-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. |
| `motion` (rebuilds) | `data-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. |
| `duration` | `data-dio-duration` | `number` 0.2 to 2 s | `0.9` | Length of a turn by click or key. |
| `curl` | `data-dio-curl` | `number` 0 to 1 | `0.6` | Tightness of the page bend; curl motion only. |
| `shade` | `data-dio-shade` | `number` 0 to 1 | `0.5` | Darkness of the fold and the cast shadow. |
| `click` | `data-dio-click` | `boolean` | `true` | A click on a closed book opens it; on a half of a spread it turns forward or back. |
| `swipe` | `data-dio-swipe` | `boolean` | `true` | Dragging and swiping turn pages. |
| `resolution` (rebuilds) | `data-dio-resolution` | `number` 512 to 4096 px | `1600` | Cap on the long side of a page when decoding. At most low 1024, medium 1280. |

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

### Variants

- `motion`: `curl`, `flat`, `fade`
  - `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.
- `mode`: `auto`, `double`, `single`

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width). Draws only while something on it moves, then stops. Only while a page moves; 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. 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.

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

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

## Cost and quality

Loads 7.5 KB 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.

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

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

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

Four cyanotype plates of seaweed as an album; each page swings flat about the spine.

```html title="index.html"
<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" alt="Cystoseira granulata: white branching fronds on deep blue" width="1000" height="1250" />
  <img src="/media/algae/plate-2.avif" alt="Cystoseira foeniculacea: fine feathery fronds on deep blue" width="1000" height="1250" />
  <img src="/media/algae/plate-3.avif" alt="Cystoseira fibrosa: long fronds with small round bladders on deep blue" width="1000" height="1250" />
  <img src="/media/algae/plate-4.avif" alt="Ectocarpus granulosus: a small dense tuft on deep blue" 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.

```html
<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.

```ts
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.

```html
<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>
```

```ts
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.

## Related

- [carousel](/docs/effects/carousel.md): Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.
- [coverflow](/docs/effects/coverflow.md): A row of covers as the classic cover flow: the middle one faces you, its neighbours turn away in perspective.
- [gallery](/docs/effects/gallery.md): A container of images shows one at a time and changes them with a displacement, a slide, a crossfade or a zoom.
- [hover-preview](/docs/effects/hover-preview.md): Over a list of projects, the picture of the item under the pointer follows the pointer, stands beside the item or holds a fixed place.
- [lightbox](/docs/effects/lightbox.md): A click opens an image full screen: its copy flies out of the grid, pages and zooms.
- [polaroids](/docs/effects/polaroids.md): Photos scattered like prints on a table: drag one to the top, click it to hold it up large.
- [stack](/docs/effects/stack.md): The images of a container become a pile of cards; a swipe, a click or a key throws the top one off.
- [book](/docs/effects/book.md): A cover image made a 3D book that turns to show its spine and opens on hover.
- [Performance and quality](/docs/guides/performance.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
