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.
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
<mainclass="box">
<ahref="#cover">
<img
src="/work/aurora-thumb.jpg"
width="160"
height="120"
data-dio="image"
data-dio-key="aurora"
/>
</a>
</main>
<templateid="list">
<ahref="#cover">
<img
src="/work/aurora-thumb.jpg"
width="160"
height="120"
data-dio="image"
data-dio-key="aurora"
/>
</a>
</template>
<templateid="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>
<ahref="#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{
functionstart(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.
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'
exportconst
const app:App
app=
functionstart(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.
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.
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.
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.
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.
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.
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.
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.
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.
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.
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.
<mainclass="box">
<ahref="#cover">
<img
src="/media/prints/night.jpg"
width="160"
height="120"
data-dio="image"
data-dio-key="night"
/>
</a>
</main>
<templateid="list">
<ahref="#cover">
<img
src="/media/prints/night.jpg"
width="160"
height="120"
data-dio="image"
data-dio-key="night"
/>
</a>
</template>
<templateid="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>
<ahref="#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.
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.
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 } }).