Skip to content

Commit 79a67c9

Browse files
authored
fix: account for parent scale when animating elements (#14957)
* WIP fix flip * fix: account for parent scale when animating elements
1 parent 80d9f99 commit 79a67c9

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

.changeset/long-weeks-brush.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: account for parent scale when animating elements

packages/svelte/src/animate/index.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,37 @@ import { cubicOut } from '../easing/index.js';
1111
* @returns {AnimationConfig}
1212
*/
1313
export function flip(node, { from, to }, params = {}) {
14+
var { delay = 0, duration = (d) => Math.sqrt(d) * 120, easing = cubicOut } = params;
15+
1416
var style = getComputedStyle(node);
15-
var zoom = get_zoom(node); // https://drafts.csswg.org/css-viewport/#effective-zoom
1617

18+
// find the transform origin, expressed as a pair of values between 0 and 1
1719
var transform = style.transform === 'none' ? '' : style.transform;
1820
var [ox, oy] = style.transformOrigin.split(' ').map(parseFloat);
21+
ox /= node.clientWidth;
22+
oy /= node.clientHeight;
23+
24+
// calculate effect of parent transforms and zoom
25+
var zoom = get_zoom(node); // https://drafts.csswg.org/css-viewport/#effective-zoom
26+
var sx = node.clientWidth / to.width / zoom;
27+
var sy = node.clientHeight / to.height / zoom;
28+
29+
// find the starting position of the transform origin
30+
var fx = from.left + from.width * ox;
31+
var fy = from.top + from.height * oy;
32+
33+
// find the ending position of the transform origin
34+
var tx = to.left + to.width * ox;
35+
var ty = to.top + to.height * oy;
36+
37+
// find the translation at the start of the transform
38+
var dx = (fx - tx) * sx;
39+
var dy = (fy - ty) * sy;
40+
41+
// find the relative scale at the start of the transform
1942
var dsx = from.width / to.width;
2043
var dsy = from.height / to.height;
2144

22-
var dx = (from.left + dsx * ox - (to.left + ox)) / zoom;
23-
var dy = (from.top + dsy * oy - (to.top + oy)) / zoom;
24-
var { delay = 0, duration = (d) => Math.sqrt(d) * 120, easing = cubicOut } = params;
25-
2645
return {
2746
delay,
2847
duration: typeof duration === 'function' ? duration(Math.sqrt(dx * dx + dy * dy)) : duration,
@@ -32,7 +51,8 @@ export function flip(node, { from, to }, params = {}) {
3251
var y = u * dy;
3352
var sx = t + u * dsx;
3453
var sy = t + u * dsy;
35-
return `transform: ${transform} scale(${sx}, ${sy}) translate(${x}px, ${y}px);`;
54+
55+
return `transform: ${transform} translate(${x}px, ${y}px) scale(${sx}, ${sy});`;
3656
}
3757
};
3858
}

0 commit comments

Comments
 (0)