# ambient

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

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.

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

A short product film of a lounge chair on a dark page, with a soft glow in the film's colors around it.

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

## Usage

Put it on: video, img.

`start()` registers `ambient`, so the attribute alone puts it on an element.

```html
<video src="/film/aurum.mp4" poster="/film/aurum.avif" controls="" playsinline="" muted="" loop="" data-dio="ambient" data-dio-strength="0.6"></video>
```

In page transitions: The glow fades with the page as it leaves.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-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. |
| `strength` | `data-dio-strength` | `number` 0 to 1 | `0.5` | Strength of the glow at the edge of the box. |
| `spread` | `data-dio-spread` | `number` 0 to 1 | `0.35` | How far the glow reaches past the box, as a share of its shorter side, at most 240 px. |
| `blur` | `data-dio-blur` | `number` 0 to 1 | `0.6` | How much the colors of the frame are blurred together. |
| `saturation` | `data-dio-saturation` | `number` 0 to 2 | `1.2` | Saturation of the light; 1 keeps the frame’s. |
| `fps` | `data-dio-fps` | `number` 0 to 30 | `10` | Color updates a second while the video plays; 0 updates only on pause, seeking and start. At most low 4, medium 8. |
| `ease` | `data-dio-ease` | `number` 0 to 2 s | `0.5` | Time the light takes to flow to new colors. |
| `resolution` (rebuilds) | `data-dio-resolution` | `number` 8 to 32 | `16` | Width of the grid of colors read from the frame; its height follows the frame. At most low 8. |

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

### Variants

- `style`: `glow`, `shadow`
  - `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.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-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. 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. 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.

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

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

## Cost and quality

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

- low: At most 4 updates a second from an 8-cell grid.
- medium: At most 8 updates a second.

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

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

Three cards on a light page, each with a soft shadow in the colors of its picture: blue, brass and green.

```html title="index.html"
<ul class="books">
  <li>
    <img src="/media/work/cyanotype.webp" alt="Cyanotype Impressions: white fronds of seaweed on deep blue" 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" alt="The Measure of the Sky: the brass face of an astrolabe" 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" alt="Polar Night: green aurora over the night side of the Earth" 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`](/docs/errors/effect-cors).

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

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

## Related

- [chroma](/docs/effects/chroma.md): A video shot on a green or blue screen, played over the page with the screen keyed out.
- [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.
- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [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.
- [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.
- [compare](/docs/effects/compare.md): Two pictures in one box, before and after, split by a line you drag, hover or scroll.
- [Performance and quality](/docs/guides/performance.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
