36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import type { CanvasAgentViewportLike } from "@/lib/canvas/agent-view-operations";
|
|
|
|
export function normalizeCanvasAgentViewAnimationDuration(
|
|
durationMs?: unknown,
|
|
fallbackMs = 280
|
|
) {
|
|
const duration = Number(durationMs);
|
|
const fallback = Number(fallbackMs);
|
|
const normalizedFallback = Number.isFinite(fallback)
|
|
? Math.max(0, Math.min(900, fallback))
|
|
: 280;
|
|
if (!Number.isFinite(duration)) return normalizedFallback;
|
|
return Math.max(0, Math.min(900, Math.round(duration)));
|
|
}
|
|
|
|
export function easeOutCanvasAgentViewProgress(progress: number) {
|
|
const clamped = Math.max(0, Math.min(1, Number(progress) || 0));
|
|
return 1 - (1 - clamped) ** 3;
|
|
}
|
|
|
|
export function interpolateCanvasAgentViewport(
|
|
from: CanvasAgentViewportLike,
|
|
to: CanvasAgentViewportLike,
|
|
progress: number
|
|
): CanvasAgentViewportLike {
|
|
const easedProgress = easeOutCanvasAgentViewProgress(progress);
|
|
const interpolate = (start: number, end: number) =>
|
|
start + (end - start) * easedProgress;
|
|
|
|
return {
|
|
x: interpolate(Number(from.x || 0), Number(to.x || 0)),
|
|
y: interpolate(Number(from.y || 0), Number(to.y || 0)),
|
|
zoom: interpolate(Number(from.zoom || 1), Number(to.zoom || 1)),
|
|
};
|
|
}
|