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

Step 6 of 7

Fly between pages

Give a thumbnail and a cover the same data-dio-key and swap the page inside transition(), so the thumbnail flies into the cover.

View as Markdown
On this page

In this step a thumbnail flies into a cover when the page changes. The two pages live in one box: a list with the thumbnail and a project page with the cover. A click swaps the contents of the box inside transition(), the same way a router swaps the page.

Mark both images with a key

Put the same data-dio-key on the thumbnail and on the cover. Both need an effect, so they are drawn on the canvas: image draws the picture as it is.

index.html
<main class="box">
<a href="#cover">
<img
src="/work/aurora-thumb.jpg"
width="160"
height="120"
data-dio="image"
data-dio-key="aurora"
/>
</a>
</main>
<template id="list">
<a href="#cover">
<img
src="/work/aurora-thumb.jpg"
width="160"
height="120"
data-dio="image"
data-dio-key="aurora"
/>
</a>
</template>
<template id="cover">
<figure>
<img
src="/work/aurora.jpg"
width="640"
height="400"
data-dio="image ripple"
data-dio-key="aurora"
/>
<figcaption>Aurora, paper and light</figcaption>
</figure>
<a href="#list">Back to the list</a>
</template>

The <template> elements hold the two pages. Their contents are not part of the document, so Diorama does not draw them until they are copied into the box.

Swap inside transition()

Catch clicks on links in the box, find the template the link points at and put a copy of it in the box. Wrap the swap in transition().

src/main.ts
import {
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
,
function transition(
app: { readonly transition?: TransitionApi | undefined; },
swap: Swap,
options?: TransitionOptions
): Transition

Calls app.transition if the plugin is running, otherwise runs swap once and returns a settled 'skipped' transition. Safe for routers that navigate before app.start() resolves.

transition
} from '@dioramajs/diorama'
export const
const app: App
app
=
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
()
const
const box: Element | null
box
=
var document: Document

window.document returns a reference to the document contained in the window.

MDN Reference

document
.
ParentNode.querySelector<Element>(selectors: string): Element
| null (+4 overloads)

Returns the first element that is a descendant of node that matches selectors.

MDN Reference

querySelector
('.box')
if (
const box: Element | null
box
) {
const box: Element
box
.
Element.addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions
): void (+1 overload)

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

MDN Reference

addEventListener
('click', (
event: Event
event
) => {
const
const link: HTMLAnchorElement | null
link
=
event: Event
event
.
Event.target: EventTarget | null

The read-only target property of the Event interface is a reference to the object onto which the event was dispatched. It is different from Event.currentTarget when the event handler is called during the bubbling or capturing phase of the event.

MDN Reference

target
instanceof
var Element: {
new (): Element;
prototype: Element;
}

Element is the most general base class from which all element objects (i.e., objects that represent elements) in a Document inherit. It only has methods and properties common to all kinds of elements. More specific classes inherit from Element.

MDN Reference

Element
?
event: Event
event
.
Event.target: Element

The read-only target property of the Event interface is a reference to the object onto which the event was dispatched. It is different from Event.currentTarget when the event handler is called during the bubbling or capturing phase of the event.

MDN Reference

target
.
Element.closest<"a">(selector: "a"): HTMLAnchorElement
| null (+3 overloads)

The closest() method of the Element interface traverses the element and its parents (heading toward the document root) until it finds a node that matches the specified CSS selector.

MDN Reference

closest
('a') : null
const
const page: Element | null
page
=
const link: HTMLAnchorElement | null
link
&&
var document: Document

window.document returns a reference to the document contained in the window.

MDN Reference

document
.
ParentNode.querySelector<Element>(selectors: string): Element
| null (+4 overloads)

Returns the first element that is a descendant of node that matches selectors.

MDN Reference

querySelector
(`template${
const link: HTMLAnchorElement
link
.
HTMLHyperlinkElementUtils.hash: string

Returns the hyperlink's URL's fragment (includes leading "#" if non-empty).

Can be set, to change the URL's fragment (ignores leading "#").

MDN Reference

hash
}`)
if (!(
const page: Element | null
page
instanceof
var HTMLTemplateElement: {
new (): HTMLTemplateElement;
prototype: HTMLTemplateElement;
}

The HTMLTemplateElement interface enables access to the contents of an HTML element.

MDN Reference

HTMLTemplateElement
)) return
event: Event
event
.
Event.preventDefault(): void

The preventDefault() method of the Event interface tells the user agent that the event is being explicitly handled, so its default action, such as page scrolling, link navigation, or pasting text, should not be taken.

MDN Reference

preventDefault
()
function transition(
app: { readonly transition?: TransitionApi | undefined; },
swap: Swap,
options?: TransitionOptions
): Transition

Calls app.transition if the plugin is running, otherwise runs swap once and returns a settled 'skipped' transition. Safe for routers that navigate before app.start() resolves.

transition
(
const app: App
app
, () =>
const box: Element
box
.
ParentNode.replaceChildren(
...nodes: (Node | string)[]
): void

Replace all children of node with nodes, while replacing strings in nodes with equivalent Text nodes.

Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.

MDN Reference

replaceChildren
(
const page: HTMLTemplateElement
page
.
HTMLTemplateElement.content: DocumentFragment

The content property of the HTMLTemplateElement interface returns the element's template contents as a DocumentFragment. This content's ownerDocument is a separate Document from the one that contains the element itself — unless the containing document is itself constructed for the purpose of holding template content.

MDN Reference

content
.
Node.cloneNode(subtree?: boolean): Node

The cloneNode() method of the Node interface returns a duplicate of the node on which this method was called. Its parameter controls if the subtree contained in the node is also cloned or not.

MDN Reference

cloneNode
(true)),
)
})
}

