# zoom

Hovering an image shows a sharp, magnified view of it under the pointer.

Over the image a high-resolution version shows magnified zoom times. In inner mode the whole box shows it and follows the pointer without empty margins; in lens mode a round loupe with a glassy rim; in side mode another element shows it and the image marks the area. Until the large file arrives the image itself is magnified, then it crossfades.

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

A product photo of a camera's speed dial next to its description; hovering the photo shows a sharp magnified view of the part under the pointer.

```html title="index.html"
<div class="product">
  <img class="dial" src="/media/shop/dial-900.webp" srcset="/media/shop/dial-900.webp 900w, /media/shop/dial-2400.webp 2400w" sizes="(max-width: 640px) 90vw, 420px" alt="The shutter speed dial and the engraving on the top plate of a rangefinder camera" width="900" height="600" data-dio="zoom" data-dio-into=".product .pane" />
  <div class="info">
    <p class="kicker">Norrvik RF-2, 1957</p>
    <p class="name">Top plate and speed dial</p>
    <p>Engraving crisp, paint worn at the corners. Speeds from 1 s to 1/1000 run true on the tester.</p>
    <p class="price">€ 1,240</p>
    <div class="pane" aria-hidden="true"></div>
  </div>
</div>
```

## Usage

Put it on: img.

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

```html
<img src="/p/watch-800.avif" srcset="/p/watch-800.avif 800w, /p/watch-2400.avif 2400w" sizes="50vw" alt="Field watch, steel case" data-dio="zoom" data-dio-full="/p/watch-3600.avif" />
```

In page transitions: Fades with the page transition; a flight starts without magnification.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `mode` (rebuilds) | `data-dio-mode` | `'inner' \| 'lens' \| 'side'` | `'inner'` | inner fills the box, lens shows a loupe, side uses another element. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'hover' \| 'click'` | `'hover'` | hover shows the view while the pointer is over the image; click shows it on a click and hides it on the next click or when the pointer leaves, so moving across the page does not zoom. |
| `zoom` | `data-dio-zoom` | `number` 1 to 8 | `2.5` | Magnification over the shown picture. |
| `radius` | `data-dio-radius` | `number` 20 to 200 px | `80` | Radius of the loupe. |
| `ease` | `data-dio-ease` | `number` 0 to 0.5 s | `0.08` | Smoothing of the view following the pointer. |
| `full` (rebuilds) | `data-dio-full` | `string` | `''` | URL of the large picture; empty takes a linked image, srcset or currentSrc. |
| `into` (rebuilds) | `data-dio-into` | `string` | `''` | Selector of the empty element that shows the view in side mode. |
| `resolution` (rebuilds) | `data-dio-resolution` | `number` 512 to 8192 px | `4096` | Cap on the long side when decoding the large picture. At most low 2048, medium 3072. |

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

### Variants

- `mode`: `inner`, `lens`, `side`
- `trigger`: `hover`, `click`
  - `hover` shows the view while the pointer is over the image.
  - `click` shows it on a click and hides it on the next click or when the pointer leaves, so moving across the page does not zoom.

## 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), [`width`](/docs/api/inputs#input-width). Draws only while something on it moves, then stops. Only while the view appears, follows or leaves; none at rest, and pointer moves outside the image draw 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 smoothing and no fade: the view follows the pointer at once.

Skipped on touch devices, which never load the engine for it. Not loaded on touch screens: it runs where (hover: hover) and (pointer: fine) match. With when: all, a tap toggles the view at its point, and a drag moves it where the image has touch-action: none or pinch-zoom.

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

start({ effects: { zoom: { when: 'all', reducedMotion: 'off' } } })
```

## Cost and quality

Loads 4.2 KB the first time a page uses it. GPU: One or two texture reads per pixel. Memory: The large picture while hovered and 10 s after: about 31 MB at 2400 px with mipmaps.

- low: Decodes at most 2048 px on the long side.
- medium: Decodes at most 3072 px on the long side.

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

## Recipes

### Condition photos under a loupe

