Optimize on-glasses animation

This commit is contained in:
James Babcock committed 2026-09-23 02:58:29 -07:00
1 parent c8d835176f
commit 08f1b9a2a2
8 files changed
+94 -46

No files matched your search

+5 -4
View File
@@ -1,4 +1,5 @@
export type MenuHighlightAnimation = { dx: number; dy: number; startedAt: number; token: number };
export const MENU_HIGHLIGHT_DURATION_MS = 300;
export type MenuHighlightAnimation = { dx: number; dy: number; startedAt: number; token: number; durationMs: number };
let nextToken = 1;
@@ -18,17 +19,17 @@ export class MenuHighlightMotion {
previous.x !== x || previous.y !== y || previous.width !== width || previous.height !== height) {
let fromX = previous?.x ?? x, fromY = previous?.y ?? y;
if (previous && this.animation) {
const t = Math.max(0, Math.min(1, (now - this.animation.startedAt) / 500));
const t = Math.max(0, Math.min(1, (now - this.animation.startedAt) / this.animation.durationMs));
const remaining = 1 - t * t * (3 - 2 * t);
fromX += this.animation.dx * remaining;
fromY += this.animation.dy * remaining;
}
const eligible = previous && previous.index !== index && this.fromIndex === previous.index &&
previous.scroll === scroll && previous.width === width && previous.height === height;
this.animation = eligible ? { dx: Math.round(fromX - x), dy: Math.round(fromY - y), startedAt: now, token: nextToken++ } : undefined;
this.animation = eligible ? { dx: Math.round(fromX - x), dy: Math.round(fromY - y), startedAt: now, token: nextToken++, durationMs: MENU_HIGHLIGHT_DURATION_MS } : undefined;
this.fromIndex = null;
this.previous = { index, scroll, x, y, width, height };
}
return this.animation && now - this.animation.startedAt < 500 ? this.animation : undefined;
return this.animation && now - this.animation.startedAt < this.animation.durationMs ? this.animation : undefined;
}
}