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

ambient

VideoAlso inPhoto

@dioramajs/media/ambientSource

A soft glow in the colors of the frame around a video or an image, like ambient mode.

View as Markdown
On this page

Live preview needs WebGPU or WebGL 2. A short product film of a lounge chair on a dark page, with a soft glow in the film's colors around it.

This example failed to draw.

Around the box lies a large, heavily blurred copy of the frame’s main colors that fades out past the edge. While the video plays the light follows its frames a few times a second and flows from color to color; on pause it stands still. The video itself stays DOM: native controls, captions, fullscreen and picture-in-picture work as without the effect. As a shadow the light falls below the box instead, narrower than it, like a drop shadow in the picture’s colors under a cover or a card. Best on dark pages; the glow is not cut by an ancestor’s overflow, unlike box-shadow.

Press play: the light follows the film a few times a second and stands still on pause.

Params of ambient
style

glow lights all around the box, like ambient mode.

Usage

Put it on video, img. start() registers ambient, 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 ambient
<div class="screening">
<video
class="film"
poster="/media/film/lounge.webp"
width="1280"
height="720"
controls
muted
loop
playsinline
preload="metadata"
aria-label="Product film, 12 seconds, no sound: the lounge chair in mustard velvet in a sunlit room"
data-dio="ambient"
>
<source
src="/media/film/lounge.webm"
type="video/webm"
/>
<source src="/media/film/lounge.mp4" type="video/mp4" />
</video>
</div>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.film', 'ambient', {
style: 'glow',
strength: 0.5,
spread: 0.35,
blur: 0.6,
saturation: 1.2,
fps: 10,
ease: 0.5,
})
import { start } from '@dioramajs/diorama'
import ambient from '@dioramajs/media/ambient'
const app = start()
app.attach('.film', ambient({
style: 'glow',
strength: 0.5,
spread: 0.35,
blur: 0.6,
saturation: 1.2,
fps: 10,
ease: 0.5,
}))

Page transitions

The glow fades with the page as it leaves. 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'glow' | 'shadow''glow'
  • glow lights all around the box, like ambient mode.
  • shadow falls below it like a colored drop shadow, for covers and cards on light pages too.
strengthdata-dio-strengthnumber0 to 10.5Strength of the glow at the edge of the box.
spreaddata-dio-spreadnumber0 to 10.35How far the glow reaches past the box, as a share of its shorter side, at most 240 px.
blurdata-dio-blurnumber0 to 10.6How much the colors of the frame are blurred together.
saturationdata-dio-saturationnumber0 to 21.2Saturation of the light; 1 keeps the frame’s.
fpsdata-dio-fpsnumber0 to 3010Color updates a second while the video plays; 0 updates only on pause, seeking and start.At most low 4, medium 8 by quality tier.
easedata-dio-easenumber0 to 2 s0.5Time the light takes to flow to new colors.
resolutionrebuildsdata-dio-resolutionnumber8 to 3216Width of the grid of colors read from the frame; its height follows the frame.At most low 8 by quality tier.

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, opacity, reducedMotion, size and width . Draws at a steady rate while it is on screen and stops when it leaves. At most fps a second while the video plays and while colors flow; none on pause or for an image once its colors are in. Scrolling moves the glow at full rate without reading the frame again. 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. The light changes only on start, pause and seeking, flowing for a second, so it does not breathe with the video.

Runs on every device. The same as with a mouse; the quality tiers lower the update rate on weak devices.

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

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

Cost and quality

Download
3.1 KB min+gzip the first time a page uses it
GPU
One plane the size of the box and its glow, one read of a tiny texture per pixel.
Memory
A few kilobytes. Each update draws the frame into a 16 × 9 canvas and reads it back on the CPU.
Frames
Draws while on screen

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

TierWhat changes
lowAt most 4 updates a second from an 8-cell grid.
mediumAt most 8 updates a second.

Recipes

Cards with a shadow in their own colors

The shadow style drops the light below the box, narrower than it, like a drop shadow tinted by the picture. It reads on light pages too, so it suits book covers, album art and product cards, where a grey box-shadow looks flat. Raise strength on a light page so the tint reads. An image takes its colors once and draws no more frames.

  • Cyanotype Impressions: white fronds of seaweed on deep blue

    Cyanotype Impressions

  • The Measure of the Sky: the brass face of an astrolabe

    The Measure of the Sky

  • Polar Night: green aurora over the night side of the Earth

    Polar Night

Live preview needs WebGPU or WebGL 2. Three cards on a light page, each with a soft shadow in the colors of its picture: blue, brass and green.

This example failed to draw.

<ul class="books">
<li>
<img
src="/media/work/cyanotype.webp"
width="480"
height="384"
data-dio="ambient"
data-dio-style="shadow"
data-dio-strength="0.9"
/>
<p>Cyanotype Impressions</p>
</li>
<li>
<img
src="/media/work/measure-of-the-sky.webp"
width="480"
height="384"
data-dio="ambient"
data-dio-style="shadow"
data-dio-strength="0.9"
/>
<p>The Measure of the Sky</p>
</li>
<li>
<img
src="/media/work/polar-night.webp"
width="480"
height="384"
data-dio="ambient"
data-dio-style="shadow"
data-dio-strength="0.9"
/>
<p>Polar Night</p>
</li>
</ul>

A video from another origin

The light is read from the frame’s pixels, and a browser lets a page read them only from a video of its own origin or one served with CORS. Add crossorigin="anonymous" to the video and have the CDN send Access-Control-Allow-Origin for your site. Without it the video plays as usual and the effect stops with EFFECT_CORS.

<video
src="https://cdn.example.com/films/lounge.mp4"
crossorigin="anonymous"
controls
playsinline
preload="metadata"
data-dio="ambient"
></video>

Many videos on one page

Each update draws the frame into a small canvas and reads it back on the CPU, by default 10 times a second while the video plays. For a grid of previews, lower fps, or set it to 0 so the light changes only on start, pause and seeking. smooth lets each change flow over more time, so fewer updates still look continuous.

<video
src="/media/film/lounge.mp4"
poster="/media/film/lounge.webp"
muted
loop
playsinline
preload="metadata"
data-dio="ambient"
data-dio-fps="3"
data-dio-ease="1"
></video>

Notes

The video stays a plain <video>: native controls, captions from <track>, fullscreen and picture-in-picture work as without the effect. A film without sound still needs a text alternative, such as an aria-label or a sentence next to it, as the example has.

The glow lies outside the element’s box and is not cut by an ancestor’s overflow: hidden, unlike box-shadow. Leave room around the video for spread, which reaches at most 240 px, and keep text out of that space. The effect suits dark pages best; on a light page use the shadow style or lower strength.

Before the first frame is decoded the light takes the poster’s colors, so a video with preload="metadata" and a poster glows before it plays and downloads only its metadata.

style and resolution rebuild the effect when they change. Set them in the markup.