Skip to content

Commit a0461a2

Browse files
committed
fix: account for parent scale when animating elements
1 parent a2be879 commit a0461a2

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

.changeset/long-weeks-brush.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+
fix: account for parent scale when animating elements

packages/svelte/src/animate/index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,36 @@ 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);
19-
var dsx = from.width / to.width;
20-
var dsy = from.height / to.height;
21-
2221
ox /= node.clientWidth;
2322
oy /= node.clientHeight;
2423

25-
var fromx = from.left + from.width * ox;
26-
var tox = to.left + to.width * ox;
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;
2728

28-
var fromy = from.top + from.height * oy;
29-
var toy = to.top + to.height * oy;
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;
3032

31-
var dx = ((fromx - tox) * (node.clientWidth / to.width)) / zoom;
32-
var dy = ((fromy - toy) * (node.clientHeight / to.height)) / zoom;
33-
var { delay = 0, duration = (d) => Math.sqrt(d) * 120, easing = cubicOut } = params;
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
42+
var dsx = from.width / to.width;
43+
var dsy = from.height / to.height;
3444

3545
return {
3646
delay,

0 commit comments

Comments
 (0)