1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,1283 @@ |
1 |
+/* |
|
2 |
+ * anime.js v3.2.0 |
|
3 |
+ * (c) 2020 Julian Garnier |
|
4 |
+ * Released under the MIT license |
|
5 |
+ * animejs.com |
|
6 |
+ */ |
|
7 |
+ |
|
8 |
+'use strict'; |
|
9 |
+ |
|
10 |
+// Defaults |
|
11 |
+ |
|
12 |
+var defaultInstanceSettings = { |
|
13 |
+ update: null, |
|
14 |
+ begin: null, |
|
15 |
+ loopBegin: null, |
|
16 |
+ changeBegin: null, |
|
17 |
+ change: null, |
|
18 |
+ changeComplete: null, |
|
19 |
+ loopComplete: null, |
|
20 |
+ complete: null, |
|
21 |
+ loop: 1, |
|
22 |
+ direction: 'normal', |
|
23 |
+ autoplay: true, |
|
24 |
+ timelineOffset: 0 |
|
25 |
+}; |
|
26 |
+ |
|
27 |
+var defaultTweenSettings = { |
|
28 |
+ duration: 1000, |
|
29 |
+ delay: 0, |
|
30 |
+ endDelay: 0, |
|
31 |
+ easing: 'easeOutElastic(1, .5)', |
|
32 |
+ round: 0 |
|
33 |
+}; |
|
34 |
+ |
|
35 |
+var validTransforms = ['translateX', 'translateY', 'translateZ', 'rotate', 'rotateX', 'rotateY', 'rotateZ', 'scale', 'scaleX', 'scaleY', 'scaleZ', 'skew', 'skewX', 'skewY', 'perspective', 'matrix', 'matrix3d']; |
|
36 |
+ |
|
37 |
+// Caching |
|
38 |
+ |
|
39 |
+var cache = { |
|
40 |
+ CSS: {}, |
|
41 |
+ springs: {} |
|
42 |
+}; |
|
43 |
+ |
|
44 |
+// Utils |
|
45 |
+ |
|
46 |
+function minMax(val, min, max) { |
|
47 |
+ return Math.min(Math.max(val, min), max); |
|
48 |
+} |
|
49 |
+ |
|
50 |
+function stringContains(str, text) { |
|
51 |
+ return str.indexOf(text) > -1; |
|
52 |
+} |
|
53 |
+ |
|
54 |
+function applyArguments(func, args) { |
|
55 |
+ return func.apply(null, args); |
|
56 |
+} |
|
57 |
+ |
|
58 |
+var is = { |
|
59 |
+ arr: function (a) { return Array.isArray(a); }, |
|
60 |
+ obj: function (a) { return stringContains(Object.prototype.toString.call(a), 'Object'); }, |
|
61 |
+ pth: function (a) { return is.obj(a) && a.hasOwnProperty('totalLength'); }, |
|
62 |
+ svg: function (a) { return a instanceof SVGElement; }, |
|
63 |
+ inp: function (a) { return a instanceof HTMLInputElement; }, |
|
64 |
+ dom: function (a) { return a.nodeType || is.svg(a); }, |
|
65 |
+ str: function (a) { return typeof a === 'string'; }, |
|
66 |
+ fnc: function (a) { return typeof a === 'function'; }, |
|
67 |
+ und: function (a) { return typeof a === 'undefined'; }, |
|
68 |
+ hex: function (a) { return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(a); }, |
|
69 |
+ rgb: function (a) { return /^rgb/.test(a); }, |
|
70 |
+ hsl: function (a) { return /^hsl/.test(a); }, |
|
71 |
+ col: function (a) { return (is.hex(a) || is.rgb(a) || is.hsl(a)); }, |
|
72 |
+ key: function (a) { return !defaultInstanceSettings.hasOwnProperty(a) && !defaultTweenSettings.hasOwnProperty(a) && a !== 'targets' && a !== 'keyframes'; } |
|
73 |
+}; |
|
74 |
+ |
|
75 |
+// Easings |
|
76 |
+ |
|
77 |
+function parseEasingParameters(string) { |
|
78 |
+ var match = /\(([^)]+)\)/.exec(string); |
|
79 |
+ return match ? match[1].split(',').map(function (p) { return parseFloat(p); }) : []; |
|
80 |
+} |
|
81 |
+ |
|
82 |
+// Spring solver inspired by Webkit Copyright © 2016 Apple Inc. All rights reserved. https://webkit.org/demos/spring/spring.js |
|
83 |
+ |
|
84 |
+function spring(string, duration) { |
|
85 |
+ |
|
86 |
+ var params = parseEasingParameters(string); |
|
87 |
+ var mass = minMax(is.und(params[0]) ? 1 : params[0], .1, 100); |
|
88 |
+ var stiffness = minMax(is.und(params[1]) ? 100 : params[1], .1, 100); |
|
89 |
+ var damping = minMax(is.und(params[2]) ? 10 : params[2], .1, 100); |
|
90 |
+ var velocity = minMax(is.und(params[3]) ? 0 : params[3], .1, 100); |
|
91 |
+ var w0 = Math.sqrt(stiffness / mass); |
|
92 |
+ var zeta = damping / (2 * Math.sqrt(stiffness * mass)); |
|
93 |
+ var wd = zeta < 1 ? w0 * Math.sqrt(1 - zeta * zeta) : 0; |
|
94 |
+ var a = 1; |
|
95 |
+ var b = zeta < 1 ? (zeta * w0 + -velocity) / wd : -velocity + w0; |
|
96 |
+ |
|
97 |
+ function solver(t) { |
|
98 |
+ var progress = duration ? (duration * t) / 1000 : t; |
|
99 |
+ if (zeta < 1) { |
|
100 |
+ progress = Math.exp(-progress * zeta * w0) * (a * Math.cos(wd * progress) + b * Math.sin(wd * progress)); |
|
101 |
+ } else { |
|
102 |
+ progress = (a + b * progress) * Math.exp(-progress * w0); |
|
103 |
+ } |
|
104 |
+ if (t === 0 || t === 1) { return t; } |
|
105 |
+ return 1 - progress; |
|
106 |
+ } |
|
107 |
+ |
|
108 |
+ function getDuration() { |
|
109 |
+ var cached = cache.springs[string]; |
|
110 |
+ if (cached) { return cached; } |
|
111 |
+ var frame = 1/6; |
|
112 |
+ var elapsed = 0; |
|
113 |
+ var rest = 0; |
|
114 |
+ while(true) { |
|
115 |
+ elapsed += frame; |
|
116 |
+ if (solver(elapsed) === 1) { |
|
117 |
+ rest++; |
|
118 |
+ if (rest >= 16) { break; } |
|
119 |
+ } else { |
|
120 |
+ rest = 0; |
|
121 |
+ } |
|
122 |
+ } |
|
123 |
+ var duration = elapsed * frame * 1000; |
|
124 |
+ cache.springs[string] = duration; |
|
125 |
+ return duration; |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ return duration ? solver : getDuration; |
|
129 |
+ |
|
130 |
+} |
|
131 |
+ |
|
132 |
+// Basic steps easing implementation https://developer.mozilla.org/fr/docs/Web/CSS/transition-timing-function |
|
133 |
+ |
|
134 |
+function steps(steps) { |
|
135 |
+ if ( steps === void 0 ) steps = 10; |
|
136 |
+ |
|
137 |
+ return function (t) { return Math.ceil((minMax(t, 0.000001, 1)) * steps) * (1 / steps); }; |
|
138 |
+} |
|
139 |
+ |
|
140 |
+// BezierEasing https://github.com/gre/bezier-easing |
|
141 |
+ |
|
142 |
+var bezier = (function () { |
|
143 |
+ |
|
144 |
+ var kSplineTableSize = 11; |
|
145 |
+ var kSampleStepSize = 1.0 / (kSplineTableSize - 1.0); |
|
146 |
+ |
|
147 |
+ function A(aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1 } |
|
148 |
+ function B(aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1 } |
|
149 |
+ function C(aA1) { return 3.0 * aA1 } |
|
150 |
+ |
|
151 |
+ function calcBezier(aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT } |
|
152 |
+ function getSlope(aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1) } |
|
153 |
+ |
|
154 |
+ function binarySubdivide(aX, aA, aB, mX1, mX2) { |
|
155 |
+ var currentX, currentT, i = 0; |
|
156 |
+ do { |
|
157 |
+ currentT = aA + (aB - aA) / 2.0; |
|
158 |
+ currentX = calcBezier(currentT, mX1, mX2) - aX; |
|
159 |
+ if (currentX > 0.0) { aB = currentT; } else { aA = currentT; } |
|
160 |
+ } while (Math.abs(currentX) > 0.0000001 && ++i < 10); |
|
161 |
+ return currentT; |
|
162 |
+ } |
|
163 |
+ |
|
164 |
+ function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) { |
|
165 |
+ for (var i = 0; i < 4; ++i) { |
|
166 |
+ var currentSlope = getSlope(aGuessT, mX1, mX2); |
|
167 |
+ if (currentSlope === 0.0) { return aGuessT; } |
|
168 |
+ var currentX = calcBezier(aGuessT, mX1, mX2) - aX; |
|
169 |
+ aGuessT -= currentX / currentSlope; |
|
170 |
+ } |
|
171 |
+ return aGuessT; |
|
172 |
+ } |
|
173 |
+ |
|
174 |
+ function bezier(mX1, mY1, mX2, mY2) { |
|
175 |
+ |
|
176 |
+ if (!(0 <= mX1 && mX1 <= 1 && 0 <= mX2 && mX2 <= 1)) { return; } |
|
177 |
+ var sampleValues = new Float32Array(kSplineTableSize); |
|
178 |
+ |
|
179 |
+ if (mX1 !== mY1 || mX2 !== mY2) { |
|
180 |
+ for (var i = 0; i < kSplineTableSize; ++i) { |
|
181 |
+ sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2); |
|
182 |
+ } |
|
183 |
+ } |
|
184 |
+ |
|
185 |
+ function getTForX(aX) { |
|
186 |
+ |
|
187 |
+ var intervalStart = 0; |
|
188 |
+ var currentSample = 1; |
|
189 |
+ var lastSample = kSplineTableSize - 1; |
|
190 |
+ |
|
191 |
+ for (; currentSample !== lastSample && sampleValues[currentSample] <= aX; ++currentSample) { |
|
192 |
+ intervalStart += kSampleStepSize; |
|
193 |
+ } |
|
194 |
+ |
|
195 |
+ --currentSample; |
|
196 |
+ |
|
197 |
+ var dist = (aX - sampleValues[currentSample]) / (sampleValues[currentSample + 1] - sampleValues[currentSample]); |
|
198 |
+ var guessForT = intervalStart + dist * kSampleStepSize; |
|
199 |
+ var initialSlope = getSlope(guessForT, mX1, mX2); |
|
200 |
+ |
|
201 |
+ if (initialSlope >= 0.001) { |
|
202 |
+ return newtonRaphsonIterate(aX, guessForT, mX1, mX2); |
|
203 |
+ } else if (initialSlope === 0.0) { |
|
204 |
+ return guessForT; |
|
205 |
+ } else { |
|
206 |
+ return binarySubdivide(aX, intervalStart, intervalStart + kSampleStepSize, mX1, mX2); |
|
207 |
+ } |
|
208 |
+ |
|
209 |
+ } |
|
210 |
+ |
|
211 |
+ return function (x) { |
|
212 |
+ if (mX1 === mY1 && mX2 === mY2) { return x; } |
|
213 |
+ if (x === 0 || x === 1) { return x; } |
|
214 |
+ return calcBezier(getTForX(x), mY1, mY2); |
|
215 |
+ } |
|
216 |
+ |
|
217 |
+ } |
|
218 |
+ |
|
219 |
+ return bezier; |
|
220 |
+ |
|
221 |
+})(); |
|
222 |
+ |
|
223 |
+var penner = (function () { |
|
224 |
+ |
|
225 |
+ // Based on jQuery UI's implemenation of easing equations from Robert Penner (http://www.robertpenner.com/easing) |
|
226 |
+ |
|
227 |
+ var eases = { linear: function () { return function (t) { return t; }; } }; |
|
228 |
+ |
|
229 |
+ var functionEasings = { |
|
230 |
+ Sine: function () { return function (t) { return 1 - Math.cos(t * Math.PI / 2); }; }, |
|
231 |
+ Circ: function () { return function (t) { return 1 - Math.sqrt(1 - t * t); }; }, |
|
232 |
+ Back: function () { return function (t) { return t * t * (3 * t - 2); }; }, |
|
233 |
+ Bounce: function () { return function (t) { |
|
234 |
+ var pow2, b = 4; |
|
235 |
+ while (t < (( pow2 = Math.pow(2, --b)) - 1) / 11) {} |
|
236 |
+ return 1 / Math.pow(4, 3 - b) - 7.5625 * Math.pow(( pow2 * 3 - 2 ) / 22 - t, 2) |
|
237 |
+ }; }, |
|
238 |
+ Elastic: function (amplitude, period) { |
|
239 |
+ if ( amplitude === void 0 ) amplitude = 1; |
|
240 |
+ if ( period === void 0 ) period = .5; |
|
241 |
+ |
|
242 |
+ var a = minMax(amplitude, 1, 10); |
|
243 |
+ var p = minMax(period, .1, 2); |
|
244 |
+ return function (t) { |
|
245 |
+ return (t === 0 || t === 1) ? t : |
|
246 |
+ -a * Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (p / (Math.PI * 2) * Math.asin(1 / a))) * (Math.PI * 2)) / p); |
|
247 |
+ } |
|
248 |
+ } |
|
249 |
+ }; |
|
250 |
+ |
|
251 |
+ var baseEasings = ['Quad', 'Cubic', 'Quart', 'Quint', 'Expo']; |
|
252 |
+ |
|
253 |
+ baseEasings.forEach(function (name, i) { |
|
254 |
+ functionEasings[name] = function () { return function (t) { return Math.pow(t, i + 2); }; }; |
|
255 |
+ }); |
|
256 |
+ |
|
257 |
+ Object.keys(functionEasings).forEach(function (name) { |
|
258 |
+ var easeIn = functionEasings[name]; |
|
259 |
+ eases['easeIn' + name] = easeIn; |
|
260 |
+ eases['easeOut' + name] = function (a, b) { return function (t) { return 1 - easeIn(a, b)(1 - t); }; }; |
|
261 |
+ eases['easeInOut' + name] = function (a, b) { return function (t) { return t < 0.5 ? easeIn(a, b)(t * 2) / 2 : |
|
262 |
+ 1 - easeIn(a, b)(t * -2 + 2) / 2; }; }; |
|
263 |
+ }); |
|
264 |
+ |
|
265 |
+ return eases; |
|
266 |
+ |
|
267 |
+})(); |
|
268 |
+ |
|
269 |
+function parseEasings(easing, duration) { |
|
270 |
+ if (is.fnc(easing)) { return easing; } |
|
271 |
+ var name = easing.split('(')[0]; |
|
272 |
+ var ease = penner[name]; |
|
273 |
+ var args = parseEasingParameters(easing); |
|
274 |
+ switch (name) { |
|
275 |
+ case 'spring' : return spring(easing, duration); |
|
276 |
+ case 'cubicBezier' : return applyArguments(bezier, args); |
|
277 |
+ case 'steps' : return applyArguments(steps, args); |
|
278 |
+ default : return applyArguments(ease, args); |
|
279 |
+ } |
|
280 |
+} |
|
281 |
+ |
|
282 |
+// Strings |
|
283 |
+ |
|
284 |
+function selectString(str) { |
|
285 |
+ try { |
|
286 |
+ var nodes = document.querySelectorAll(str); |
|
287 |
+ return nodes; |
|
288 |
+ } catch(e) { |
|
289 |
+ return; |
|
290 |
+ } |
|
291 |
+} |
|
292 |
+ |
|
293 |
+// Arrays |
|
294 |
+ |
|
295 |
+function filterArray(arr, callback) { |
|
296 |
+ var len = arr.length; |
|
297 |
+ var thisArg = arguments.length >= 2 ? arguments[1] : void 0; |
|
298 |
+ var result = []; |
|
299 |
+ for (var i = 0; i < len; i++) { |
|
300 |
+ if (i in arr) { |
|
301 |
+ var val = arr[i]; |
|
302 |
+ if (callback.call(thisArg, val, i, arr)) { |
|
303 |
+ result.push(val); |
|
304 |
+ } |
|
305 |
+ } |
|
306 |
+ } |
|
307 |
+ return result; |
|
308 |
+} |
|
309 |
+ |
|
310 |
+function flattenArray(arr) { |
|
311 |
+ return arr.reduce(function (a, b) { return a.concat(is.arr(b) ? flattenArray(b) : b); }, []); |
|
312 |
+} |
|
313 |
+ |
|
314 |
+function toArray(o) { |
|
315 |
+ if (is.arr(o)) { return o; } |
|
316 |
+ if (is.str(o)) { o = selectString(o) || o; } |
|
317 |
+ if (o instanceof NodeList || o instanceof HTMLCollection) { return [].slice.call(o); } |
|
318 |
+ return [o]; |
|
319 |
+} |
|
320 |
+ |
|
321 |
+function arrayContains(arr, val) { |
|
322 |
+ return arr.some(function (a) { return a === val; }); |
|
323 |
+} |
|
324 |
+ |
|
325 |
+// Objects |
|
326 |
+ |
|
327 |
+function cloneObject(o) { |
|
328 |
+ var clone = {}; |
|
329 |
+ for (var p in o) { clone[p] = o[p]; } |
|
330 |
+ return clone; |
|
331 |
+} |
|
332 |
+ |
|
333 |
+function replaceObjectProps(o1, o2) { |
|
334 |
+ var o = cloneObject(o1); |
|
335 |
+ for (var p in o1) { o[p] = o2.hasOwnProperty(p) ? o2[p] : o1[p]; } |
|
336 |
+ return o; |
|
337 |
+} |
|
338 |
+ |
|
339 |
+function mergeObjects(o1, o2) { |
|
340 |
+ var o = cloneObject(o1); |
|
341 |
+ for (var p in o2) { o[p] = is.und(o1[p]) ? o2[p] : o1[p]; } |
|
342 |
+ return o; |
|
343 |
+} |
|
344 |
+ |
|
345 |
+// Colors |
|
346 |
+ |
|
347 |
+function rgbToRgba(rgbValue) { |
|
348 |
+ var rgb = /rgb\((\d+,\s*[\d]+,\s*[\d]+)\)/g.exec(rgbValue); |
|
349 |
+ return rgb ? ("rgba(" + (rgb[1]) + ",1)") : rgbValue; |
|
350 |
+} |
|
351 |
+ |
|
352 |
+function hexToRgba(hexValue) { |
|
353 |
+ var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; |
|
354 |
+ var hex = hexValue.replace(rgx, function (m, r, g, b) { return r + r + g + g + b + b; } ); |
|
355 |
+ var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
|
356 |
+ var r = parseInt(rgb[1], 16); |
|
357 |
+ var g = parseInt(rgb[2], 16); |
|
358 |
+ var b = parseInt(rgb[3], 16); |
|
359 |
+ return ("rgba(" + r + "," + g + "," + b + ",1)"); |
|
360 |
+} |
|
361 |
+ |
|
362 |
+function hslToRgba(hslValue) { |
|
363 |
+ var hsl = /hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.exec(hslValue) || /hsla\((\d+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)/g.exec(hslValue); |
|
364 |
+ var h = parseInt(hsl[1], 10) / 360; |
|
365 |
+ var s = parseInt(hsl[2], 10) / 100; |
|
366 |
+ var l = parseInt(hsl[3], 10) / 100; |
|
367 |
+ var a = hsl[4] || 1; |
|
368 |
+ function hue2rgb(p, q, t) { |
|
369 |
+ if (t < 0) { t += 1; } |
|
370 |
+ if (t > 1) { t -= 1; } |
|
371 |
+ if (t < 1/6) { return p + (q - p) * 6 * t; } |
|
372 |
+ if (t < 1/2) { return q; } |
|
373 |
+ if (t < 2/3) { return p + (q - p) * (2/3 - t) * 6; } |
|
374 |
+ return p; |
|
375 |
+ } |
|
376 |
+ var r, g, b; |
|
377 |
+ if (s == 0) { |
|
378 |
+ r = g = b = l; |
|
379 |
+ } else { |
|
380 |
+ var q = l < 0.5 ? l * (1 + s) : l + s - l * s; |
|
381 |
+ var p = 2 * l - q; |
|
382 |
+ r = hue2rgb(p, q, h + 1/3); |
|
383 |
+ g = hue2rgb(p, q, h); |
|
384 |
+ b = hue2rgb(p, q, h - 1/3); |
|
385 |
+ } |
|
386 |
+ return ("rgba(" + (r * 255) + "," + (g * 255) + "," + (b * 255) + "," + a + ")"); |
|
387 |
+} |
|
388 |
+ |
|
389 |
+function colorToRgb(val) { |
|
390 |
+ if (is.rgb(val)) { return rgbToRgba(val); } |
|
391 |
+ if (is.hex(val)) { return hexToRgba(val); } |
|
392 |
+ if (is.hsl(val)) { return hslToRgba(val); } |
|
393 |
+} |
|
394 |
+ |
|
395 |
+// Units |
|
396 |
+ |
|
397 |
+function getUnit(val) { |
|
398 |
+ var split = /[+-]?\d*\.?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?(%|px|pt|em|rem|in|cm|mm|ex|ch|pc|vw|vh|vmin|vmax|deg|rad|turn)?$/.exec(val); |
|
399 |
+ if (split) { return split[1]; } |
|
400 |
+} |
|
401 |
+ |
|
402 |
+function getTransformUnit(propName) { |
|
403 |
+ if (stringContains(propName, 'translate') || propName === 'perspective') { return 'px'; } |
|
404 |
+ if (stringContains(propName, 'rotate') || stringContains(propName, 'skew')) { return 'deg'; } |
|
405 |
+} |
|
406 |
+ |
|
407 |
+// Values |
|
408 |
+ |
|
409 |
+function getFunctionValue(val, animatable) { |
|
410 |
+ if (!is.fnc(val)) { return val; } |
|
411 |
+ return val(animatable.target, animatable.id, animatable.total); |
|
412 |
+} |
|
413 |
+ |
|
414 |
+function getAttribute(el, prop) { |
|
415 |
+ return el.getAttribute(prop); |
|
416 |
+} |
|
417 |
+ |
|
418 |
+function convertPxToUnit(el, value, unit) { |
|
419 |
+ var valueUnit = getUnit(value); |
|
420 |
+ if (arrayContains([unit, 'deg', 'rad', 'turn'], valueUnit)) { return value; } |
|
421 |
+ var cached = cache.CSS[value + unit]; |
|
422 |
+ if (!is.und(cached)) { return cached; } |
|
423 |
+ var baseline = 100; |
|
424 |
+ var tempEl = document.createElement(el.tagName); |
|
425 |
+ var parentEl = (el.parentNode && (el.parentNode !== document)) ? el.parentNode : document.body; |
|
426 |
+ parentEl.appendChild(tempEl); |
|
427 |
+ tempEl.style.position = 'absolute'; |
|
428 |
+ tempEl.style.width = baseline + unit; |
|
429 |
+ var factor = baseline / tempEl.offsetWidth; |
|
430 |
+ parentEl.removeChild(tempEl); |
|
431 |
+ var convertedUnit = factor * parseFloat(value); |
|
432 |
+ cache.CSS[value + unit] = convertedUnit; |
|
433 |
+ return convertedUnit; |
|
434 |
+} |
|
435 |
+ |
|
436 |
+function getCSSValue(el, prop, unit) { |
|
437 |
+ if (prop in el.style) { |
|
438 |
+ var uppercasePropName = prop.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); |
|
439 |
+ var value = el.style[prop] || getComputedStyle(el).getPropertyValue(uppercasePropName) || '0'; |
|
440 |
+ return unit ? convertPxToUnit(el, value, unit) : value; |
|
441 |
+ } |
|
442 |
+} |
|
443 |
+ |
|
444 |
+function getAnimationType(el, prop) { |
|
445 |
+ if (is.dom(el) && !is.inp(el) && (getAttribute(el, prop) || (is.svg(el) && el[prop]))) { return 'attribute'; } |
|
446 |
+ if (is.dom(el) && arrayContains(validTransforms, prop)) { return 'transform'; } |
|
447 |
+ if (is.dom(el) && (prop !== 'transform' && getCSSValue(el, prop))) { return 'css'; } |
|
448 |
+ if (el[prop] != null) { return 'object'; } |
|
449 |
+} |
|
450 |
+ |
|
451 |
+function getElementTransforms(el) { |
|
452 |
+ if (!is.dom(el)) { return; } |
|
453 |
+ var str = el.style.transform || ''; |
|
454 |
+ var reg = /(\w+)\(([^)]*)\)/g; |
|
455 |
+ var transforms = new Map(); |
|
456 |
+ var m; while (m = reg.exec(str)) { transforms.set(m[1], m[2]); } |
|
457 |
+ return transforms; |
|
458 |
+} |
|
459 |
+ |
|
460 |
+function getTransformValue(el, propName, animatable, unit) { |
|
461 |
+ var defaultVal = stringContains(propName, 'scale') ? 1 : 0 + getTransformUnit(propName); |
|
462 |
+ var value = getElementTransforms(el).get(propName) || defaultVal; |
|
463 |
+ if (animatable) { |
|
464 |
+ animatable.transforms.list.set(propName, value); |
|
465 |
+ animatable.transforms['last'] = propName; |
|
466 |
+ } |
|
467 |
+ return unit ? convertPxToUnit(el, value, unit) : value; |
|
468 |
+} |
|
469 |
+ |
|
470 |
+function getOriginalTargetValue(target, propName, unit, animatable) { |
|
471 |
+ switch (getAnimationType(target, propName)) { |
|
472 |
+ case 'transform': return getTransformValue(target, propName, animatable, unit); |
|
473 |
+ case 'css': return getCSSValue(target, propName, unit); |
|
474 |
+ case 'attribute': return getAttribute(target, propName); |
|
475 |
+ default: return target[propName] || 0; |
|
476 |
+ } |
|
477 |
+} |
|
478 |
+ |
|
479 |
+function getRelativeValue(to, from) { |
|
480 |
+ var operator = /^(\*=|\+=|-=)/.exec(to); |
|
481 |
+ if (!operator) { return to; } |
|
482 |
+ var u = getUnit(to) || 0; |
|
483 |
+ var x = parseFloat(from); |
|
484 |
+ var y = parseFloat(to.replace(operator[0], '')); |
|
485 |
+ switch (operator[0][0]) { |
|
486 |
+ case '+': return x + y + u; |
|
487 |
+ case '-': return x - y + u; |
|
488 |
+ case '*': return x * y + u; |
|
489 |
+ } |
|
490 |
+} |
|
491 |
+ |
|
492 |
+function validateValue(val, unit) { |
|
493 |
+ if (is.col(val)) { return colorToRgb(val); } |
|
494 |
+ if (/\s/g.test(val)) { return val; } |
|
495 |
+ var originalUnit = getUnit(val); |
|
496 |
+ var unitLess = originalUnit ? val.substr(0, val.length - originalUnit.length) : val; |
|
497 |
+ if (unit) { return unitLess + unit; } |
|
498 |
+ return unitLess; |
|
499 |
+} |
|
500 |
+ |
|
501 |
+// getTotalLength() equivalent for circle, rect, polyline, polygon and line shapes |
|
502 |
+// adapted from https://gist.github.com/SebLambla/3e0550c496c236709744 |
|
503 |
+ |
|
504 |
+function getDistance(p1, p2) { |
|
505 |
+ return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)); |
|
506 |
+} |
|
507 |
+ |
|
508 |
+function getCircleLength(el) { |
|
509 |
+ return Math.PI * 2 * getAttribute(el, 'r'); |
|
510 |
+} |
|
511 |
+ |
|
512 |
+function getRectLength(el) { |
|
513 |
+ return (getAttribute(el, 'width') * 2) + (getAttribute(el, 'height') * 2); |
|
514 |
+} |
|
515 |
+ |
|
516 |
+function getLineLength(el) { |
|
517 |
+ return getDistance( |
|
518 |
+ {x: getAttribute(el, 'x1'), y: getAttribute(el, 'y1')}, |
|
519 |
+ {x: getAttribute(el, 'x2'), y: getAttribute(el, 'y2')} |
|
520 |
+ ); |
|
521 |
+} |
|
522 |
+ |
|
523 |
+function getPolylineLength(el) { |
|
524 |
+ var points = el.points; |
|
525 |
+ var totalLength = 0; |
|
526 |
+ var previousPos; |
|
527 |
+ for (var i = 0 ; i < points.numberOfItems; i++) { |
|
528 |
+ var currentPos = points.getItem(i); |
|
529 |
+ if (i > 0) { totalLength += getDistance(previousPos, currentPos); } |
|
530 |
+ previousPos = currentPos; |
|
531 |
+ } |
|
532 |
+ return totalLength; |
|
533 |
+} |
|
534 |
+ |
|
535 |
+function getPolygonLength(el) { |
|
536 |
+ var points = el.points; |
|
537 |
+ return getPolylineLength(el) + getDistance(points.getItem(points.numberOfItems - 1), points.getItem(0)); |
|
538 |
+} |
|
539 |
+ |
|
540 |
+// Path animation |
|
541 |
+ |
|
542 |
+function getTotalLength(el) { |
|
543 |
+ if (el.getTotalLength) { return el.getTotalLength(); } |
|
544 |
+ switch(el.tagName.toLowerCase()) { |
|
545 |
+ case 'circle': return getCircleLength(el); |
|
546 |
+ case 'rect': return getRectLength(el); |
|
547 |
+ case 'line': return getLineLength(el); |
|
548 |
+ case 'polyline': return getPolylineLength(el); |
|
549 |
+ case 'polygon': return getPolygonLength(el); |
|
550 |
+ } |
|
551 |
+} |
|
552 |
+ |
|
553 |
+function setDashoffset(el) { |
|
554 |
+ var pathLength = getTotalLength(el); |
|
555 |
+ el.setAttribute('stroke-dasharray', pathLength); |
|
556 |
+ return pathLength; |
|
557 |
+} |
|
558 |
+ |
|
559 |
+// Motion path |
|
560 |
+ |
|
561 |
+function getParentSvgEl(el) { |
|
562 |
+ var parentEl = el.parentNode; |
|
563 |
+ while (is.svg(parentEl)) { |
|
564 |
+ if (!is.svg(parentEl.parentNode)) { break; } |
|
565 |
+ parentEl = parentEl.parentNode; |
|
566 |
+ } |
|
567 |
+ return parentEl; |
|
568 |
+} |
|
569 |
+ |
|
570 |
+function getParentSvg(pathEl, svgData) { |
|
571 |
+ var svg = svgData || {}; |
|
572 |
+ var parentSvgEl = svg.el || getParentSvgEl(pathEl); |
|
573 |
+ var rect = parentSvgEl.getBoundingClientRect(); |
|
574 |
+ var viewBoxAttr = getAttribute(parentSvgEl, 'viewBox'); |
|
575 |
+ var width = rect.width; |
|
576 |
+ var height = rect.height; |
|
577 |
+ var viewBox = svg.viewBox || (viewBoxAttr ? viewBoxAttr.split(' ') : [0, 0, width, height]); |
|
578 |
+ return { |
|
579 |
+ el: parentSvgEl, |
|
580 |
+ viewBox: viewBox, |
|
581 |
+ x: viewBox[0] / 1, |
|
582 |
+ y: viewBox[1] / 1, |
|
583 |
+ w: width / viewBox[2], |
|
584 |
+ h: height / viewBox[3] |
|
585 |
+ } |
|
586 |
+} |
|
587 |
+ |
|
588 |
+function getPath(path, percent) { |
|
589 |
+ var pathEl = is.str(path) ? selectString(path)[0] : path; |
|
590 |
+ var p = percent || 100; |
|
591 |
+ return function(property) { |
|
592 |
+ return { |
|
593 |
+ property: property, |
|
594 |
+ el: pathEl, |
|
595 |
+ svg: getParentSvg(pathEl), |
|
596 |
+ totalLength: getTotalLength(pathEl) * (p / 100) |
|
597 |
+ } |
|
598 |
+ } |
|
599 |
+} |
|
600 |
+ |
|
601 |
+function getPathProgress(path, progress) { |
|
602 |
+ function point(offset) { |
|
603 |
+ if ( offset === void 0 ) offset = 0; |
|
604 |
+ |
|
605 |
+ var l = progress + offset >= 1 ? progress + offset : 0; |
|
606 |
+ return path.el.getPointAtLength(l); |
|
607 |
+ } |
|
608 |
+ var svg = getParentSvg(path.el, path.svg); |
|
609 |
+ var p = point(); |
|
610 |
+ var p0 = point(-1); |
|
611 |
+ var p1 = point(+1); |
|
612 |
+ switch (path.property) { |
|
613 |
+ case 'x': return (p.x - svg.x) * svg.w; |
|
614 |
+ case 'y': return (p.y - svg.y) * svg.h; |
|
615 |
+ case 'angle': return Math.atan2(p1.y - p0.y, p1.x - p0.x) * 180 / Math.PI; |
|
616 |
+ } |
|
617 |
+} |
|
618 |
+ |
|
619 |
+// Decompose value |
|
620 |
+ |
|
621 |
+function decomposeValue(val, unit) { |
|
622 |
+ // const rgx = /-?\d*\.?\d+/g; // handles basic numbers |
|
623 |
+ // const rgx = /[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g; // handles exponents notation |
|
624 |
+ var rgx = /[+-]?\d*\.?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g; // handles exponents notation |
|
625 |
+ var value = validateValue((is.pth(val) ? val.totalLength : val), unit) + ''; |
|
626 |
+ return { |
|
627 |
+ original: value, |
|
628 |
+ numbers: value.match(rgx) ? value.match(rgx).map(Number) : [0], |
|
629 |
+ strings: (is.str(val) || unit) ? value.split(rgx) : [] |
|
630 |
+ } |
|
631 |
+} |
|
632 |
+ |
|
633 |
+// Animatables |
|
634 |
+ |
|
635 |
+function parseTargets(targets) { |
|
636 |
+ var targetsArray = targets ? (flattenArray(is.arr(targets) ? targets.map(toArray) : toArray(targets))) : []; |
|
637 |
+ return filterArray(targetsArray, function (item, pos, self) { return self.indexOf(item) === pos; }); |
|
638 |
+} |
|
639 |
+ |
|
640 |
+function getAnimatables(targets) { |
|
641 |
+ var parsed = parseTargets(targets); |
|
642 |
+ return parsed.map(function (t, i) { |
|
643 |
+ return {target: t, id: i, total: parsed.length, transforms: { list: getElementTransforms(t) } }; |
|
644 |
+ }); |
|
645 |
+} |
|
646 |
+ |
|
647 |
+// Properties |
|
648 |
+ |
|
649 |
+function normalizePropertyTweens(prop, tweenSettings) { |
|
650 |
+ var settings = cloneObject(tweenSettings); |
|
651 |
+ // Override duration if easing is a spring |
|
652 |
+ if (/^spring/.test(settings.easing)) { settings.duration = spring(settings.easing); } |
|
653 |
+ if (is.arr(prop)) { |
|
654 |
+ var l = prop.length; |
|
655 |
+ var isFromTo = (l === 2 && !is.obj(prop[0])); |
|
656 |
+ if (!isFromTo) { |
|
657 |
+ // Duration divided by the number of tweens |
|
658 |
+ if (!is.fnc(tweenSettings.duration)) { settings.duration = tweenSettings.duration / l; } |
|
659 |
+ } else { |
|
660 |
+ // Transform [from, to] values shorthand to a valid tween value |
|
661 |
+ prop = {value: prop}; |
|
662 |
+ } |
|
663 |
+ } |
|
664 |
+ var propArray = is.arr(prop) ? prop : [prop]; |
|
665 |
+ return propArray.map(function (v, i) { |
|
666 |
+ var obj = (is.obj(v) && !is.pth(v)) ? v : {value: v}; |
|
667 |
+ // Default delay value should only be applied to the first tween |
|
668 |
+ if (is.und(obj.delay)) { obj.delay = !i ? tweenSettings.delay : 0; } |
|
669 |
+ // Default endDelay value should only be applied to the last tween |
|
670 |
+ if (is.und(obj.endDelay)) { obj.endDelay = i === propArray.length - 1 ? tweenSettings.endDelay : 0; } |
|
671 |
+ return obj; |
|
672 |
+ }).map(function (k) { return mergeObjects(k, settings); }); |
|
673 |
+} |
|
674 |
+ |
|
675 |
+ |
|
676 |
+function flattenKeyframes(keyframes) { |
|
677 |
+ var propertyNames = filterArray(flattenArray(keyframes.map(function (key) { return Object.keys(key); })), function (p) { return is.key(p); }) |
|
678 |
+ .reduce(function (a,b) { if (a.indexOf(b) < 0) { a.push(b); } return a; }, []); |
|
679 |
+ var properties = {}; |
|
680 |
+ var loop = function ( i ) { |
|
681 |
+ var propName = propertyNames[i]; |
|
682 |
+ properties[propName] = keyframes.map(function (key) { |
|
683 |
+ var newKey = {}; |
|
684 |
+ for (var p in key) { |
|
685 |
+ if (is.key(p)) { |
|
686 |
+ if (p == propName) { newKey.value = key[p]; } |
|
687 |
+ } else { |
|
688 |
+ newKey[p] = key[p]; |
|
689 |
+ } |
|
690 |
+ } |
|
691 |
+ return newKey; |
|
692 |
+ }); |
|
693 |
+ }; |
|
694 |
+ |
|
695 |
+ for (var i = 0; i < propertyNames.length; i++) loop( i ); |
|
696 |
+ return properties; |
|
697 |
+} |
|
698 |
+ |
|
699 |
+function getProperties(tweenSettings, params) { |
|
700 |
+ var properties = []; |
|
701 |
+ var keyframes = params.keyframes; |
|
702 |
+ if (keyframes) { params = mergeObjects(flattenKeyframes(keyframes), params); } |
|
703 |
+ for (var p in params) { |
|
704 |
+ if (is.key(p)) { |
|
705 |
+ properties.push({ |
|
706 |
+ name: p, |
|
707 |
+ tweens: normalizePropertyTweens(params[p], tweenSettings) |
|
708 |
+ }); |
|
709 |
+ } |
|
710 |
+ } |
|
711 |
+ return properties; |
|
712 |
+} |
|
713 |
+ |
|
714 |
+// Tweens |
|
715 |
+ |
|
716 |
+function normalizeTweenValues(tween, animatable) { |
|
717 |
+ var t = {}; |
|
718 |
+ for (var p in tween) { |
|
719 |
+ var value = getFunctionValue(tween[p], animatable); |
|
720 |
+ if (is.arr(value)) { |
|
721 |
+ value = value.map(function (v) { return getFunctionValue(v, animatable); }); |
|
722 |
+ if (value.length === 1) { value = value[0]; } |
|
723 |
+ } |
|
724 |
+ t[p] = value; |
|
725 |
+ } |
|
726 |
+ t.duration = parseFloat(t.duration); |
|
727 |
+ t.delay = parseFloat(t.delay); |
|
728 |
+ return t; |
|
729 |
+} |
|
730 |
+ |
|
731 |
+function normalizeTweens(prop, animatable) { |
|
732 |
+ var previousTween; |
|
733 |
+ return prop.tweens.map(function (t) { |
|
734 |
+ var tween = normalizeTweenValues(t, animatable); |
|
735 |
+ var tweenValue = tween.value; |
|
736 |
+ var to = is.arr(tweenValue) ? tweenValue[1] : tweenValue; |
|
737 |
+ var toUnit = getUnit(to); |
|
738 |
+ var originalValue = getOriginalTargetValue(animatable.target, prop.name, toUnit, animatable); |
|
739 |
+ var previousValue = previousTween ? previousTween.to.original : originalValue; |
|
740 |
+ var from = is.arr(tweenValue) ? tweenValue[0] : previousValue; |
|
741 |
+ var fromUnit = getUnit(from) || getUnit(originalValue); |
|
742 |
+ var unit = toUnit || fromUnit; |
|
743 |
+ if (is.und(to)) { to = previousValue; } |
|
744 |
+ tween.from = decomposeValue(from, unit); |
|
745 |
+ tween.to = decomposeValue(getRelativeValue(to, from), unit); |
|
746 |
+ tween.start = previousTween ? previousTween.end : 0; |
|
747 |
+ tween.end = tween.start + tween.delay + tween.duration + tween.endDelay; |
|
748 |
+ tween.easing = parseEasings(tween.easing, tween.duration); |
|
749 |
+ tween.isPath = is.pth(tweenValue); |
|
750 |
+ tween.isColor = is.col(tween.from.original); |
|
751 |
+ if (tween.isColor) { tween.round = 1; } |
|
752 |
+ previousTween = tween; |
|
753 |
+ return tween; |
|
754 |
+ }); |
|
755 |
+} |
|
756 |
+ |
|
757 |
+// Tween progress |
|
758 |
+ |
|
759 |
+var setProgressValue = { |
|
760 |
+ css: function (t, p, v) { return t.style[p] = v; }, |
|
761 |
+ attribute: function (t, p, v) { return t.setAttribute(p, v); }, |
|
762 |
+ object: function (t, p, v) { return t[p] = v; }, |
|
763 |
+ transform: function (t, p, v, transforms, manual) { |
|
764 |
+ transforms.list.set(p, v); |
|
765 |
+ if (p === transforms.last || manual) { |
|
766 |
+ var str = ''; |
|
767 |
+ transforms.list.forEach(function (value, prop) { str += prop + "(" + value + ") "; }); |
|
768 |
+ t.style.transform = str; |
|
769 |
+ } |
|
770 |
+ } |
|
771 |
+}; |
|
772 |
+ |
|
773 |
+// Set Value helper |
|
774 |
+ |
|
775 |
+function setTargetsValue(targets, properties) { |
|
776 |
+ var animatables = getAnimatables(targets); |
|
777 |
+ animatables.forEach(function (animatable) { |
|
778 |
+ for (var property in properties) { |
|
779 |
+ var value = getFunctionValue(properties[property], animatable); |
|
780 |
+ var target = animatable.target; |
|
781 |
+ var valueUnit = getUnit(value); |
|
782 |
+ var originalValue = getOriginalTargetValue(target, property, valueUnit, animatable); |
|
783 |
+ var unit = valueUnit || getUnit(originalValue); |
|
784 |
+ var to = getRelativeValue(validateValue(value, unit), originalValue); |
|
785 |
+ var animType = getAnimationType(target, property); |
|
786 |
+ setProgressValue[animType](target, property, to, animatable.transforms, true); |
|
787 |
+ } |
|
788 |
+ }); |
|
789 |
+} |
|
790 |
+ |
|
791 |
+// Animations |
|
792 |
+ |
|
793 |
+function createAnimation(animatable, prop) { |
|
794 |
+ var animType = getAnimationType(animatable.target, prop.name); |
|
795 |
+ if (animType) { |
|
796 |
+ var tweens = normalizeTweens(prop, animatable); |
|
797 |
+ var lastTween = tweens[tweens.length - 1]; |
|
798 |
+ return { |
|
799 |
+ type: animType, |
|
800 |
+ property: prop.name, |
|
801 |
+ animatable: animatable, |
|
802 |
+ tweens: tweens, |
|
803 |
+ duration: lastTween.end, |
|
804 |
+ delay: tweens[0].delay, |
|
805 |
+ endDelay: lastTween.endDelay |
|
806 |
+ } |
|
807 |
+ } |
|
808 |
+} |
|
809 |
+ |
|
810 |
+function getAnimations(animatables, properties) { |
|
811 |
+ return filterArray(flattenArray(animatables.map(function (animatable) { |
|
812 |
+ return properties.map(function (prop) { |
|
813 |
+ return createAnimation(animatable, prop); |
|
814 |
+ }); |
|
815 |
+ })), function (a) { return !is.und(a); }); |
|
816 |
+} |
|
817 |
+ |
|
818 |
+// Create Instance |
|
819 |
+ |
|
820 |
+function getInstanceTimings(animations, tweenSettings) { |
|
821 |
+ var animLength = animations.length; |
|
822 |
+ var getTlOffset = function (anim) { return anim.timelineOffset ? anim.timelineOffset : 0; }; |
|
823 |
+ var timings = {}; |
|
824 |
+ timings.duration = animLength ? Math.max.apply(Math, animations.map(function (anim) { return getTlOffset(anim) + anim.duration; })) : tweenSettings.duration; |
|
825 |
+ timings.delay = animLength ? Math.min.apply(Math, animations.map(function (anim) { return getTlOffset(anim) + anim.delay; })) : tweenSettings.delay; |
|
826 |
+ timings.endDelay = animLength ? timings.duration - Math.max.apply(Math, animations.map(function (anim) { return getTlOffset(anim) + anim.duration - anim.endDelay; })) : tweenSettings.endDelay; |
|
827 |
+ return timings; |
|
828 |
+} |
|
829 |
+ |
|
830 |
+var instanceID = 0; |
|
831 |
+ |
|
832 |
+function createNewInstance(params) { |
|
833 |
+ var instanceSettings = replaceObjectProps(defaultInstanceSettings, params); |
|
834 |
+ var tweenSettings = replaceObjectProps(defaultTweenSettings, params); |
|
835 |
+ var properties = getProperties(tweenSettings, params); |
|
836 |
+ var animatables = getAnimatables(params.targets); |
|
837 |
+ var animations = getAnimations(animatables, properties); |
|
838 |
+ var timings = getInstanceTimings(animations, tweenSettings); |
|
839 |
+ var id = instanceID; |
|
840 |
+ instanceID++; |
|
841 |
+ return mergeObjects(instanceSettings, { |
|
842 |
+ id: id, |
|
843 |
+ children: [], |
|
844 |
+ animatables: animatables, |
|
845 |
+ animations: animations, |
|
846 |
+ duration: timings.duration, |
|
847 |
+ delay: timings.delay, |
|
848 |
+ endDelay: timings.endDelay |
|
849 |
+ }); |
|
850 |
+} |
|
851 |
+ |
|
852 |
+// Core |
|
853 |
+ |
|
854 |
+var activeInstances = []; |
|
855 |
+var pausedInstances = []; |
|
856 |
+var raf; |
|
857 |
+ |
|
858 |
+var engine = (function () { |
|
859 |
+ function play() { |
|
860 |
+ raf = requestAnimationFrame(step); |
|
861 |
+ } |
|
862 |
+ function step(t) { |
|
863 |
+ var activeInstancesLength = activeInstances.length; |
|
864 |
+ if (activeInstancesLength) { |
|
865 |
+ var i = 0; |
|
866 |
+ while (i < activeInstancesLength) { |
|
867 |
+ var activeInstance = activeInstances[i]; |
|
868 |
+ if (!activeInstance.paused) { |
|
869 |
+ activeInstance.tick(t); |
|
870 |
+ } else { |
|
871 |
+ var instanceIndex = activeInstances.indexOf(activeInstance); |
|
872 |
+ if (instanceIndex > -1) { |
|
873 |
+ activeInstances.splice(instanceIndex, 1); |
|
874 |
+ activeInstancesLength = activeInstances.length; |
|
875 |
+ } |
|
876 |
+ } |
|
877 |
+ i++; |
|
878 |
+ } |
|
879 |
+ play(); |
|
880 |
+ } else { |
|
881 |
+ raf = cancelAnimationFrame(raf); |
|
882 |
+ } |
|
883 |
+ } |
|
884 |
+ return play; |
|
885 |
+})(); |
|
886 |
+ |
|
887 |
+function handleVisibilityChange() { |
|
888 |
+ if (document.hidden) { |
|
889 |
+ activeInstances.forEach(function (ins) { return ins.pause(); }); |
|
890 |
+ pausedInstances = activeInstances.slice(0); |
|
891 |
+ anime.running = activeInstances = []; |
|
892 |
+ } else { |
|
893 |
+ pausedInstances.forEach(function (ins) { return ins.play(); }); |
|
894 |
+ } |
|
895 |
+} |
|
896 |
+ |
|
897 |
+if (typeof document !== 'undefined') { |
|
898 |
+ document.addEventListener('visibilitychange', handleVisibilityChange); |
|
899 |
+} |
|
900 |
+ |
|
901 |
+// Public Instance |
|
902 |
+ |
|
903 |
+function anime(params) { |
|
904 |
+ if ( params === void 0 ) params = {}; |
|
905 |
+ |
|
906 |
+ |
|
907 |
+ var startTime = 0, lastTime = 0, now = 0; |
|
908 |
+ var children, childrenLength = 0; |
|
909 |
+ var resolve = null; |
|
910 |
+ |
|
911 |
+ function makePromise(instance) { |
|
912 |
+ var promise = window.Promise && new Promise(function (_resolve) { return resolve = _resolve; }); |
|
913 |
+ instance.finished = promise; |
|
914 |
+ return promise; |
|
915 |
+ } |
|
916 |
+ |
|
917 |
+ var instance = createNewInstance(params); |
|
918 |
+ var promise = makePromise(instance); |
|
919 |
+ |
|
920 |
+ function toggleInstanceDirection() { |
|
921 |
+ var direction = instance.direction; |
|
922 |
+ if (direction !== 'alternate') { |
|
923 |
+ instance.direction = direction !== 'normal' ? 'normal' : 'reverse'; |
|
924 |
+ } |
|
925 |
+ instance.reversed = !instance.reversed; |
|
926 |
+ children.forEach(function (child) { return child.reversed = instance.reversed; }); |
|
927 |
+ } |
|
928 |
+ |
|
929 |
+ function adjustTime(time) { |
|
930 |
+ return instance.reversed ? instance.duration - time : time; |
|
931 |
+ } |
|
932 |
+ |
|
933 |
+ function resetTime() { |
|
934 |
+ startTime = 0; |
|
935 |
+ lastTime = adjustTime(instance.currentTime) * (1 / anime.speed); |
|
936 |
+ } |
|
937 |
+ |
|
938 |
+ function seekChild(time, child) { |
|
939 |
+ if (child) { child.seek(time - child.timelineOffset); } |
|
940 |
+ } |
|
941 |
+ |
|
942 |
+ function syncInstanceChildren(time) { |
|
943 |
+ if (!instance.reversePlayback) { |
|
944 |
+ for (var i = 0; i < childrenLength; i++) { seekChild(time, children[i]); } |
|
945 |
+ } else { |
|
946 |
+ for (var i$1 = childrenLength; i$1--;) { seekChild(time, children[i$1]); } |
|
947 |
+ } |
|
948 |
+ } |
|
949 |
+ |
|
950 |
+ function setAnimationsProgress(insTime) { |
|
951 |
+ var i = 0; |
|
952 |
+ var animations = instance.animations; |
|
953 |
+ var animationsLength = animations.length; |
|
954 |
+ while (i < animationsLength) { |
|
955 |
+ var anim = animations[i]; |
|
956 |
+ var animatable = anim.animatable; |
|
957 |
+ var tweens = anim.tweens; |
|
958 |
+ var tweenLength = tweens.length - 1; |
|
959 |
+ var tween = tweens[tweenLength]; |
|
960 |
+ // Only check for keyframes if there is more than one tween |
|
961 |
+ if (tweenLength) { tween = filterArray(tweens, function (t) { return (insTime < t.end); })[0] || tween; } |
|
962 |
+ var elapsed = minMax(insTime - tween.start - tween.delay, 0, tween.duration) / tween.duration; |
|
963 |
+ var eased = isNaN(elapsed) ? 1 : tween.easing(elapsed); |
|
964 |
+ var strings = tween.to.strings; |
|
965 |
+ var round = tween.round; |
|
966 |
+ var numbers = []; |
|
967 |
+ var toNumbersLength = tween.to.numbers.length; |
|
968 |
+ var progress = (void 0); |
|
969 |
+ for (var n = 0; n < toNumbersLength; n++) { |
|
970 |
+ var value = (void 0); |
|
971 |
+ var toNumber = tween.to.numbers[n]; |
|
972 |
+ var fromNumber = tween.from.numbers[n] || 0; |
|
973 |
+ if (!tween.isPath) { |
|
974 |
+ value = fromNumber + (eased * (toNumber - fromNumber)); |
|
975 |
+ } else { |
|
976 |
+ value = getPathProgress(tween.value, eased * toNumber); |
|
977 |
+ } |
|
978 |
+ if (round) { |
|
979 |
+ if (!(tween.isColor && n > 2)) { |
|
980 |
+ value = Math.round(value * round) / round; |
|
981 |
+ } |
|
982 |
+ } |
|
983 |
+ numbers.push(value); |
|
984 |
+ } |
|
985 |
+ // Manual Array.reduce for better performances |
|
986 |
+ var stringsLength = strings.length; |
|
987 |
+ if (!stringsLength) { |
|
988 |
+ progress = numbers[0]; |
|
989 |
+ } else { |
|
990 |
+ progress = strings[0]; |
|
991 |
+ for (var s = 0; s < stringsLength; s++) { |
|
992 |
+ var a = strings[s]; |
|
993 |
+ var b = strings[s + 1]; |
|
994 |
+ var n$1 = numbers[s]; |
|
995 |
+ if (!isNaN(n$1)) { |
|
996 |
+ if (!b) { |
|
997 |
+ progress += n$1 + ' '; |
|
998 |
+ } else { |
|
999 |
+ progress += n$1 + b; |
|
1000 |
+ } |
|
1001 |
+ } |
|
1002 |
+ } |
|
1003 |
+ } |
|
1004 |
+ setProgressValue[anim.type](animatable.target, anim.property, progress, animatable.transforms); |
|
1005 |
+ anim.currentValue = progress; |
|
1006 |
+ i++; |
|
1007 |
+ } |
|
1008 |
+ } |
|
1009 |
+ |
|
1010 |
+ function setCallback(cb) { |
|
1011 |
+ if (instance[cb] && !instance.passThrough) { instance[cb](instance); } |
|
1012 |
+ } |
|
1013 |
+ |
|
1014 |
+ function countIteration() { |
|
1015 |
+ if (instance.remaining && instance.remaining !== true) { |
|
1016 |
+ instance.remaining--; |
|
1017 |
+ } |
|
1018 |
+ } |
|
1019 |
+ |
|
1020 |
+ function setInstanceProgress(engineTime) { |
|
1021 |
+ var insDuration = instance.duration; |
|
1022 |
+ var insDelay = instance.delay; |
|
1023 |
+ var insEndDelay = insDuration - instance.endDelay; |
|
1024 |
+ var insTime = adjustTime(engineTime); |
|
1025 |
+ instance.progress = minMax((insTime / insDuration) * 100, 0, 100); |
|
1026 |
+ instance.reversePlayback = insTime < instance.currentTime; |
|
1027 |
+ if (children) { syncInstanceChildren(insTime); } |
|
1028 |
+ if (!instance.began && instance.currentTime > 0) { |
|
1029 |
+ instance.began = true; |
|
1030 |
+ setCallback('begin'); |
|
1031 |
+ } |
|
1032 |
+ if (!instance.loopBegan && instance.currentTime > 0) { |
|
1033 |
+ instance.loopBegan = true; |
|
1034 |
+ setCallback('loopBegin'); |
|
1035 |
+ } |
|
1036 |
+ if (insTime <= insDelay && instance.currentTime !== 0) { |
|
1037 |
+ setAnimationsProgress(0); |
|
1038 |
+ } |
|
1039 |
+ if ((insTime >= insEndDelay && instance.currentTime !== insDuration) || !insDuration) { |
|
1040 |
+ setAnimationsProgress(insDuration); |
|
1041 |
+ } |
|
1042 |
+ if (insTime > insDelay && insTime < insEndDelay) { |
|
1043 |
+ if (!instance.changeBegan) { |
|
1044 |
+ instance.changeBegan = true; |
|
1045 |
+ instance.changeCompleted = false; |
|
1046 |
+ setCallback('changeBegin'); |
|
1047 |
+ } |
|
1048 |
+ setCallback('change'); |
|
1049 |
+ setAnimationsProgress(insTime); |
|
1050 |
+ } else { |
|
1051 |
+ if (instance.changeBegan) { |
|
1052 |
+ instance.changeCompleted = true; |
|
1053 |
+ instance.changeBegan = false; |
|
1054 |
+ setCallback('changeComplete'); |
|
1055 |
+ } |
|
1056 |
+ } |
|
1057 |
+ instance.currentTime = minMax(insTime, 0, insDuration); |
|
1058 |
+ if (instance.began) { setCallback('update'); } |
|
1059 |
+ if (engineTime >= insDuration) { |
|
1060 |
+ lastTime = 0; |
|
1061 |
+ countIteration(); |
|
1062 |
+ if (!instance.remaining) { |
|
1063 |
+ instance.paused = true; |
|
1064 |
+ if (!instance.completed) { |
|
1065 |
+ instance.completed = true; |
|
1066 |
+ setCallback('loopComplete'); |
|
1067 |
+ setCallback('complete'); |
|
1068 |
+ if (!instance.passThrough && 'Promise' in window) { |
|
1069 |
+ resolve(); |
|
1070 |
+ promise = makePromise(instance); |
|
1071 |
+ } |
|
1072 |
+ } |
|
1073 |
+ } else { |
|
1074 |
+ startTime = now; |
|
1075 |
+ setCallback('loopComplete'); |
|
1076 |
+ instance.loopBegan = false; |
|
1077 |
+ if (instance.direction === 'alternate') { |
|
1078 |
+ toggleInstanceDirection(); |
|
1079 |
+ } |
|
1080 |
+ } |
|
1081 |
+ } |
|
1082 |
+ } |
|
1083 |
+ |
|
1084 |
+ instance.reset = function() { |
|
1085 |
+ var direction = instance.direction; |
|
1086 |
+ instance.passThrough = false; |
|
1087 |
+ instance.currentTime = 0; |
|
1088 |
+ instance.progress = 0; |
|
1089 |
+ instance.paused = true; |
|
1090 |
+ instance.began = false; |
|
1091 |
+ instance.loopBegan = false; |
|
1092 |
+ instance.changeBegan = false; |
|
1093 |
+ instance.completed = false; |
|
1094 |
+ instance.changeCompleted = false; |
|
1095 |
+ instance.reversePlayback = false; |
|
1096 |
+ instance.reversed = direction === 'reverse'; |
|
1097 |
+ instance.remaining = instance.loop; |
|
1098 |
+ children = instance.children; |
|
1099 |
+ childrenLength = children.length; |
|
1100 |
+ for (var i = childrenLength; i--;) { instance.children[i].reset(); } |
|
1101 |
+ if (instance.reversed && instance.loop !== true || (direction === 'alternate' && instance.loop === 1)) { instance.remaining++; } |
|
1102 |
+ setAnimationsProgress(instance.reversed ? instance.duration : 0); |
|
1103 |
+ }; |
|
1104 |
+ |
|
1105 |
+ // Set Value helper |
|
1106 |
+ |
|
1107 |
+ instance.set = function(targets, properties) { |
|
1108 |
+ setTargetsValue(targets, properties); |
|
1109 |
+ return instance; |
|
1110 |
+ }; |
|
1111 |
+ |
|
1112 |
+ instance.tick = function(t) { |
|
1113 |
+ now = t; |
|
1114 |
+ if (!startTime) { startTime = now; } |
|
1115 |
+ setInstanceProgress((now + (lastTime - startTime)) * anime.speed); |
|
1116 |
+ }; |
|
1117 |
+ |
|
1118 |
+ instance.seek = function(time) { |
|
1119 |
+ setInstanceProgress(adjustTime(time)); |
|
1120 |
+ }; |
|
1121 |
+ |
|
1122 |
+ instance.pause = function() { |
|
1123 |
+ instance.paused = true; |
|
1124 |
+ resetTime(); |
|
1125 |
+ }; |
|
1126 |
+ |
|
1127 |
+ instance.play = function() { |
|
1128 |
+ if (!instance.paused) { return; } |
|
1129 |
+ if (instance.completed) { instance.reset(); } |
|
1130 |
+ instance.paused = false; |
|
1131 |
+ activeInstances.push(instance); |
|
1132 |
+ resetTime(); |
|
1133 |
+ if (!raf) { engine(); } |
|
1134 |
+ }; |
|
1135 |
+ |
|
1136 |
+ instance.reverse = function() { |
|
1137 |
+ toggleInstanceDirection(); |
|
1138 |
+ instance.completed = instance.reversed ? false : true; |
|
1139 |
+ resetTime(); |
|
1140 |
+ }; |
|
1141 |
+ |
|
1142 |
+ instance.restart = function() { |
|
1143 |
+ instance.reset(); |
|
1144 |
+ instance.play(); |
|
1145 |
+ }; |
|
1146 |
+ |
|
1147 |
+ instance.reset(); |
|
1148 |
+ |
|
1149 |
+ if (instance.autoplay) { instance.play(); } |
|
1150 |
+ |
|
1151 |
+ return instance; |
|
1152 |
+ |
|
1153 |
+} |
|
1154 |
+ |
|
1155 |
+// Remove targets from animation |
|
1156 |
+ |
|
1157 |
+function removeTargetsFromAnimations(targetsArray, animations) { |
|
1158 |
+ for (var a = animations.length; a--;) { |
|
1159 |
+ if (arrayContains(targetsArray, animations[a].animatable.target)) { |
|
1160 |
+ animations.splice(a, 1); |
|
1161 |
+ } |
|
1162 |
+ } |
|
1163 |
+} |
|
1164 |
+ |
|
1165 |
+function removeTargets(targets) { |
|
1166 |
+ var targetsArray = parseTargets(targets); |
|
1167 |
+ for (var i = activeInstances.length; i--;) { |
|
1168 |
+ var instance = activeInstances[i]; |
|
1169 |
+ var animations = instance.animations; |
|
1170 |
+ var children = instance.children; |
|
1171 |
+ removeTargetsFromAnimations(targetsArray, animations); |
|
1172 |
+ for (var c = children.length; c--;) { |
|
1173 |
+ var child = children[c]; |
|
1174 |
+ var childAnimations = child.animations; |
|
1175 |
+ removeTargetsFromAnimations(targetsArray, childAnimations); |
|
1176 |
+ if (!childAnimations.length && !child.children.length) { children.splice(c, 1); } |
|
1177 |
+ } |
|
1178 |
+ if (!animations.length && !children.length) { instance.pause(); } |
|
1179 |
+ } |
|
1180 |
+} |
|
1181 |
+ |
|
1182 |
+// Stagger helpers |
|
1183 |
+ |
|
1184 |
+function stagger(val, params) { |
|
1185 |
+ if ( params === void 0 ) params = {}; |
|
1186 |
+ |
|
1187 |
+ var direction = params.direction || 'normal'; |
|
1188 |
+ var easing = params.easing ? parseEasings(params.easing) : null; |
|
1189 |
+ var grid = params.grid; |
|
1190 |
+ var axis = params.axis; |
|
1191 |
+ var fromIndex = params.from || 0; |
|
1192 |
+ var fromFirst = fromIndex === 'first'; |
|
1193 |
+ var fromCenter = fromIndex === 'center'; |
|
1194 |
+ var fromLast = fromIndex === 'last'; |
|
1195 |
+ var isRange = is.arr(val); |
|
1196 |
+ var val1 = isRange ? parseFloat(val[0]) : parseFloat(val); |
|
1197 |
+ var val2 = isRange ? parseFloat(val[1]) : 0; |
|
1198 |
+ var unit = getUnit(isRange ? val[1] : val) || 0; |
|
1199 |
+ var start = params.start || 0 + (isRange ? val1 : 0); |
|
1200 |
+ var values = []; |
|
1201 |
+ var maxValue = 0; |
|
1202 |
+ return function (el, i, t) { |
|
1203 |
+ if (fromFirst) { fromIndex = 0; } |
|
1204 |
+ if (fromCenter) { fromIndex = (t - 1) / 2; } |
|
1205 |
+ if (fromLast) { fromIndex = t - 1; } |
|
1206 |
+ if (!values.length) { |
|
1207 |
+ for (var index = 0; index < t; index++) { |
|
1208 |
+ if (!grid) { |
|
1209 |
+ values.push(Math.abs(fromIndex - index)); |
|
1210 |
+ } else { |
|
1211 |
+ var fromX = !fromCenter ? fromIndex%grid[0] : (grid[0]-1)/2; |
|
1212 |
+ var fromY = !fromCenter ? Math.floor(fromIndex/grid[0]) : (grid[1]-1)/2; |
|
1213 |
+ var toX = index%grid[0]; |
|
1214 |
+ var toY = Math.floor(index/grid[0]); |
|
1215 |
+ var distanceX = fromX - toX; |
|
1216 |
+ var distanceY = fromY - toY; |
|
1217 |
+ var value = Math.sqrt(distanceX * distanceX + distanceY * distanceY); |
|
1218 |
+ if (axis === 'x') { value = -distanceX; } |
|
1219 |
+ if (axis === 'y') { value = -distanceY; } |
|
1220 |
+ values.push(value); |
|
1221 |
+ } |
|
1222 |
+ maxValue = Math.max.apply(Math, values); |
|
1223 |
+ } |
|
1224 |
+ if (easing) { values = values.map(function (val) { return easing(val / maxValue) * maxValue; }); } |
|
1225 |
+ if (direction === 'reverse') { values = values.map(function (val) { return axis ? (val < 0) ? val * -1 : -val : Math.abs(maxValue - val); }); } |
|
1226 |
+ } |
|
1227 |
+ var spacing = isRange ? (val2 - val1) / maxValue : val1; |
|
1228 |
+ return start + (spacing * (Math.round(values[i] * 100) / 100)) + unit; |
|
1229 |
+ } |
|
1230 |
+} |
|
1231 |
+ |
|
1232 |
+// Timeline |
|
1233 |
+ |
|
1234 |
+function timeline(params) { |
|
1235 |
+ if ( params === void 0 ) params = {}; |
|
1236 |
+ |
|
1237 |
+ var tl = anime(params); |
|
1238 |
+ tl.duration = 0; |
|
1239 |
+ tl.add = function(instanceParams, timelineOffset) { |
|
1240 |
+ var tlIndex = activeInstances.indexOf(tl); |
|
1241 |
+ var children = tl.children; |
|
1242 |
+ if (tlIndex > -1) { activeInstances.splice(tlIndex, 1); } |
|
1243 |
+ function passThrough(ins) { ins.passThrough = true; } |
|
1244 |
+ for (var i = 0; i < children.length; i++) { passThrough(children[i]); } |
|
1245 |
+ var insParams = mergeObjects(instanceParams, replaceObjectProps(defaultTweenSettings, params)); |
|
1246 |
+ insParams.targets = insParams.targets || params.targets; |
|
1247 |
+ var tlDuration = tl.duration; |
|
1248 |
+ insParams.autoplay = false; |
|
1249 |
+ insParams.direction = tl.direction; |
|
1250 |
+ insParams.timelineOffset = is.und(timelineOffset) ? tlDuration : getRelativeValue(timelineOffset, tlDuration); |
|
1251 |
+ passThrough(tl); |
|
1252 |
+ tl.seek(insParams.timelineOffset); |
|
1253 |
+ var ins = anime(insParams); |
|
1254 |
+ passThrough(ins); |
|
1255 |
+ children.push(ins); |
|
1256 |
+ var timings = getInstanceTimings(children, params); |
|
1257 |
+ tl.delay = timings.delay; |
|
1258 |
+ tl.endDelay = timings.endDelay; |
|
1259 |
+ tl.duration = timings.duration; |
|
1260 |
+ tl.seek(0); |
|
1261 |
+ tl.reset(); |
|
1262 |
+ if (tl.autoplay) { tl.play(); } |
|
1263 |
+ return tl; |
|
1264 |
+ }; |
|
1265 |
+ return tl; |
|
1266 |
+} |
|
1267 |
+ |
|
1268 |
+anime.version = '3.2.0'; |
|
1269 |
+anime.speed = 1; |
|
1270 |
+anime.running = activeInstances; |
|
1271 |
+anime.remove = removeTargets; |
|
1272 |
+anime.get = getOriginalTargetValue; |
|
1273 |
+anime.set = setTargetsValue; |
|
1274 |
+anime.convertPx = convertPxToUnit; |
|
1275 |
+anime.path = getPath; |
|
1276 |
+anime.setDashoffset = setDashoffset; |
|
1277 |
+anime.stagger = stagger; |
|
1278 |
+anime.timeline = timeline; |
|
1279 |
+anime.easing = parseEasings; |
|
1280 |
+anime.penner = penner; |
|
1281 |
+anime.random = function (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }; |
|
1282 |
+ |
|
1283 |
+module.exports = anime; |