Diorama

Made for a big screen.

This site is one 3D scene, built for a computer: a wide window and a mouse. Open dioramajs.dev on a desktop or a laptop to see it.This site is one 3D scene, built for a wide window. Make this one wider, or open dioramajs.dev on a larger screen.

GitHub

DioramaDocs

compare

Photo

@dioramajs/media/compareSource

Two pictures in one box, before and after, split by a line you drag, hover or scroll.

View as Markdown
On this page
Room 7 before: peeling plaster, a bare iron bed frame, two old suitcases and a stool Room 7 after: lime-washed walls, a terracotta floor, a daybed with cushions, an armchair and a plant

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.

Drag the line, or tab to the box and press the arrow keys. Switch mode, by and direction for the other ways to compare; by scroll needs a pinned section, as in the recipe below.
Params of compare
mode

split draws a line between the images.

by

drag by hand.

direction

x moves a vertical line sideways.

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.

Terminal window
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.

ParamTypeDefaultDescription
positiondata-dio-positionnumber0 to 10.5Line position as a share of the box along direction; dragging writes it.
byrebuildsdata-dio-by'drag' | 'hover' | 'scroll' | 'manual''drag'
  • drag by hand.
  • hover follows the pointer.
  • scroll a pinned section.
directionrebuildsdata-dio-direction'x' | 'y''x'
  • x moves a vertical line sideways.
  • y moves a horizontal one.
moderebuildsdata-dio-mode'split' | 'lens' | 'fade''split'
  • split draws a line between the images.
  • lens shows the second image in a circle at the pointer.
  • fade crossfades the whole box from the second image (position 0) to the first (position 1), for then-and-now photos taken from the same spot.
radiusdata-dio-radiusnumber20 to 400 px120Radius of the lens.
softnessdata-dio-softnessnumber0 to 64 px0Softness of the border between the images; not used by fade.
thicknessdata-dio-thicknessnumber0 to 12 px2Width of the line, 0 for none.
colordata-dio-colorcolor'#ffffff'Color of the line and the handle.
handledata-dio-handlebooleantrueDraws a round handle with arrows on the line.
startdata-dio-startnumber0 to 10.2Section progress where scrolling starts to move the line.
enddata-dio-endnumber0 to 10.8Section progress where scrolling stops moving the line.
hintdata-dio-hintbooleantrueWiggles the line once when the box first shows.
itemsrebuildsdata-dio-itemsstring'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.

Straight scan: a road through the copper basin of Ducktown, Tennessee, 1939, flat and faded The same slide in the lab's profile: deeper red earth, clean whites, fine grain

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.

Params of compare
<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.

Room 7 before: peeling plaster, a bare iron bed frame, two old suitcases and a stool Room 7 after: lime-washed walls, a terracotta floor, a daybed with cushions, an armchair and a plant

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.

Params of compare
<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.