Skip to content

Commit

Permalink
fix: account for parent scale when animating elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 8, 2025
1 parent a2be879 commit a0461a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-weeks-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: account for parent scale when animating elements
32 changes: 21 additions & 11 deletions packages/svelte/src/animate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,36 @@ import { cubicOut } from '../easing/index.js';
* @returns {AnimationConfig}
*/
export function flip(node, { from, to }, params = {}) {
var { delay = 0, duration = (d) => Math.sqrt(d) * 120, easing = cubicOut } = params;

var style = getComputedStyle(node);
var zoom = get_zoom(node); // https://drafts.csswg.org/css-viewport/#effective-zoom

// find the transform origin, expressed as a pair of values between 0 and 1
var transform = style.transform === 'none' ? '' : style.transform;
var [ox, oy] = style.transformOrigin.split(' ').map(parseFloat);
var dsx = from.width / to.width;
var dsy = from.height / to.height;

ox /= node.clientWidth;
oy /= node.clientHeight;

var fromx = from.left + from.width * ox;
var tox = to.left + to.width * ox;
// calculate effect of parent transforms and zoom
var zoom = get_zoom(node); // https://drafts.csswg.org/css-viewport/#effective-zoom
var sx = node.clientWidth / to.width / zoom;
var sy = node.clientHeight / to.height / zoom;

var fromy = from.top + from.height * oy;
var toy = to.top + to.height * oy;
// find the starting position of the transform origin
var fx = from.left + from.width * ox;
var fy = from.top + from.height * oy;

var dx = ((fromx - tox) * (node.clientWidth / to.width)) / zoom;
var dy = ((fromy - toy) * (node.clientHeight / to.height)) / zoom;
var { delay = 0, duration = (d) => Math.sqrt(d) * 120, easing = cubicOut } = params;
// find the ending position of the transform origin
var tx = to.left + to.width * ox;
var ty = to.top + to.height * oy;

// find the translation at the start of the transform
var dx = (fx - tx) * sx;
var dy = (fy - ty) * sy;

// find the relative scale at the start of the transform
var dsx = from.width / to.width;
var dsy = from.height / to.height;

return {
delay,
Expand Down

0 comments on commit a0461a2

Please sign in to comment.