Live preview needs WebGPU or WebGL 2. A box with a thumbnail that flies into a cover with its caption on click, and back into the thumbnail on Back to the list.

This example failed to draw.

<main class="box">
<a href="#cover">
<img
src="/media/prints/night.jpg"
width="160"
height="120"
data-dio="image"
data-dio-key="night"
/>
</a>
</main>
<template id="list">
<a href="#cover">
<img
src="/media/prints/night.jpg"
width="160"
height="120"
data-dio="image"
data-dio-key="night"
/>
</a>
</template>
<template id="cover">
<figure>
<img
src="/media/prints/night.jpg"
width="640"
height="400"
data-dio="image ripple"
data-dio-key="night"
/>
<figcaption>Night, paper and light</figcaption>
</figure>
<a href="#list">Back to the list</a>
</template>
import { transition } from '@dioramajs/diorama'
import { app } from '../../docs/diorama.ts'
// src/main.ts of this step, with the box and templates looked up inside the example.
export default (example: HTMLElement) => {
const box = example.querySelector('.box')
if (!box) return
const onClick = (event: Event) => {
const link =
event.target instanceof Element ? event.target.closest('a') : null
const page = link && example.querySelector(`template${link.hash}`)
if (!(page instanceof HTMLTemplateElement)) return
event.preventDefault()
transition(app, () =>
box.replaceChildren(page.content.cloneNode(true)),
)
}
box.addEventListener('click', onClick)
return () => box.removeEventListener('click', onClick)
}

Click the thumbnail. It grows into the cover over 0.8 seconds, changing its crop on the way, and the caption appears around it. Click Back and the cover flies back into the thumbnail.

Hold the caption until the landing

While the thumbnail flies, the cover’s element waits under it with data-dio-state="entering". Use it in CSS to keep the caption hidden until the landing.

src/style.css
figure:has([data-dio-state="entering"]) figcaption {
opacity: 0;
}
figcaption {
transition: opacity 0.3s;
}

Click the thumbnail again: the caption now fades in after the cover lands.

Why it works

transition() takes a snapshot of every drawn element with a key before the swap and runs the swap. After it, it looks for elements with the same keys in the new page. Each pair becomes a flight: the copy of the old element moves, resizes and recrops into the box of the new one, and hands over to the new element’s own copy on the way. The canvas stays the same across the swap, so the same picture keeps moving while the DOM under it changes.

Diorama also watches the DOM and pairs keyed elements that leave and arrive without transition(). That is how keyed elements fly with Astro’s <ClientRouter />, Nuxt and Next.js without your code. transition() makes the change explicit: the snapshot is taken before the swap, a swap that returns a promise (a fetch of the next page) is awaited, and the call returns the transition, so await transition(app, swap) resolves once every flight has landed.

Change the flight for every transition in start(), for example start({ transitions: { duration: 1.2 } }).