# NASA Worldview · 滑动对比两个日期的卫星影像

在 Worldview 的对比模式中，为同一地区的两组影像选择不同日期。左右拖动分界线，就能在地图位置保持不变时逐步查看任一侧的影像。

[官方来源](https://worldview.earthdata.nasa.gov/index.html)

## 相关动效 · 图像对比

# 图像对比

拖动分隔线，对照同一张平面图的两个版本。

## 试一试

拖动分隔线，或选择下方预设，对照原方案与修改方案。

## 交互细节

分隔线初始位于 50%。三个预设分别展示原方案、各半对照和修改方案。

## 各平台的操作方式

- iOS · 左右拖动分隔线对比两张图，也可选择下方的预设比例。
- Android · 左右拖动分隔线对比两张图，也可选择下方的预设比例。
- PC · 拖动分隔线，或选择下方预设，对照原方案与修改方案。

## 适用场景

改版前后对比、平面图校对或摄影后期效果展示。

## 实现要求

> 叠放并对齐两张图，用可拖动、可键盘操作的分隔线调整显示比例。

## 来源与致谢

- [ESA/Webb 图像对比工具](<https://esawebb.org/images/comparisons/weic2216/>)

## 如何验证效果

ESA/Webb 的对比工具叠放同一对象的两幅图像。本示例中的两张平面图共用同一比例和原点，拖动滑块时只改变上层图像的可见范围。分别查看两端，再回到中点，然后用方向键、Home 和 End 重复操作。也可以用三个位置按钮代替拖动。

## 独立 HTML 示例

将以下代码保存为 HTML 文件，用浏览器打开即可离线体验。

```html
<!doctype html>
<html lang="zh-Hans"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>展厅布局对比 · 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>展厅布局对比</h1><p>在同一张平面图上，对比入口和阅读区的布局。</p>
<section class="panel" aria-label="展厅平面图">
 <div class="drawing-head"><strong>展厅一楼</strong><span>平面图 · 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="原方案显示比例" aria-describedby="description">
 </div>
 <div class="drawing-tools"><div class="positions" aria-label="分隔线位置"><button data-value="100">修改前</button><button data-value="50">各半</button><button data-value="0">修改后</button></div><output id="ratio" for="split"></output></div>
 <p id="description">新版将右侧的储藏室改为接待台，并在中央通道增设阅读座位。</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 [['入口',157,121],[renovated?'接待台':'储藏室',337,124],[renovated?'阅读区':'通道',240,238],['展厅',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?'修改后':'修改前',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 + "% · 修改后 " + (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>
```
