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

chroma

VideoAlso in3D and product

@dioramajs/media/chromaSource

A video shot on a green or blue screen, played over the page with the screen keyed out.

View as Markdown
On this page

Rangefinder

Live preview needs WebGPU or WebGL 2. A black rangefinder camera turning on a warm page; it was filmed on a green screen, and the green is gone.

This example failed to draw.

The product or presenter stands on your page as if the video had transparency, from one ordinary MP4 or WebM that plays in every browser, Safari included, where WebM alpha does not play and HEVC with alpha is hard to make. Pixels close to the screen color turn clear, with a soft ramp at the edge; green that the screen threw onto the subject is taken out, fully at the edge and by spill inside, and the edge itself is averaged over a few neighboring pixels so it does not crawl. Before the video plays its poster shows keyed the same way.
Params of chroma
by

color keys out the screen param.

Usage

Put it on video on a green or blue screen, muted for autoplay, with a poster of its first frame; your own play button and captions next to it. start() registers chroma, 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 chroma
<div class="studio">
<p class="brand">Rangefinder</p>
<video
class="camera"
src="/media/film/camera-green.webm"
poster="/media/film/camera-green.webp"
width="720"
height="480"
autoplay
muted
loop
playsinline
aria-label="A black rangefinder camera with brass edges, turning"
data-dio="chroma"
></video>
</div>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.camera', 'chroma', {
by: 'color',
similarity: 0.45,
softness: 0.1,
spill: 0.5,
blur: 1,
})
import { start } from '@dioramajs/diorama'
import chroma from '@dioramajs/media/chroma'
const app = start()
app.attach('.camera', chroma({
by: 'color',
similarity: 0.45,
softness: 0.1,
spill: 0.5,
blur: 1,
}))

Page transitions

Fades with the page transition. How a flight flies.

Params

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

ParamTypeDefaultDescription
byrebuildsdata-dio-by'color' | 'auto''color'
  • color keys out the screen param.
  • auto reads the screen color every frame from whichever top corner of the frame is greener, for unevenly lit footage.
screendata-dio-screencolor'#00b140'Color of the screen to remove, green by default; #0047bb for blue.
similaritydata-dio-similaritynumber0 to 10.45How far from the screen color a pixel may be and still turn clear.
softnessdata-dio-softnessnumber0 to 0.50.1Width of the soft ramp from clear to opaque past similarity; 0 cuts hard.
spilldata-dio-spillnumber0 to 10.5Share of the screen’s hue taken out of the subject, all of it at the see-through edge; 0 keeps every color as shot.
blurdata-dio-blurnumber0 to 4 px1Distance in px of the neighbors each pixel’s key is averaged with; 0 is off.At most low 0 by quality tier.
cropdata-dio-crop[number, number, number, number]0 to 0.5[0, 0, 0, 0]Share of the frame cut off at the top, right, bottom and left, for light stands and the screen’s end.

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

Inputs and frames

It reads size . Draws only while something on it moves, then stops. One frame per video frame while it plays and is on screen, at the video’s own rate; none while it is paused, before it plays or off screen. 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. Keyed as usual: keying is not motion. Whether the video plays is up to the site; under reduced motion, start it paused and let the visitor play it.

Runs on every device. The same as on desktop; tapping works as the site’s own buttons do.

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

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

Cost and quality

Download
2.0 KB min+gzip the first time a page uses it
GPU
Five texture reads per pixel of the video’s box with blur, one without, and a small color transform.
Memory
One video frame on the GPU, and the poster until the video plays. The video itself is as large as the file: no second copy with alpha.
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
lowNo blur: one texture read per pixel instead of five.

Recipes

A product that turns on the page

Film the product on a green screen, or render it on one, and export an ordinary MP4 or WebM. chroma takes the screen out while it plays, so the product stands on your page with its own soft shadow gone and the page behind it. This works in every browser: Safari plays no WebM with alpha, and HEVC with alpha needs Apple tools to make.

<video
src="/film/bottle.mp4"
poster="/film/bottle.webp"
autoplay
muted
loop
playsinline
aria-label="The bottle, turning"
data-dio="chroma"
></video>

Make the poster the first frame, on the same green: the effect keys it too, so the page shows the product before the film loads.

Tune the key

Start from the defaults and change one value at a time while the film plays.

  • similarity is how far from the screen color a pixel may be and still turn clear. Raise it if patches of screen stay; lower it if the edges of the product go see-through.
  • softness softens the cut from clear to opaque. Hair, fur and motion blur want more.
  • spill takes the screen’s green out of the product, where light from the screen fell on it. All of it is taken out of the see-through edge; 0 keeps every color as shot, for a product that is green itself.
  • blur averages each pixel’s key with neighbors that many pixels away, so a noisy edge does not crawl.
  • crop cuts the top, right, bottom and left of the frame, for light stands or the end of the screen in the shot.

Unevenly lit footage

A screen that is brighter in the middle than in the corners has more than one green. With by set to auto the key is read from each frame instead of screen, from whichever top corner is greener, so it follows the light as it changes. Keep at least one top corner on the screen, or move them in with crop.

<video
src="/film/studio.mp4"
autoplay
muted
loop
playsinline
data-dio="chroma"
data-dio-by="auto"
></video>

A blue screen

Set screen to the color of the screen. Blue suits products with green in them.

<video
src="/film/plant.mp4"
autoplay
muted
loop
playsinline
data-dio="chroma"
data-dio-screen="#0047bb"
></video>

A presenter with your own controls

The copy hides the video element, and with it the browser’s own controls and captions. Give the video a play button and captions next to it; the effect follows whatever the video does.

<figure class="host">
<video
class="talk"
src="/talk/intro.mp4"
poster="/talk/intro.webp"
playsinline
data-dio="chroma"
></video>
<button class="play" aria-pressed="false">Play</button>
</figure>
const talk = document.querySelector('.talk')
const play = document.querySelector('.play')
play.addEventListener('click', () =>
talk.paused ? talk.play() : talk.pause(),
)
talk.addEventListener('play', () =>
play.setAttribute('aria-pressed', 'true'),
)
talk.addEventListener('pause', () =>
play.setAttribute('aria-pressed', 'false'),
)

Notes

Without WebGL the visitor would see the green screen. Hide the video then with one rule, as the example does:

[data-dio~="chroma"][data-dio-state="off"] {
visibility: hidden;
}

On the first screen the video waits hidden until its keyed copy is drawn, at most 3 seconds, so the green never flashes. The effect turns preload="none" into metadata, since a video without a frame has nothing to key.

Footage filmed on black, such as fire, smoke or sparks, needs no key: mix-blend-mode: screen on the video lays it over a dark page.

A video from another origin needs crossorigin="anonymous" and a server that sends Access-Control-Allow-Origin.

by rebuilds the effect when it changes. Set it in the markup.