@webflow/runway-scroll
v0.4.0
Published
Native infinite scroll without hijacking the browser: the page loops, an empty runway scrolls, and one coordinate drives everything on screen
Downloads
244
Readme
runway-scroll
Native infinite scroll without hijacking the browser. The page loops, an empty runway is what actually scrolls, and one coordinate drives everything on screen.
import { RunwayScroll } from '@webflow/runway-scroll';
const runway = new RunwayScroll({
frame: ({ t }) => draw(t) // 0 <= t < 800, on every frame the page moves
});That is the whole setup. The runway is inserted for you, the lap is eight
screens of scrolling, and t is where you are along it in loop-vh, where 100
is one screen. The browser scrolls natively: no wheel or touch handler cancels
anything, and nothing paces the page unless asked, so t is the browser's own
offset, folded, and a chunky wheel notch lands whole in the frame it happened.
Or hand it the page, and there is nothing to draw:
new RunwayScroll({ content: document.body });The body is pinned to the screen and scrolled to the loop's place on every
frame it moves. No wrapper, no transform: it stays a real page, so
position: sticky and fixed, find-in-page, focus and anchors all work as
they do anywhere. See Letting it draw the page
for what that costs.
Behind what is drawn sits a runway a million pixels tall, a whole number of laps, every offset is folded onto one lap, and the visitor is put back in the middle of the runway by whole laps when nothing is moving and they have drifted a quarter of it away. Every lap draws the same picture, so the fold is invisible, and there is always runway left in both directions.
A site that wants a say in the pacing opts in. Each of these is off unless set, and each works on its own; none of them touches a drag, which moves the page with the finger.
new RunwayScroll({
frame: ({ t }) => draw(t),
ease: 9, // follower damping: t closes on the offset instead of being it
maxVhPerS: 500, // momentum ceiling, in loop-vh per second; a rate limit without ease
backlogVh: 200 // how far ahead of the screen a fling may commit; the rest is dropped
});Hand it to an agent
Everything an AI coding agent needs to use runway-scroll well ships in the package:
- The guide,
node_modules/@webflow/runway-scroll/skills/runway-scroll/: how to lay a lap out so the seam is a cut, whether to let the library draw the page, when pacing is worth opting into, a loop in a box and its gate, and how to verify it in a browser. It is an Agent Skill, so it can also be installed for your agent withnpx skills add webflow/runway. - The types,
dist/index.d.ts,dist/probe.d.tsanddist/engine.d.ts, with the contract of each option and member. - The probe,
@webflow/runway-scroll/probe: where the loop is, on screen, while you build it.
The prompts in
skills/runway-scroll/assets/prompts.json
are shown here once each has passed a run by a fresh agent that had only the
published package.
To have your agent read the guide whenever it touches the loop, add this to the
project's AGENTS.md or CLAUDE.md:
<!-- BEGIN:runway-scroll-agent-rules -->
## The loop
This site loops with runway-scroll. Before changing how a page scrolls or adding a loop, read
`node_modules/@webflow/runway-scroll/skills/runway-scroll/SKILL.md` and follow it. The page's place is `t`,
never `scrollY`, which is the runway's offset; the lap ends on a copy of its first screen, so the
seam is a cut; and a loop inside the page stays behind a gate.
<!-- END:runway-scroll-agent-rules -->What is in it
RunwayScroll(runway-scroll). Puts a runway in, reads the offset, smooths it, folds it, and writes it back only when that cannot be seen. Takes the window by default, or any element that scrolls (scroller), for a loop inside a box. Draws the page too, if it is given the body (content).RunwayEngine(@webflow/runway-scroll/engine). The same loop for a page that runs its own frame loop and sets its own lap, in px: A host of its own.wrap,recenterDelta,nearestCopyDelta(runway-scroll). The arithmetic, pure and tested on its own.mountProbe(@webflow/runway-scroll/probe). A draggable instrument showing the lap with its sections and the runway with the copy you stand on; click a section's band and the loop goes there. It asks for frames only while the page moves, folds down to its readout, and keeps where it was dragged and whether it was folded for the tab. Styles are injected; nothing to import.examples/vanilla. Loop Studio, a site that happens to loop, where the footer is the hero.
API
new RunwayScroll(options?: RunwayOptions) // and that starts it
runway.destroy(); // takes the runway away with it, and unpins a body it drew
runway.t; // 0 <= t < lapVh, the loop coordinate in loop-vh
runway.tPx; // the same place along the lap, in pixels
runway.period; // one lap in pixels: eight screens, or what `content` scrolls
runway.laps; // laps in the runway: as many as make a million pixels, never fewer than four
runway.lapVh; // 800, or the lap of `content` in screens times 100
runway.rawScroll; // the scroller's own offset, unfolded
runway.touching; // a finger is down
runway.scrollTo(t); // to a loop coordinate, the short way round: a cut
runway.scrollTo(t, { duration: 1 }); // a glide there, in seconds; a scroll or a finger takes over
runway.scrollToPx(px, glide?); // the same, in pixels along the lapRunwayOptions is frame, scroller, content, and the three pacing numbers
above. Everything else decides itself: laps of eight screens each, as many as
make a million pixels, measured from the runway it inserted and re-measured on
a resize; or, with content, laps of whatever the body scrolls, re-measured
whenever that changes. Either way a new lap is applied when the page next comes
to rest, never in the middle of a coast. On a touch screen, a screen that
changes only in height (an in-app browser's toolbar, a soft keyboard) keeps the
lap of screens it has; a turn changes its width, and the lap with it.
runway.laps is how many there are.
frame is called on every frame while the loop moves or settles, and not while
it is still: the last call before it stops has a velocity of 0, and whatever
moves the page again (a scroll, a finger, a resize, a cut, the page shown
again) brings the next. velocity is in loop-vh per second over the last
100 ms of wall clock, so a stalled frame, a frame with no scroll event in it
and a chunky wheel's notch all read as the rate the page moved. A cut is not
movement, and nor is a jump the browser makes to show something, or whatever
moved the page while it was hidden: the page is shown there at once, unpaced.
A glide is scrollTo with a duration, and an easing if the default
ease-in-out will not do. It goes the short way round from where the page is
drawn and calls onComplete on the frame it lands. It moves the page and
writes nothing to the scroller until then, so it cannot end a coast; a scroll
or a finger takes over wherever it has got to, and another scrollTo replaces
it.
import { mountProbe } from '@webflow/runway-scroll/probe';
const probe = mountProbe(runway, {
sections: '#lap > section', // default `section`
labelOf: (el, i) => el.id || String(i + 1), // default
corner: 'bottom-right', // where it opens until it's dragged; default `bottom-left`
goTo: (t) => runway.scrollTo(t, { duration: 1 }) // a band clicked; default
});
probe.destroy();A click on a band of the fold hands goTo where that band's section starts,
in loop-vh. By default the loop glides there over a second, or cuts there for a
reader who prefers reduced motion.
Laying a page out for it
One lap is 800 loop-vh: eight screens of scrolling, always. Draw the page from
t and end the lap on the frame it began on — clone the first block after the
last one — so crossing the seam is a cut with nothing to blend.
If your content is exactly eight screens tall, tPx is the number of pixels to
move it by. If it is not, drive it from the fraction instead, and it still
tracks the finger closely as long as the two are near:
new RunwayScroll({
frame: ({ t }) => {
paper.style.transform = `translate3d(0, ${-(t / 800) * contentPx}px, 0)`;
}
});The runway goes into document.body, or into the scroller element when you
pass one, so construct it after the DOM exists and give a scrolling box a
definite height. It is zero-width, inert and sized in lvh, so a flex or grid
parent has nothing to lay out and a phone's URL bar cannot move the lap under a
finger.
Letting it draw the page
Moving a page with a transform is the cheap way and the fake one. A transformed
ancestor becomes the containing block of everything fixed inside it, nothing
scrolls for sticky to stick to, and the browser has no scroll to make when it
wants to show something: a find-in-page match, a focused field, an anchor.
Give the loop the body instead and the page is scrolled, not moved:
const runway = new RunwayScroll({
content: document.body,
frame: ({ t }) => (bar.style.transform = `scaleX(${t / runway.lapVh})`) // optional
});| | a transform, from frame | content: document.body |
| ---------------------------- | ----------------------------- | --------------------------- |
| Markup | a fixed stage around the page | none |
| sticky, fixed inside | broken | work |
| Find-in-page, focus, anchors | found, never shown | shown, and the loop follows |
| The lap | 800 loop-vh, eight screens | what the body scrolls, 1:1 |
| A frame that scrolls costs | compositing | a repaint of the screen |
That last row is the price, and no CSS buys it back. A browser composites a
scroll only in a box the visitor could have scrolled, and a pinned body is
overflow: hidden: Chromium re-records and re-rasters what it holds for any
change in its offset, WebKit schedules a full repaint of it, and will-change
is not consulted in either. Keep what is in flow cheap to paint, and give the
heavy pieces a layer of their own with will-change: opacity, which composites
without becoming a containing block the way transform does. A page that is
mostly a canvas, or that has to hold 60 frames on a modest phone, is still
better drawn from frame.
iOS asks for one more layer. A box that scrolls across inside the page, a code block or a wide table, is a scroll view of its own there, and what it holds is drawn apart from the body. WebKit holds on to the drawing of a fixed layer while it is on the screen, but not always to a scroll view's inside it: for the frame where the loop writes the window's offset (at rest, by whole laps, or for a cut) such a box shows nothing. Give what each one holds a layer of its own, and it is kept as the body is:
pre > code {
display: block;
width: max-content; /* as wide as its longest line, so it scrolls as far */
min-width: 100%;
will-change: opacity;
}What it does, so the page does not have to:
- Pins the body with inline,
!importantdeclarations (a scroll lock that setsposition: relative !importanton a locked body would otherwise take the pin off) and puts every one back ondestroy, leaving the page where the lap was. Fixed in WebKit, sticky everywhere else. A fixed body is placed against the screen, so a write to the window's offset moves nothing that is drawn. A sticky one is placed against the offset, and after a write iOS draws it a frame late, where the old offset put it, whole laps off the screen: a frame with no page in it, only what is fixed. WebKit's find-in-page shows a match by scrolling the body itself, so the window is never needed for it. Chrome's scrolls only what the visitor could, and its walk up from a match stops at a fixed box but passes through a sticky one: it scrolls the window by the distance to the match, and the loop draws the match into view. So outside WebKit the body is sticky. - Takes the lap from the body: its scroll range, a pixel of scrolling to a
pixel of page, which is what makes that distance come out right. There is no
runway element; the root is given the height, because nothing it holds gives
it one: a fixed body is out of the flow, and a sticky one is a screen tall and
cannot leave the root's box.
tis still loop-vh, 100 to a screen, solapVhis no longer 800 but the page's own length, and pacing is still in screens. - Follows the browser when it scrolls the body itself (a focus, an anchor,
scrollIntoView, Safari's find-in-page): it goes there the short way round. A step bigger than a screen and faster than any fling is taken whole even with a backlog set, which would otherwise drop it and put the page back. - Leaves a new lap for rest. An image that lands mid-coast changes what the body scrolls; the lap and the runway change together the next time nothing is moving, and the page keeps the pixel it was at, not the fraction. The one exception is a reload. The page is made before its fonts arrive, on a lap it will not keep, and the offset the browser restores was folded by the lap the last visit ended on. So until the visitor moves the page themselves, a new lap is folded against that offset instead, whether it was already waiting or turns up later, and the reload lands where the visit left off.
One rule replaces "clone the first block": whatever follows the last section
must be exactly one screen of the first (height: 100lvh; overflow:
hidden), because the lap is measured now and not declared. Anything else you
put in the body in flow lengthens the lap; fixed and absolute do not.
It is the body, in the window, in standards mode, and it throws for anything
else before it touches a style. A loop inside a box draws itself from frame.
A host of its own: runway-scroll/engine
A page that already runs a frame loop, for a WebGL scene or a canvas, and has
a lap of its own rather than eight screens, takes the loop as RunwayEngine.
It is the same runway, fold, rest, pacing and glides as RunwayScroll, but the
host owns the clock and the coordinate: the engine works in px, never asks the
browser for a frame, and draws nothing. The host's loop calls tick and draws
from tPx, and the engine says when it needs a frame, so the loop still runs
only while something moves:
import { RunwayEngine } from '@webflow/runway-scroll/engine';
let raf = 0;
const wake = () => (raf ||= requestAnimationFrame(loop));
const engine = new RunwayEngine({
runway: document.getElementById('runway'), // an empty block in the page's flow
period: 0, // no lap until the scene has measured one
requestFrame: wake
});
function loop(now) {
raf = 0;
engine.tick(now); // first, so the frame is drawn where the page is now
if (engine.period > 0) scene.draw(engine.tPx / engine.period);
if (scene.moving) wake(); // the scene's own motion, for as long as it lasts
}
scene.onMeasure = (lapPx) => engine.setPeriod(lapPx);new RunwayEngine(options: EngineOptions) // and that starts it
engine.destroy(); // puts the runway's height back, and a scroller still held
engine.tick(now); // one frame, at the host's frame timestamp; one it did not ask for does nothing
engine.setPeriod(px); // a new lap, in px
engine.scrollToPx(px, glide?); // as runway.scrollToPx: a cut, or a glide with a duration
engine.freeze(); // hold the scroller; hands back the release
engine.maxPxPerS = px; // the pacing, in px; settable while it runs, not positive is off
engine.backlogPx = px;
engine.tPx; // 0 <= tPx < period: draw from this
engine.leadPx; // where the page will rest: a glide's destination during a glide
engine.movedPx; // the follower's move on the last tick; 0 across a cut, a fold, a landing and a carry
engine.rawScroll; // the scroller's own offset, unfolded
engine.period; // one lap in px, 0 before the first
engine.laps; // as RunwayScroll's: as many as make RUNWAY_PX, never fewer than four
engine.touching; // a finger is down
engine.settled; // the follower is on the lead
engine.gliding; // a glide is on its way
engine.carrying; // the browser is carrying the page to its top
engine.landed; // landed, with nothing left of a clamped landingEngineOptions is runway, period, scroller, ease, maxPxPerS,
backlogPx and requestFrame. It is a class of its own, not more
RunwayOptions, because a host's clock leaves nothing for the browser's frames
to do and a host's lap leaves nothing for content to do.
- The runway is the host's element. An empty block in the scroller's flow.
The engine sizes it to
lapstimesperiodpx, as an inline style, when it lands and whenever a new lap is applied, which is only at rest, and puts its height back ondestroy.RUNWAY_PXis exported with it. - The lap is the host's, in px. A
periodof 0 is no lap yet: nothing is written, and the engine lands on the first positivesetPeriod, reading the offset the page arrived at before it sizes the runway, so a restored offset lands on the same place along the lap. A new lap within 2 px of the last is drift, taken at once. Anything else waits for rest, never under a finger, in a coast or while frozen, and keeps the place as a fraction of the lap; the latest one asked for wins. - The clock is the host's.
tick(now)takes the timestamprequestAnimationFramehanded the host.requestFrameis called whenever the engine needs a frame, from insideticktoo, for the next one; it stops once the page has come to rest. Calltickbefore drawing. freeze()is for an overlay that owns the page for a while. Until every hold is released, scroll events are ignored and nothing is written, and the follower settles onto the lead. On the last release the scroller is put back where it was held, if anything moved it. A cut while held moves where it is put back, anddestroyreleases what is still held.carryingis the cut to the top on its way (below, in "How it works"), to read from the loop, not a hook: a host that draws something over the page can cover the cut while it is true.
How it works, and why it is shaped this way
Three coordinates. rawScroll is what the browser says. The lead is where
the loop has agreed to go: the raw offset, less whatever a backlog drops when a
fling asks for more than it allows; the surplus is dropped, never queued. t
is the follower, eased and rate-limited where asked and the lead itself where
not, and everything on screen reads it. tPx is the same place in pixels
along the lap.
Every write is fenced behind rest. A scrollTop write during an iOS coast
ends the coast, so the re-center, the gap-close (which puts the scroller under
the follower after a backlog has dropped travel or a glide has moved it) and a
parked resize all wait for: no finger down, the follower settled, and no scroll
event for long enough. How long depends on what moved the page. A coast has no
gaps in it, so once a finger has been down the page must go quiet for 150 ms. A
wheel, a trackpad or a key moves it in steps, and a step nothing has followed
for 50 ms has landed with no momentum to end; that is also the gap between two
notches of a chunky wheel, so a steady roll is re-centered between notches,
never at the runway's end. The one exception is the boundary fold: anything, a
drag included, is folded once it actually reaches a tenth of a lap from the
runway's end, because clamping there would cost the page until the visitor next
stops. Never sooner under a finger: iOS ends a pan the moment the offset is
written under it, and the coast its release would have thrown. The runway's
bottom is where the scroller clamps, a screen short of its last pixel, and the
fold keeps room for two of the stream's biggest steps, so a notch or the fastest
frame of a fling never runs into the end.
The fold is always whole laps, so the wrapped coordinate is unchanged and
nothing on screen moves. A million pixels of runway, half a million each
way, because the only way back to the middle is a write, and a write ends an iOS
coast: a run of quick flicks never rests, and a runway of a few laps runs out
under it in a second or two, stopping the page dead where it is folded. Never
fewer than four laps, and always an even count, because the re-center band is
centered on a seam: with an even count that seam is the runway's exact middle. A
seam on purpose: it is t 0, where every cold load lands and most visits rest,
so it is the place that needs the hysteresis most. A band centered on a lap's
own middle would put the seam on its edge, where a sub-pixel drift in the lap at
rest is enough to fold.
Rest spends the runway rather than write. The band is a quarter of the
runway less half a lap each way (slackFor), and never less than half a lap,
which is what four laps give. Inside it, rest writes nothing, and only past it
is the visitor put back within half a lap of the middle. A write is whole laps,
so it moves nothing that is placed against the screen: a fixed body, or a page
drawn from frame on a fixed stage. What is placed against the offset is
another matter. After a write, iOS draws a sticky element a frame late, where
the old offset put it, which is why the body is pinned fixed there; but a box's
layer has to be sticky. With a band this wide a write comes once in a quarter of
a million pixels of travel one way. A lap that changes is taken at the copy of
the place nearest the offset, not at the middle, so it is often no write at all.
Arriving at offset 0 proves nothing by itself. A hard fling gets there too. What separates the browser carrying the page (the iOS status-bar tap, or Home) from the visitor scrolling it is a backward rate no gesture reaches, read on the wall clock rather than the clamped frame time, and kept up for more than a lap: a single step that fast is the browser showing something. From the middle of a million pixels the browser takes a few hundred milliseconds to get there, and nothing may be written on the way, or its scroll stops wherever it had got to. So while it carries the page, the page holds where it is and is not folded, and at 0 it cuts to the lap's start, the picture offset 0 already promised. A finger, a step forward or rest ends a carry.
Rate over distance, wall clock over frame time. A frame is not a fixed
amount of time and dt is clamped, so any per-frame distance threshold reads
several times too large exactly when the page is busiest. The velocity frame
reports is taken the same way: the follower's travel over the last 100 ms of
wall clock, read across a stall rather than divided by its clamped share of it.
Frames only while something moves. The engine asks for an animation frame only while there is something to do: a scroll, a finger down, a follower or a glide still closing, a landing or a new lap to apply, and the rest to be proven after them. Once rest is proven and acted on and the velocity has come to 0, it stops asking, and a scroll, a finger, a resize, a cut or the page being shown again wakes it. A still page costs nothing, and neither does its probe.
What this shape costs, whoever draws. The browser scrolls the runway off
the main thread and the page is drawn on it, so the page is a frame or two
behind the finger, stops when the thread does while the offset keeps going, and
on an iPhone moves at Safari's 60 frames a second however fast the glass is.
scrollY is the runway's offset and never the page's. None of it can be
engineered away from inside: it is the trade for a page that loops, that a
canvas can be drawn in step with, and that the platform still scrolls.
Install
pnpm add @webflow/runway-scrollpnpm release 0.3.0 verifies, bumps (the guide's library-version too), tags
and publishes to npm; the push is left to you, and main carries no dist/.
Development
pnpm install
pnpm build && pnpm check && pnpm test && pnpm lint
pnpm --dir examples/vanilla dev
pnpm e2epnpm e2e is the part the unit tests cannot be: Loop Studio in headless Chrome,
driven over the DevTools protocol with real wheel input, once from the dev
server, once as the built file opened from disk, and once more from the dev
server with WebKit's vendor, for the fixed pin WebKit gets. It checks that the
body is drawn where the window is on every frame, that no travel is lost and
nothing clamps across several laps, that a still page asks for no frames at all,
that a glide leaves the window alone until it lands, that sticky, fixed,
anchors, focus and a reload behave, that a lap which changes is taken at rest,
and that a page the browser carries to its top lands on the lap's start with
nothing written on the way. It builds first, starts its own vite and Chrome and
takes them down again;
--no-build skips the build, --shots <dir> saves screenshots, and CHROME
points it at a binary it cannot find. Run it after any change to the engine. The
find bar is the one thing it cannot press: Ctrl+F is still checked by hand.
License
MIT © Webflow, Inc. Written by Josh Kirk.
The MIT license covers the code in this repository. It does not grant any rights to the Webflow name, the Webflow logo, or Webflow's fonts, which are trademarks or proprietary assets of Webflow, Inc.
