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

spotlight

PhotoAlso inVideo

@dioramajs/media/spotlightSource

A flashlight over a photo: the image darkens around a soft circle at the pointer.

View as Markdown
On this page
A brass astrolabe, its rete of star pointers over a plate engraved with the signs of the zodiac

Live preview needs WebGPU or WebGL 2. A brass astrolabe on black, dimmed at rest, with a soft circle of light that follows the pointer across it.

This example failed to draw.

Move the pointer over the picture, or press and drag with a finger. The light trails the pointer and closes when it leaves.
Params of spotlight

Usage

Put it on img, video. start() registers spotlight, so the attribute alone puts it on an element. The tabs below are the example above, with the values you set.

To change its code, copy the source into your effects folder; the copy replaces the built-in.

Terminal window
npx @dioramajs/diorama add spotlight
<figure class="lit">
<img
src="/media/portfolio/astrolabe.webp"
width="1400"
height="933"
data-dio="spotlight"
data-dio-radius="150"
data-dio-rest="0.55"
style="touch-action: none"
/>
</figure>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('img', 'spotlight', {
radius: 150,
softness: 0.5,
dim: 0.8,
rest: 0.55,
ease: 0.08,
duration: 0.3,
})
import { start } from '@dioramajs/diorama'
import spotlight from '@dioramajs/media/spotlight'
const app = start()
app.attach('img', spotlight({
radius: 150,
softness: 0.5,
dim: 0.8,
rest: 0.55,
ease: 0.08,
duration: 0.3,
}))

Page transitions

Fades with the page transition; a flight carries the image without the light. How a flight flies.

Params

Every param is also an attribute, and both app.attach() and the handle take it by name.

ParamTypeDefaultDescription
stylerebuildsdata-dio-style'light' | 'reveal' | 'blur''light'
  • light darkens the rest.
  • reveal shows the src picture in the circle.
  • blur blurs the rest too.
radiusdata-dio-radiusnumber20 to 600 px140Radius of the lit circle.
softnessdata-dio-softnessnumber0 to 10.5Share of the radius that fades from light to dark: 0 is a hard edge.
dimdata-dio-dimnumber0 to 10.8How far the image outside the light goes toward color: 0 keeps it as is.
colordata-dio-colorcolor'#000000'Shade the outside fades toward, black by default.
blurdata-dio-blurnumber0 to 40 px12Blur radius outside the light in blur style.At most low 8 by quality tier.
restdata-dio-restnumber0 to 10Share of dim kept while the pointer is away: 0 shows the full image at rest.
easedata-dio-easenumber0 to 0.5 s0.08Smoothing of the light following the pointer: 0 sticks to it.
durationdata-dio-durationnumber0 to 1.5 s0.3Length of the fade as the light comes on and goes off.
srcrebuildsdata-dio-srcstring''URL of the picture the circle shows in reveal style.

Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.

Inputs and frames

It reads delta, height, presence, reducedMotion, size, x and y . Draws only while something on it moves, then stops. Only while the light fades or trails the pointer; none at rest, and a still pointer draws nothing. How a frame is decided.

Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. No fade and no smoothing: the light appears, jumps and goes at once.

Runs on every device. The light follows a finger while it is pressed and goes when it lifts. Add touch-action: none to the image to keep a drag from scrolling the page.

To change what reduced motion does, patch it in start().

import { start } from '@dioramajs/diorama'
start({ effects: { spotlight: { reducedMotion: 'off' } } })

Cost and quality

Download
1.7 KB min+gzip the first time a page uses it
GPU
One texture read per pixel; 17 in blur style, 2 in reveal style.
Memory
The image, plus the second picture in reveal style.
Frames
Idle at rest

It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.

TierWhat changes
lowBlur radius at most 8 px.

Recipes

A collection piece seen by flashlight

For a museum, an auction lot or a product with fine detail, keep part of the dim at rest with rest, so the picture reads as “in the dark” before the visitor reaches for it. A larger radius suits a large photo; softness near 1 gives a torch, near 0 a hard theater spot.

<img
src="/collection/astrolabe.avif"
data-dio="spotlight"
data-dio-radius="160"
data-dio-rest="0.6"
/>

An x-ray under the pointer

style="reveal" shows a second picture inside the circle: the movement behind a watch face, a building before and after renovation, a plan over a photo. Both pictures should share their framing, since the second one is drawn at the same object-fit and position as the first. With dim="0" the rest of the image stays as it is.

<img
src="/watch/front.avif"
data-dio="spotlight"
data-dio-style="reveal"
data-dio-src="/watch/movement.avif"
data-dio-dim="0"
style="touch-action: none"
/>

Find the detail

style="blur" blurs everything outside the light as well, which turns a busy interior or a map into a small game of looking.

<img
src="/rooms/loft.avif"
data-dio="spotlight"
data-dio-style="blur"
data-dio-dim="0.3"
data-dio-blur="16"
/>

Notes

The light only draws while it fades or trails the pointer; a still pointer costs nothing. On touch screens it follows a pressed finger; add touch-action: none to the image when a drag should move the light instead of scrolling the page.

The picture stays a plain <img>, so its alt text is all a screen reader needs. Under reduced motion the light appears and moves without fades or smoothing.

style and src rebuild the effect; every other param changes live.