# GitHub · Compare two versions of an image

In an image diff, choose Swipe and drag the divider across a detail. Crossing the edge of an icon, for example, reveals the same pixels in the earlier and later versions.

[Official source](https://docs.github.com/en/repositories/working-with-files/using-files/working-with-non-code-files#swipe)

## Related motion · Image Comparison

# Image Comparison

A movable divider reveals two versions of the same floor plan.

## Try it

Drag the divider, move it with the arrow keys, or choose one of the preset positions.

## Interaction details

The slider starts at 50%. The three presets show the original plan, a half-and-half view, or the revised plan.

## Platform controls

- iOS · Drag the divider across the plan, or tap one of the preset positions below.
- Android · Drag the divider across the plan, or tap one of the preset positions below.
- PC · Drag the divider, move it with the arrow keys, or choose one of the preset positions.

## Where to use it

Comparing before-and-after images that share the same coordinates.

## Implementation prompt

> Keep both images aligned as the handle reveals more or less of the original plan.

## Sources and credits

- [ESA/Webb image comparison](<https://esawebb.org/images/comparisons/weic2216/>)

## Checking the behavior

The ESA/Webb comparison reveals two aligned views of the same object. In this example both floor plans use the same scale and origin. Clip the upper layer as the handle moves. Compare both endpoints, return to the midpoint and repeat with the arrow keys, Home and End. The three position buttons provide an alternative to dragging.

## Standalone HTML

Save the code below as an HTML file and open it in your browser. The example works offline.

```html
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Gallery Layout Comparison · UI Motion Lab</title>
<style>:root{color-scheme:light;--paper:#f5f3ec;--ink:#262820;--line:#c7c6b7;--accent:#98402b}
*{box-sizing:border-box}body{margin:0;background:var(--paper);color:var(--ink);font:15px/1.55 Arial,'Apple SD Gothic Neo','Malgun Gothic',sans-serif;overflow-wrap:anywhere}
main{width:min(810px,calc(100% - 36px));margin:clamp(20px,5vh,58px) auto}
main h1{margin:0;font-size:clamp(28px,4.1vw,47px);font-weight:700;line-height:1.15;letter-spacing:-.065em}
main>p{color:#56594e;margin:8px 0 22px;line-height:1.55}.panel{padding:clamp(15px,2.7vw,28px);background:#fffdf6;border:1px solid var(--line);border-radius:2px}
button,select,input,textarea{font:inherit}button,select{min-height:40px}button{cursor:pointer}button:disabled{cursor:default;opacity:.45}
button,select{background:#faf9f0;color:inherit;border:1px solid #a6a798;border-radius:3px;padding:5px 12px}
:focus-visible{outline:3px solid #945030;outline-offset:3px}button:enabled:hover,select:hover{border-color:var(--ink)}
[hidden]{display:none!important}.lab-embedded [data-lab-shell]{display:none!important}.lab-embedded main{margin:clamp(12px,2vh,25px) auto}
figure,fieldset{margin:0}p:last-child{margin-bottom:0}fieldset{min-width:0}
@media(max-width:460px){main{width:calc(100% - 24px);margin:16px auto}main>p{font-size:13px;margin:5px 0 14px}main h1{font-size:26px}.panel{padding:13px}}
@media(prefers-reduced-motion:reduce){*,*::before,*::after{animation-duration:.01ms!important;transition-duration:.01ms!important;scroll-behavior:auto!important}}
.lab-reduce *,.lab-reduce *::before,.lab-reduce *::after{animation-duration:.01ms!important;transition-duration:.01ms!important;scroll-behavior:auto!important}
.panel{padding:0;overflow:hidden}.drawing-head,.drawing-tools{display:flex;justify-content:space-between;gap:12px;align-items:center;padding:12px 16px}.drawing-head span,.drawing-tools output,#description{font-size:12px}.drawing-head strong{font-size:15px}.drawing-head{border-bottom:1px solid var(--line)}
.sheet{--split:50%;position:relative;aspect-ratio:2;overflow:hidden;background:#f8f4e7}
.sheet canvas{position:absolute;inset:0;width:100%;height:100%}.before{clip-path:inset(0 calc(100% - var(--split)) 0 0)}
.divider{position:absolute;left:var(--split);top:0;bottom:0;width:2px;background:#262820;transform:translateX(-50%);pointer-events:none}
.divider b{position:absolute;left:50%;top:50%;width:38px;height:38px;transform:translate(-50%,-50%);display:grid;place-items:center;background:#262820;color:white;border-radius:50%;font:17px Arial}
#split{position:absolute;inset:0;width:100%;height:100%;margin:0;opacity:0;cursor:ew-resize;touch-action:pan-y}.sheet:focus-within{outline:3px solid var(--accent);outline-offset:-3px}.drawing-tools{flex-wrap:wrap;border-top:1px solid var(--line);padding-bottom:0}
.positions{display:inline-flex;flex-wrap:wrap;gap:3px}.positions button{min-height:34px;font-size:12px}.positions button[aria-pressed=true]{background:#2e392d;color:white;border-color:#2e392d}#ratio{font-variant-numeric:tabular-nums}#description{margin:8px 16px 12px;color:#55594d}
</style>
<script>
const viewOptions=new URLSearchParams(location.search);
document.documentElement.classList.toggle('lab-embedded',viewOptions.get('embed')==='1');
document.documentElement.classList.toggle('lab-reduce',viewOptions.get('reduce')==='1');
</script>
</head>
<body class="lab-example">
<main><h1>Gallery Layout Comparison</h1><p>Compare the entrance and reading area on the same floor plan.</p>
<section class="panel" aria-label="Gallery floor plan">
 <div class="drawing-head"><strong>Gallery, first floor</strong><span>Floor plan · A–01</span></div>
 <div class="sheet" id="sheet">
  <canvas class="plan after" width="1280" height="640" aria-hidden="true"></canvas>
  <canvas class="plan before" width="1280" height="640" aria-hidden="true"></canvas>
  <div class="divider" aria-hidden="true"><b>↔</b></div>
  <input id="split" type="range" min="0" max="100" step="1" value="50" aria-label="Portion of the original plan visible" aria-describedby="description">
 </div>
 <div class="drawing-tools"><div class="positions" aria-label="Comparison position"><button data-value="100">Before</button><button data-value="50">Half and half</button><button data-value="0">After</button></div><output id="ratio" for="split"></output></div>
 <p id="description">The revision replaces the storage room on the right with a reception desk and adds reading seats in the central passage.</p>
</section>
</main>
<script>
function drawPlan(canvas, renovated) {
  const c=canvas.getContext('2d');
  c.scale(2,2);
  const ink=renovated?'#2d534d':'#5a483a';
  const ground=renovated?'#e4ece0':'#f1e5ce';
  c.fillStyle=ground;c.fillRect(0,0,640,320);
  const stroke=(x1,y1,x2,y2,width=1)=>{c.beginPath();c.moveTo(x1,y1);c.lineTo(x2,y2);c.lineWidth=width;c.stroke()};
  c.strokeStyle=renovated?'#bccfbd':'#ddd0b7';
  for(let x=0;x<=640;x+=14)stroke(x,0,x,320,.45);
  for(let y=0;y<=320;y+=14)stroke(0,y,640,y,.45);
  c.strokeStyle=ink;
  const room=(x,y,w,h)=>{c.lineWidth=4;c.strokeRect(x,y,w,h)};
  room(58,48,522,218);stroke(270,48,270,147,5);stroke(410,48,410,266,5);stroke(58,148,410,148,5);
  const gap=(x,y,w)=>{c.fillStyle=ground;c.fillRect(x,y-4,w,8);c.strokeStyle=ink;stroke(x,y,x,y-24,1);c.beginPath();c.arc(x,y,24,-Math.PI/2,0);c.stroke()};
  gap(110,266,25);gap(312,148,25);
  c.lineWidth=1;c.strokeStyle=ink;
  const furniture=(x,y,w,h)=>{c.fillStyle=renovated?'#99b6a3':'#d2b896';c.fillRect(x,y,w,h);c.strokeRect(x,y,w,h)};
  for(const x of [436,488,540]){furniture(x,67,12,43);furniture(x,196,12,43)}
  for(const x of [82,117,152,187])furniture(x,64,18,12);
  if(renovated){
    furniture(298,69,83,12);furniture(369,81,12,22);
    for(const x of [184,242,300]){furniture(x,185,31,14);furniture(x+9,176,12,6);furniture(x+9,202,12,6)}
  }else{for(const x of [289,316,343,370])furniture(x,65,13,41)}
  c.font='600 15px sans-serif';c.textAlign='center';c.fillStyle=ink;
  for(const [label,x,y] of [['Entrance',157,121],[renovated?'Reception':'Storage',337,124],[renovated?'Reading':'Passage',240,238],['Gallery',498,157]])c.fillText(label,x,y);
  c.lineWidth=.75;stroke(58,31,580,31);stroke(58,27,58,36);stroke(580,27,580,36);c.font='10px sans-serif';c.fillText('12 600',319,25);
  stroke(40,48,40,266);stroke(36,48,44,48);stroke(36,266,44,266);
  c.textAlign='left';c.font='12px sans-serif';c.fillText(renovated?'After':'Before',58,299);
  c.fillStyle=ink;c.fillRect(494,290,43,3);c.strokeRect(537,290,43,3);c.font='10px sans-serif';c.fillText('0',494,306);c.fillText('2 m',561,306);
}
drawPlan(document.querySelector('.after'),true);
drawPlan(document.querySelector('.before'),false);
const slider = document.querySelector("#split");
const sheet = document.querySelector("#sheet");
function render() {
  const before = slider.valueAsNumber;
  const label = "Before " + before + "% · After " + (100 - before) + "%";
  sheet.style.setProperty("--split", before + "%");
  document.querySelector("#ratio").textContent = label;
  slider.setAttribute("aria-valuetext", label);
  document.querySelectorAll("[data-value]").forEach(b=>b.setAttribute("aria-pressed",String(Number(b.dataset.value)===before)));
}
slider.addEventListener("input", render);
document.querySelectorAll(".positions button").forEach(button => {
  button.addEventListener("click", () => {
    slider.value = button.dataset.value;
    render();
  });
});
render();
</script>
</body></html>
```
