forked from Blendman974/faceclaw
Display-list based menu slide animation
This commit is contained in:
1 parent
92e8def0f0
commit
c8d835176f
28 files changed
+1546
-263
No files matched your search
@@ -0,0 +1,34 @@
|
||||
export type MenuHighlightAnimation = { dx: number; dy: number; startedAt: number; token: number };
|
||||
|
||||
let nextToken = 1;
|
||||
|
||||
/** Eligibility is decided by menu navigation, before the display-list bridge. */
|
||||
export class MenuHighlightMotion {
|
||||
private previous?: { index: number; scroll: number; x: number; y: number; width: number; height: number };
|
||||
private fromIndex: number | null = null;
|
||||
private animation?: MenuHighlightAnimation;
|
||||
|
||||
navigate(from: number, wrapped: boolean): void {
|
||||
this.fromIndex = wrapped ? null : from;
|
||||
}
|
||||
|
||||
paint(index: number, scroll: number, x: number, y: number, width: number, height: number, now: number): MenuHighlightAnimation | undefined {
|
||||
const previous = this.previous;
|
||||
if (!previous || previous.index !== index || previous.scroll !== scroll ||
|
||||
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 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.fromIndex = null;
|
||||
this.previous = { index, scroll, x, y, width, height };
|
||||
}
|
||||
return this.animation && now - this.animation.startedAt < 500 ? this.animation : undefined;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user