# How a layer follows scroll

How Diorama measures elements, computes progress and pin, reads scroll from the window, GSAP or Lenis, and keeps up with compositor scroll.

The canvas is one fixed layer over the whole viewport, yet every copy on it sits exactly on its element while the page scrolls. This page explains how the boxes are measured, where the scroll position comes from, and what happens when the browser scrolls faster than JavaScript can draw.

A photo drawn by Diorama next to a diagram of its DOM box, its plane and the compositor offset between two frames, with a slider for the scroll the compositor has done, a followScroll switch and a 50 ms stall per frame.

```html title="index.html"
<div class="scene">
  <img class="photo" src="/media/watcher.jpg" alt="A paper figure watching the ridges" width="300" height="400" data-dio="image" />
  <svg class="diagram" viewBox="0 0 300 400" role="presentation"></svg>
</div>
```

```html title="panel.html"
<label>Compositor scroll since the last frame <input class="ahead" type="range" min="0" max="400" step="1" value="160" /> <output class="ahead-value">160</output> px</label>
<label><input class="follow" type="checkbox" checked /> followScroll</label>
<label><input class="stall" type="checkbox" /> 50 ms stall per frame</label>
<button class="up" type="button">Scroll up</button>
<button class="down" type="button">Scroll down</button>
```

```ts title="script.ts"
import { app } from '../../docs/diorama.ts'

const SVG = 'http://www.w3.org/2000/svg'
const OFF = 'canvas[data-dio-canvas] { animation: none !important }'

// The diagram is a model of one moment between two frames: the compositor has scrolled the page
// by the slider's amount and the main thread has not drawn yet. The switches act on this page
// for real: a stylesheet that removes the canvas animation turns followScroll off (the library
// checks for it every frame), and the stall is a system that blocks each frame for 50 ms.
export default (example: HTMLElement) => {
  const svg = example.querySelector('.diagram')
  const ahead = example.querySelector<HTMLInputElement>('.ahead')
  const aheadValue = example.querySelector('.ahead-value')
  const follow = example.querySelector<HTMLInputElement>('.follow')
  const stall = example.querySelector<HTMLInputElement>('.stall')
  if (!svg) return
  const off: (() => void)[] = []
  const on = (
    el: Element | null,
    type: string,
    listener: () => void,
  ) => {
    el?.addEventListener(type, listener)
    off.push(() => el?.removeEventListener(type, listener))
  }

  const sheet = document.createElement('style')
  sheet.textContent = OFF
  on(follow, 'change', () => {
    if (follow?.checked) sheet.remove()
    else document.head.append(sheet)
    // The library re-reads the switch in its next frame.
    app.invalidate()
    draw()
  })
  off.push(() => {
    sheet.remove()
    app.invalidate()
  })

  let busy: (() => void) | undefined
  on(stall, 'change', () => {
    busy?.()
    busy = undefined
    if (stall?.checked)
      busy = app.system('render', () => {
        const end = performance.now() + 50
        while (performance.now() < end);
      })
  })
  off.push(() => busy?.())

  on(example.querySelector('.up'), 'click', () =>
    window.scrollBy({ top: -400, behavior: 'smooth' }),
  )
  on(example.querySelector('.down'), 'click', () =>
    window.scrollBy({ top: 400, behavior: 'smooth' }),
  )
  on(ahead, 'input', () => draw())

  const style = getComputedStyle(example)
  const accent = style.getPropertyValue('--link').trim() || '#86393b'
  const muted =
    style.getPropertyValue('--text-muted').trim() || '#6d282b'
  const el = (
    tag: string,
    attrs: Record<string, string | number>,
    text?: string,
  ) => {
    const node = document.createElementNS(SVG, tag)
    for (const [k, v] of Object.entries(attrs))
      node.setAttribute(k, String(v))
    if (text) node.textContent = text
    return node
  }
  function draw(): void {
    if (!svg) return
    const s = Number(ahead?.value ?? 0)
    if (aheadValue) aheadValue.textContent = String(s)
    const following = follow?.checked !== false
    // 300 units are a 1000 px viewport; the box sat at 450 px when the last frame drew.
    const k = 0.3
    const drawnAt = 135
    const dom = drawnAt - s * k
    const plane = following ? dom : drawnAt
    const h = 120
    svg.replaceChildren(
      el('rect', {
        x: 10,
        y: 0,
        width: 280,
        height: 300,
        fill: 'none',
        stroke: muted,
        'stroke-dasharray': 4,
      }),
      el('text', { x: 16, y: 16, fill: muted }, 'viewport'),
      el('rect', {
        x: 40,
        y: dom,
        width: 90,
        height: h,
        fill: 'none',
        stroke: muted,
        'stroke-width': 2,
      }),
      el('text', { x: 40, y: dom - 6, fill: muted }, 'DOM box'),
      el('rect', {
        x: 170,
        y: plane,
        width: 90,
        height: h,
        fill: accent,
        opacity: 0.3,
        stroke: accent,
      }),
      el('text', { x: 170, y: plane - 6, fill: accent }, 'plane'),
      ...(following && s > 0
        ? [
            el('line', {
              x1: 270,
              y1: drawnAt,
              x2: 270,
              y2: plane,
              stroke: accent,
              'stroke-width': 2,
            }),
            el('line', {
              x1: 264,
              y1: drawnAt,
              x2: 276,
              y2: drawnAt,
              stroke: accent,
              'stroke-width': 2,
            }),
            el(
              'text',
              { x: 170, y: plane + h + 16, fill: accent },
              'moved on the compositor',
            ),
          ]
        : []),
      el(
        'text',
        { x: 14, y: 330, fill: muted },
        `plane behind its box: ${following ? 0 : s} px`,
      ),
      el(
        'text',
        { x: 14, y: 350, fill: muted },
        `compositor offset of the canvas: ${following ? s : 0} px`,
      ),
    )
  }
  draw()
  return () => {
    for (const f of off) f()
  }
}
```

