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

Your first page transition

Give two elements on two pages the same data-dio-key and the first one flies into the second on navigation.

View as Markdown
On this page

A flight moves the layer of an element from one page to the element with the same key on the next page. The canvas stays alive between pages, so the same picture moves, resizes and recrops on its way.

Give both elements a key

Put the same data-dio-key on the thumbnail in the list and on the cover of the project page.

work.html
<a href="/work/aurora">
<img
src="/img/aurora-thumb.avif"
data-dio="image"
data-dio-key="aurora"
/>
</a>
work/aurora.html
<img
src="/img/aurora-cover.avif"
data-dio="image ripple"
data-dio-key="aurora"
/>

A flight needs the app to survive navigation, so the page must change without a full reload.

Wrap your own swap of the page in transition(). It takes a snapshot of the keyed elements, runs the swap and flies them to their partners.

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
()
async function
function go(url: string): Promise<void>
go
(
url: string
url
: string):
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<void> {
const
const html: string
html
= await (await
function fetch(
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response>
fetch
(
url: string
url
)).
Body.text(): Promise<string>
text
()
const
const next: Document
next
= new
var DOMParser: new () => DOMParser

The DOMParser interface provides the ability to parse XML or HTML source code from a string into a DOM Document.

MDN Reference

DOMParser
().
DOMParser.parseFromString(
string: string,
type: DOMParserSupportedType
): Document

The parseFromString() method of the DOMParser interface parses an input containing either HTML or XML, returning a Document with the type given in the contentType property.

MDN Reference

parseFromString
(
const html: string
html
, 'text/html')
await
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
, () => {
var document: Document

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

MDN Reference

document
.
ParentNode.querySelector<"main">(selectors: "main"): HTMLElement
| null (+4 overloads)

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

MDN Reference

querySelector
('main')
?.
ChildNode.replaceWith(
...nodes: (Node | string)[]
): void

Replaces 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

replaceWith
(
const next: Document
next
.
ParentNode.querySelector<"main">(selectors: "main"): HTMLElement
| null (+4 overloads)

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

MDN Reference

querySelector
('main') ?? '')
var history: History

The Window.history read-only property returns a reference to the History object, which provides an interface for manipulating the browser session history (pages visited in the tab or frame that the current page is loaded in).

MDN Reference

history
.
History.pushState(
data: any,
unused: string,
url?: string | URL | null
): void

The pushState() method of the History interface adds an entry to the browser's session history stack.

MDN Reference

pushState
(null, '',
url: string
url
)
})
}
var document: Document

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

MDN Reference

document
.
Document.addEventListener<"click">(
type: "click",
listener: (this: Document, ev: PointerEvent) => any,
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: PointerEvent
event
) => {
const
const link: HTMLAnchorElement | null
link
= (
event: PointerEvent
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
as
interface 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
).
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')
if (!
const link: HTMLAnchorElement | null
link
||
const link: HTMLAnchorElement
link
.
HTMLHyperlinkElementUtils.origin: string

Returns the hyperlink's URL's origin.

MDN Reference

origin
!==
var location: Location

The read-only location property of the Window interface returns a Location object with information about the current location of the document.

MDN Reference

location
.
Location.origin: string

The origin read-only property of the Location interface returns a string containing the Unicode serialization of the origin of the location's URL.

MDN Reference

origin
) return
event: PointerEvent
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
()
void
function go(url: string): Promise<void>
go
(
const link: HTMLAnchorElement
link
.
HTMLHyperlinkElementUtils.href: string

Returns the hyperlink's URL.

Can be set, to change the URL.

MDN Reference

href
)
})

Add <ClientRouter /> to the layout. Astro swaps the page in place and the keyed elements fly without more code.

src/layouts/Layout.astro
---
import { ClientRouter } from 'astro:transitions'
---
<html lang="en">
<head>
<ClientRouter />
</head>
<body>
<slot />
</body>
</html>

<NuxtPage> without a page transition swaps pages in one render. The keyed elements fly without more code.

The App Router removes the old page and inserts the new one in one commit. The keyed elements fly without more code. With loading.tsx the cover arrives later and the thumbnail waits for it, invisible, for up to 1 second.

The CDN build exports transition() too. Wrap your own swap of the page in it, the same way as with Vite. Turbo Drive swaps the page itself, and keyed elements fly without code.

Tune the flight

A flight takes 0.8 seconds with a cubic in-out ease. Change the defaults for every transition in start().

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
} 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
({
StartOptions.transitions?:
| boolean
| (
Partial<TransitionsOptions>
& { readonly cover?: CoverName | CoverOptions; }
)
| undefined

Page transitions by data-dio-key. On by default, false turns them off. cover adds a GL cover the pages change behind: { cover: 'ink' } or { cover: { name: 'curtain', color } }.

transitions
: {
duration?: number | undefined

Seconds. Default 0.8.

duration
: 0.9 } })

While a thumbnail flies, the DOM of its target is hidden with data-dio-state="entering". Use it to hold back a caption until the landing.

figure:has([data-dio-state="entering"]) figcaption {
opacity: 0;
}

Pass transitions: false to start() to turn page transitions off.