All effectsScroll & reveal

Scroll-Linked Line Drawing

滚动联动画线

A drawn stroke advances along a path as scroll progress increases.

Scroll inside the preview

Where to use it

Trace a journey or progress through a small diagram. Choose a route with a legible start and end, and pair its key stops with nearby text so the motion is easy to follow.

Practical example

A park route guide traces its walking loop from the visitor center to the lake and back as the reader moves through each stop's description. The labeled stops stay visible throughout.

AI prompt

Add Scroll-Linked Line Drawing to [target]. Precompute the length of each segment and draw only the portion covered by the normalized scroll distance. Keep the full stroke visible for reduced motion. Keep the content readable in reduced-motion mode and preserve keyboard navigation.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Scroll-Linked Line Drawing</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .scroll-panel {
      position: relative;
      width: min(100%, 215px);
      height: 133px;
      overflow: auto;
      overscroll-behavior: contain;
      scrollbar-width: thin;
      scrollbar-color: #9881cb #2e2540;
      border: 1px solid #806ca8;
      border-radius: 9px;
      background: #2d2342;
      color: #f5efff;
      font: 650 12px system-ui, sans-serif;
    }
    .scroll-panel p { margin: 0; }
    .scroll-panel hr { border: 0; margin: 6px 0; height: 5px; border-radius: 9px; background: #ae96dc4a; }
    .scroll-panel hr:nth-of-type(2n) { width: 72%; }
    .scroll-note { color: #ddcff1; text-align: center; font-size: 10px; }
    .scroll-panel:focus-visible { outline-offset: 2px; }
    @media (prefers-reduced-motion: reduce) {
      .scroll-panel, .scroll-panel *, .scroll-panel *::before, .scroll-panel *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
      }
    }

    .scroll-track {
      position: relative;
      min-height: 335px;
      background: repeating-linear-gradient(0deg, #9c7fac26 0 1px, transparent 1px 23px);
    }
    .scroll-pin {
      position: sticky; top: 0; height: 124px;
      display: grid; align-content: center; justify-items: center; gap: 5px;
      padding: 8px;
    }
    .motion-art {
      position: relative; width: 92px; height: 58px;
      border: 1px solid #dfc6f6b0; border-radius: 11px;
      background: radial-gradient(circle at 70% 30%,#fbe0a6 0 9px,transparent 10px),
        linear-gradient(145deg,#b98cb2 30%,#8d6abc 65%,#49346e);
      box-shadow: 0 5px 8px #1c142b70;
    }
    .motion-art::after {
      content: ''; position: absolute; inset: 40% 0 0; border-radius: 0 0 10px 10px;
      background: #443469; clip-path: polygon(0% 40%,32% 0%,63% 62%,81% 20%,100% 56%,100% 100%,0 100%);
    }
    canvas { display: block; width: min(100%, 160px); height: 56px; background: #3e3057; border: 1px solid #9478ca; border-radius: 8px; }
  </style>
</head>
<body>
  <main class="demo">
    <div class="scroll-panel" role="region" tabindex="0" aria-label="Scroll line drawing demo">
    <div class="scroll-track"><div class="scroll-pin">
      <canvas width="320" height="112" aria-hidden="true"></canvas>
      <p class="scroll-note">Scroll to draw ↓</p>
    </div></div>
    </div>
  </main>
  <script>
    const canvas = document.querySelector('canvas');
    const context = canvas.getContext('2d');
    const panel = document.querySelector('.scroll-panel');
    const reduce = matchMedia('(prefers-reduced-motion: reduce)');
    const points = [[23,83],[72,54],[123,75],[175,28],[232,56],[295,28]];
    const segments = points.slice(1).map(([x, y], index) => {
      const start = points[index];
      return { start, x, y, length: Math.hypot(x - start[0], y - start[1]) };
    });
    const length = segments.reduce((sum, segment) => sum + segment.length, 0);
    function draw() {
      if (!context) return;
      const span = panel.scrollHeight - panel.clientHeight;
      let remaining = length * (reduce.matches || !span ? 1 : Math.min(1, .11 + panel.scrollTop / span * .89));
      context.clearRect(0, 0, canvas.width, canvas.height);
      context.beginPath();
      context.moveTo(...points[0]);
      for (const { start, x, y, length } of segments) {
        const amount = Math.min(1, remaining / length);
        context.lineTo(start[0] + (x - start[0]) * amount, start[1] + (y - start[1]) * amount);
        remaining -= length;
        if (remaining <= 0) break;
      }
      context.strokeStyle = '#e0b4ff';
      context.lineWidth = 8;
      context.lineJoin = 'round';
      context.lineCap = 'round';
      context.stroke();
    }
    panel.addEventListener('scroll', draw, { passive: true });
    reduce.addEventListener('change', draw);
    draw();
  </script>
</body>
</html>

Original example by UI Skills