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 {
|
||||
/**
|
||||
* Time constant for the ease-out chase. Smaller values react faster; the
|
||||
* 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,
|
||||
};
|
||||
const NAVIGATION_DURATION_MS = 220;
|
||||
const REDUCED_NAVIGATION_DURATION_MS = 80;
|
||||
const SETTLE_DISTANCE_PX = 0.5;
|
||||
|
||||
export interface ThreadCameraViewport {
|
||||
scrollTop: number;
|
||||
@@ -44,27 +20,6 @@ interface ThreadCameraOptions {
|
||||
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 {
|
||||
return {
|
||||
request: (callback) => window.requestAnimationFrame(callback),
|
||||
@@ -87,6 +42,7 @@ export class ThreadCameraController {
|
||||
private phase: "idle" | "following" = "idle";
|
||||
private target = 0;
|
||||
private lastTimestamp: number | null = null;
|
||||
private deadline = 0;
|
||||
|
||||
constructor(
|
||||
getViewport: () => ThreadCameraViewport | null,
|
||||
@@ -133,17 +89,19 @@ export class ThreadCameraController {
|
||||
const current = viewport.scrollTop;
|
||||
this.target = Math.max(0, top);
|
||||
|
||||
const motion = this.currentMotion();
|
||||
if (this.phase === "following") {
|
||||
return "retargeted";
|
||||
}
|
||||
if (Math.abs(this.target - current) <= motion.settleDistancePx) {
|
||||
if (Math.abs(this.target - current) <= SETTLE_DISTANCE_PX) {
|
||||
this.write(viewport, this.target);
|
||||
return "settled";
|
||||
}
|
||||
|
||||
this.phase = "following";
|
||||
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);
|
||||
return "started";
|
||||
}
|
||||
@@ -169,54 +127,22 @@ export class ThreadCameraController {
|
||||
return;
|
||||
}
|
||||
|
||||
const motion = this.currentMotion();
|
||||
const previousTimestamp = this.lastTimestamp ?? timestamp - (1000 / 60);
|
||||
const deltaMs = Math.min(
|
||||
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) {
|
||||
const previousTimestamp = this.lastTimestamp ?? timestamp;
|
||||
const remainingMs = Math.max(0, this.deadline - timestamp);
|
||||
if (remainingMs === 0) {
|
||||
this.write(viewport, this.target);
|
||||
this.phase = "idle";
|
||||
this.lastTimestamp = null;
|
||||
this.cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
const deltaSeconds = deltaMs / 1000;
|
||||
const easedTop = easeOutChase(
|
||||
current,
|
||||
this.target,
|
||||
deltaSeconds,
|
||||
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;
|
||||
}
|
||||
const previousRemainingMs = Math.max(remainingMs, this.deadline - previousTimestamp);
|
||||
const remainingFraction = (remainingMs / previousRemainingMs) ** 3;
|
||||
const current = viewport.scrollTop;
|
||||
this.write(viewport, this.target + (current - this.target) * remainingFraction);
|
||||
this.lastTimestamp = timestamp;
|
||||
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 {
|
||||
try {
|
||||
viewport.scrollTop = top;
|
||||
|
||||
Reference in new issue
Block a user