UNPKG

6.79 kBJavaScriptView Raw
1import { cubicInOut, linear, cubicOut } from '../easing';
2import { is_function, assign } from '../internal';
3
4/*! *****************************************************************************
5Copyright (c) Microsoft Corporation. All rights reserved.
6Licensed under the Apache License, Version 2.0 (the "License"); you may not use
7this file except in compliance with the License. You may obtain a copy of the
8License at http://www.apache.org/licenses/LICENSE-2.0
9
10THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
12WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
13MERCHANTABLITY OR NON-INFRINGEMENT.
14
15See the Apache Version 2.0 License for specific language governing permissions
16and limitations under the License.
17***************************************************************************** */
18
19function __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++) {
25 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
26 t[p[i]] = s[p[i]];
27 }
28 return t;
29}
30
31function blur(node, { delay = 0, duration = 400, easing = cubicInOut, amount = 5, opacity = 0 }) {
32 const style = getComputedStyle(node);
33 const target_opacity = +style.opacity;
34 const f = style.filter === 'none' ? '' : style.filter;
35 const od = target_opacity * (1 - opacity);
36 return {
37 delay,
38 duration,
39 easing,
40 css: (_t, u) => `opacity: ${target_opacity - (od * u)}; filter: ${f} blur(${u * amount}px);`
41 };
42}
43function fade(node, { delay = 0, duration = 400, easing = linear }) {
44 const o = +getComputedStyle(node).opacity;
45 return {
46 delay,
47 duration,
48 easing,
49 css: t => `opacity: ${t * o}`
50 };
51}
52function fly(node, { delay = 0, duration = 400, easing = cubicOut, x = 0, y = 0, opacity = 0 }) {
53 const style = getComputedStyle(node);
54 const target_opacity = +style.opacity;
55 const transform = style.transform === 'none' ? '' : style.transform;
56 const od = target_opacity * (1 - opacity);
57 return {
58 delay,
59 duration,
60 easing,
61 css: (t, u) => `
62 transform: ${transform} translate(${(1 - t) * x}px, ${(1 - t) * y}px);
63 opacity: ${target_opacity - (od * u)}`
64 };
65}
66function slide(node, { delay = 0, duration = 400, easing = cubicOut }) {
67 const style = getComputedStyle(node);
68 const opacity = +style.opacity;
69 const height = parseFloat(style.height);
70 const padding_top = parseFloat(style.paddingTop);
71 const padding_bottom = parseFloat(style.paddingBottom);
72 const margin_top = parseFloat(style.marginTop);
73 const margin_bottom = parseFloat(style.marginBottom);
74 const border_top_width = parseFloat(style.borderTopWidth);
75 const border_bottom_width = parseFloat(style.borderBottomWidth);
76 return {
77 delay,
78 duration,
79 easing,
80 css: t => `overflow: hidden;` +
81 `opacity: ${Math.min(t * 20, 1) * opacity};` +
82 `height: ${t * height}px;` +
83 `padding-top: ${t * padding_top}px;` +
84 `padding-bottom: ${t * padding_bottom}px;` +
85 `margin-top: ${t * margin_top}px;` +
86 `margin-bottom: ${t * margin_bottom}px;` +
87 `border-top-width: ${t * border_top_width}px;` +
88 `border-bottom-width: ${t * border_bottom_width}px;`
89 };
90}
91function scale(node, { delay = 0, duration = 400, easing = cubicOut, start = 0, opacity = 0 }) {
92 const style = getComputedStyle(node);
93 const target_opacity = +style.opacity;
94 const transform = style.transform === 'none' ? '' : style.transform;
95 const sd = 1 - start;
96 const od = target_opacity * (1 - opacity);
97 return {
98 delay,
99 duration,
100 easing,
101 css: (_t, u) => `
102 transform: ${transform} scale(${1 - (sd * u)});
103 opacity: ${target_opacity - (od * u)}
104 `
105 };
106}
107function draw(node, { delay = 0, speed, duration, easing = cubicInOut }) {
108 const len = node.getTotalLength();
109 if (duration === undefined) {
110 if (speed === undefined) {
111 duration = 800;
112 }
113 else {
114 duration = len / speed;
115 }
116 }
117 else if (typeof duration === 'function') {
118 duration = duration(len);
119 }
120 return {
121 delay,
122 duration,
123 easing,
124 css: (t, u) => `stroke-dasharray: ${t * len} ${u * len}`
125 };
126}
127function crossfade(_a) {
128 var { fallback } = _a, defaults = __rest(_a, ["fallback"]);
129 const to_receive = new Map();
130 const to_send = new Map();
131 function crossfade(from, node, params) {
132 const { delay = 0, duration = d => Math.sqrt(d) * 30, easing = cubicOut } = assign(assign({}, defaults), params);
133 const to = node.getBoundingClientRect();
134 const dx = from.left - to.left;
135 const dy = from.top - to.top;
136 const dw = from.width / to.width;
137 const dh = from.height / to.height;
138 const d = Math.sqrt(dx * dx + dy * dy);
139 const style = getComputedStyle(node);
140 const transform = style.transform === 'none' ? '' : style.transform;
141 const opacity = +style.opacity;
142 return {
143 delay,
144 duration: is_function(duration) ? duration(d) : duration,
145 easing,
146 css: (t, u) => `
147 opacity: ${t * opacity};
148 transform-origin: top left;
149 transform: ${transform} translate(${u * dx}px,${u * dy}px) scale(${t + (1 - t) * dw}, ${t + (1 - t) * dh});
150 `
151 };
152 }
153 function transition(items, counterparts, intro) {
154 return (node, params) => {
155 items.set(params.key, {
156 rect: node.getBoundingClientRect()
157 });
158 return () => {
159 if (counterparts.has(params.key)) {
160 const { rect } = counterparts.get(params.key);
161 counterparts.delete(params.key);
162 return crossfade(rect, node, params);
163 }
164 // if the node is disappearing altogether
165 // (i.e. wasn't claimed by the other list)
166 // then we need to supply an outro
167 items.delete(params.key);
168 return fallback && fallback(node, params, intro);
169 };
170 };
171 }
172 return [
173 transition(to_send, to_receive, false),
174 transition(to_receive, to_send, true)
175 ];
176}
177
178export { blur, crossfade, draw, fade, fly, scale, slide };