MCPcopy
hub / github.com/opentrace/opentrace / animateViewport

Function animateViewport

ui/src/components/pixi/viewport.ts:103–136  ·  view source on GitHub ↗
(
  from: Viewport,
  to: Viewport,
  durationMs: number,
  onFrame: (vp: Viewport) => void,
  onComplete?: () => void,
)

Source from the content-addressed store, hash-verified

101
102/** Smoothly animate from one viewport to another. Returns a cancel function. */
103export function animateViewport(
104 from: Viewport,
105 to: Viewport,
106 durationMs: number,
107 onFrame: (vp: Viewport) => void,
108 onComplete?: () => void,
109): () => void {
110 const startTime = performance.now();
111 let cancelled = false;
112
113 function step() {
114 if (cancelled) return;
115 const elapsed = performance.now() - startTime;
116 const t = Math.min(elapsed / durationMs, 1);
117 // Ease-out cubic
118 const ease = 1 - Math.pow(1 - t, 3);
119 const vp: Viewport = {
120 x: from.x + (to.x - from.x) * ease,
121 y: from.y + (to.y - from.y) * ease,
122 scale: from.scale + (to.scale - from.scale) * ease,
123 };
124 onFrame(vp);
125 if (t < 1) {
126 requestAnimationFrame(step);
127 } else {
128 onComplete?.();
129 }
130 }
131
132 requestAnimationFrame(step);
133 return () => {
134 cancelled = true;
135 };
136}

Callers 4

zoomToFitMethod · 0.90
zoomToNodesMethod · 0.90
zoomInMethod · 0.90
zoomOutMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected