fix(webui): bound explicit scroll navigation duration
This commit is contained in:
1 parent
f39702f1cf
commit
4ed04a5df9
2 files changed
+56
-91
No files matched your search
@@ -1,30 +1,6 @@
|
|||||||
interface ThreadCameraMotionProfile {
|
const NAVIGATION_DURATION_MS = 220;
|
||||||
/**
|
const REDUCED_NAVIGATION_DURATION_MS = 80;
|
||||||
* Time constant for the ease-out chase. Smaller values react faster; the
|
const SETTLE_DISTANCE_PX = 0.5;
|
||||||
* camera closes roughly 95% of an uncapped distance in three time constants.
|
|
||||||
*/
|
|
||||||
responseTimeMs: number;
|
|
||||||
/** Prevents a long explicit navigation from turning into a one-frame jump. */
|
|
||||||
maxSpeedPxPerSecond: number;
|
|
||||||
/** Avoids spending frames chasing sub-pixel layout noise. */
|
|
||||||
settleDistancePx: number;
|
|
||||||
/** Limits catch-up after a throttled or backgrounded animation frame. */
|
|
||||||
maxFrameDeltaMs: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const THREAD_CAMERA_NAVIGATION_MOTION: Readonly<ThreadCameraMotionProfile> = {
|
|
||||||
responseTimeMs: 110,
|
|
||||||
maxSpeedPxPerSecond: 12_000,
|
|
||||||
settleDistancePx: 0.5,
|
|
||||||
maxFrameDeltaMs: 50,
|
|
||||||
};
|
|
||||||
|
|
||||||
const THREAD_CAMERA_REDUCED_NAVIGATION_MOTION: Readonly<ThreadCameraMotionProfile> = {
|
|
||||||
responseTimeMs: 45,
|
|
||||||
maxSpeedPxPerSecond: 24_000,
|
|
||||||
settleDistancePx: 0.5,
|
|
||||||
maxFrameDeltaMs: 50,
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface ThreadCameraViewport {
|
export interface ThreadCameraViewport {
|
||||||
scrollTop: number;
|
scrollTop: number;
|
||||||
@@ -44,27 +20,6 @@ interface ThreadCameraOptions {
|
|||||||
prefersReducedMotion?: () => boolean;
|
prefersReducedMotion?: () => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A time-based ease-out chase rather than a start/end tween. The target can
|
|
||||||
* move during explicit history navigation without restarting a duration or
|
|
||||||
* adding another frame loop.
|
|
||||||
*/
|
|
||||||
function easeOutChase(
|
|
||||||
current: number,
|
|
||||||
target: number,
|
|
||||||
deltaSeconds: number,
|
|
||||||
profile: Pick<ThreadCameraMotionProfile, "responseTimeMs" | "maxSpeedPxPerSecond">,
|
|
||||||
): number {
|
|
||||||
const distance = target - current;
|
|
||||||
const responseSeconds = Math.max(0.001, profile.responseTimeMs / 1000);
|
|
||||||
const timeStep = Math.max(0.001, deltaSeconds);
|
|
||||||
const easeOutFraction = 1 - Math.exp(-timeStep / responseSeconds);
|
|
||||||
const uncappedStep = distance * easeOutFraction;
|
|
||||||
const maxStep = Math.max(0, profile.maxSpeedPxPerSecond) * timeStep;
|
|
||||||
const step = Math.max(-maxStep, Math.min(maxStep, uncappedStep));
|
|
||||||
return current + step;
|
|
||||||
}
|
|
||||||
|
|
||||||
function defaultScheduler(): ThreadCameraScheduler {
|
function defaultScheduler(): ThreadCameraScheduler {
|
||||||
return {
|
return {
|
||||||
request: (callback) => window.requestAnimationFrame(callback),
|
request: (callback) => window.requestAnimationFrame(callback),
|
||||||
@@ -87,6 +42,7 @@ export class ThreadCameraController {
|
|||||||
private phase: "idle" | "following" = "idle";
|
private phase: "idle" | "following" = "idle";
|
||||||
private target = 0;
|
private target = 0;
|
||||||
private lastTimestamp: number | null = null;
|
private lastTimestamp: number | null = null;
|
||||||
|
private deadline = 0;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
getViewport: () => ThreadCameraViewport | null,
|
getViewport: () => ThreadCameraViewport | null,
|
||||||
@@ -133,17 +89,19 @@ export class ThreadCameraController {
|
|||||||
const current = viewport.scrollTop;
|
const current = viewport.scrollTop;
|
||||||
this.target = Math.max(0, top);
|
this.target = Math.max(0, top);
|
||||||
|
|
||||||
const motion = this.currentMotion();
|
|
||||||
if (this.phase === "following") {
|
if (this.phase === "following") {
|
||||||
return "retargeted";
|
return "retargeted";
|
||||||
}
|
}
|
||||||
if (Math.abs(this.target - current) <= motion.settleDistancePx) {
|
if (Math.abs(this.target - current) <= SETTLE_DISTANCE_PX) {
|
||||||
this.write(viewport, this.target);
|
this.write(viewport, this.target);
|
||||||
return "settled";
|
return "settled";
|
||||||
}
|
}
|
||||||
|
|
||||||
this.phase = "following";
|
this.phase = "following";
|
||||||
this.lastTimestamp = this.scheduler.now();
|
this.lastTimestamp = this.scheduler.now();
|
||||||
|
this.deadline = this.lastTimestamp + (this.prefersReducedMotion()
|
||||||
|
? REDUCED_NAVIGATION_DURATION_MS
|
||||||
|
: NAVIGATION_DURATION_MS);
|
||||||
this.frameId = this.scheduler.request(this.advance);
|
this.frameId = this.scheduler.request(this.advance);
|
||||||
return "started";
|
return "started";
|
||||||
}
|
}
|
||||||
@@ -169,54 +127,22 @@ export class ThreadCameraController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const motion = this.currentMotion();
|
const previousTimestamp = this.lastTimestamp ?? timestamp;
|
||||||
const previousTimestamp = this.lastTimestamp ?? timestamp - (1000 / 60);
|
const remainingMs = Math.max(0, this.deadline - timestamp);
|
||||||
const deltaMs = Math.min(
|
if (remainingMs === 0) {
|
||||||
motion.maxFrameDeltaMs,
|
|
||||||
Math.max(1, timestamp - previousTimestamp),
|
|
||||||
);
|
|
||||||
this.lastTimestamp = timestamp;
|
|
||||||
const current = viewport.scrollTop;
|
|
||||||
const remainingDistance = this.target - current;
|
|
||||||
if (Math.abs(remainingDistance) <= motion.settleDistancePx) {
|
|
||||||
this.write(viewport, this.target);
|
this.write(viewport, this.target);
|
||||||
this.phase = "idle";
|
this.cancel();
|
||||||
this.lastTimestamp = null;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const deltaSeconds = deltaMs / 1000;
|
const previousRemainingMs = Math.max(remainingMs, this.deadline - previousTimestamp);
|
||||||
const easedTop = easeOutChase(
|
const remainingFraction = (remainingMs / previousRemainingMs) ** 3;
|
||||||
current,
|
const current = viewport.scrollTop;
|
||||||
this.target,
|
this.write(viewport, this.target + (current - this.target) * remainingFraction);
|
||||||
deltaSeconds,
|
this.lastTimestamp = timestamp;
|
||||||
motion,
|
|
||||||
);
|
|
||||||
// Some browsers quantize scrollTop writes to whole pixels. Keep the
|
|
||||||
// ease-out curve, but never let its subpixel tail round back to the same
|
|
||||||
// position forever.
|
|
||||||
const minimumStep = Math.min(1, Math.abs(remainingDistance));
|
|
||||||
const nextTop =
|
|
||||||
Math.abs(easedTop - current) < minimumStep
|
|
||||||
? current + Math.sign(remainingDistance) * minimumStep
|
|
||||||
: easedTop;
|
|
||||||
const settled = Math.abs(this.target - nextTop) <= motion.settleDistancePx;
|
|
||||||
this.write(viewport, settled ? this.target : nextTop);
|
|
||||||
|
|
||||||
if (settled) {
|
|
||||||
this.phase = "idle";
|
|
||||||
this.lastTimestamp = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.frameId = this.scheduler.request(this.advance);
|
this.frameId = this.scheduler.request(this.advance);
|
||||||
};
|
};
|
||||||
|
|
||||||
private currentMotion(): ThreadCameraMotionProfile {
|
|
||||||
return this.prefersReducedMotion()
|
|
||||||
? THREAD_CAMERA_REDUCED_NAVIGATION_MOTION
|
|
||||||
: THREAD_CAMERA_NAVIGATION_MOTION;
|
|
||||||
}
|
|
||||||
|
|
||||||
private write(viewport: ThreadCameraViewport, top: number): void {
|
private write(viewport: ThreadCameraViewport, top: number): void {
|
||||||
try {
|
try {
|
||||||
viewport.scrollTop = top;
|
viewport.scrollTop = top;
|
||||||
|
|||||||
@@ -39,6 +39,45 @@ function cameraHarness(prefersReducedMotion = false) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("ThreadCameraController", () => {
|
describe("ThreadCameraController", () => {
|
||||||
|
it("uses the same duration for nearby and distant targets", () => {
|
||||||
|
const durations = [100, 100_000].map((target) => {
|
||||||
|
const { camera, viewport, advance } = cameraHarness();
|
||||||
|
camera.navigateTo(target);
|
||||||
|
let elapsed = 0;
|
||||||
|
while (camera.isFollowing() && elapsed < 1_000) {
|
||||||
|
advance(16);
|
||||||
|
elapsed += 16;
|
||||||
|
}
|
||||||
|
expect(viewport.scrollTop).toBe(target);
|
||||||
|
expect(elapsed).toBeLessThanOrEqual(250);
|
||||||
|
return elapsed;
|
||||||
|
});
|
||||||
|
expect(durations[0]).toBe(durations[1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([1_000, 100_000, -100_000])("finishes a %i px navigation within 250ms", (distance) => {
|
||||||
|
const { camera, viewport, frames, advance } = cameraHarness();
|
||||||
|
viewport.scrollTop = distance < 0 ? -distance : 0;
|
||||||
|
const target = viewport.scrollTop + distance;
|
||||||
|
camera.navigateTo(target);
|
||||||
|
advance(16);
|
||||||
|
expect(viewport.scrollTop).not.toBe(target);
|
||||||
|
advance(234);
|
||||||
|
expect(viewport.scrollTop).toBe(target);
|
||||||
|
expect(camera.isFollowing()).toBe(false);
|
||||||
|
expect(frames).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps the original deadline when layout retargets navigation", () => {
|
||||||
|
const { camera, viewport, advance } = cameraHarness();
|
||||||
|
camera.navigateTo(100_000);
|
||||||
|
advance(100);
|
||||||
|
expect(camera.navigateTo(120_000)).toBe("retargeted");
|
||||||
|
advance(150);
|
||||||
|
expect(viewport.scrollTop).toBe(120_000);
|
||||||
|
expect(camera.isFollowing()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
it("pins automatic follow in the geometry frame without camera debt", () => {
|
it("pins automatic follow in the geometry frame without camera debt", () => {
|
||||||
const { camera, viewport, frames } = cameraHarness();
|
const { camera, viewport, frames } = cameraHarness();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user