( from: Viewport, to: Viewport, durationMs: number, onFrame: (vp: Viewport) => void, onComplete?: () => void, )
| 101 | |
| 102 | /** Smoothly animate from one viewport to another. Returns a cancel function. */ |
| 103 | export 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 | } |
no outgoing calls
no test coverage detected