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

book

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

View as Markdown
On this page
The Blue Shore by Mara Ellison, a year of cyanotypes

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.

Move the pointer over the book, drag it to see the back, or focus it and press the arrow keys. Raise pages for a thicker book.
Params of book
style

hardcover has thick boards over the pages and a rounded spine.

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.

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

ParamTypeDefaultDescription
stylerebuildsdata-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.
pagesdata-dio-pagesnumber16 to 1600320Page count, which sets the thickness.
spinerebuildsdata-dio-spinestring''Spine color or image URL; empty takes the color of the left edge of the cover.
backrebuildsdata-dio-backstring''Back cover image URL or color; empty uses the spine color.
yawdata-dio-yawnumber-180 to 180°0Rest turn; positive shows the spine.
swingdata-dio-swingnumber0 to 60°36Turn under the pointer or keyboard focus.
opendata-dio-opennumber0 to 60°12How far the front cover opens on hover.
tiltdata-dio-tiltnumber0 to 25°5.7How far it leans toward the pointer.
dragdata-dio-dragbooleantrueTurns it under a drag, and with the arrow keys when it has a tabindex.
shadowdata-dio-shadownumber0 to 10.5Density 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.

Low Water by Ines Calder, notes from the tide line

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.

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

  • The Blue Shore
  • Low Water
  • Salt Garden

Live preview needs WebGPU or WebGL 2. 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.

This example failed to draw.

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