Skip to content

Commit 321e3ea

Browse files
feat: better flip animations (#13317)
* fix: flip not accounting for scaled lengths * zoom is uniform, one calculation will do * stash width/height multipliers * use var * scale-first appears to work better? * more robust zoom calculation * changeset --------- Co-authored-by: Etai Nadler <[email protected]>
1 parent d653040 commit 321e3ea

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

.changeset/cold-socks-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
feat: account for `zoom` when calculating animation transforms

packages/svelte/src/animate/index.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,48 @@ import { cubicOut } from '../easing/index.js';
1212
* @returns {AnimationConfig}
1313
*/
1414
export function flip(node, { from, to }, params = {}) {
15-
const style = getComputedStyle(node);
16-
const transform = style.transform === 'none' ? '' : style.transform;
17-
const [ox, oy] = style.transformOrigin.split(' ').map(parseFloat);
18-
const dx = from.left + (from.width * ox) / to.width - (to.left + ox);
19-
const dy = from.top + (from.height * oy) / to.height - (to.top + oy);
20-
const { delay = 0, duration = (d) => Math.sqrt(d) * 120, easing = cubicOut } = params;
15+
var style = getComputedStyle(node);
16+
var zoom = get_zoom(node); // https://drafts.csswg.org/css-viewport/#effective-zoom
17+
18+
var transform = style.transform === 'none' ? '' : style.transform;
19+
var [ox, oy] = style.transformOrigin.split(' ').map(parseFloat);
20+
var dsx = from.width / to.width;
21+
var dsy = from.height / to.height;
22+
23+
var dx = (from.left + dsx * ox - (to.left + ox)) / zoom;
24+
var dy = (from.top + dsy * oy - (to.top + oy)) / zoom;
25+
var { delay = 0, duration = (d) => Math.sqrt(d) * 120, easing = cubicOut } = params;
26+
2127
return {
2228
delay,
2329
duration: typeof duration === 'function' ? duration(Math.sqrt(dx * dx + dy * dy)) : duration,
2430
easing,
2531
css: (t, u) => {
26-
const x = u * dx;
27-
const y = u * dy;
28-
const sx = t + (u * from.width) / to.width;
29-
const sy = t + (u * from.height) / to.height;
30-
return `transform: ${transform} translate(${x}px, ${y}px) scale(${sx}, ${sy});`;
32+
var x = u * dx;
33+
var y = u * dy;
34+
var sx = t + u * dsx;
35+
var sy = t + u * dsy;
36+
return `transform: ${transform} scale(${sx}, ${sy}) translate(${x}px, ${y}px);`;
3137
}
3238
};
3339
}
40+
41+
/**
42+
* @param {Element} element
43+
*/
44+
function get_zoom(element) {
45+
if ('currentCSSZoom' in element) {
46+
return /** @type {number} */ (element.currentCSSZoom);
47+
}
48+
49+
/** @type {Element | null} */
50+
var current = element;
51+
var zoom = 1;
52+
53+
while (current !== null) {
54+
zoom *= +getComputedStyle(current).zoom;
55+
current = /** @type {Element | null} */ (current.parentNode);
56+
}
57+
58+
return zoom;
59+
}

0 commit comments

Comments
 (0)