Skip to content

Commit 64a8d76

Browse files
committed
feat(core): UIScrollView zoom constants and the zoom stage geometry
The UIKit constants table gains zoomMedia, measured on iOS 26.2 in a UIKit lab app and in Photos: the give past the zoom limits, the eased settle and its duration law (a release past a limit and zoom(to:animated:)), Photos' aspect-fill double tap and its maximum zoom. Lightbox/zoomStage.ts is the pure geometry of a zooming scroll view in a photo viewer, checked against those numbers.
1 parent 3c791d3 commit 64a8d76

8 files changed

Lines changed: 915 additions & 0 deletions

File tree

‎.changeset/uikit-zoom-media.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[chore] Internal: UIScrollView zooming for a photo viewer in the UIKit constants table (the give past the zoom limits, the settle's eased curve and duration law, Photos' aspect-fill double tap and maximum zoom, measured on iOS 26.2), and the zoom stage geometry Lightbox's touch zoom is built on. Not exported from the package entry point; no component reads it yet.
6+
7+
@vjeux

‎packages/cli/assets/docs/motion.doc.mjs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@ const calmTheme = defineTheme({
445445
'duration',
446446
'500ms',
447447
],
448+
[
449+
'zoomMedia.settle.curve',
450+
'--motion-zoom-media-settle-curve',
451+
'curve',
452+
'cubic-bezier(0.42, 0, 0.58, 1)',
453+
],
448454
// motion-roles:end
449455
],
450456
},
Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file zoomStage.test.ts
5+
* @input vitest, the zoom stage geometry and the UIKit zoom constants
6+
* @output Checks the zoom stage against UIKit's measured zooming: the
7+
* overshoot law past the limits (and its inverse), the settle's duration
8+
* law and eased curve, Photos' aspect-fill double tap and maximum zoom, the
9+
* point kept under the fingers, the resting place for a scale, the rubber
10+
* band of a drag and the transform kept across a resize
11+
* @position Testing; zoomStage.ts without a browser
12+
*/
13+
14+
import {describe, expect, it} from 'vitest';
15+
import {cubicBezier} from '../motion/bezier';
16+
import {rubberBand} from '../motion/rubberBand';
17+
import {zoomMedia} from '../motion/uikit/zoomMedia';
18+
import {
19+
IDENTITY,
20+
doubleTapScaleFor,
21+
doubleTapTarget,
22+
draggedTransform,
23+
isZoomed,
24+
maxScaleFor,
25+
overshootScale,
26+
pannedBy,
27+
pinchScaleFor,
28+
rawForDragged,
29+
releaseTarget,
30+
restRange,
31+
restingTransform,
32+
settleCurves,
33+
settleSeconds,
34+
transformForResize,
35+
transformKeepingPoint,
36+
type ZoomGeometry,
37+
type ZoomTransform,
38+
} from './zoomStage';
39+
40+
/**
41+
* The lab's stage: a 402 × 874 screen showing a 3000 × 2000 photo fitted
42+
* 402 × 268 at its vertical centre, on a 3× screen.
43+
*/
44+
function landscape(overrides: Partial<ZoomGeometry> = {}): ZoomGeometry {
45+
const width = 402;
46+
const height = 874;
47+
const fit = {x: 0, y: (874 - 268) / 2, width: 402, height: 268};
48+
const doubleTapScale = doubleTapScaleFor(width, height, 402, 268);
49+
return {
50+
width,
51+
height,
52+
fit,
53+
minScale: 1,
54+
maxScale: maxScaleFor(3000, 402, 3, doubleTapScale),
55+
doubleTapScale,
56+
...overrides,
57+
};
58+
}
59+
60+
/** Where the photo point (u, v) of the fitted box shows on the stage. */
61+
function shown(
62+
geometry: ZoomGeometry,
63+
transform: ZoomTransform,
64+
u: number,
65+
v: number,
66+
) {
67+
return {
68+
x: geometry.fit.x + transform.x + u * transform.scale,
69+
y: geometry.fit.y + transform.y + v * transform.scale,
70+
};
71+
}
72+
73+
describe('the overshoot past the zoom limits (UIKit lab, iOS 26.2)', () => {
74+
const geometry = landscape({minScale: 1, maxScale: 4});
75+
76+
it('follows the pinch 1:1 inside the limits', () => {
77+
for (const scale of [1, 1.5, 2.25, 4]) {
78+
expect(overshootScale(geometry, scale)).toBe(scale);
79+
}
80+
});
81+
82+
it('gives way past the maximum as measured', () => {
83+
// Pinch 4.5 / 5 / 5.5 / 6 past a maximum of 4.
84+
const measured = [
85+
[4.5, 4.444],
86+
[5, 4.8],
87+
[5.5, 5.091],
88+
[6, 5.333],
89+
];
90+
for (const [pinch, zoom] of measured) {
91+
expect(overshootScale(geometry, pinch)).toBeCloseTo(zoom, 3);
92+
}
93+
});
94+
95+
it('gives way past the minimum as measured', () => {
96+
const measured = [
97+
[0.85, 0.87],
98+
[0.7, 0.769],
99+
[0.474, 0.655],
100+
[0.323, 0.596],
101+
];
102+
for (const [pinch, zoom] of measured) {
103+
expect(overshootScale(geometry, pinch)).toBeCloseTo(zoom, 3);
104+
}
105+
});
106+
107+
it('never reaches twice the maximum or half the minimum', () => {
108+
expect(overshootScale(geometry, 1e6)).toBeLessThan(8);
109+
expect(overshootScale(geometry, 1e-6)).toBeGreaterThan(0.5);
110+
});
111+
112+
it('inverts, so a pinch can continue from a photo already past a limit', () => {
113+
for (const pinch of [0.3, 0.6, 0.95, 1, 2, 4, 4.4, 7, 20]) {
114+
expect(
115+
pinchScaleFor(geometry, overshootScale(geometry, pinch)),
116+
).toBeCloseTo(pinch, 9);
117+
}
118+
});
119+
});
120+
121+
describe('the settle (UIScrollView zoom animation)', () => {
122+
it('lasts 0.1 s + 0.3 s × |ln(zoom ratio)|, at most 0.4 s, as measured', () => {
123+
expect(settleSeconds(5.333, 4)).toBeCloseTo(0.186, 3);
124+
expect(settleSeconds(4.8, 4)).toBeCloseTo(0.155, 3);
125+
expect(settleSeconds(0.596, 1)).toBeCloseTo(0.255, 3);
126+
expect(settleSeconds(1, 2.5)).toBeCloseTo(0.375, 3);
127+
expect(settleSeconds(2.5, 1)).toBeCloseTo(0.375, 3);
128+
expect(settleSeconds(1, 4)).toBe(0.4);
129+
// A move at the same zoom (a keyboard step).
130+
expect(settleSeconds(3, 3)).toBeCloseTo(0.1, 9);
131+
});
132+
133+
it('moves zoom and offset together on easeInEaseOut', () => {
134+
const from: ZoomTransform = {x: 0, y: 0, scale: 1};
135+
const to: ZoomTransform = {x: -400, y: -100, scale: 3};
136+
const curves = settleCurves(from, to);
137+
const ease = cubicBezier([0.42, 0, 0.58, 1]);
138+
expect(zoomMedia.settle.curve).toEqual([0.42, 0, 0.58, 1]);
139+
expect(curves.duration).toBe(0.4);
140+
for (const p of [0, 0.1, 0.25, 0.5, 0.75, 1]) {
141+
const t = p * curves.duration;
142+
expect(curves.scale.at(t)).toBeCloseTo(1 + 2 * ease(p), 9);
143+
expect(curves.x.at(t)).toBeCloseTo(-400 * ease(p), 9);
144+
expect(curves.y.at(t)).toBeCloseTo(-100 * ease(p), 9);
145+
}
146+
// Ease in and out: at rest at both ends, fastest in the middle.
147+
expect(curves.x.velocity(0)).toBeCloseTo(0, 0);
148+
expect(curves.x.velocity(curves.duration)).toBeCloseTo(0, 0);
149+
expect(Math.abs(curves.x.velocity(curves.duration / 2))).toBeGreaterThan(
150+
1000,
151+
);
152+
});
153+
});
154+
155+
describe("Photos' double tap and maximum zoom (iOS 26.2)", () => {
156+
it('zooms until the photo fills the screen (aspect fill)', () => {
157+
// Measured: landscape 402 × 268 → 3.261; portrait 402 × 603 → 1.449.
158+
expect(doubleTapScaleFor(402, 874, 402, 268)).toBeCloseTo(3.261, 3);
159+
expect(doubleTapScaleFor(402, 874, 402, 603)).toBeCloseTo(1.449, 3);
160+
});
161+
162+
it('falls back to 2× when the photo already fills the stage', () => {
163+
expect(doubleTapScaleFor(400, 300, 400, 300)).toBe(2);
164+
});
165+
166+
it('allows 6 times the zoom that shows one image pixel per device pixel', () => {
167+
// Measured: 14.93 for 3000 × 2000 and 9.95 for 2000 × 3000, fitted 402 pt
168+
// wide at 3×.
169+
expect(maxScaleFor(3000, 402, 3, 3.261)).toBeCloseTo(14.93, 2);
170+
expect(maxScaleFor(2000, 402, 3, 1.449)).toBeCloseTo(9.95, 2);
171+
// Never below the double tap's zoom.
172+
expect(maxScaleFor(200, 402, 3, 3.261)).toBe(3.261);
173+
});
174+
175+
it('brings a centre tap to the centre, at the aspect-fill zoom', () => {
176+
const geometry = landscape();
177+
const target = doubleTapTarget(geometry, IDENTITY, {x: 201, y: 437});
178+
expect(target.scale).toBeCloseTo(874 / 268, 9);
179+
// The photo point under the tap (its centre) ends at the screen centre,
180+
// and the photo exactly fills the screen's height.
181+
const centre = shown(geometry, target, 201, 134);
182+
expect(centre.x).toBeCloseTo(201, 9);
183+
expect(centre.y).toBeCloseTo(437, 9);
184+
expect(geometry.fit.y + target.y).toBeCloseTo(0, 9);
185+
});
186+
187+
it('brings an off-centre tap to the centre', () => {
188+
const geometry = landscape();
189+
// A third of the way across the photo.
190+
const target = doubleTapTarget(geometry, IDENTITY, {x: 134, y: 437});
191+
expect(shown(geometry, target, 134, 134).x).toBeCloseTo(201, 9);
192+
});
193+
194+
it('stops at the edge for a tap near it', () => {
195+
const geometry = landscape();
196+
const target = doubleTapTarget(geometry, IDENTITY, {x: 20, y: 437});
197+
// The photo's left edge rests on the screen's left edge.
198+
expect(geometry.fit.x + target.x).toBeCloseTo(0, 9);
199+
});
200+
201+
it('returns to the fitted size when zoomed', () => {
202+
const geometry = landscape();
203+
const zoomed = doubleTapTarget(geometry, IDENTITY, {x: 201, y: 437});
204+
expect(isZoomed(geometry, zoomed)).toBe(true);
205+
expect(doubleTapTarget(geometry, zoomed, {x: 50, y: 50})).toEqual(IDENTITY);
206+
});
207+
});
208+
209+
describe('the point under the fingers', () => {
210+
it('stays under the centroid as it moves and the zoom changes', () => {
211+
const geometry = landscape();
212+
const start: ZoomTransform = {x: -300, y: -100, scale: 2.5};
213+
const anchor = {x: 150, y: 400};
214+
const u = (anchor.x - geometry.fit.x - start.x) / start.scale;
215+
const v = (anchor.y - geometry.fit.y - start.y) / start.scale;
216+
const centroid = {x: 190, y: 360};
217+
const next = transformKeepingPoint(geometry, start, anchor, centroid, 4.2);
218+
const point = shown(geometry, next, u, v);
219+
expect(point.x).toBeCloseTo(centroid.x, 9);
220+
expect(point.y).toBeCloseTo(centroid.y, 9);
221+
expect(next.scale).toBe(4.2);
222+
});
223+
224+
it('settles a release past the maximum about the last centroid', () => {
225+
const geometry = landscape({maxScale: 4});
226+
const centroid = {x: 220, y: 430};
227+
// Released at 5.333 with the content point under the centroid at u, v.
228+
const released: ZoomTransform = {x: -700, y: -500, scale: 5.333};
229+
const target = releaseTarget(geometry, released, centroid);
230+
expect(target.scale).toBe(4);
231+
const u = (centroid.x - geometry.fit.x - released.x) / released.scale;
232+
const v = (centroid.y - geometry.fit.y - released.y) / released.scale;
233+
const point = shown(geometry, target, u, v);
234+
expect(point.x).toBeCloseTo(centroid.x, 9);
235+
expect(point.y).toBeCloseTo(centroid.y, 9);
236+
});
237+
238+
it('keeps a release inside the limits where it is', () => {
239+
const geometry = landscape();
240+
const released: ZoomTransform = {x: -100, y: -50, scale: 2};
241+
expect(releaseTarget(geometry, released, {x: 0, y: 0})).toBe(released);
242+
});
243+
244+
it('settles a release past the minimum to the fitted size, centred', () => {
245+
const geometry = landscape();
246+
const released: ZoomTransform = {x: 40, y: 30, scale: 0.6};
247+
expect(releaseTarget(geometry, released, {x: 180, y: 420})).toEqual(
248+
IDENTITY,
249+
);
250+
});
251+
});
252+
253+
describe('the resting place', () => {
254+
it('centres the photo on an axis where it is smaller than the stage', () => {
255+
const geometry = landscape();
256+
// 2× is 804 × 536: wider than the screen, shorter than it.
257+
const range = restRange(geometry, 2);
258+
expect(range.x).toEqual({min: 402 - 804, max: 0});
259+
expect(range.y.min).toBe(range.y.max);
260+
expect(geometry.fit.y + range.y.min + 536 / 2).toBeCloseTo(874 / 2, 9);
261+
});
262+
263+
it('keeps a zoomed photo with no gap at either edge', () => {
264+
const geometry = landscape();
265+
expect(restingTransform(geometry, {x: 50, y: 0, scale: 2}).x).toBe(0);
266+
expect(restingTransform(geometry, {x: -900, y: 0, scale: 2}).x).toBe(
267+
402 - 804,
268+
);
269+
});
270+
271+
it('pans by keyboard steps inside those bounds', () => {
272+
const geometry = landscape();
273+
const from: ZoomTransform = {
274+
x: -10,
275+
y: restRange(geometry, 2).y.min,
276+
scale: 2,
277+
};
278+
expect(pannedBy(geometry, from, 50, 0).x).toBe(0);
279+
expect(pannedBy(geometry, from, -50, 0).x).toBe(-60);
280+
expect(pannedBy(geometry, from, 0, 50).y).toBe(from.y);
281+
});
282+
});
283+
284+
describe('the rubber band of a drag', () => {
285+
const geometry = landscape();
286+
287+
it('follows the finger inside the bounds', () => {
288+
const raw: ZoomTransform = {x: -200, y: 0, scale: 2};
289+
expect(draggedTransform(geometry, raw).x).toBe(-200);
290+
});
291+
292+
it("gives way past an edge with UIScrollView's rubber band over the stage width", () => {
293+
const raw: ZoomTransform = {x: 120, y: 0, scale: 2};
294+
expect(draggedTransform(geometry, raw).x).toBeCloseTo(
295+
rubberBand(120, 402, 0.55),
296+
9,
297+
);
298+
const far: ZoomTransform = {x: 402 - 804 - 300, y: 0, scale: 2};
299+
expect(draggedTransform(geometry, far).x).toBeCloseTo(
300+
402 - 804 + rubberBand(-300, 402, 0.55),
301+
9,
302+
);
303+
});
304+
305+
it('does not move the photo on an axis where it fits', () => {
306+
const raw: ZoomTransform = {x: -200, y: 500, scale: 2};
307+
expect(draggedTransform(geometry, raw).y).toBe(
308+
restRange(geometry, 2).y.min,
309+
);
310+
});
311+
312+
it('inverts, so a finger landing on a banded photo picks it up where it shows', () => {
313+
const raw: ZoomTransform = {x: 150, y: 0, scale: 2};
314+
const banded = draggedTransform(geometry, raw);
315+
expect(rawForDragged(geometry, banded).x).toBeCloseTo(150, 9);
316+
});
317+
});
318+
319+
describe('a resize while zoomed (rotation)', () => {
320+
it('keeps the photo point at the centre and its size on screen', () => {
321+
const before = landscape();
322+
const transform: ZoomTransform = {x: -500, y: -150, scale: 3};
323+
const u = (201 - before.fit.x - transform.x) / transform.scale / 402;
324+
const v = (437 - before.fit.y - transform.y) / transform.scale / 268;
325+
// Landscape orientation: 874 × 402, the photo fitted 603 × 402.
326+
const after: ZoomGeometry = {
327+
width: 874,
328+
height: 402,
329+
fit: {x: (874 - 603) / 2, y: 0, width: 603, height: 402},
330+
minScale: 1,
331+
maxScale: maxScaleFor(3000, 603, 3, 874 / 603),
332+
doubleTapScale: 874 / 603,
333+
};
334+
const next = transformForResize(before, after, transform);
335+
// The same on-screen size: 402 × 3 = 1206 px wide either way.
336+
expect(603 * next.scale).toBeCloseTo(402 * 3, 9);
337+
const centre = shown(after, next, u * 603, v * 402);
338+
expect(centre.x).toBeCloseTo(437, 6);
339+
expect(centre.y).toBeCloseTo(201, 6);
340+
});
341+
342+
it('stays at the fitted size when not zoomed', () => {
343+
const before = landscape();
344+
const after = landscape({width: 500});
345+
expect(transformForResize(before, after, IDENTITY)).toEqual(IDENTITY);
346+
});
347+
});

0 commit comments

Comments
 (0)