Scroll-scrubbed 3D demo

A paper boat, steered by your scrollbar.

Scroll to carry it 38 km from a mountain spring to the sea. Every frame is computed from one number, so you can stop, reverse or jump anywhere.

Loading the 3D scene…

1 of 6

The spring

Skip to how it's built ↓

    How this page is wired

    The effect has four layers. None of them needs an animation library. Three.js only draws the frame.

    Layer 1: CSS

    Pin the stage

    The section is 8 screens tall and holds a stage that sticks to the top. The extra height is what you are scrolling through, while the canvas stays still on screen.

    .story { height: 800svh; }
    .stage { position: sticky; top: 0; height: 100svh; }
    Layer 2: JavaScript

    Turn scroll into eased time

    Scroll position sets a target between 0 and 40. The displayed time chases that target every frame, which gives the motion its weight. Watch the two markers in the "Under the hood" panel separate when you scroll fast.

    const r = story.getBoundingClientRect();
    const progress = clamp(-r.top / (r.height - stage.clientHeight));
    const target = progress * 40;
    
    // Frame-rate independent easing
    time += (target - time) * (1 - Math.exp(-dt * 5));
    Layer 3: The world

    Compute the whole frame from t

    Nothing in the scene stores progress or plays on a clock. The boat, sky, camera and mill wheel are all read from tables using t, which is why scrolling backwards runs the story in reverse without extra code.

    // [time, distance, elevation, azimuth]
    const CAMERA = [[0, 110, 24, 60], [6, 24, 16, -70], …];
    // [startTime, endTime, startPos, endPos] along the river
    const MOVES  = [[1.5, 10.5, 0, .25], [12, 16.5, .25, .42], …];
    // [time, top, horizon, sun, sunPower, fill, elevation, azimuth, night]
    const SKY    = [[0, "#A8D8EA", "#EAF4E8", …], …];
    
    function render(t) {
      boat.position.copy(riverAt(boatPosition(t)));
      trail.geometry.setDrawRange(0, trailCount(t));
      wheel.rotation.z = -t * 1.3;
      windows.emissiveIntensity = skyAt(t).night * 1.7;
      placeCamera(cameraAt(t));
      renderer.render(scene, camera);
    }
    Layer 4: HTML

    Read the same t in the interface

    The clock, distance, caption and chapter bars are ordinary HTML on top of the canvas. They use the same number, so they cannot drift out of step with the 3D scene.

    const chapter = lastIndexWhere(CHAPTERS, c => t >= c.start);
    captionTitle.textContent = CHAPTERS[chapter].title;
    clock.textContent = formatClock(lookup(CLOCK, t));
    bars[chapter].style.transform = `scaleX(${chapterProgress})`;

    Three things to try

    1. Stop on the mill wheel in chapter 3. It stops too, because it only moves with t.
    2. Flick the scrollbar a long way and watch the eased marker catch up with the raw one.
    3. Scroll back and forth across dusk in chapter 4. The windows and lamps turn on and off in step.

    Guards this page also uses

    1. Three.js loads only after the page is idle, and the page is complete without it.
    2. Drawing stops off screen, in a hidden tab, and whenever t has not changed.
    3. Small screens and low-core devices get fewer triangles and a lower pixel ratio.
    4. Reduced-motion settings and WebGL failures fall back to a readable chapter list.

    Technique studied on choparexpress.uz. The scene, story and code on this page are original.