# chroma

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

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.

Module `media`, category Video. Import `@dioramajs/media/chroma`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/media/src/chroma.ts)

A black rangefinder camera turning on a warm page; it was filmed on a green screen, and the green is gone.

```html title="index.html"
<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>
```

## 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.

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

In page transitions: Fades with the page transition.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `by` (rebuilds) | `data-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. |
| `screen` | `data-dio-screen` | `color` | `'#00b140'` | Color of the screen to remove, green by default; #0047bb for blue. |
| `similarity` | `data-dio-similarity` | `number` 0 to 1 | `0.45` | How far from the screen color a pixel may be and still turn clear. |
| `softness` | `data-dio-softness` | `number` 0 to 0.5 | `0.1` | Width of the soft ramp from clear to opaque past similarity; 0 cuts hard. |
| `spill` | `data-dio-spill` | `number` 0 to 1 | `0.5` | Share of the screen’s hue taken out of the subject, all of it at the see-through edge; 0 keeps every color as shot. |
| `blur` | `data-dio-blur` | `number` 0 to 4 px | `1` | Distance in px of the neighbors each pixel’s key is averaged with; 0 is off. At most low 0. |
| `crop` | `data-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. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `by`: `color`, `auto`
  - `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.

## Inputs and frames

Reads [`size`](/docs/api/inputs#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. See [how a frame is decided](/docs/concepts/frames).

## 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.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { chroma: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 2.0 KB 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.

- low: No blur: one texture read per pixel instead of five.

See [the quality plugin](/docs/effects/quality).

## 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.

```html
<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`.

```html
<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.

```html
<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.

```html
<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>
```

```ts
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:

```css
[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.

## Related

- [ambient](/docs/effects/ambient.md): A soft glow in the colors of the frame around a video or an image, like ambient mode.
- [scrub](/docs/effects/scrub.md): A video scrubbed by scroll: its time follows the sticky section it plays in, forward and back.
- [video-hover](/docs/effects/video-hover.md): A thumbnail plays a short muted video preview while the pointer rests on it or its link has keyboard focus.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [book](/docs/effects/book.md): A cover image made a 3D book that turns to show its spine and opens on hover.
- [box](/docs/effects/box.md): A product box built from pictures of its faces: turn it, tilt it, open its lid.
- [carousel](/docs/effects/carousel.md): Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.
- [coin](/docs/effects/coin.md): A coin, medal or token struck from a logo or a photo: it turns, catches the light and flips over.
- [Performance and quality](/docs/guides/performance.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
