book
A cover image made a 3D book that turns to show its spine and opens on hover.
Live preview needs WebGPU or WebGL 2. 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.
This example failed to draw.
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. 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 book<img class="cover" src="/media/books/blue-shore.webp" width="900" height="1350" data-dio="book" tabindex="0" style="touch-action: pan-y"/>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.cover', 'book', { style: 'hardcover', pages: 320, yaw: 0, swing: 36, open: 12, tilt: 5.7, drag: true, shadow: 0.5,})import { start } from '@dioramajs/diorama'import book from '@dioramajs/model/book'
const app = start()app.attach('.cover', book({ style: 'hardcover', pages: 320, yaw: 0, swing: 36, open: 12, tilt: 5.7, drag: true, shadow: 0.5,}))Page transitions
Leaves by dithering its pixels away while shrinking to 96%, and its shadow fades with it. 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 | 'hardcover' | 'paperback' | 'hardcover' |
|
pagesdata-dio-pages | number16 to 1600 | 320 | Page count, which sets the thickness. |
spinerebuildsdata-dio-spine | string | '' | Spine color or image URL; empty takes the color of the left edge of the cover. |
backrebuildsdata-dio-back | string | '' | Back cover image URL or color; empty uses the spine color. |
yawdata-dio-yaw | number-180 to 180° | 0 | Rest turn; positive shows the spine. |
swingdata-dio-swing | number0 to 60° | 36 | Turn under the pointer or keyboard focus. |
opendata-dio-open | number0 to 60° | 12 | How far the front cover opens on hover. |
tiltdata-dio-tilt | number0 to 25° | 5.7 | How far it leans toward the pointer. |
dragdata-dio-drag | boolean | true | Turns it under a drag, and with the arrow keys when it has a tabindex. |
shadowdata-dio-shadow | number0 to 1 | 0.5 | Density of the shadow on the page. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta, height, opacity, presence, reducedMotion and 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. 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. 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.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { book: { reducedMotion: 'off' } } })Cost and quality
- Download
- 4.8 KB min+gzip 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.
- Frames
- Idle at rest
It runs from the low tier up. Every tier draws it the same way. The quality plugin picks the tier for each device.
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.
Live preview needs WebGPU or WebGL 2. A cream paperback with a blue cyanotype, turned to show a spine with its title; a drag shows the back cover with a blurb.
This example failed to draw.
<img class="cover" src="/media/books/low-water.webp" 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.
<ul class="shelf"> <li> <a href="/blue-shore"> <img src="/media/books/blue-shore.webp" 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" 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" 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.
<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.
