1 | import { cubicOut, cubicInOut } from '../easing';
|
2 | import { is_function, assign } from '../internal';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function __rest(s, e) {
|
20 | var t = {};
|
21 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
22 | t[p] = s[p];
|
23 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
24 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
|
25 | t[p[i]] = s[p[i]];
|
26 | return t;
|
27 | }
|
28 |
|
29 | function fade(node, { delay = 0, duration = 400 }) {
|
30 | const o = +getComputedStyle(node).opacity;
|
31 | return {
|
32 | delay,
|
33 | duration,
|
34 | css: t => `opacity: ${t * o}`
|
35 | };
|
36 | }
|
37 | function fly(node, { delay = 0, duration = 400, easing = cubicOut, x = 0, y = 0, opacity = 0 }) {
|
38 | const style = getComputedStyle(node);
|
39 | const target_opacity = +style.opacity;
|
40 | const transform = style.transform === 'none' ? '' : style.transform;
|
41 | const od = target_opacity * (1 - opacity);
|
42 | return {
|
43 | delay,
|
44 | duration,
|
45 | easing,
|
46 | css: (t, u) => `
|
47 | transform: ${transform} translate(${(1 - t) * x}px, ${(1 - t) * y}px);
|
48 | opacity: ${target_opacity - (od * u)}`
|
49 | };
|
50 | }
|
51 | function slide(node, { delay = 0, duration = 400, easing = cubicOut }) {
|
52 | const style = getComputedStyle(node);
|
53 | const opacity = +style.opacity;
|
54 | const height = parseFloat(style.height);
|
55 | const padding_top = parseFloat(style.paddingTop);
|
56 | const padding_bottom = parseFloat(style.paddingBottom);
|
57 | const margin_top = parseFloat(style.marginTop);
|
58 | const margin_bottom = parseFloat(style.marginBottom);
|
59 | const border_top_width = parseFloat(style.borderTopWidth);
|
60 | const border_bottom_width = parseFloat(style.borderBottomWidth);
|
61 | return {
|
62 | delay,
|
63 | duration,
|
64 | easing,
|
65 | css: t => `overflow: hidden;` +
|
66 | `opacity: ${Math.min(t * 20, 1) * opacity};` +
|
67 | `height: ${t * height}px;` +
|
68 | `padding-top: ${t * padding_top}px;` +
|
69 | `padding-bottom: ${t * padding_bottom}px;` +
|
70 | `margin-top: ${t * margin_top}px;` +
|
71 | `margin-bottom: ${t * margin_bottom}px;` +
|
72 | `border-top-width: ${t * border_top_width}px;` +
|
73 | `border-bottom-width: ${t * border_bottom_width}px;`
|
74 | };
|
75 | }
|
76 | function scale(node, { delay = 0, duration = 400, easing = cubicOut, start = 0, opacity = 0 }) {
|
77 | const style = getComputedStyle(node);
|
78 | const target_opacity = +style.opacity;
|
79 | const transform = style.transform === 'none' ? '' : style.transform;
|
80 | const sd = 1 - start;
|
81 | const od = target_opacity * (1 - opacity);
|
82 | return {
|
83 | delay,
|
84 | duration,
|
85 | easing,
|
86 | css: (_t, u) => `
|
87 | transform: ${transform} scale(${1 - (sd * u)});
|
88 | opacity: ${target_opacity - (od * u)}
|
89 | `
|
90 | };
|
91 | }
|
92 | function draw(node, { delay = 0, speed, duration, easing = cubicInOut }) {
|
93 | const len = node.getTotalLength();
|
94 | if (duration === undefined) {
|
95 | if (speed === undefined) {
|
96 | duration = 800;
|
97 | }
|
98 | else {
|
99 | duration = len / speed;
|
100 | }
|
101 | }
|
102 | else if (typeof duration === 'function') {
|
103 | duration = duration(len);
|
104 | }
|
105 | return {
|
106 | delay,
|
107 | duration,
|
108 | easing,
|
109 | css: (t, u) => `stroke-dasharray: ${t * len} ${u * len}`
|
110 | };
|
111 | }
|
112 | function crossfade(_a) {
|
113 | var { fallback } = _a, defaults = __rest(_a, ["fallback"]);
|
114 | const to_receive = new Map();
|
115 | const to_send = new Map();
|
116 | function crossfade(from, node, params) {
|
117 | const { delay = 0, duration = d => Math.sqrt(d) * 30, easing = cubicOut } = assign(assign({}, defaults), params);
|
118 | const to = node.getBoundingClientRect();
|
119 | const dx = from.left - to.left;
|
120 | const dy = from.top - to.top;
|
121 | const d = Math.sqrt(dx * dx + dy * dy);
|
122 | const style = getComputedStyle(node);
|
123 | const transform = style.transform === 'none' ? '' : style.transform;
|
124 | const opacity = +style.opacity;
|
125 | return {
|
126 | delay,
|
127 | duration: is_function(duration) ? duration(d) : duration,
|
128 | easing,
|
129 | css: (t, u) => `
|
130 | opacity: ${t * opacity};
|
131 | transform: ${transform} translate(${u * dx}px,${u * dy}px);
|
132 | `
|
133 | };
|
134 | }
|
135 | function transition(items, counterparts, intro) {
|
136 | return (node, params) => {
|
137 | items.set(params.key, {
|
138 | rect: node.getBoundingClientRect()
|
139 | });
|
140 | return () => {
|
141 | if (counterparts.has(params.key)) {
|
142 | const { rect } = counterparts.get(params.key);
|
143 | counterparts.delete(params.key);
|
144 | return crossfade(rect, node, params);
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 | items.delete(params.key);
|
150 | return fallback && fallback(node, params, intro);
|
151 | };
|
152 | };
|
153 | }
|
154 | return [
|
155 | transition(to_send, to_receive, false),
|
156 | transition(to_receive, to_send, true)
|
157 | ];
|
158 | }
|
159 |
|
160 | export { fade, fly, slide, scale, draw, crossfade };
|