@statelyai/flow 0.13.4 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +5 -1
  2. package/dist/esm/index.d.ts +13 -2
  3. package/dist/esm/index.d.ts.map +1 -0
  4. package/dist/esm/index.js +39 -14
  5. package/dist/esm/index.js.map +1 -0
  6. package/dist/esm/layout/dagre.d.ts +2 -1
  7. package/dist/esm/layout/dagre.d.ts.map +1 -0
  8. package/dist/esm/layout/dagre.js +2 -0
  9. package/dist/esm/layout/dagre.js.map +1 -0
  10. package/dist/esm/layout/elk.d.ts +2 -1
  11. package/dist/esm/layout/elk.d.ts.map +1 -0
  12. package/dist/esm/layout/elk.js +2 -0
  13. package/dist/esm/layout/elk.js.map +1 -0
  14. package/dist/esm/layout/stately.d.ts +2 -1
  15. package/dist/esm/layout/stately.d.ts.map +1 -0
  16. package/dist/esm/layout/stately.js +2 -0
  17. package/dist/esm/layout/stately.js.map +1 -0
  18. package/dist/esm/shared.d.ts +2 -1
  19. package/dist/esm/shared.d.ts.map +1 -0
  20. package/dist/esm/shared.js +2 -0
  21. package/dist/esm/shared.js.map +1 -0
  22. package/package.json +4 -3
  23. package/src/__fixtures__/nested.ts +106 -0
  24. package/src/appearances.test.ts +428 -0
  25. package/src/appearances.ts +144 -0
  26. package/src/authoritative-route.ts +14 -0
  27. package/src/changeSet.test.ts +132 -0
  28. package/src/changeSet.ts +337 -0
  29. package/src/config.ts +41 -0
  30. package/src/configure.test.ts +212 -0
  31. package/src/connection-lifetime.ts +1 -0
  32. package/src/connectionPreview.test.ts +102 -0
  33. package/src/connectionPreview.ts +82 -0
  34. package/src/content.ts +203 -0
  35. package/src/createFlow.fit-frame.test.ts +256 -0
  36. package/src/createFlow.modern.test.ts +743 -0
  37. package/src/createFlow.test.ts +632 -0
  38. package/src/createFlow.ts +3463 -0
  39. package/src/createFlow.view.test.ts +212 -0
  40. package/src/createFlow.viewport-controls.test.ts +221 -0
  41. package/src/edge-geometry.ts +318 -0
  42. package/src/engine.cache.test.ts +728 -0
  43. package/src/engine.hierarchy.test.ts +1023 -0
  44. package/src/engine.test.ts +1965 -0
  45. package/src/engine.ts +3385 -0
  46. package/src/entity-changes.test.ts +309 -0
  47. package/src/entity-changes.ts +395 -0
  48. package/src/examples/flow-based-math.test.ts +25 -0
  49. package/src/examples/flow-based-math.ts +210 -0
  50. package/src/external-edit.test.ts +337 -0
  51. package/src/external-edit.ts +358 -0
  52. package/src/factories.test.ts +104 -0
  53. package/src/factories.ts +70 -0
  54. package/src/geometry.ts +13 -0
  55. package/src/graph-index.test.ts +56 -0
  56. package/src/graph-index.ts +194 -0
  57. package/src/hierarchy.test.ts +175 -0
  58. package/src/hierarchy.ts +330 -0
  59. package/src/highlights.test.ts +130 -0
  60. package/src/index.ts +469 -0
  61. package/src/input/index.ts +615 -0
  62. package/src/input/input.test.ts +2577 -0
  63. package/src/input/keymap.ts +111 -0
  64. package/src/input/modes.ts +1715 -0
  65. package/src/input/reparent.test.ts +214 -0
  66. package/src/layout/dagre.ts +138 -0
  67. package/src/layout/elk.ts +1382 -0
  68. package/src/layout/index.ts +12 -0
  69. package/src/layout/layout.test.ts +1248 -0
  70. package/src/layout/shared.ts +260 -0
  71. package/src/layout/stately-globals.d.ts +9 -0
  72. package/src/layout/stately.test.ts +246 -0
  73. package/src/layout/stately.ts +528 -0
  74. package/src/layout-animation.test.ts +223 -0
  75. package/src/layout-contract.test.ts +414 -0
  76. package/src/marker.test.ts +23 -0
  77. package/src/marker.ts +23 -0
  78. package/src/measurement-anchor.test.ts +232 -0
  79. package/src/minimap.test.ts +96 -0
  80. package/src/minimap.ts +137 -0
  81. package/src/nested-fixture.test.ts +178 -0
  82. package/src/port-measurement.test.ts +27 -0
  83. package/src/port-measurements.test.ts +187 -0
  84. package/src/profile.ts +124 -0
  85. package/src/projections.test.ts +103 -0
  86. package/src/projections.ts +295 -0
  87. package/src/render-invalidation.test.ts +62 -0
  88. package/src/render-invalidation.ts +55 -0
  89. package/src/renderer-geometry.test.ts +356 -0
  90. package/src/renderer-transactions.test.ts +234 -0
  91. package/src/routing/avoid.ts +417 -0
  92. package/src/routing/index.test.ts +361 -0
  93. package/src/routing/index.ts +366 -0
  94. package/src/routing/readTracking.test.ts +103 -0
  95. package/src/routing/readTracking.ts +233 -0
  96. package/src/selection-commands.test.ts +355 -0
  97. package/src/serialize.test.ts +167 -0
  98. package/src/serialize.ts +254 -0
  99. package/src/shapes/index.test.ts +45 -0
  100. package/src/shapes/index.ts +142 -0
  101. package/src/snap.test.ts +123 -0
  102. package/src/snap.ts +395 -0
  103. package/src/spatial.test.ts +676 -0
  104. package/src/spatial.ts +457 -0
  105. package/src/store/index.ts +4121 -0
  106. package/src/store.test.ts +2903 -0
  107. package/src/toolbar.test.ts +31 -0
  108. package/src/toolbar.ts +62 -0
  109. package/src/types.test.ts +15 -0
  110. package/src/types.ts +487 -0
  111. package/src/validation.ts +558 -0
  112. package/src/view.ts +172 -0
  113. package/src/viewport/animate.ts +285 -0
  114. package/src/viewport/index.ts +296 -0
  115. package/src/viewport.test.ts +569 -0
