compare
Two pictures in one box, before and after, split by a line you drag, hover or scroll.
Live preview needs WebGPU or WebGL 2. Two renders of one hotel room, before and after its renovation, split by a line with a round handle that you drag.
This example failed to draw.
Usage
Put it on a container with two images and an <input type="range">. start() registers compare, 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 compare<figure class="compare" data-dio="compare" style="touch-action: pan-y"> <img src="/media/hotel/room-7-before.avif" width="1200" height="750" /> <img src="/media/hotel/room-7-after.avif" width="1200" height="750" /> <input type="range" min="0" max="100" value="50" aria-label="Room 7, before and after the renovation" /></figure>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.compare', 'compare', { mode: 'split', by: 'drag', direction: 'x', position: 0.5, radius: 120, softness: 0, thickness: 2, color: '#ffffff', handle: true, start: 0.2, end: 0.8, hint: true,})import { start } from '@dioramajs/diorama'import compare from '@dioramajs/media/compare'
const app = start()app.attach('.compare', compare({ mode: 'split', by: 'drag', direction: 'x', position: 0.5, radius: 120, softness: 0, thickness: 2, color: '#ffffff', handle: true, start: 0.2, end: 0.8, hint: true,}))Markup it reads
input[type="range"]inside: Keyboard and screen reader control of the line; dragging writes its value and fires input.
Page transitions
Fades with the page transition; a keyed flight carries the current split. 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 |
|---|---|---|---|
positiondata-dio-position | number0 to 1 | 0.5 | Line position as a share of the box along direction; dragging writes it. |
byrebuildsdata-dio-by | 'drag' | 'hover' | 'scroll' | 'manual' | 'drag' |
|
directionrebuildsdata-dio-direction | 'x' | 'y' | 'x' |
|
moderebuildsdata-dio-mode | 'split' | 'lens' | 'fade' | 'split' |
|
radiusdata-dio-radius | number20 to 400 px | 120 | Radius of the lens. |
softnessdata-dio-softness | number0 to 64 px | 0 | Softness of the border between the images; not used by fade. |
thicknessdata-dio-thickness | number0 to 12 px | 2 | Width of the line, 0 for none. |
colordata-dio-color | color | '#ffffff' | Color of the line and the handle. |
handledata-dio-handle | boolean | true | Draws a round handle with arrows on the line. |
startdata-dio-start | number0 to 1 | 0.2 | Section progress where scrolling starts to move the line. |
enddata-dio-end | number0 to 1 | 0.8 | Section progress where scrolling stops moving the line. |
hintdata-dio-hint | boolean | true | Wiggles the line once when the box first shows. |
itemsrebuildsdata-dio-items | string | 'img' | Selector of the two images inside the container. |
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, reducedMotion, size, visible, width and y
. Draws only while something on it moves, then stops. Only while the line eases or the hint plays; none at rest, and pointer moves outside the box 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 hint and no easing: the line follows input at once.
Runs on every device. A tap moves the line to the point. Dragging works with touch-action: pan-y (pan-x for direction y); hover acts as drag.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { compare: { reducedMotion: 'off' } } })Cost and quality
- Download
- 3.3 KB min+gzip the first time a page uses it
- GPU
- Two texture reads per pixel and a distance field for the handle.
- Memory
- Both images as textures.
- Frames
- Idle at rest
It runs from the low tier up. Every tier draws it the same way. The quality plugin picks the tier for each device.
Recipes
Give the box a static split
compare reads two images and a range inside a container, and the site’s CSS lays them out. Stack the images in one grid cell and clip the second at the middle: that is the split readers see before the engine loads and without WebGL, and it matches the effect at rest. The range stays focusable for keyboards and screen readers.
.compare { position: relative; display: grid; aspect-ratio: 16 / 10;}.compare > img { grid-area: 1 / 1; width: 100%; height: 100%; object-fit: cover;}.compare > img + img { clip-path: inset(0 0 0 50%);}.compare > input { position: absolute; width: 1px; height: 1px; opacity: 0;}/* Only once the effect is known to be off: before it loads, Tab must still reach the range. */.compare[data-dio-state="off"] > input { display: none;}Retouching under a lens
A photo lab or an upscaling service shows its work best in a lens: the reader moves a circle over the original and sees the processed version inside it, on the details they care about. mode="lens" with by="hover" follows the pointer without a press.
Live preview needs WebGPU or WebGL 2. A 1939 slide of a road through a copper basin, flat and faded, with a lens at the pointer that shows the same slide in deeper color.
This example failed to draw.
<figure class="compare" data-dio="compare" data-dio-mode="lens" data-dio-by="hover" data-dio-radius="150" style="touch-action: pan-y"> <img src="/media/shop/road-straight.webp" width="932" height="640" /> <img src="/media/shop/road-profile.webp" width="932" height="640" /> <input type="range" min="0" max="100" value="50" aria-label="Straight scan and the lab's profile" /></figure>A renovation that opens as you scroll
With by="scroll" the line crosses the box while a pinned section scrolls past, from start to end of its progress. Mark the section with data-dio-section and make its stage sticky; here the box pins under the header for a screen and a half of scroll.
Live preview needs WebGPU or WebGL 2. Room 7 before and after its renovation, with the line sweeping from right to left while the section scrolls.
This example failed to draw.
<figure class="compare" data-dio="compare" data-dio-by="scroll" data-dio-color="#f3ead9"> <img src="/media/hotel/room-7-before.avif" width="1200" height="750" /> <img src="/media/hotel/room-7-after.avif" width="1200" height="750" /> <input type="range" min="0" max="100" value="50" aria-label="Room 7, before and after the renovation" /></figure><section class="renovation" data-dio-section style="height: 250vh"> <div style="position: sticky; top: 0; height: 100vh"> <figure class="compare" data-dio="compare" data-dio-by="scroll" >…</figure> </div></section>Then and now from the same spot
When both photos were taken from the same place, such as an archive print and today’s view of a harbor, a line would cut through the same building twice. mode="fade" crossfades the whole box instead, from the second image at position 0 to the first at 1.
<figure class="compare" data-dio="compare" data-dio-mode="fade" style="touch-action: pan-y"> <img src="/img/harbor-1910.avif" /> <img src="/img/harbor-today.avif" /> <input type="range" min="0" max="100" value="50" aria-label="The harbor in 1910 and today" /></figure>Notes
Keep captions such as Before and After outside the container or above the canvas with a z-index of 2: inside the box the copy draws over them.
Write touch-action: pan-y on the container for a vertical line, pan-x for direction="y", so a finger can drag the line while the page still scrolls the other way. Without it a tap still moves the line to the point.
Each image keeps its own object-fit and object-position, so two photos of one scene line up when they share an aspect ratio and framing. Nothing draws until both have loaded: keep the second file as light as the first.