## One canvas, many layers

`start()` creates one canvas with `position: fixed` that covers the viewport, above the page by default and below it with `canvas: 'behind'`. Pointer events pass through it, so the page stays interactive.

The camera is set so that at depth 0 one world unit is one CSS pixel. Each element with effects gets a layer, a group placed at the centre of the element’s box and scaled to its size. Moving a copy to its element is a matter of writing the box into the layer every frame.

## Measuring boxes

Boxes are read in the `layout` stage of each frame, in one pass of reads before any write, so layout is forced at most once.

- Visible layers are measured with `getBoundingClientRect` in every drawn frame. That covers transforms on the element and its ancestors, sticky and fixed positions and inline styles written by GSAP or a slider.
- Layers within one viewport of the screen are measured as well, so a copy is in place the moment it scrolls in.
- Layers further away keep their page position and only subtract the scroll.
- A clipping ancestor (`overflow` other than `visible`, or `clip-path: inset()`) is measured with the element and clips the copy, so a mask reveal works on the canvas as it does in the DOM.

`data-dio-track="layout"` switches an element to the cheap path for good: its page position is cached when layout changes and the scroll is subtracted. Use it for large grids of images that never move by themselves.

A DOM change that fires no event, such as a CSS transition on a parent, would not wake the loop. A motion watcher covers it: for visible elements with a transform, `will-change`, a fixed position or opacity below 1, it compares boxes once per display frame and requests a frame only when one moved. It sleeps after a second without change and wakes on scroll, pointer, resize and the start of a CSS transition or animation.

## Progress and pin

Two scroll inputs come from the element’s box, along the scroll axis.

`progress` is 0 when the element’s top enters at the bottom of the viewport and 1 when its bottom leaves at the top. It runs for any element.

```text
progress = clamp((viewportHeight - top) / (viewportHeight + height), 0, 1)
```

