# book

A cover image made a 3D book that turns to show its spine and opens on hover.

At rest the book faces the viewer, its cover exactly where the image was, with a soft shadow under it. Under the pointer it swings to show its spine and the front cover opens a little over the cream page edges. A drag turns it all the way round to the back cover.

Module `model`, category 3D and product. Import `@dioramajs/model/book`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/model/src/book.ts)

A hardcover with a deep blue cover of white seaweed fronds; under the pointer it turns to show its spine and its cover opens a little.

```html title="index.html"
<img class="cover" src="/media/books/blue-shore.webp" alt="The Blue Shore by Mara Ellison, a year of cyanotypes" width="900" height="1350" data-dio="book" tabindex="0" style="touch-action: pan-y" />
```

## Usage

Put it on: img: the front cover; the box is the cover, and without WebGL the plain cover image shows.

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

```html
<img src="/books/tidewater.jpg" alt="Tidewater, a novel" width="800" height="1200" data-dio="book" />
```

In page transitions: Leaves by dithering its pixels away while shrinking to 96%, and its shadow fades with it.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'hardcover' \| 'paperback'` | `'hardcover'` | hardcover has thick boards over the pages and a rounded spine; paperback a thin flush cover and a flat spine. |
| `pages` | `data-dio-pages` | `number` 16 to 1600 | `320` | Page count, which sets the thickness. |
| `spine` (rebuilds) | `data-dio-spine` | `string` | `''` | Spine color or image URL; empty takes the color of the left edge of the cover. |
| `back` (rebuilds) | `data-dio-back` | `string` | `''` | Back cover image URL or color; empty uses the spine color. |
| `yaw` | `data-dio-yaw` | `number` -180 to 180° | `0` | Rest turn; positive shows the spine. |
| `swing` | `data-dio-swing` | `number` 0 to 60° | `36` | Turn under the pointer or keyboard focus. |
| `open` | `data-dio-open` | `number` 0 to 60° | `12` | How far the front cover opens on hover. |
| `tilt` | `data-dio-tilt` | `number` 0 to 25° | `5.7` | How far it leans toward the pointer. |
| `drag` | `data-dio-drag` | `boolean` | `true` | Turns it under a drag, and with the arrow keys when it has a tabindex. |
| `shadow` | `data-dio-shadow` | `number` 0 to 1 | `0.5` | Density of the shadow on the page. |

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

### Variants

- `style`: `hardcover`, `paperback`
  - `hardcover` has thick boards over the pages and a rounded spine.
  - `paperback` a thin flush cover and a flat spine.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`presence`](/docs/api/inputs#presence), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`width`](/docs/api/inputs#input-width). Draws only while something on it moves, then stops. Only while it swings, opens, leans, coasts after a drag or eases back; none at rest, and pointer moves elsewhere draw nothing. 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. It rests facing the viewer: no swing, opening or lean. Dragging and arrow keys still turn it, without coasting.

Runs on every device. Nothing hovers. A finger turns the book only when the element has `touch-action: pan-y` or `none`, otherwise the gesture scrolls the page.

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

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

## Cost and quality

Loads 4.8 KB the first time a page uses it. GPU: About two thousand triangles with a simple light, no environment map. Memory: The cover, and the spine and back images when given.

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

## Recipes

### A paperback with its own spine and back

`spine` and `back` take an image URL or a CSS color. A spine image reads top to bottom, as on a shelf; make it as wide as the book is thick, about 0.06 of the cover height for 240 pages. Without them the spine takes the color of the cover’s left edge, and the back takes the spine color.

A cream paperback with a blue cyanotype, turned to show a spine with its title; a drag shows the back cover with a blurb.

```html title="index.html"
<img class="cover" src="/media/books/low-water.webp" alt="Low Water by Ines Calder, notes from the tide line" width="900" height="1350" data-dio="book" data-dio-style="paperback" data-dio-pages="240" data-dio-spine="/media/books/low-water-spine.webp" data-dio-back="/media/books/low-water-back.webp" data-dio-yaw="30" tabindex="0" style="touch-action: pan-y" />
```

### A shelf of covers

Books in a grid each react on their own, and a book under the pointer rises above its neighbors. Lower `swing` and `open` when covers sit close together. Wrap each cover in a link to its page: the link keeps its focus ring, and keyboard focus turns the book as hover does.

Three books in a row, a blue hardcover, a cream paperback and a brown hardcover, each turning a little toward its spine under the pointer.

```html title="index.html"
<ul class="shelf">
  <li><a href="/blue-shore"><img src="/media/books/blue-shore.webp" alt="The Blue Shore" width="900" height="1350" data-dio="book" data-dio-pages="280" data-dio-swing="20" data-dio-open="8" /></a></li>
  <li><a href="/low-water"><img src="/media/books/low-water.webp" alt="Low Water" width="900" height="1350" data-dio="book" data-dio-style="paperback" data-dio-pages="240" data-dio-spine="/media/books/low-water-spine.webp" data-dio-swing="20" data-dio-open="8" /></a></li>
  <li><a href="/salt-garden"><img src="/media/books/salt-garden.webp" alt="Salt Garden" width="900" height="1350" data-dio="book" data-dio-pages="420" data-dio-swing="20" data-dio-open="8" /></a></li>
</ul>
```

### A course workbook

`yaw` sets the rest pose. A book at rest turned 20 degrees shows its spine and thickness without any hover, which suits a course or ebook landing page where the book is a picture of the product.

```html
<img
  src="/course/workbook.png"
  width="900"
  height="1350"
  data-dio="book"
  data-dio-style="paperback"
  data-dio-pages="120"
  data-dio-spine="#1d3b5a"
  data-dio-yaw="20"
/>
```

## Notes

At rest the cover is drawn exactly where the image is, at its size, so nothing jumps when the engine takes over. Without WebGL the plain cover image stays.

The book draws only while it moves: a hover, a drag, the lean or the easing back. The paper edges, the boards and the spine are built in code, so a book costs one cover image and nothing else to download.

Give the image the aspect ratio of the cover. The thickness comes from `pages`, the way a printer counts them: 320 pages of a 6 × 9 inch book are about 16 mm thick.

## Related

- [box](/docs/effects/box.md): A product box built from pictures of its faces: turn it, tilt it, open its lid.
- [coin](/docs/effects/coin.md): A coin, medal or token struck from a logo or a photo: it turns, catches the light and flips over.
- [device](/docs/effects/device.md): A screenshot or a video shown in a 3D phone, tablet, laptop or browser window.
- [explode](/docs/effects/explode.md): An exploded view of a model: its parts move apart as the section scrolls.
- [hotspots](/docs/effects/hotspots.md): Feature callouts pinned to points of a model, real buttons that follow it as it turns.
- [label](/docs/effects/label.md): A flat label wrapped around a can, a bottle or a jar that turns to show all of it.
- [model](/docs/effects/model.md): A glTF model fitted into the element, lit, turnable and recolored from markup.
- [panorama](/docs/effects/panorama.md): A 360° photo in a box that turns by drag, keys and gyroscope, with hotspots.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
