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

Server rendering

Render effect markup on the server, start Diorama in the browser after hydration, and keep the server free of app state.

View as Markdown
On this page

Effects are attributes, so a server renders them like any other HTML. The app lives in the browser only: it measures the DOM, draws on a canvas and loads three.js there. This page covers what runs where with Next.js, Nuxt, SvelteKit, Astro and other server renderers, static generation included.

Render the markup on the server

data-dio and its params are plain attributes. A server component, a Nuxt page or an Astro template writes them without any Diorama code on the server.

src/app/page.tsx
export default function Page() {
return (
<img
src="/cover.jpg"
width={800}
height={450}
data-dio="ripple"
/>
)
}

Until the app starts, the element is ordinary HTML, so the page reads and lays out the same before the engine arrives and on devices that never load it. When the canvas copy is ready, it takes the element’s exact box, and the DOM picture is hidden only from that frame on.

Do not render data-dio-state on the server or set it in templates. The runtime writes it and fixes values it did not write, such as one copied from a cached page.

Start in the browser

Start the app in code that runs only in the browser, after hydration.

FrameworkWhere to call start()
Next.jsuseEffect of a client component rendered by the root layout
NuxtonNuxtReady in a client plugin, app/plugins/diorama.client.ts
SvelteKitonMount in the root +layout.svelte
AstroA <script> in the shared layout

Importing @dioramajs/diorama on the server is safe: it reads no window or document at import and starts no timers, and effect modules load in the browser only. Calling start() is what to keep out of server code. In Node the app is inert, but the module keeps it for the life of the server process, and server code has nothing to draw.

Starting before hydration has a visible cost in React: the runtime may write data-dio-state on a server-rendered element first, and React 19 prints a hydration mismatch warning in development. The node is kept, but useEffect runs after hydration and avoids the warning.

Share the app with components

A module that calls start() at its top level runs on the server as soon as a server-rendered component imports it. Export a function instead, and call it from browser code only: effects, event handlers, client plugins. start() returns the same app on every call, so every caller gets one app with the same options.

src/lib/diorama.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'
/** The page's app. Call it from browser code only. */
export const
const getApp: () => App

The page's app. Call it from browser code only.

getApp
= () =>
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.background?: string | number | undefined

Clear colour: a number or any CSS colour, e.g. '#08080b'. Transparent by default.

background
: '#08080b' })
src/app/diorama.tsx
'use client'
import { useEffect } from 'react'
import { getApp } from '@/lib/diorama'
export function Diorama() {
useEffect(() => void getApp(), [])
return null
}
src/app/hero.tsx
'use client'
import { useEffect, useRef } from 'react'
import { getApp } from '@/lib/diorama'
export function Hero() {
const ref = useRef<HTMLImageElement>(null)
useEffect(
() => getApp().attach(ref.current!, 'brighten', { strength: 2 }),
[],
)
return <img ref={ref} src="/cover.jpg" />
}

Render <Diorama /> once in the root layout. Effects of child components run before the effect of the layout, so Hero may be the first to call getApp(); with the options in one place, that makes no difference.

app/plugins/diorama.client.ts
import { getApp } from '~/lib/diorama'
export default defineNuxtPlugin(() => {
onNuxtReady(() => {
getApp()
})
})

.client.ts plugins run in the browser only, and onNuxtReady waits for hydration. In components, call getApp().attach(...) in onMounted.

src/layouts/Layout.astro
<script>
import { getApp } from '../lib/diorama'
getApp()
</script>

Astro bundles the script and runs it once in the browser, and the app lives across ClientRouter navigations. Islands are React, Vue or Svelte components and follow the other tabs. Markup with data-dio in .astro files needs no island.

Call getApp() in onMount of the root +layout.svelte, and in onMount or an attachment of each component.

src/routes/+layout.svelte
<script lang="ts">
import { onMount } from 'svelte'
import { getApp } from '$lib/diorama'
let { children } = $props()
onMount(() => {
getApp()
})
</script>
{@render children()}

A handle from attach() works at once. Until the app runs, it records params written to it and hands them over.

Know what runs where

CodeOn the serverIn the browser
data-dio and data-dio-* attributesRendered as HTMLRead when the app starts and on every change
import { start } from '@dioramajs/diorama'No side effectsNo side effects
start()Keep it out: an inert app the process holds on toStarts the app; later calls return it
transition(app, swap)Calls swap and returns a skipped transitionFlies keyed elements to their partners
Effect filesNever importedImported when an element with the effect nears the viewport
data-dio-stateNever render itWritten by the runtime

transition() is safe to call before the app runs, without the transitions plugin, after the app is destroyed and in Node: it calls swap once and resolves with the status 'skipped'. A router recipe can call it from shared code.

Next.js

  • Server components write markup only. Keep start() and code that uses the app in client components.
  • next/image passes unknown attributes to the <img> it renders, so data-dio and its params work on it. Add crossOrigin="anonymous" for images from another origin.
  • withDiorama writes src/diorama.gen.ts, the registry of your effects folder with lazy imports, and points both bundlers at it. Commit it. The server build includes the registry, but nothing on the server calls it.
  • App Router navigations swap the page in place. The app and the canvas stay, the effects of the old page are released and those of the new page created.

Static generation

A statically generated page is server rendering at build time, and the same rules apply: markup in the HTML, start() in the browser. Reduced motion, touch, Save-Data and the GPU are all decided in the browser, so one HTML file serves every visitor.