# 滚动章节

阅读制作流程时，海报随当前步骤逐步成形。

## 试一试

滚动三个章节，或点击阶段名称，观察海报从草图变为成品。

## 交互细节

海报预览中的草图、色版和印制效果分别对应一个制作阶段。

## 各平台的操作方式

- iOS · 向上滑动或点按阶段名称，查看海报的变化。
- Android · 向上滑动或点按阶段名称，查看海报的变化。
- PC · 滚动三个章节，或点击阶段名称，观察海报从草图变为成品。

## 适用场景

需要边阅读文字边观察图像变化的制作教程。

## 实现要求

> 让固定的海报预览随当前章节更新，并提供直接切换阶段的控件。

## 来源与致谢

章节导航参考了《The Boat》。示例中的海报及其印制过程由 UI Motion Lab 创作。

- [澳大利亚 SBS 的《The Boat》](<https://www.sbs.com.au/theboat/>)
- [Distil 制作手记](<https://www.distil.im/projects/the-boat>)
- [Awwwards 项目页](<https://www.awwwards.com/sites/the-boat>)

## 如何验证效果

在宽屏上滚动时，海报留在文字旁边。可以依次滚过三个阶段，也可以通过章节链接跳转。选择链接后，检查插图、当前章节和阅读位置是否一致。参考作品《The Boat》于 2015 年 5 月 7 日获得 Awwwards 的 Site of the Day 奖。

## 独立 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>残影 · 滚动章节</title>
<script>
const labSettings=new URLSearchParams(location.search);
document.documentElement.classList.toggle('lab-embedded',labSettings.get('embed')==='1');
document.documentElement.classList.toggle('lab-reduce',labSettings.get('reduce')==='1');
</script>

<style>*{box-sizing:border-box}
:root{--paper:#eeeadc;--ink:#292c22;--muted:#666858;--accent:#9e3f26;color-scheme:light}
body{margin:0;background:var(--paper);color:var(--ink);font:15px/1.5 Inter,Pretendard,"Malgun Gothic",system-ui,sans-serif}
main{max-width:1200px;margin:0 auto;padding:clamp(22px,4vw,58px) clamp(16px,4vw,58px) 60px}
.intro{max-width:65ch;margin-bottom:24px}.intro h1{font-size:clamp(27px,4.5vw,55px);letter-spacing:-.07em;line-height:1.12;margin:0 0 8px}.intro p{margin:0;color:var(--muted)}
.pressroom{display:grid;grid-template-columns:minmax(0,1.58fr) minmax(180px,.75fr);align-items:start;gap:clamp(18px,4vw,66px)}
.mount{position:sticky;top:18px;margin:0;min-width:0;background:#d1cfbe;box-shadow:inset 0 1px #fff6}
.art{width:100%;height:clamp(240px,65vh,630px);display:flex;align-items:center;justify-content:center;background:linear-gradient(130deg,#dad9cc,#c9c6b5)}
.paper{position:relative;aspect-ratio:3/4;height:85%;max-width:88%;background:#f8f0d8;transform:rotate(-1.2deg);box-shadow:1px 1px 0 #817859,3px 6px 10px #35352636}
canvas{position:absolute;inset:0;width:100%;height:100%;opacity:0;transition:opacity .27s ease-out}
figure[data-scene="sketch"] .pass-sketch,figure[data-scene="ink"] .pass-ink,figure[data-scene="print"] .pass-print{opacity:1}
figcaption{display:flex;justify-content:space-between;align-items:center;border-top:1px solid #abaa95;color:#4a4e3c;padding:8px 12px;font:550 10px/1.4 Consolas,monospace;letter-spacing:.045em}
figcaption strong{font:650 12px/1.3 Inter,Pretendard,system-ui,sans-serif;letter-spacing:0}
main nav{position:sticky;top:0;z-index:2;display:flex;gap:0;padding:0;background:var(--paper);border-bottom:1px solid #aaa994;margin:0}
main nav a{color:var(--muted);padding:12px 9px;font-weight:650;font-size:12px;text-decoration:none;white-space:nowrap;border-bottom:2px solid transparent}
main nav a:first-child{padding-left:0}main nav a[aria-current]{color:var(--accent);border-bottom-color:var(--accent)}
section.chapter{min-height:74vh;padding:clamp(24px,5vw,80px) 0;scroll-margin-top:45px;border-bottom:1px solid #b8b7a5}
.chapter .sequence{font:550 11px/1.2 Consolas,monospace;color:#615b45;letter-spacing:.06em}
.chapter h2{font-size:clamp(22px,3vw,38px);line-height:1.12;letter-spacing:-.065em;margin:10px 0 12px}
.chapter p{font-size:clamp(13px,1.35vw,17px);line-height:1.65;margin:0;word-break:keep-all}
.lab-embedded .intro{display:none}
.lab-embedded main{padding-top:clamp(16px,2vw,28px)}
.lab-reduce *{transition:none!important;scroll-behavior:auto!important}
a:focus-visible{outline:3px solid #875032;outline-offset:2px}
@media(max-width:600px){
 main{padding:16px 16px 36px}.intro{margin:8px 0 18px}
 .pressroom{display:flex;flex-direction:column;gap:0}
 .mount{top:0;width:100%;z-index:2;--art-height:clamp(125px,43vh,330px)}
 .art{height:var(--art-height)}figcaption{padding:5px 9px}
 .chapters{width:100%}main nav{top:calc(clamp(125px,43vh,330px) + 27px);background:var(--paper);z-index:2}
 main nav a{padding:10px 13px}.lab-embedded main{padding:0 12px 30px}.lab-embedded .mount{width:calc(100% + 24px);margin-inline:-12px}
 section.chapter{min-height:66vh;padding:17px 2px 50px;scroll-margin-top:calc(clamp(125px,43vh,330px) + 78px)}
}
@media(max-height:380px){.pressroom{display:grid;grid-template-columns:minmax(0,1fr) minmax(150px,.8fr);gap:18px}.art{height:70vh}.mount{width:auto!important;margin:0!important;top:0}.chapters nav{top:0}section.chapter{scroll-margin-top:42px}}
@media(prefers-reduced-motion:reduce){canvas{transition:none}}</style></head>
<body class="lab-example">

<main><header class="intro"><h1>残影</h1><p>滚动浏览一张海报从绘画、套准到印制的三个阶段。</p></header>
<div class="pressroom"><figure class="mount" data-scene="sketch" role="img" aria-label="《残影》海报的制作阶段">
  <div class="art"><div class="paper"><canvas class="pass-sketch" aria-hidden="true"></canvas><canvas class="pass-ink" aria-hidden="true"></canvas><canvas class="pass-print" aria-hidden="true"></canvas></div></div>
  <figcaption><strong>草图</strong><span>PLATE 01 / 03</span></figcaption>
</figure><div class="chapters"><nav aria-label="制作阶段"><a href="#sketch" aria-current="step">草图</a><a href="#ink">色版</a><a href="#print">印制</a></nav>
<section id="sketch" class="chapter"><span class="sequence">01 / 03</span><h2>草图</h2><p>画出三层山脊，把太阳放在最远处的山脊后方。曲线之间的间距营造出山峦的纵深。</p></section>
<section id="ink" class="chapter"><span class="sequence">02 / 03</span><h2>色版</h2><p>先用朱红色印上太阳和远山。第二块色版叠印后，中景山脊在油墨重合处呈现绿色。</p></section>
<section id="print" class="chapter"><span class="sequence">03 / 03</span><h2>印制</h2><p>最后印上深色前景和标题。以纸张底色留出的细线勾勒山脊。</p></section>
</div></div></main>

<script>
const passes = ['sketch','ink','print'];

// All marks and contours are drawn from this composition. Render the three
// plates once so scrolling only crossfades between finished canvases.
function drawPoster(canvas, pass) {
  const c = canvas.getContext('2d');
  if (!c) return;
  const W=720, H=960;
  canvas.width=W; canvas.height=H;
  let state=863171;
  const noise=()=>{state=(Math.imul(state,1664525)+1013904223)>>>0;return state/4294967296};
  const ink=pass==='sketch'?'#897f65':'#273c36';
  const completed=pass==='print';
  c.fillStyle='#f3e8c9';c.fillRect(0,0,W,H);
  for (let i=0;i<17500;i++) {
    const dotSize=noise()*2.4+.5;
    c.fillStyle=noise()>.5?'rgba(85,58,26,.075)':'rgba(255,255,240,.22)';
    c.fillRect(noise()*W,noise()*H,dotSize,dotSize*.58);
  }
  const text=(s,x,y,font,color,align='left')=>{c.font=font;c.fillStyle=color;c.textAlign=align;c.fillText(s,x,y)};
  const rule=(x1,y1,x2,y2,color,width=1)=>{c.beginPath();c.moveTo(x1,y1);c.lineTo(x2,y2);c.strokeStyle=color;c.lineWidth=width;c.stroke()};
  const top=155,bottom=779,left=42,right=W-42;
  const field=() => {c.beginPath();c.rect(left,top,right-left,bottom-top);c.clip()};
  const pencil='#988c71';
  c.save();field();
  if(pass==='sketch'){
    for(let x=left;x<right;x+=52)rule(x,top,x,bottom,'#c1b499',.55);
    for(let y=top;y<bottom;y+=52)rule(left,y,right,y,'#c1b499',.55);
  }
  const cx=442,cy=360,r=151;
  c.save();c.beginPath();c.arc(cx,cy,r,0,Math.PI*2);c.clip();
  if(pass!=='sketch'){c.fillStyle=completed?'#c95330':'#d96b3d';c.fillRect(cx-r,cy-r,2*r,2*r)}
  for(let y=-r;y<=r;y+=5){
    const chord=Math.sqrt(r*r-y*y);
    const drift=2.8*Math.sin(y*.065);
    rule(cx-chord+drift,cy+y,cx+chord,cy+y+(pass==='sketch'?0:3),pass==='sketch'?'#a79a7f':'#efae72',pass==='sketch'?.5:.85);
  }
  c.restore();
  c.beginPath();c.arc(cx,cy,r,0,Math.PI*2);c.lineWidth=1;c.strokeStyle=pass==='sketch'?pencil:'#c46c43';c.stroke();
  function crest(x,ridge) {
    const a=x/W;
    return [515,626,716][ridge]-[91,107,75][ridge]*Math.sin(a*4.15+ridge*.68)
      +26*Math.sin(a*10.5+ridge*1.31)+12*Math.sin(a*19.3+.75*ridge)+5*Math.sin(a*39.1+.6*ridge);
  }
  for(let ridge=0;ridge<3;ridge++){
    const outline=()=>{c.beginPath();for(let x=left;x<=right+4;x+=3){const y=crest(x,ridge);if(x===left)c.moveTo(x,y);else c.lineTo(x,y)}};
    c.save();outline();c.lineTo(right+5,bottom+2);c.lineTo(left-2,bottom+2);c.closePath();
    c.fillStyle=pass==='sketch'?'#eee4c7':(completed?['#d58856','#768471','#283e37']:['#d89b65','#ab9b68','#758875'])[ridge];
    c.fill();c.clip();
    for(let j=0;j<105;j++){
      c.beginPath();
      for(let x=left-2;x<=right+3;x+=4){
        const d=j*(completed?4.2:5.2);
        const y=crest(x,ridge)+d+Math.sin(x*.021+j*.11)*Math.min(13,j*.38)+Math.sin(x*.061+j)*.8;
        if(x===left-2)c.moveTo(x,y);else c.lineTo(x,y);
      }
      c.strokeStyle=pass==='sketch'?'rgba(107,98,73,.46)':(completed?['#a6653f','#4d6c60','#a8ad8e'][ridge]:['#9f714d','#607664','#c2b68e'][ridge]);
      c.lineWidth=pass==='sketch'?.75:(ridge===2?1.35:1.1);
      c.stroke();
    }
    c.restore();outline();c.strokeStyle=pass==='sketch'?pencil:ink;c.lineWidth=pass==='sketch'?.8:1.25;c.stroke();
  }
  c.restore();
  [left,right].forEach(x=>[top,bottom].forEach(y=>{rule(x-8,y,x+8,y,'#635d4d',1);rule(x,y-8,x,y+8,'#635d4d',1)}));
  text('MOTION LAB   /   PRINT STUDIES',left,56,'600 14px Consolas, monospace',ink);
  text('EDITION 01',right,56,'600 14px Consolas, monospace',ink,'right');
  rule(left,76,right,76,'#8e8469',1);
  text('AFTERIMAGE',left,129,'bold 51px Georgia, serif',pass==='sketch'?'#93896f':ink);
  if(completed){
    text('残影',left,866,'bold 67px "Malgun Gothic", sans-serif',ink);
    text('Landscape',right,837,'italic 30px Georgia, serif',ink,'right');
    text('in three plates.',right,870,'italic 30px Georgia, serif',ink,'right');
  } else {
    c.save();c.strokeStyle=pencil;c.lineWidth=1;c.font='bold 67px "Malgun Gothic",sans-serif';c.textAlign='left';c.strokeText('残影',left,866);c.restore();
    for(let y=821;y<862;y+=13)rule(right-120,y,right,y,pencil,1);
  }
  rule(left,901,right,901,'#8e8469',1);
  text('FIELD NOTES  /  LANDSCAPE NO.01',left,929,'600 14px Consolas, monospace',ink);
  text('ML–053',right,929,'600 14px Consolas, monospace',ink,'right');
}
passes.forEach(name=>drawPoster(document.querySelector('.pass-'+name),name));
const chapters = Array.from(document.querySelectorAll('.chapter'));
const links = Array.from(document.querySelectorAll('main nav a'));
const figure = document.querySelector('figure');
const caption = figure.querySelector('figcaption');
let pending = false;
function updateScene(){
  pending = false;
  const narrow = innerWidth<=600 && innerHeight>380;
  const navBottom = document.querySelector('main nav').getBoundingClientRect().bottom;
  const marker = narrow ? Math.min(innerHeight-4,Math.max(innerHeight*.48,navBottom+21)) : innerHeight*.46;
  let index = 0;
  for(let i=1;i<chapters.length;i++) if(chapters[i].getBoundingClientRect().top<=marker) index=i;
  if(scrollY+innerHeight >= document.documentElement.scrollHeight-2) index=chapters.length-1;
  const chapter = chapters[index], name=chapter.querySelector('h2').textContent;
  figure.dataset.scene=chapter.id;
  figure.setAttribute('aria-label','《残影》海报， '+name);
  caption.querySelector('strong').textContent=name;
  caption.querySelector('span').textContent='PLATE 0'+(index+1)+' / 03';
  links.forEach((link,i)=>{if(i===index)link.setAttribute('aria-current','step');else link.removeAttribute('aria-current')});
}
function schedule(){if(!pending){pending=true;requestAnimationFrame(updateScene)}}
window.addEventListener('scroll',schedule,{passive:true});
window.addEventListener('resize',schedule);
window.addEventListener('pageshow',schedule);
window.addEventListener('hashchange',schedule);
updateScene();
</script></body></html>
```