A shop selling used or handmade goods shows the scratches and the stitching up close. In `lens` mode a round loupe follows the pointer and the rest of the photo stays in view. Each image lists its large file in `srcset`, so no extra attribute is needed.

Three photos of a vintage camera in a row, each showing a round magnifying loupe under the pointer.

```html title="index.html"
<ul class="shots">
  <li><img src="/media/shop/front-900.webp" srcset="/media/shop/front-900.webp 900w, /media/shop/front-2400.webp 2400w" sizes="240px" alt="Front of the camera, with the lens mounted" width="900" height="600" data-dio="zoom" data-dio-mode="lens" /></li>
  <li><img src="/media/shop/lens-900.webp" srcset="/media/shop/lens-900.webp 900w, /media/shop/lens-2400.webp 2400w" sizes="240px" alt="The 50 mm lens on the body" width="900" height="600" data-dio="zoom" data-dio-mode="lens" /></li>
  <li><img src="/media/shop/brassing-900.webp" srcset="/media/shop/brassing-900.webp 900w, /media/shop/brassing-2400.webp 2400w" sizes="240px" alt="The right end of the body, worn to the brass" width="900" height="600" data-dio="zoom" data-dio-mode="lens" /></li>
</ul>
```

### An old map that zooms on click

On a page that is mostly read, a zoom that follows every pass of the pointer gets in the way. With `trigger="click"` the view opens where the reader clicks and closes on the next click or when the pointer leaves. A `zoom-in` cursor tells the reader it is there.

A bird's eye map of Santa Barbara from 1877 that shows the streets four times larger after a click.

```html title="index.html"
<img class="map" src="/media/hotel/santa-barbara-1877-1600.webp" alt="Bird's eye view of Santa Barbara, California, 1877, looking north to the mountains" width="1600" height="1089" data-dio="zoom" data-dio-trigger="click" data-dio-zoom="4" data-dio-full="/media/hotel/santa-barbara-1877-3600.webp" style="cursor: zoom-in" />
```

### A view beside the photo

`side` mode draws the magnified view into another element, as large shops do next to the product photo, and marks the area on the photo. Give the element a size, leave it empty and hide it from screen readers: it holds only the picture.

```html
<div class="product">
  <img
    src="/p/bag-900.avif"
    srcset="/p/bag-900.avif 900w, /p/bag-2400.avif 2400w"
    sizes="50vw"
    data-dio="zoom"
    data-dio-mode="side"
    data-dio-into="#bag-zoom"
  />
  <div class="details">
    <div id="bag-zoom" aria-hidden="true"></div>
  </div>
</div>
```

### Zoom and a lightbox for touch and keyboards

The zoom works for a mouse or a pen only. Wrap each photo in a link to its large file and put `lightbox` on the list: a click or Enter opens the photo full screen, and inside the viewer a pinch or a double tap zooms it on a touch screen. Keep `trigger="hover"` here, since a click already opens the viewer.

```html
<ul data-dio="lightbox">
  <li>
    <a href="/p/front-2400.webp">
      <img
        src="/p/front-900.webp"
        data-dio="zoom"
        data-dio-mode="lens"
      />
    </a>
  </li>
  <li>
    <a href="/p/back-2400.webp">
      <img
        src="/p/back-900.webp"
        data-dio="zoom"
        data-dio-mode="lens"
      />
    </a>
  </li>
</ul>
```

## Notes

The large file is found in this order: `full`, the `href` of a link around the image when it points to an image file, the widest `srcset` candidate, then `currentSrc`. It loads on the first hover and stays for 10 s after the pointer leaves, then its texture is freed and the bytes stay in the cache. A 2400 px picture takes about 31 MB of GPU memory while it is held.

Keep the large file at 2400 to 4000 px on the long side. `resolution` caps the decode at 4096 px, and low and medium quality tiers cap it lower, so a bigger file costs download time and gains nothing on most devices.

The image and its `alt` do not change, and touch screens never load the effect. Pair it with `lightbox` where readers on phones need the detail too.

## 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.
- [Loading and preloading](/docs/guides/loading.md)
- [Performance and quality](/docs/guides/performance.md)