@@ -0,0 +1,569 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import {
3
+ createViewport,
4
+ toSVGTransform,
5
+ toViewBox,
6
+ screenToCanvas,
7
+ canvasToScreen,
8
+ getVisibleBounds,
9
+ viewportForBounds,
10
+ fitFrameAroundExclusions,
11
+ viewportCenteredOn,
12
+ panBy,
13
+ zoomTo,
14
+ clampZoom,
15
+ animateViewport,
16
+ } from './viewport';
17
+ import { createFlowStore } from './store';
18
+
19
+
20
+ // createViewport
21
+
22
+
23
+ describe('createViewport', () => {
24
+ it('creates default viewport at origin, zoom=1', () => {
25
+ expect(createViewport()).toEqual({ x: 0, y: 0, zoom: 1 });
26
+ });
27
+
28
+ it('accepts custom values', () => {
29
+ expect(createViewport(10, 20, 2)).toEqual({ x: 10, y: 20, zoom: 2 });
30
+ });
31
+ });
32
+
33
+
34
+ // toSVGTransform / toViewBox
35
+
36
+
37
+ describe('toSVGTransform', () => {
38
+ it('produces correct transform string', () => {
39
+ expect(toSVGTransform({ x: 10, y: 20, zoom: 1.5 }))
40
+ .toBe('translate(10, 20) scale(1.5)');
41
+ });
42
+ });
43
+
44
+ describe('toViewBox', () => {
45
+ it('computes viewBox from viewport + size', () => {
46
+ const vb = toViewBox({ x: 0, y: 0, zoom: 1 }, { width: 800, height: 600 });
47
+ expect(vb).toBe('0 0 800 600');
48
+ });
49
+
50
+ it('accounts for pan and zoom', () => {
51
+ const vb = toViewBox({ x: -100, y: -50, zoom: 2 }, { width: 800, height: 600 });
52
+ // x = -(-100)/2 = 50, y = -(-50)/2 = 25, w = 800/2 = 400, h = 600/2 = 300
53
+ expect(vb).toBe('50 25 400 300');
54
+ });
55
+ });
56
+
57
+
58
+ // screenToCanvas / canvasToScreen
59
+
60
+
61
+ describe('screenToCanvas', () => {
62
+ it('identity at default viewport', () => {
63
+ const vp = createViewport();
64
+ expect(screenToCanvas({ x: 100, y: 200 }, vp)).toEqual({ x: 100, y: 200 });
65
+ });
66
+
67
+ it('accounts for pan', () => {
68
+ const vp = { x: 50, y: 100, zoom: 1 };
69
+ expect(screenToCanvas({ x: 150, y: 200 }, vp)).toEqual({ x: 100, y: 100 });
70
+ });
71
+
72
+ it('accounts for zoom', () => {
73
+ const vp = { x: 0, y: 0, zoom: 2 };
74
+ expect(screenToCanvas({ x: 100, y: 200 }, vp)).toEqual({ x: 50, y: 100 });
75
+ });
76
+ });
77
+
78
+ describe('canvasToScreen', () => {
79
+ it('round-trips with screenToCanvas', () => {
80
+ const vp = { x: 30, y: -20, zoom: 1.5 };
81
+ const screen = { x: 200, y: 300 };
82
+ const canvas = screenToCanvas(screen, vp);
83
+ const back = canvasToScreen(canvas, vp);
84
+ expect(back.x).toBeCloseTo(screen.x);
85
+ expect(back.y).toBeCloseTo(screen.y);
86
+ });
87
+ });
88
+
89
+
90
+ // getVisibleBounds
91
+
92
+
93
+ describe('getVisibleBounds', () => {
94
+ it('returns full canvas at default viewport', () => {
95
+ const vp = createViewport();
96
+ const b = getVisibleBounds(vp, { width: 800, height: 600 });
97
+ expect(b.x).toBeCloseTo(0);
98
+ expect(b.y).toBeCloseTo(0);
99
+ expect(b.width).toBe(800);
100
+ expect(b.height).toBe(600);
101
+ });
102
+
103
+ it('accounts for pan and zoom', () => {
104
+ const vp = { x: -100, y: -50, zoom: 2 };
105
+ const b = getVisibleBounds(vp, { width: 800, height: 600 });
106
+ expect(b.x).toBe(50);
107
+ expect(b.y).toBe(25);
108
+ expect(b.width).toBe(400);
109
+ expect(b.height).toBe(300);
110
+ });
111
+ });
112
+
113
+
114
+ // viewportForBounds (fitView math)
115
+
116
+
117
+ describe('viewportForBounds', () => {
118
+ it('centers a small rect in a larger screen', () => {
119
+ const vp = viewportForBounds(
120
+ { x: 0, y: 0, width: 100, height: 100 },
121
+ { width: 800, height: 600 },
122
+ { padding: 0 },
123
+ );
124
+ // zoom = min(800/100, 600/100) = 6, clamped to maxZoom=2
125
+ expect(vp.zoom).toBe(2);
126
+ // center = (50, 50), x = 400 - 50*2 = 300, y = 300 - 50*2 = 200
127
+ expect(vp.x).toBe(300);
128
+ expect(vp.y).toBe(200);
129
+ });
130
+
131
+ it('respects minZoom', () => {
132
+ const vp = viewportForBounds(
133
+ { x: 0, y: 0, width: 10000, height: 10000 },
134
+ { width: 100, height: 100 },
135
+ { padding: 0, minZoom: 0.5 },
136
+ );
137
+ expect(vp.zoom).toBe(0.5);
138
+ });
139
+
140
+ it('respects padding', () => {
141
+ const noPad = viewportForBounds(
142
+ { x: 0, y: 0, width: 200, height: 200 },
143
+ { width: 400, height: 400 },
144
+ { padding: 0 },
145
+ );
146
+ const withPad = viewportForBounds(
147
+ { x: 0, y: 0, width: 200, height: 200 },
148
+ { width: 400, height: 400 },
149
+ { padding: 0.5 },
150
+ );
151
+ // More padding → lower zoom
152
+ expect(withPad.zoom).toBeLessThan(noPad.zoom);
153
+ });
154
+
155
+ it('returns default viewport for zero-size bounds', () => {
156
+ const vp = viewportForBounds(
157
+ { x: 0, y: 0, width: 0, height: 0 },
158
+ { width: 800, height: 600 },
159
+ );
160
+ expect(vp).toEqual({ x: 0, y: 0, zoom: 1 });
161
+ });
162
+ });
163
+
164
+ describe('fitFrameAroundExclusions', () => {
165
+ it('reserves measured edge bands for floating overlays', () => {
166
+ expect(
167
+ fitFrameAroundExclusions(
168
+ { x: 0, y: 0, width: 800, height: 600 },
169
+ [
170
+ {
171
+ rect: { x: 16, y: 56, width: 48, height: 200 },
172
+ side: 'left',
173
+ gap: 8,
174
+ },
175
+ {
176
+ rect: { x: 300, y: 540, width: 200, height: 44 },
177
+ side: 'bottom',
178
+ gap: 8,
179
+ },
180
+ ],
181
+ ),
182
+ ).toEqual({ x: 72, y: 0, width: 728, height: 532 });
183
+ });
184
+
185
+ it('infers the nearest edge and ignores off-frame or empty overlays', () => {
186
+ expect(
187
+ fitFrameAroundExclusions(
188
+ { x: 100, y: 50, width: 600, height: 400 },
189
+ [
190
+ { rect: { x: 300, y: 410, width: 180, height: 30 } },
191
+ { rect: { x: -100, y: -100, width: 20, height: 20 } },
192
+ { rect: { x: 0, y: 0, width: 0, height: 20 } },
193
+ ],
194
+ ),
195
+ ).toEqual({ x: 100, y: 50, width: 600, height: 360 });
196
+ });
197
+ });
198
+
199
+
200
+ // panBy / zoomTo / clampZoom
201
+
202
+
203
+ describe('panBy', () => {
204
+ it('adds delta to viewport position', () => {
205
+ const vp = panBy({ x: 10, y: 20, zoom: 1.5 }, { x: 5, y: -3 });
206
+ expect(vp).toEqual({ x: 15, y: 17, zoom: 1.5 });
207
+ });
208
+ });
209
+
210
+ describe('zoomTo', () => {
211
+ it('zooms towards a center point', () => {
212
+ const vp = zoomTo({ x: 0, y: 0, zoom: 1 }, 2, { x: 400, y: 300 });
213
+ // ratio = 2/1 = 2
214
+ // x = 400 - (400 - 0) * 2 = 400 - 800 = -400
215
+ expect(vp.x).toBe(-400);
216
+ expect(vp.y).toBe(-300);
217
+ expect(vp.zoom).toBe(2);
218
+ });
219
+
220
+ it('zooming to same level is a no-op', () => {
221
+ const original = { x: 50, y: 50, zoom: 1.5 };
222
+ const vp = zoomTo(original, 1.5, { x: 100, y: 100 });
223
+ expect(vp.x).toBeCloseTo(50);
224
+ expect(vp.y).toBeCloseTo(50);
225
+ });
226
+ });
227
+
228
+ describe('clampZoom', () => {
229
+ it('clamps below min', () => {
230
+ expect(clampZoom(0.05, 0.1, 4)).toBe(0.1);
231
+ });
232
+
233
+ it('clamps above max', () => {
234
+ expect(clampZoom(5, 0.1, 4)).toBe(4);
235
+ });
236
+
237
+ it('passes through within range', () => {
238
+ expect(clampZoom(1.5, 0.1, 4)).toBe(1.5);
239
+ });
240
+ });
241
+
242
+ describe('viewportCenteredOn', () => {
243
+ it('places the canvas point at the screen center', () => {
244
+ const vp = viewportCenteredOn({ x: 100, y: 50 }, { width: 800, height: 600 }, 2);
245
+ // screenToCanvas(center) should give back the point
246
+ expect((400 - vp.x) / vp.zoom).toBeCloseTo(100);
247
+ expect((300 - vp.y) / vp.zoom).toBeCloseTo(50);
248
+ expect(vp.zoom).toBe(2);
249
+ });
250
+ });
251
+
252
+ function manualRaf() {
253
+ let now = 0;
254
+ let nextId = 1;
255
+ const callbacks = new Map<number, (time: number) => void>();
256
+ return {
257
+ raf(callback: (time: number) => void) {
258
+ const id = nextId++;
259
+ callbacks.set(id, callback);
260
+ return id;
261
+ },
262
+ cancelRaf(id: number) {
263
+ callbacks.delete(id);
264
+ },
265
+ tick(ms: number) {
266
+ now += ms;
267
+ const pending = [...callbacks.values()];
268
+ callbacks.clear();
269
+ for (const callback of pending) callback(now);
270
+ },
271
+ };
272
+ }
273
+
274
+ describe('animateViewport', () => {
275
+ it('focal-zooms with log-space zoom (origin anchor)', async () => {
276
+ const store = createFlowStore();
277
+ const ticker = manualRaf();
278
+
279
+ const animation = animateViewport(
280
+ store,
281
+ { x: 100, y: 50, zoom: 4 },
282
+ { duration: 100, easing: (t) => t, raf: ticker.raf, cancelRaf: ticker.cancelRaf },
283
+ );
284
+
285
+ ticker.tick(0);
286
+ ticker.tick(50);
287
+
288
+ expect(store.getSnapshot().context.viewport.x).toBeCloseTo(50);
289
+ expect(store.getSnapshot().context.viewport.y).toBeCloseTo(25);
290
+ expect(store.getSnapshot().context.viewport.zoom).toBeCloseTo(2);
291
+
292
+ ticker.tick(50);
293
+ await animation.done;
294
+
295
+ expect(store.getSnapshot().context.viewport).toEqual({ x: 100, y: 50, zoom: 4 });
296
+ });
297
+
298
+ it('smoothly lerps each viewport component with a configurable factor', () => {
299
+ const store = createFlowStore();
300
+ const ticker = manualRaf();
301
+
302
+ store.trigger.setViewport({ x: -40, y: 300, zoom: 0.5 });
303
+ const to = { x: 600, y: -200, zoom: 3 };
304
+
305
+ animateViewport(store, to, {
306
+ lerp: 0.25,
307
+ raf: ticker.raf,
308
+ cancelRaf: ticker.cancelRaf,
309
+ });
310
+
311
+ ticker.tick(0);
312
+ expect(store.getSnapshot().context.viewport).toEqual({
313
+ x: -40,
314
+ y: 300,
315
+ zoom: 0.5,
316
+ });
317
+
318
+ ticker.tick(1000 / 60);
319
+
320
+ expect(store.getSnapshot().context.viewport).toEqual({
321
+ x: 120,
322
+ y: 175,
323
+ zoom: 1.125,
324
+ });
325
+
326
+ ticker.tick(1000 / 60);
327
+
328
+ expect(store.getSnapshot().context.viewport).toEqual({
329
+ x: 240,
330
+ y: 81.25,
331
+ zoom: 1.59375,
332
+ });
333
+ });
334
+
335
+ it('normalizes the lerp factor across display refresh rates', () => {
336
+ const at60Hz = createFlowStore();
337
+ const at120Hz = createFlowStore();
338
+ const ticker60 = manualRaf();
339
+ const ticker120 = manualRaf();
340
+ const target = { x: 100, y: 50, zoom: 2 };
341
+
342
+ animateViewport(at60Hz, target, {
343
+ lerp: 0.2,
344
+ raf: ticker60.raf,
345
+ cancelRaf: ticker60.cancelRaf,
346
+ });
347
+ animateViewport(at120Hz, target, {
348
+ lerp: 0.2,
349
+ raf: ticker120.raf,
350
+ cancelRaf: ticker120.cancelRaf,
351
+ });
352
+
353
+ ticker60.tick(0);
354
+ ticker60.tick(1000 / 60);
355
+ ticker120.tick(0);
356
+ ticker120.tick(1000 / 120);
357
+ ticker120.tick(1000 / 120);
358
+
359
+ const viewport60 = at60Hz.getSnapshot().context.viewport;
360
+ const viewport120 = at120Hz.getSnapshot().context.viewport;
361
+ expect(viewport120.x).toBeCloseTo(viewport60.x, 10);
362
+ expect(viewport120.y).toBeCloseTo(viewport60.y, 10);
363
+ expect(viewport120.zoom).toBeCloseTo(viewport60.zoom, 10);
364
+ });
365
+
366
+ it('rejects invalid or conflicting lerp options', () => {
367
+ const store = createFlowStore();
368
+ const target = { x: 100, y: 50, zoom: 2 };
369
+
370
+ for (const factor of [0, -0.1, 1.1, Number.NaN]) {
371
+ expect(() => animateViewport(store, target, { lerp: factor })).toThrow(
372
+ 'Viewport lerp factor must be finite and in (0, 1].',
373
+ );
374
+ }
375
+ expect(() =>
376
+ animateViewport(store, target, { lerp: 0.2, spring: {} }),
377
+ ).toThrow('Viewport animation cannot use both spring and lerp.');
378
+ });
379
+
380
+ it('cancel stops future animator frames', () => {
381
+ const store = createFlowStore();
382
+ const ticker = manualRaf();
383
+
384
+ const animation = animateViewport(
385
+ store,
386
+ { x: 100, y: 0, zoom: 1 },
387
+ { duration: 100, easing: (t) => t, raf: ticker.raf, cancelRaf: ticker.cancelRaf },
388
+ );
389
+
390
+ ticker.tick(0);
391
+ ticker.tick(25);
392
+ expect(store.getSnapshot().context.viewport.x).toBe(25);
393
+
394
+ animation.cancel();
395
+ ticker.tick(75);
396
+
397
+ expect(store.getSnapshot().context.viewport.x).toBe(25);
398
+ });
399
+
400
+ it('duration 0 applies synchronously', async () => {
401
+ const store = createFlowStore();
402
+ const animation = animateViewport(store, { x: 1, y: 2, zoom: 3 }, { duration: 0 });
403
+
404
+ await animation.done;
405
+
406
+ expect(store.getSnapshot().context.viewport).toEqual({ x: 1, y: 2, zoom: 3 });
407
+ });
408
+
409
+ it('keeps the target center pinned on screen throughout the zoom (no curve)', () => {
410
+ const store = createFlowStore();
411
+ const ticker = manualRaf();
412
+
413
+ // Non-trivial start so a linear x/y lerp would visibly curve.
414
+ store.trigger.setViewport({ x: -40, y: 300, zoom: 0.5 });
415
+
416
+ const from = { ...store.getSnapshot().context.viewport };
417
+ const to = { x: 600, y: -200, zoom: 3 };
418
+
419
+ // The focal anchor the implementation uses with no explicit pivot is canvas
420
+ // origin. Its screen position must move MONOTONICALLY and LINEARLY between
421
+ // its start and end screen positions — a linear x/y lerp (the old buggy
422
+ // behavior) would instead trace a curve because zoom moves in log space.
423
+ const focalCanvas = { x: 0, y: 0 };
424
+ const screenStart = {
425
+ x: focalCanvas.x * from.zoom + from.x,
426
+ y: focalCanvas.y * from.zoom + from.y,
427
+ };
428
+ const screenEnd = {
429
+ x: focalCanvas.x * to.zoom + to.x,
430
+ y: focalCanvas.y * to.zoom + to.y,
431
+ };
432
+
433
+ animateViewport(store, to, {
434
+ duration: 100,
435
+ easing: (t) => t,
436
+ raf: ticker.raf,
437
+ cancelRaf: ticker.cancelRaf,
438
+ });
439
+
440
+ const zooms: number[] = [];
441
+ for (let ms = 0; ms <= 100; ms += 10) {
442
+ ticker.tick(ms === 0 ? 0 : 10);
443
+ const t = ms / 100;
444
+ const vp = store.getSnapshot().context.viewport;
445
+ // Focal invariance (anti-curve): anchor screen pos == linear interpolation.
446
+ const screen = {
447
+ x: focalCanvas.x * vp.zoom + vp.x,
448
+ y: focalCanvas.y * vp.zoom + vp.y,
449
+ };
450
+ expect(screen.x).toBeCloseTo(
451
+ screenStart.x + (screenEnd.x - screenStart.x) * t,
452
+ 6,
453
+ );
454
+ expect(screen.y).toBeCloseTo(
455
+ screenStart.y + (screenEnd.y - screenStart.y) * t,
456
+ 6,
457
+ );
458
+ zooms.push(vp.zoom);
459
+ }
460
+
461
+ // Zoom is monotonic increasing and ends exactly at target.
462
+ for (let i = 1; i < zooms.length; i++) {
463
+ expect(zooms[i]).toBeGreaterThanOrEqual(zooms[i - 1] - 1e-9);
464
+ }
465
+ expect(from.zoom).toBe(0.5);
466
+ expect(store.getSnapshot().context.viewport).toEqual(to);
467
+ });
468
+
469
+ it('honors an explicit screen-space center pivot', () => {
470
+ const store = createFlowStore();
471
+ const ticker = manualRaf();
472
+
473
+ store.trigger.setViewport({ x: 10, y: 20, zoom: 1 });
474
+ const from = { ...store.getSnapshot().context.viewport };
475
+ const to = { x: 200, y: 80, zoom: 2.5 };
476
+ const pivot = { x: 320, y: 240 };
477
+
478
+ // Canvas point under the pivot at the start viewport.
479
+ const focalCanvas = {
480
+ x: (pivot.x - from.x) / from.zoom,
481
+ y: (pivot.y - from.y) / from.zoom,
482
+ };
483
+
484
+ const animation = animateViewport(store, to, {
485
+ duration: 100,
486
+ easing: (t) => t,
487
+ center: pivot,
488
+ raf: ticker.raf,
489
+ cancelRaf: ticker.cancelRaf,
490
+ });
491
+
492
+ // At the start, the pivot's canvas point sits exactly under the pivot on
493
+ // screen and the interpolation pins it monotonically toward its target
494
+ // screen position.
495
+ ticker.tick(0);
496
+ const vp0 = store.getSnapshot().context.viewport;
497
+ expect(focalCanvas.x * vp0.zoom + vp0.x).toBeCloseTo(pivot.x, 6);
498
+ expect(focalCanvas.y * vp0.zoom + vp0.y).toBeCloseTo(pivot.y, 6);
499
+
500
+ ticker.tick(100);
501
+ expect(store.getSnapshot().context.viewport).toEqual(to);
502
+ void animation;
503
+ });
504
+
505
+ it('spring option settles at the target', async () => {
506
+ const store = createFlowStore();
507
+ const ticker = manualRaf();
508
+
509
+ store.trigger.setViewport({ x: -100, y: 50, zoom: 0.75 });
510
+ const to = { x: 400, y: -60, zoom: 2 };
511
+
512
+ const animation = animateViewport(store, to, {
513
+ spring: { stiffness: 170, damping: 26, mass: 1 },
514
+ raf: ticker.raf,
515
+ cancelRaf: ticker.cancelRaf,
516
+ });
517
+
518
+ // Advance until settled (guard against runaway).
519
+ for (let i = 0; i < 2000; i++) {
520
+ ticker.tick(16);
521
+ const vp = store.getSnapshot().context.viewport;
522
+ if (
523
+ Math.abs(vp.zoom - to.zoom) < 1e-3 &&
524
+ Math.abs(vp.x - to.x) < 1e-2 &&
525
+ Math.abs(vp.y - to.y) < 1e-2
526
+ ) {
527
+ break;
528
+ }
529
+ }
530
+
531
+ await animation.done;
532
+ expect(store.getSnapshot().context.viewport).toEqual(to);
533
+ });
534
+
535
+ it('spring keeps the focal anchor pinned while settling', () => {
536
+ const store = createFlowStore();
537
+ const ticker = manualRaf();
538
+
539
+ const from = { x: 0, y: 0, zoom: 1 };
540
+ store.trigger.setViewport(from);
541
+ const to = { x: 300, y: 120, zoom: 4 };
542
+ // Focal anchor (no pivot) is canvas origin; its screen pos runs from `from`
543
+ // origin to `to` origin.
544
+ const focalCanvas = { x: 0, y: 0 };
545
+ const screenStart = { x: from.x, y: from.y };
546
+ const screenEnd = { x: to.x, y: to.y };
547
+ const logFrom = Math.log(from.zoom);
548
+ const logTo = Math.log(to.zoom);
549
+
550
+ const animation = animateViewport(store, to, {
551
+ spring: {},
552
+ raf: ticker.raf,
553
+ cancelRaf: ticker.cancelRaf,
554
+ });
555
+
556
+ for (let i = 0; i < 30; i++) {
557
+ ticker.tick(16);
558
+ const vp = store.getSnapshot().context.viewport;
559
+ // Recover the spring progress from the (log-space) zoom, then assert the
560
+ // anchor's screen position matches the same-progress focal interpolation.
561
+ const p = (Math.log(vp.zoom) - logFrom) / (logTo - logFrom);
562
+ const screenX = focalCanvas.x * vp.zoom + vp.x;
563
+ const screenY = focalCanvas.y * vp.zoom + vp.y;
564
+ expect(screenX).toBeCloseTo(screenStart.x + (screenEnd.x - screenStart.x) * p, 4);
565
+ expect(screenY).toBeCloseTo(screenStart.y + (screenEnd.y - screenStart.y) * p, 4);
566
+ }
567
+ animation.cancel();
568
+ });
569
+ });