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 1 of 7

Put an effect on an image

Put the built-in ripple effect on an image with one attribute, tune it with another and stack a second effect.

View as Markdown
On this page

In this step you put the built-in ripple effect on an image, change its params from markup and add a second effect to the same image. The seven steps of Learn build on each other: by the end you have written three effects, driven a param from code, flown an image between pages and checked the page with reduced motion and on a touch device.

The steps use the setup from Install with Vite: src/main.ts calls start(), and effect files live in src/effects/. Astro and Next.js use the same folder; Nuxt uses app/effects/.

To write one complete effect in a single sitting instead, start with Your own effect in 10 minutes: a light that follows the pointer over a product photo, from an empty file to touch and reduced motion.

Start the app

Call start() once in the module every page loads. It registers the image effects image, ripple and bend and returns the app at once.

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
()

Add the attribute

Put data-dio="ripple" on an image. Give the image its size in width and height, as you would without Diorama: the effect draws over the box the image takes in the layout.

index.html
<img
src="/cover.jpg"
width="800"
height="450"
data-dio="ripple"
/>
Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. A photo of paper ridges with the ripple effect: rings spread from the pointer while it is over the image.

This example failed to draw.

<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="ripple"
/>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('img', 'ripple')
import { start } from '@dioramajs/diorama'
import ripple from '@dioramajs/media/ripple'
const app = start()
app.attach('img', ripple())

Move the pointer over the image. Rings spread from the pointer and settle when it leaves. Scroll the page: the picture stays on its place in the layout.

Change a param

Each param of an effect is an attribute: data-dio- plus the param name. ripple has three params: strength (default 1), frequency (40) and speed (6).

index.html
<img
src="/cover.jpg"
width="800"
height="450"
data-dio="ripple"
data-dio-strength="2"
data-dio-frequency="60"
/>
Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. The same photo with ripple at strength 2 and frequency 60, with sliders for strength, frequency and speed.

This example failed to draw.

Params of ripple
<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="ripple"
data-dio-strength="2"
data-dio-frequency="60"
/>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('img', 'ripple', {
strength: 2,
frequency: 60,
speed: 6,
})
import { start } from '@dioramajs/diorama'
import ripple from '@dioramajs/media/ripple'
const app = start()
app.attach('img', ripple({
strength: 2,
frequency: 60,
speed: 6,
}))

The rings are now twice as deep and closer together. Change the attribute in the browser’s DevTools and the image follows on the next frame: Diorama watches the attributes of every element with data-dio.

Stack a second effect

data-dio takes several names separated by spaces. Add bend, which curves the image and splits its colors while the page scrolls.

index.html
<img
src="/cover.jpg"
width="800"
height="450"
data-dio="ripple bend"
data-dio-ripple-strength="2"
data-dio-split="0.5"
/>
Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. The photo with ripple and bend: rings under the pointer, a curve and a color split while the page scrolls fast.

This example failed to draw.

Hover for the rings, then scroll the page fast for the bend and the color split.
<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="ripple bend"
data-dio-ripple-strength="2"
data-dio-split="0.5"
/>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('img', 'ripple')
import { start } from '@dioramajs/diorama'
import ripple from '@dioramajs/media/ripple'
const app = start()
app.attach('img', ripple())

Both ripple and bend have a strength param, so the short data-dio-strength would be ambiguous and Diorama ignores it with a warning in development. Name the effect in the attribute instead: data-dio-ripple-strength. split belongs to bend only, so its short form works.

Why it works

start() puts one canvas over the page. An element with data-dio gets a layer on that canvas: a copy of its picture that follows the element’s box on every drawn frame. Once the copy is drawn, Diorama hides the <img> itself, so you see one image, now drawn by the engine. Until then, and wherever the engine is unavailable, the page shows the plain image.

The engine is three.js and the effect code. Diorama downloads them in idle time after the page loads and starts them when the first element with data-dio comes within one viewport of the screen.