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

stack

Galleries and viewersAlso inPhoto

@dioramajs/media/stackSource

The images of a container become a pile of cards; a swipe, a click or a key throws the top one off.

View as Markdown
On this page
The headland: a grassy point dropping into the sea under a pale sky The boulder beach: rounded stones at the edge of the water The islets: dark rocks in a blue bay, a flat-topped mountain in the haze The point break: surf over brown rocks at the foot of the cliffs The cliff path: a green slope falling to the sea The white shore: pale rocks and low bushes above a grey sea

Live preview needs WebGPU or WebGL 2. Six places on the coast as a pile of photos; drag the top one away, click it or use the arrow keys, and the next comes forward.

This example failed to draw.

The top card covers the box, framed like its image, and the next cards lie under it with a soft shadow: scattered at small angles, fanned about a point under the box, or stepped back and smaller. The top card follows a drag and tilts with it; let go far or fast enough, it flies off the way it was thrown while the pile moves up a place, otherwise it springs back. A click flicks it away from the side that was clicked, and with tabindex on the container the arrows bring the next or the previous card. The images stay in the page in order with their alt text; without WebGL the container is the layout you wrote.

The headland

Params of stack
style

pile scatters the cards.

Usage

Put it on a container of images the size of one card. start() registers stack, 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 stack
<section
class="trips"
data-dio="stack"
tabindex="0"
aria-label="Six places on the coast. Click, swipe or use the arrow keys for the next one."
style="touch-action: none"
>
<img
src="/media/coast/headland-800.avif"
width="800"
height="534"
/>
<img
src="/media/coast/boulders-600.avif"
width="600"
height="750"
/>
<img
src="/media/coast/islets-600.avif"
width="600"
height="600"
/>
<img
src="/media/coast/surf-800.avif"
width="800"
height="534"
/>
<img
src="/media/coast/slope-800.avif"
width="800"
height="534"
/>
<img
src="/media/coast/shore-800.avif"
width="800"
height="534"
/>
</section>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.trips', 'stack', {
style: 'pile',
rotate: 6,
offset: 12,
duration: 0.6,
shadow: 0.35,
loop: true,
swipe: true,
click: true,
})
import { start } from '@dioramajs/diorama'
import stack from '@dioramajs/media/stack'
const app = start()
app.attach('.trips', stack({
style: 'pile',
rotate: 6,
offset: 12,
duration: 0.6,
shadow: 0.35,
loop: true,
swipe: true,
click: true,
}))

Events

It dispatches these on its element; they bubble.

  • dio:index, detail: { index: number }. A card is on top, after a throw or a change of index; index is its place among the images, from 0. Bubbles.

Page transitions

Fades with the page transition. 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 100Card on top from 0; a fraction holds the top card that far in flight.
stylerebuildsdata-dio-style'pile' | 'fan' | 'cascade''pile'
  • pile scatters the cards.
  • fan opens them to both sides.
  • cascade steps each one back.
rotatedata-dio-rotatenumber0 to 30°6Largest angle of a card under the top one; pile and fan only.
offsetdata-dio-offsetnumber0 to 60 px12Scatter of the pile, step of the cascade, depth of the fan, in px.
durationdata-dio-durationnumber0.2 to 2 s0.6Length of a throw by click or key.
shadowdata-dio-shadownumber0 to 10.35Darkness of the shadow under each card, 0 for none.
loopdata-dio-loopbooleantrueA thrown card goes under the pile; off, the last card stays on top.
swipedata-dio-swipebooleantrueDragging or swiping the top card throws it.
clickdata-dio-clickbooleantrueA click on the pile throws the top card.
itemsrebuildsdata-dio-itemsstring'img'Selector of the images inside the container.
seeddata-dio-seednumber1 to 1001Arrangement of the scattered pile.

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 card is in flight or held; 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. Cards change at once: no flight and no card following the finger, while a swipe, a click or a key still brings the next one.

