# compare

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

The first image shows up to the line and the second after it. The line has a round handle with arrows and a hard or soft edge; in lens mode a circle at the pointer shows the second image instead. The line follows a drag, the pointer, the scroll of a pinned section or the param, and wiggles once when the box first shows.

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

Two renders of one hotel room, before and after its renovation, split by a line with a round handle that you drag.

```html title="index.html"
<figure class="compare" data-dio="compare" style="touch-action: pan-y">
  <img src="/media/hotel/room-7-before.avif" alt="Room 7 before: peeling plaster, a bare iron bed frame, two old suitcases and a stool" width="1200" height="750" />
  <img src="/media/hotel/room-7-after.avif" alt="Room 7 after: lime-washed walls, a terracotta floor, a daybed with cushions, an armchair and a plant" width="1200" height="750" />
  <input type="range" min="0" max="100" value="50" aria-label="Room 7, before and after the renovation" />
</figure>
```

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

```html
<figure class="compare" data-dio="compare" data-dio-mode="fade" style="touch-action: pan-y"><img src="/harbour-1910.avif" alt="The harbour in 1910" /><img src="/harbour-today.avif" alt="The harbour today" /><input type="range" min="0" max="100" value="50" aria-label="1910 and today" /></figure>
```

- `input[type="range"]` inside: Keyboard and screen reader control of the line; dragging writes its value and fires input.

In page transitions: Fades with the page transition; a keyed flight carries the current split.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `position` | `data-dio-position` | `number` 0 to 1 | `0.5` | Line position as a share of the box along direction; dragging writes it. |
| `by` (rebuilds) | `data-dio-by` | `'drag' \| 'hover' \| 'scroll' \| 'manual'` | `'drag'` | What moves the line: drag by hand; hover follows the pointer; scroll a pinned section; manual. |
| `direction` (rebuilds) | `data-dio-direction` | `'x' \| 'y'` | `'x'` | x moves a vertical line sideways; y moves a horizontal one. |
| `mode` (rebuilds) | `data-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. |
| `radius` | `data-dio-radius` | `number` 20 to 400 px | `120` | Radius of the lens. |
| `softness` | `data-dio-softness` | `number` 0 to 64 px | `0` | Softness of the border between the images; not used by fade. |
| `thickness` | `data-dio-thickness` | `number` 0 to 12 px | `2` | Width of the line, 0 for none. |
| `color` | `data-dio-color` | `color` | `'#ffffff'` | Color of the line and the handle. |
| `handle` | `data-dio-handle` | `boolean` | `true` | Draws a round handle with arrows on the line. |
| `start` | `data-dio-start` | `number` 0 to 1 | `0.2` | Section progress where scrolling starts to move the line. |
| `end` | `data-dio-end` | `number` 0 to 1 | `0.8` | Section progress where scrolling stops moving the line. |
| `hint` | `data-dio-hint` | `boolean` | `true` | Wiggles the line once when the box first shows. |
| `items` (rebuilds) | `data-dio-items` | `string` | `'img'` | Selector of the two images inside the container. |

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

### Variants

- `mode`: `split`, `lens`, `fade`
  - `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.
- `by`: `drag`, `hover`, `scroll`, `manual`
  - `drag` by hand.
  - `hover` follows the pointer.
  - `scroll` a pinned section.
- `direction`: `x`, `y`
  - `x` moves a vertical line sideways.
  - `y` moves a horizontal one.

## Inputs and frames

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

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

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

## Cost and quality

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

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

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

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

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.

```html title="index.html"
<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" alt="Straight scan: a road through the copper basin of Ducktown, Tennessee, 1939, flat and faded" width="932" height="640" />
  <img src="/media/shop/road-profile.webp" alt="The same slide in the lab's profile: deeper red earth, clean whites, fine grain" 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 and after its renovation, with the line sweeping from right to left while the section scrolls.

```html title="index.html"
<figure class="compare" data-dio="compare" data-dio-by="scroll" data-dio-color="#f3ead9">
  <img src="/media/hotel/room-7-before.avif" alt="Room 7 before: peeling plaster, a bare iron bed frame, two old suitcases and a stool" width="1200" height="750" />
  <img src="/media/hotel/room-7-after.avif" alt="Room 7 after: lime-washed walls, a terracotta floor, a daybed with cushions, an armchair and a plant" width="1200" height="750" />
  <input type="range" min="0" max="100" value="50" aria-label="Room 7, before and after the renovation" />
</figure>
```

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

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

## Related

- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [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.
- [pixelate](/docs/effects/pixelate.md): The image resolves from big pixels to sharp as it scrolls into view, or breaks into pixels on hover.
- [Reduced motion and touch](/docs/learn/every-visitor.md)
