# spotlight

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

While the pointer is over the image, everything outside a circle of radius px around it fades toward color by dim, and the circle trails the pointer by ease. In reveal style the circle shows a second picture instead, like an x-ray or a before and after; in blur style the outside is also blurred by blur px. When the pointer leaves, the light closes and the image returns to its full look, or to a resting dim set by rest.

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

A brass astrolabe on black, dimmed at rest, with a soft circle of light that follows the pointer across it.

```html title="index.html"
<figure class="lit">
  <img src="/media/portfolio/astrolabe.webp" alt="A brass astrolabe, its rete of star pointers over a plate engraved with the signs of the zodiac" width="1400" height="933" data-dio="spotlight" data-dio-radius="150" data-dio-rest="0.55" style="touch-action: none" />
</figure>
```

## Usage

Put it on: img, video.

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

```html
<img src="/collection/astrolabe.avif" alt="Brass astrolabe, 1580" data-dio="spotlight" data-dio-radius="160" data-dio-rest="0.6" />
```

In page transitions: Fades with the page transition; a flight carries the image without the light.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'light' \| 'reveal' \| 'blur'` | `'light'` | light darkens the rest; reveal shows the src picture in the circle; blur blurs the rest too. |
| `radius` | `data-dio-radius` | `number` 20 to 600 px | `140` | Radius of the lit circle. |
| `softness` | `data-dio-softness` | `number` 0 to 1 | `0.5` | Share of the radius that fades from light to dark: 0 is a hard edge. |
| `dim` | `data-dio-dim` | `number` 0 to 1 | `0.8` | How far the image outside the light goes toward color: 0 keeps it as is. |
| `color` | `data-dio-color` | `color` | `'#000000'` | Shade the outside fades toward, black by default. |
| `blur` | `data-dio-blur` | `number` 0 to 40 px | `12` | Blur radius outside the light in blur style. At most low 8. |
| `rest` | `data-dio-rest` | `number` 0 to 1 | `0` | Share of dim kept while the pointer is away: 0 shows the full image at rest. |
| `ease` | `data-dio-ease` | `number` 0 to 0.5 s | `0.08` | Smoothing of the light following the pointer: 0 sticks to it. |
| `duration` | `data-dio-duration` | `number` 0 to 1.5 s | `0.3` | Length of the fade as the light comes on and goes off. |
| `src` (rebuilds) | `data-dio-src` | `string` | `''` | URL of the picture the circle shows in reveal style. |

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

### Variants

- `style`: `light`, `reveal`, `blur`
  - `light` darkens the rest.
  - `reveal` shows the src picture in the circle.
  - `blur` blurs the rest too.

## Inputs and frames

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

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

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

## Cost and quality

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

- low: Blur radius at most 8 px.

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

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

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

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

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

## Related

- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [compare](/docs/effects/compare.md): Two pictures in one box, before and after, split by a line you drag, hover or scroll.
- [flare](/docs/effects/flare.md): A lens flare or a glossy light sweep over a photo that follows the pointer or the scroll.
- [frost](/docs/effects/frost.md): Frosted glass over a photo, or over the whole page, that the pointer wipes clear and that slowly fogs back.
- [glitch](/docs/effects/glitch.md): A digital glitch in short bursts: blocks slide sideways, color channels split, scanlines flicker.
- [image](/docs/effects/image.md): Draws the image or video unchanged, as a GL copy that page flights and other effects build on.
- [liquid](/docs/effects/liquid.md): The pointer leaves a fluid trail on a photo that settles back like water.
- [parallax](/docs/effects/parallax.md): The image drifts inside its frame with the pointer and the scroll; with a depth map, in 2.5D.
- [Follow the pointer](/docs/learn/pointer.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
