Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds rotation property #14028

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/svelte/src/animate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ export function flip(node, { from, to }, params = {}) {

var transform = style.transform === 'none' ? '' : style.transform;
var [ox, oy] = style.transformOrigin.split(' ').map(parseFloat);

var m = new DOMMatrixReadOnly(transform);

var dsx = from.width / to.width;
var dsy = from.height / to.height;

var dx = (from.left + dsx * ox - (to.left + ox)) / zoom;
var dy = (from.top + dsy * oy - (to.top + oy)) / zoom;
var { delay = 0, duration = (d) => Math.sqrt(d) * 120, easing = cubicOut } = params;

var rf = Math.atan2(m.m21, m.m11) + Math.atan2(from.bottom - from.top, from.right - from.left);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about validating this calculation like this:

 function get_initial_angle(transform) {
	if (typeof transform === 'string') return 0

	var m = new DOMMatrixReadOnly(transform);

	return Math.atan2(m.m21, m.m11);
 }
 
	 var rf = get_initial_angle(transform) + Math.atan2(from.bottom - from.top, from.right - from.left);

I think skipping these calculations if transform is none would be cool when multiple elements without initial transforms are animated but I'm unsure how much it would impact complex transformations

var rt = Math.atan2(to.bottom - to.top, to.right - to.left);

var rn = normalize_angle(rf - rt);

return {
delay,
duration: typeof duration === 'function' ? duration(Math.sqrt(dx * dx + dy * dy)) : duration,
Expand All @@ -32,11 +40,32 @@ export function flip(node, { from, to }, params = {}) {
var y = u * dy;
var sx = t + u * dsx;
var sy = t + u * dsy;
return `transform: ${transform} scale(${sx}, ${sy}) translate(${x}px, ${y}px);`;
var r = u * rn;

return `transform: ${transform} scale(${sx}, ${sy}) translate(${x}px, ${y}px) rotate(${r}rad);`;
}
};
}

/**
* Prevent extra flips
*
* @param {number} angle
*/
function normalize_angle(angle) {
var n = angle % (2 * Math.PI);

if (n > Math.PI) {
n -= 2 * Math.PI;
}

if (n < -Math.PI) {
n += 2 * Math.PI;
}

return n;
}

/**
* @param {Element} element
*/
Expand Down