Skip to content

Commit 49679cc

Browse files
authored
Merge pull request sveltejs#2407 from sveltejs/better-transitions
better transitions
2 parents 5fc2fab + 2a2361f commit 49679cc

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

transition.mjs

+25-12
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ export function fly(node, {
1818
duration = 400,
1919
easing = cubicOut,
2020
x = 0,
21-
y = 0
21+
y = 0,
22+
opacity = 0
2223
}) {
2324
const style = getComputedStyle(node);
24-
const opacity = +style.opacity;
25+
const target_opacity = +style.opacity;
2526
const transform = style.transform === 'none' ? '' : style.transform;
2627

28+
const od = target_opacity * (1 - opacity);
29+
2730
return {
2831
delay,
2932
duration,
3033
easing,
31-
css: t => `
34+
css: (t, u) => `
3235
transform: ${transform} translate(${(1 - t) * x}px, ${(1 - t) * y}px);
33-
opacity: ${t * opacity}`
36+
opacity: ${target_opacity - (od * u)}`
3437
};
3538
}
3639

@@ -73,32 +76,42 @@ export function scale(node, {
7376
start = 0,
7477
opacity = 0
7578
}) {
76-
const sd = 1 - start;
77-
const od = 1 - opacity;
79+
const style = getComputedStyle(node);
80+
const target_opacity = +style.opacity;
81+
const transform = style.transform === 'none' ? '' : style.transform;
7882

79-
const transform = (
80-
node.style.transform ||
81-
getComputedStyle(node).transform
82-
).replace('none', '');
83+
const sd = 1 - start;
84+
const od = target_opacity * (1 - opacity);
8385

8486
return {
8587
delay,
8688
duration,
8789
easing,
8890
css: (t, u) => `
8991
transform: ${transform} scale(${1 - (sd * u)});
90-
opacity: ${1 - (od * u)}
92+
opacity: ${target_opacity - (od * u)}
9193
`
9294
};
9395
}
9496

9597
export function draw(node, {
9698
delay = 0,
97-
duration = 800,
99+
speed,
100+
duration,
98101
easing = cubicInOut
99102
}) {
100103
const len = node.getTotalLength();
101104

105+
if (duration === undefined) {
106+
if (speed === undefined) {
107+
duration = 800;
108+
} else {
109+
duration = len / speed;
110+
}
111+
} else if (typeof duration === 'function') {
112+
duration = duration(len);
113+
}
114+
102115
return {
103116
delay,
104117
duration,

0 commit comments

Comments
 (0)