Runs on every device. A tap throws the top card. Dragging it needs touch-action: pan-y or none on the container, so the page keeps its own gestures otherwise.

To change what reduced motion does, patch it in start().

import { start } from '@dioramajs/diorama'
start({ effects: { stack: { reducedMotion: 'off' } } })

Cost and quality

Download
5.1 KB min+gzip the first time a page uses it
GPU
Up to five card quads with a picture and a shadow each, one texture read per pixel.
Memory
Up to six pictures at a time: the top card, the pile under it and the card thrown last.
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 fan of projects that stops at the last one

The fan style opens the cards under the top one to both sides, like a hand of cards, for a short list such as featured work or this week’s picks. With loop set to false the last card stays on top: a drag only nudges it and a click does nothing.

Cyanotype: prints of seaweed in deep blue Atlas of Ice: maps of the polar shelves Hare’s Fur: glazed tea bowls Polar Night: photographs of the winter dark

Live preview needs WebGPU or WebGL 2. Four project covers fanned out; each click throws the top one off until the last.

This example failed to draw.

Params of stack
<section
class="covers"
data-dio="stack"
data-dio-style="fan"
data-dio-loop="false"
data-dio-rotate="7"
tabindex="0"
aria-label="Four projects. Click or use the arrow keys for the next one."
style="touch-action: pan-y"
>
<img
src="/media/work/cyanotype.webp"
width="480"
height="384"
/>
<img
src="/media/work/atlas-of-ice.webp"
width="480"
height="384"
/>
<img
src="/media/work/hares-fur.webp"
width="480"
height="384"
/>
<img
src="/media/work/polar-night.webp"
width="480"
height="384"
/>
</section>

Name the card on top

Nothing in the page tells a screen reader that another card came to the top. Put a caption with aria-live="polite" next to the pile and fill it from dio:index, whose index is the place of the card on top, from 0. The example at the top of this page names the place this way.

<section
class="trips"
data-dio="stack"
tabindex="0"
aria-label="Places on the coast. Click, swipe or use the arrow keys for the next one."
style="touch-action: none"
>
<img src="/media/coast/headland-800.avif" />
<img src="/media/coast/islets-600.avif" />
</section>
<p class="place" aria-live="polite">The headland</p>
const trips = document.querySelector('.trips')
const place = document.querySelector('.place')
const cards = [...trips.querySelectorAll('img')]
trips.addEventListener('dio:index', (e) => {
place.textContent = cards[e.detail.index].alt.split(':')[0]
})

Buttons for the next and the previous card

A pile reads as something to swipe, but a button says it plainly and works for everyone. Set index on the instance: one more throws the top card off, one less brings the last thrown card back onto the pile.

const trips = document.querySelector('.trips')
document.querySelector('.next').addEventListener('click', () => {
const stack = app.effects.get(trips, 'stack')
stack?.set({ index: Math.round(stack.params.index) + 1 })
})

Notes

The container is the size of one card: the top card covers its box, and the cards under it, their shadows and a thrown card reach past it. Leave some room around the box, about offset times three plus the corners a turned card shows, and keep captions and buttons outside it. A thrown card is cut by an ancestor with overflow: hidden, as a CSS transform would be.

Each card takes the picture of its image cut to the box, at the image’s own object-position; object-fit: contain on an image keeps it whole on a plain card. The corners follow the container’s border-radius.

The images stay in the page in their order, each with its alt text, so a screen reader reaches all of them, and without WebGL the container is the strip you styled. With tabindex="0" on the container the pile takes focus and changes with the arrow keys, PageUp, PageDown, Home and End; give it an aria-label that says what is in it and how to move on.

A drag with a mouse always carries the card. On touch screens it needs touch-action on the container: none lets the card go any way, pan-y keeps the page scrolling up and down while sideways drags carry the card.

style and items rebuild the effect when they change. Set them in the markup rather than switching them while the pile is on screen.