Layer 1: CSSPin 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: JavaScriptTurn 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 worldCompute 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: HTMLRead 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})`;
Technique studied on choparexpress.uz. The scene, story and code on this page are original.