zoom
Hovering an image shows a sharp, magnified view of it under the pointer.
Norrvik RF-2, 1957
Top plate and speed dial
Engraving crisp, paint worn at the corners. Speeds from 1 s to 1/1000 run true on the tester.
€ 1,240
Live preview needs WebGPU or WebGL 2. 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.
This example failed to draw.
Usage
Put it on img. start() registers zoom, 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.
npx @dioramajs/diorama add zoom<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" 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>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.dial', 'zoom', { mode: 'inner', trigger: 'hover', zoom: 2.5, radius: 80, ease: 0.08,})import { start } from '@dioramajs/diorama'import zoom from '@dioramajs/media/zoom'
const app = start()app.attach('.dial', zoom({ mode: 'inner', trigger: 'hover', zoom: 2.5, radius: 80, ease: 0.08,}))Page transitions
Fades with the page transition; a flight starts without magnification. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
moderebuildsdata-dio-mode | 'inner' | 'lens' | 'side' | 'inner' | inner fills the box, lens shows a loupe, side uses another element. |
triggerrebuildsdata-dio-trigger | 'hover' | 'click' | 'hover' |
|
zoomdata-dio-zoom | number1 to 8 | 2.5 | Magnification over the shown picture. |
radiusdata-dio-radius | number20 to 200 px | 80 | Radius of the loupe. |
easedata-dio-ease | number0 to 0.5 s | 0.08 | Smoothing of the view following the pointer. |
fullrebuildsdata-dio-full | string | '' | URL of the large picture; empty takes a linked image, srcset or currentSrc. |
intorebuildsdata-dio-into | string | '' | Selector of the empty element that shows the view in side mode. |
resolutionrebuildsdata-dio-resolution | number512 to 8192 px | 4096 | Cap on the long side when decoding the large picture.At most low 2048, medium 3072 by quality tier. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta, height, presence, reducedMotion and 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. 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. 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.
To run it on touch devices too, or to change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { zoom: { when: 'all', reducedMotion: 'off' } } })Cost and quality
- Download
- 4.2 KB min+gzip 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.
- Frames
- Idle at rest
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | Decodes at most 2048 px on the long side. |
medium | Decodes at most 3072 px on the long side. |
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.
Live preview needs WebGPU or WebGL 2. Three photos of a vintage camera in a row, each showing a round magnifying loupe under the pointer.
This example failed to draw.
<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" 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" 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" 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.
Live preview needs WebGPU or WebGL 2. A bird's eye map of Santa Barbara from 1877 that shows the streets four times larger after a click.
This example failed to draw.
<img class="map" src="/media/hotel/santa-barbara-1877-1600.webp" 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.
<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.
<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.