`pin` describes a tall element whose content sticks while it scrolls past: 0 while its top is below the viewport top, 1 once it has scrolled `height - viewportHeight` past it. An element no taller than the viewport switches from 0 to 1 as its top passes the viewport top; use `progress` for those.

```text
pin = clamp(-top / (height - viewportHeight), 0, 1)
```

CSS `position: sticky` needs nothing else. A ScrollTrigger pin wraps the section in a spacer and fixes it inline, which this formula reads only as 0 or 1. With `gsap` and `ScrollTrigger` passed to `start()`, effects inside a pinned trigger take its scrubbed progress as `pin` instead. For any other source, give an element its own.

```ts
const section = document.querySelector('#story')
if (section) app.effects.pin(section, () => trigger.progress)
```

The function is read once per frame. Calling the returned function restores the built-in formula.

## Where the scroll position comes from

Without plugins, Diorama caches the position from window `scroll` events and requests a frame on each one. Plugins replace the source.

| Setup                | Frames come from                                   | Scroll is read               |
| -------------------- | -------------------------------------------------- | ---------------------------- |
| Native scroll        | `requestAnimationFrame`                            | from the last `scroll` event |
| `gsap` in `start()`  | `gsap.ticker`, after the tweens                    | live, inside the frame       |
| `lenis` in `start()` | `requestAnimationFrame`, after `lenis.raf`         | from Lenis                   |
| Both                 | `gsap.ticker`, after `lenis.raf` and ScrollTrigger | from Lenis                   |

In a browser frame, `requestAnimationFrame` callbacks run in order and then the page paints once. Diorama runs its frame in the same callback, after everything that moves the page: GSAP tweens, then Lenis, then ScrollTrigger pins and scrubs, then the Diorama frame. The canvas and the DOM end up in the same paint.

`velocity` is the smoothed scroll speed from -1 to 1, where 1 is 3000 px a second downwards. A jump longer than the viewport, such as an anchor link or scroll restoration, is a jump rather than motion and does not flash the velocity.

## Compositor scroll and followScroll

Browsers scroll on a separate compositor thread for touch, the keyboard, the scrollbar and the wheel without smooth scrolling. The compositor moves the page at once, while a fixed canvas keeps showing the last frame the main thread drew. Until the next frame lands, copies trail their elements: up to 400 px on a slow integrated GPU. Lenis does not help with keys or the scrollbar, because it handles only the wheel.

`followScroll`, on by default, closes the gap with a CSS scroll-driven animation. The canvas gets an animation on the root scroll timeline that moves it up by exactly the scroll offset, and the compositor applies it in each of its own frames. Each Diorama frame writes the opposite offset with `translate`, in the same task as the draw. At rest the two cancel out; between frames the canvas travels with the page.

| Path, integrated GPU            | Without       | With followScroll           |
| ------------------------------- | ------------- | --------------------------- |
| Wheel without Lenis             | 400 px        | 0                           |
| Keyboard with Lenis             | 330 to 530 px | up to 8 px in single frames |
| Scrollbar with Lenis            | 440 px        | up to 1 px                  |
| Wheel and `scrollTo` with Lenis | 0             | 0                           |

The cost is that everything on the canvas moves with the page until the frame lands, including content fixed to the viewport. A strip of the same height stays empty at the viewport edge for that moment. A site with a full-screen scene and native scrolling turns it off with `stage({ followScroll: false })` in `plugins`.

`followScroll` covers vertical page scroll only. It needs CSS scroll-driven animations, so browsers without them keep the earlier behavior. It leaves a canvas you pass yourself with `stage({ canvas })` alone, and it turns itself off if a stylesheet removes the animation, for example `* { animation: none !important }`.

## Page swaps

Routers such as Astro and Turbo replace the whole `<body>`. The canvas Diorama created moves into the new body in the same task, before the paint, with the same WebGL context. Size observers move to the new body and measure the new page, so the first frame of the new page is already in place.
