Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Change umd package name to (animejs -> anime) / Update build scripts

Julian Garnier61b07c902200
diff --git a/build.js b/build.js
index 2c06efe5..ed2bd322 100644
--- a/build.js
+++ b/build.js
@@ -6,9 +6,14 @@ const sizer = require('gzip-size');
const pkg = require('./package');
const umd = pkg['umd:main'];
+const date = new Date();
+const version = pkg.version;
+
const banner = `/*
- 2017 Julian Garnier
+ anime.js v${ version }
+ © ${ date.getFullYear() } Julian Garnier
Released under the MIT license
+ animejs.com
*/`;
console.info('Compiling... 😤');
@@ -48,7 +53,7 @@ rollup({
banner,
file: umd,
format: 'umd',
- name: pkg.name
+ name: pkg['umd:name']
}).then(_ => {
const data = fs.readFileSync(umd, 'utf8');
diff --git a/lib/anime.es.js b/lib/anime.es.js
index 5be5dbe1..e13787f3 100644
--- a/lib/anime.es.js
+++ b/lib/anime.es.js
@@ -1,37 +1,50 @@
/*
- 2017 Julian Garnier
+ anime.js v3.0.0
+ © 2018 Julian Garnier
Released under the MIT license
+ animejs.com
*/
// Defaults
var defaultInstanceSettings = {
- update: undefined,
- begin: undefined,
- run: undefined,
- complete: undefined,
+ update: null,
+ begin: null,
+ run: null,
+ complete: null,
loop: 1,
direction: 'normal',
autoplay: true,
- timeOffset: 0
+ startTime: 0,
+ timelineOffset: 0
};
var defaultTweenSettings = {
duration: 1000,
delay: 0,
endDelay: 0,
- easing: 'easeOutElastic',
- elasticity: 500,
+ easing: 'easeOutElastic(1, .5)',
round: 0
};
var validTransforms = ['translateX', 'translateY', 'translateZ', 'rotate', 'rotateX', 'rotateY', 'rotateZ', 'scale', 'scaleX', 'scaleY', 'scaleZ', 'skew', 'skewX', 'skewY', 'perspective'];
+// Caching
+
+var cache = {
+ CSS: {},
+ springs: {}
+};
+
// Utils
function stringContains(str, text) {
return str.indexOf(text) > -1;
}
+function applyArguments(func, args) {
+ return func.apply(null, args);
+}
+
var is = {
arr: function (a) { return Array.isArray(a); },
obj: function (a) { return stringContains(Object.prototype.toString.call(a), 'Object'); },
@@ -47,6 +60,83 @@ var is = {
col: function (a) { return (is.hex(a) || is.rgb(a) || is.hsl(a)); }
};
+function parseEasingParameters(string) {
+ var match = /\(([^)]+)\)/.exec(string);
+ return match ? match[1].split(',').map(function (p) { return parseFloat(p); }) : [];
+}
+
+// Spring solver inspired by Webkit Copyright © 2016 Apple Inc. All rights reserved. https://webkit.org/demos/spring/spring.js
+
+function spring(string, duration) {
+
+ var params = parseEasingParameters(string);
+ var mass = minMaxValue(is.und(params[0]) ? 1 : params[0], .1, 100);
+ var stiffness = minMaxValue(is.und(params[1]) ? 100 : params[1], .1, 100);
+ var damping = minMaxValue(is.und(params[2]) ? 10 : params[2], .1, 100);
+ var velocity = minMaxValue(is.und(params[3]) ? 0 : params[3], .1, 100);
+ var w0 = Math.sqrt(stiffness / mass);
+ var zeta = damping / (2 * Math.sqrt(stiffness * mass));
+ var wd = zeta < 1 ? w0 * Math.sqrt(1 - zeta * zeta) : 0;
+ var a = 1;
+ var b = zeta < 1 ? (zeta * w0 + -velocity) / wd : -velocity + w0;
+
+ function solver(t) {
+ var progress = duration ? (duration * t) / 1000 : t;
+ if (zeta < 1) {
+ progress = Math.exp(-progress * zeta * w0) * (a * Math.cos(wd * progress) + b * Math.sin(wd * progress));
+ } else {
+ progress = (a + b * progress) * Math.exp(-progress * w0);
+ }
+ if (t === 0 || t === 1) { return t; }
+ return 1 - progress;
+ }
+
+ function getDuration() {
+ var cached = cache.springs[string];
+ if (cached) { return cached; }
+ var frame = 1/6;
+ var elapsed = 0;
+ var rest = 0;
+ while(true) {
+ elapsed += frame;
+ if (solver(elapsed) === 1) {
+ rest++;
+ if (rest >= 16) { break; }
+ } else {
+ rest = 0;
+ }
+ }
+ var duration = elapsed * frame * 1000;
+ cache.springs[string] = duration;
+ return duration;
+ }
+
+ return duration ? solver : getDuration;
+
+}
+
+// Elastic easing adapted from jQueryUI http://api.jqueryui.com/easings/
+
+function elastic(amplitude, period) {
+ if ( amplitude === void 0 ) amplitude = 1;
+ if ( period === void 0 ) period = .5;
+
+ var a = minMaxValue(amplitude, 1, 10);
+ var p = minMaxValue(period, .1, 2);
+ return function (t) {
+ return (t === 0 || t === 1) ? t :
+ -a * Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (p / (Math.PI * 2) * Math.asin(1 / a))) * (Math.PI * 2)) / p);
+ }
+}
+
+// Basic steps easing implementation https://developer.mozilla.org/fr/docs/Web/CSS/transition-timing-function
+
+function steps(steps) {
+ if ( steps === void 0 ) steps = 10;
+
+ return function (t) { return Math.round(t * steps) * (1 / steps); };
+}
+
// BezierEasing https://github.com/gre/bezier-easing
var bezier = (function () {
@@ -54,14 +144,14 @@ var bezier = (function () {
var kSplineTableSize = 11;
var kSampleStepSize = 1.0 / (kSplineTableSize - 1.0);
- function A (aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1 }
- function B (aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1 }
- function C (aA1) { return 3.0 * aA1 }
+ function A(aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1 }
+ function B(aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1 }
+ function C(aA1) { return 3.0 * aA1 }
- function calcBezier (aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT }
- function getSlope (aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1) }
+ function calcBezier(aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT }
+ function getSlope(aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1) }
- function binarySubdivide (aX, aA, aB, mX1, mX2) {
+ function binarySubdivide(aX, aA, aB, mX1, mX2) {
var currentX, currentT, i = 0;
do {
currentT = aA + (aB - aA) / 2.0;
@@ -71,7 +161,7 @@ var bezier = (function () {
return currentT;
}
- function newtonRaphsonIterate (aX, aGuessT, mX1, mX2) {
+ function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
for (var i = 0; i < 4; ++i) {
var currentSlope = getSlope(aGuessT, mX1, mX2);
if (currentSlope === 0.0) { return aGuessT; }
@@ -120,8 +210,7 @@ var bezier = (function () {
return function (x) {
if (mX1 === mY1 && mX2 === mY2) { return x; }
- if (x === 0) { return 0; }
- if (x === 1) { return 1; }
+ if (x === 0 || x === 1) { return x; }
return calcBezier(getTForX(x), mY1, mY2);
}
@@ -135,71 +224,75 @@ var easings = (function () {
var names = ['Quad', 'Cubic', 'Quart', 'Quint', 'Sine', 'Expo', 'Circ', 'Back', 'Elastic'];
- // Elastic easing adapted from jQueryUI http://api.jqueryui.com/easings/
-
- function elastic(t, p) {
- return t === 0 || t === 1 ? t :
- -Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (p / (Math.PI * 2.0) * Math.asin(1))) * (Math.PI * 2)) / p );
- }
-
// Approximated Penner equations http://matthewlein.com/ceaser/
- var equations = {
+ var penner = {
In: [
- [0.550, 0.085, 0.680, 0.530], /* InQuad */
- [0.550, 0.055, 0.675, 0.190], /* InCubic */
- [0.895, 0.030, 0.685, 0.220], /* InQuart */
- [0.755, 0.050, 0.855, 0.060], /* InQuint */
- [0.470, 0.000, 0.745, 0.715], /* InSine */
- [0.950, 0.050, 0.795, 0.035], /* InExpo */
- [0.600, 0.040, 0.980, 0.335], /* InCirc */
- [0.600, -0.280, 0.735, 0.045], /* InBack */
- elastic /* InElastic */
- ], Out: [
- [0.250, 0.460, 0.450, 0.940], /* OutQuad */
- [0.215, 0.610, 0.355, 1.000], /* OutCubic */
- [0.165, 0.840, 0.440, 1.000], /* OutQuart */
- [0.230, 1.000, 0.320, 1.000], /* OutQuint */
- [0.390, 0.575, 0.565, 1.000], /* OutSine */
- [0.190, 1.000, 0.220, 1.000], /* OutExpo */
- [0.075, 0.820, 0.165, 1.000], /* OutCirc */
- [0.175, 0.885, 0.320, 1.275], /* OutBack */
- function (t, f) { return 1 - elastic(1 - t, f); } /* OutElastic */
- ], InOut: [
- [0.455, 0.030, 0.515, 0.955], /* InOutQuad */
- [0.645, 0.045, 0.355, 1.000], /* InOutCubic */
- [0.770, 0.000, 0.175, 1.000], /* InOutQuart */
- [0.860, 0.000, 0.070, 1.000], /* InOutQuint */
- [0.445, 0.050, 0.550, 0.950], /* InOutSine */
- [1.000, 0.000, 0.000, 1.000], /* InOutExpo */
- [0.785, 0.135, 0.150, 0.860], /* InOutCirc */
- [0.680, -0.550, 0.265, 1.550], /* InOutBack */
- function (t, f) { return t < .5 ? elastic(t * 2, f) / 2 : 1 - elastic(t * -2 + 2, f) / 2; } /* InOutElastic */
+ [0.550, 0.085, 0.680, 0.530], /* inQuad */
+ [0.550, 0.055, 0.675, 0.190], /* inCubic */
+ [0.895, 0.030, 0.685, 0.220], /* inQuart */
+ [0.755, 0.050, 0.855, 0.060], /* inQuint */
+ [0.470, 0.000, 0.745, 0.715], /* inSine */
+ [0.950, 0.050, 0.795, 0.035], /* inExpo */
+ [0.600, 0.040, 0.980, 0.335], /* inCirc */
+ [0.600,-0.280, 0.735, 0.045], /* inBack */
+ elastic /* inElastic */
+ ],
+ Out: [
+ [0.250, 0.460, 0.450, 0.940], /* outQuad */
+ [0.215, 0.610, 0.355, 1.000], /* outCubic */
+ [0.165, 0.840, 0.440, 1.000], /* outQuart */
+ [0.230, 1.000, 0.320, 1.000], /* outQuint */
+ [0.390, 0.575, 0.565, 1.000], /* outSine */
+ [0.190, 1.000, 0.220, 1.000], /* outExpo */
+ [0.075, 0.820, 0.165, 1.000], /* outCirc */
+ [0.175, 0.885, 0.320, 1.275], /* outBack */
+ function (a, p) { return function (t) { return 1 - elastic(a, p)(1 - t); }; } /* outElastic */
+ ],
+ InOut: [
+ [0.455, 0.030, 0.515, 0.955], /* inOutQuad */
+ [0.645, 0.045, 0.355, 1.000], /* inOutCubic */
+ [0.770, 0.000, 0.175, 1.000], /* inOutQuart */
+ [0.860, 0.000, 0.070, 1.000], /* inOutQuint */
+ [0.445, 0.050, 0.550, 0.950], /* inOutSine */
+ [1.000, 0.000, 0.000, 1.000], /* inOutExpo */
+ [0.785, 0.135, 0.150, 0.860], /* inOutCirc */
+ [0.680,-0.550, 0.265, 1.550], /* inOutBack */
+ function (a, p) { return function (t) { return t < .5 ? elastic(a, p)(t * 2) / 2 : 1 - elastic(a, p)(t * -2 + 2) / 2; }; } /* inOutElastic */
]
};
- var functions = {
- linear: bezier(0.250, 0.250, 0.750, 0.750)
+ var eases = {
+ linear: [0.250, 0.250, 0.750, 0.750]
};
- var loop = function ( type ) {
- equations[type].forEach(function (f, i) {
- functions['ease' + type + names[i]] = is.fnc(f) ? f : bezier.apply(null, f);
+ var loop = function ( equation ) {
+ penner[equation].forEach(function (ease, i) {
+ eases['ease'+equation+names[i]] = ease;
});
};
- for (var type in equations) loop( type );
+ for (var equation in penner) loop( equation );
- return functions;
+ return eases;
})();
-// Strings
-
-function stringToHyphens(str) {
- return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
+function parseEasings(tween) {
+ var string = tween.easing;
+ var name = string.split('(')[0];
+ var args = parseEasingParameters(string);
+ var ease = easings[name];
+ switch (name) {
+ case 'spring' : return spring(string, tween.duration);
+ case 'cubicBezier' : return applyArguments(bezier, args);
+ case 'steps' : return applyArguments(steps, args);
+ default : return is.fnc(ease) ? applyArguments(ease, args) : applyArguments(bezier, ease);
+ }
}
+// Strings
+
function selectString(str) {
try {
var nodes = document.querySelectorAll(str);
@@ -334,15 +427,39 @@ function getFunctionValue(val, animatable) {
return val(animatable.target, animatable.id, animatable.total);
}
-function getCSSValue(el, prop) {
+function getAttribute(el, prop) {
+ return el.getAttribute(prop);
+}
+
+function convertPxToUnit(el, value, unit) {
+ var valueUnit = getUnit(value);
+ if (arrayContains([unit, 'deg', 'rad', 'turn'], valueUnit)) { return value; }
+ var cached = cache.CSS[value + unit];
+ if (!is.und(cached)) { return cached; }
+ var baseline = 100;
+ var tempEl = document.createElement(el.tagName);
+ var parentEl = el.parentNode || document.body;
+ parentEl.appendChild(tempEl);
+ tempEl.style.position = 'absolute';
+ tempEl.style.width = baseline + unit;
+ var factor = baseline / tempEl.offsetWidth;
+ parentEl.removeChild(tempEl);
+ var convertedUnit = factor * parseFloat(value);
+ cache.CSS[value + unit] = convertedUnit;
+ return convertedUnit;
+}
+
+function getCSSValue(el, prop, unit) {
if (prop in el.style) {
- return el.style[prop] || getComputedStyle(el).getPropertyValue(stringToHyphens(prop)) || '0';
+ var uppercasePropName = prop.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
+ var value = el.style[prop] || getComputedStyle(el).getPropertyValue(uppercasePropName) || '0';
+ return unit ? convertPxToUnit(el, value, unit) : value;
}
}
function getAnimationType(el, prop) {
+ if (is.dom(el) && (getAttribute(el, prop) || (is.svg(el) && el[prop]))) { return 'attribute'; }
if (is.dom(el) && arrayContains(validTransforms, prop)) { return 'transform'; }
- if (is.dom(el) && (el.getAttribute(prop) || (is.svg(el) && el[prop]))) { return 'attribute'; }
if (is.dom(el) && (prop !== 'transform' && getCSSValue(el, prop))) { return 'css'; }
if (el[prop] != null) { return 'object'; }
}
@@ -356,19 +473,19 @@ function getElementTransforms(el) {
return transforms;
}
-function getTransformValue(el, propName, animatable) {
+function getTransformValue(el, propName, animatable, unit) {
var defaultVal = stringContains(propName, 'scale') ? 1 : 0 + getTransformUnit(propName);
- var prop = getElementTransforms(el).get(propName) || defaultVal;
- animatable.transforms.list.set(propName, prop);
+ var value = getElementTransforms(el).get(propName) || defaultVal;
+ animatable.transforms.list.set(propName, value);
animatable.transforms['last'] = propName;
- return prop;
+ return unit ? convertPxToUnit(el, value, unit) : value;
}
-function getOriginalTargetValue(target, propName, animatable) {
+function getOriginalTargetValue(target, propName, unit, animatable) {
switch (getAnimationType(target, propName)) {
- case 'transform': return getTransformValue(target, propName, animatable);
- case 'css': return getCSSValue(target, propName);
- case 'attribute': return target.getAttribute(propName);
+ case 'transform': return getTransformValue(target, propName, animatable, unit);
+ case 'css': return getCSSValue(target, propName, unit);
+ case 'attribute': return getAttribute(target, propName);
default: return target[propName] || 0;
}
}
@@ -393,7 +510,7 @@ function validateValue(val, unit) {
return unit && !/\s/g.test(val) ? unitLess + unit : unitLess;
}
-// getTotalLength() equivalent for circle, rect, polyline, polygon and line shapes.
+// getTotalLength() equivalent for circle, rect, polyline, polygon and line shapes
// adapted from https://gist.github.com/SebLambla/3e0550c496c236709744
function getDistance(p1, p2) {
@@ -401,17 +518,17 @@ function getDistance(p1, p2) {
}
function getCircleLength(el) {
- return 2 * Math.PI * el.getAttribute('r');
+ return Math.PI * 2 * getAttribute(el, 'r');
}
function getRectLength(el) {
- return (el.getAttribute('width') * 2) + (el.getAttribute('height') * 2);
+ return (getAttribute(el, 'width') * 2) + (getAttribute(el, 'height') * 2);
}
function getLineLength(el) {
return getDistance(
- {x: el.getAttribute('x1'), y: el.getAttribute('y1')},
- {x: el.getAttribute('x2'), y: el.getAttribute('y2')}
+ {x: getAttribute(el, 'x1'), y: getAttribute(el, 'y1')},
+ {x: getAttribute(el, 'x2'), y: getAttribute(el, 'y2')}
);
}
@@ -512,6 +629,8 @@ function getAnimatables(targets) {
function normalizePropertyTweens(prop, tweenSettings) {
var settings = cloneObject(tweenSettings);
+ // Override duration if easing is a spring
+ if (/^spring/.test(settings.easing)) { settings.duration = spring(settings.easing); }
if (is.arr(prop)) {
var l = prop.length;
var isFromTo = (l === 2 && !is.obj(prop[0]));
@@ -525,24 +644,19 @@ function normalizePropertyTweens(prop, tweenSettings) {
}
var propArray = is.arr(prop) ? prop : [prop];
return propArray.map(function (v, i) {
+ var obj = (is.obj(v) && !is.pth(v)) ? v : {value: v};
// Default delay value should be applied only on the first tween
- var delay = !i ? tweenSettings.delay : 0;
- // Use path object as a tween value
- var obj = is.obj(v) && !is.pth(v) ? v : {value: v};
- // Set default delay value
- if (is.und(obj.delay)) { obj.delay = delay; }
+ if (is.und(obj.delay)) { obj.delay = !i ? tweenSettings.delay : 0; }
return obj;
}).map(function (k) { return mergeObjects(k, settings); });
}
function getProperties(instanceSettings, tweenSettings, params) {
var properties = [];
- var settings = mergeObjects(instanceSettings, tweenSettings);
for (var p in params) {
- if (!settings.hasOwnProperty(p) && p !== 'targets') {
+ if (!instanceSettings.hasOwnProperty(p) && !tweenSettings.hasOwnProperty(p) && p !== 'targets') {
properties.push({
name: p,
- offset: settings['timeOffset'],
tweens: normalizePropertyTweens(params[p], tweenSettings)
});
}
@@ -567,26 +681,23 @@ function normalizeTweenValues(tween, animatable) {
return t;
}
-function normalizeEasing(val) {
- return is.arr(val) ? bezier.apply(this, val) : easings[val];
-}
-
function normalizeTweens(prop, animatable) {
var previousTween;
return prop.tweens.map(function (t) {
var tween = normalizeTweenValues(t, animatable);
var tweenValue = tween.value;
- var originalValue = getOriginalTargetValue(animatable.target, prop.name, animatable);
+ var to = is.arr(tweenValue) ? tweenValue[1] : tweenValue;
+ var toUnit = getUnit(to);
+ var originalValue = getOriginalTargetValue(animatable.target, prop.name, toUnit, animatable);
var previousValue = previousTween ? previousTween.to.original : originalValue;
var from = is.arr(tweenValue) ? tweenValue[0] : previousValue;
- var to = getRelativeValue(is.arr(tweenValue) ? tweenValue[1] : tweenValue, from);
- var unit = getUnit(to) || getUnit(from) || getUnit(originalValue);
+ var fromUnit = getUnit(from) || getUnit(originalValue);
+ var unit = toUnit || fromUnit;
tween.from = decomposeValue(from, unit);
- tween.to = decomposeValue(to, unit);
- tween.start = previousTween ? previousTween.end : prop.offset;
+ tween.to = decomposeValue(getRelativeValue(to, from), unit);
+ tween.start = previousTween ? previousTween.end : 0;
tween.end = tween.start + tween.delay + tween.duration + tween.endDelay;
- tween.easing = normalizeEasing(tween.easing);
- tween.elasticity = (1000 - minMaxValue(tween.elasticity, 1, 999)) / 1000;
+ tween.easing = parseEasings(tween);
tween.isPath = is.pth(tweenValue);
tween.isColor = is.col(tween.from.original);
if (tween.isColor) { tween.round = 1; }
@@ -605,32 +716,14 @@ var setProgressValue = {
transforms.list.set(p, v);
if (p === transforms.last || manual) {
var str = '';
- for (var i = 0, list = transforms.list; i < list.length; i += 1) {
- var ref = list[i];
- var prop = ref[0];
- var value = ref[1];
-
+ transforms.list.forEach(function (value, prop) {
str += prop + "(" + value + ") ";
- }
+ });
t.style.transform = str;
}
}
};
-function setTargetValue(targets, properties) {
- var animatables = getAnimatables(targets);
- animatables.forEach(function (animatable) {
- for (var property in properties) {
- var value = properties[property];
- var originalValue = getOriginalTargetValue(animatable.target, property, animatable);
- var unit = getUnit(value) || getUnit(originalValue);
- var to = getRelativeValue(validateValue(value, unit), originalValue);
- var animType = getAnimationType(animatable.target, property);
- setProgressValue[animType](animatable.target, property, to, animatable.transforms, true);
- }
- });
-}
-
// Animations
function createAnimation(animatable, prop) {
@@ -658,12 +751,12 @@ function getAnimations(animatables, properties) {
// Create Instance
-function getInstanceTimings(type, animations, instanceSettings, tweenSettings) {
+function getInstanceTimings(type, animations, tweenSettings) {
var isDelay = (type === 'delay');
if (animations.length) {
return (isDelay ? Math.min : Math.max).apply(Math, animations.map(function (anim) { return anim[type]; }));
} else {
- return isDelay ? tweenSettings.delay : instanceSettings.timeOffset + tweenSettings.delay + tweenSettings.duration;
+ return isDelay ? tweenSettings.delay : tweenSettings.delay + tweenSettings.duration;
}
}
@@ -674,11 +767,12 @@ function createNewInstance(params) {
var properties = getProperties(instanceSettings, tweenSettings, params);
var animations = getAnimations(animatables, properties);
return mergeObjects(instanceSettings, {
+ states: {default: params},
children: [],
animatables: animatables,
animations: animations,
- duration: getInstanceTimings('duration', animations, instanceSettings, tweenSettings),
- delay: getInstanceTimings('delay', animations, instanceSettings, tweenSettings)
+ duration: getInstanceTimings('duration', animations, tweenSettings),
+ delay: getInstanceTimings('delay', animations, tweenSettings)
});
}
@@ -686,35 +780,48 @@ function createNewInstance(params) {
var activeInstances = [];
var pausedInstances = [];
-var raf = 0;
+var raf;
var engine = (function () {
- function play() { raf = requestAnimationFrame(step); }
+ function play() {
+ raf = requestAnimationFrame(step);
+ }
function step(t) {
- if (pausedInstances.length) {
- activeInstances = filterArray(activeInstances, function (ins) { return !pausedInstances.includes(ins); });
- pausedInstances = [];
- }
- if (activeInstances.length) {
+ var activeInstancesLength = activeInstances.length;
+ if (activeInstancesLength) {
var i = 0;
- while (i < activeInstances.length) {
+ while (i < activeInstancesLength) {
var activeInstance = activeInstances[i];
if (!activeInstance.paused) {
- activeInstances[i].tick(t);
+ activeInstance.tick(t);
} else {
- pausedInstances.push(activeInstance);
+ var instanceIndex = activeInstances.indexOf(activeInstance);
+ if (instanceIndex > -1) {
+ activeInstances.splice(instanceIndex, 1);
+ activeInstancesLength = activeInstances.length;
+ }
}
i++;
}
play();
} else {
- cancelAnimationFrame(raf);
- raf = 0;
+ raf = cancelAnimationFrame(raf);
}
}
return play;
})();
+function handleVisibilityChange() {
+ if (document.hidden) {
+ activeInstances.forEach(function (ins) { return ins.pause(); });
+ pausedInstances = activeInstances.slice(0);
+ activeInstances = [];
+ } else {
+ pausedInstances.forEach(function (ins) { return ins.play(); });
+ }
+}
+
+document.addEventListener('visibilitychange', handleVisibilityChange);
// Public Instance
@@ -722,8 +829,8 @@ function anime(params) {
if ( params === void 0 ) params = {};
- var now, startTime, lastTime = 0;
-
+ var startTime = 0, lastTime = 0, now = 0;
+ var children, childrenLength = 0;
var resolve = null;
function makePromise() {
@@ -736,22 +843,23 @@ function anime(params) {
function toggleInstanceDirection() {
instance.reversed = !instance.reversed;
- var children = instance.children;
children.forEach(function (child) { return child.reversed = instance.reversed; });
}
+ function resetTime() {
+ startTime = 0;
+ lastTime = adjustTime(instance.currentTime) * (1 / anime.speed);
+ }
+
function adjustTime(time) {
return instance.reversed ? instance.duration - time : time;
}
- function syncInstanceChildren(time, manual) {
- var children = instance.children;
- var childrenLength = children.length;
- if (!manual || time >= instance.currentTime) {
- for (var i = 0; i < childrenLength; i++) { children[i].seek(time); }
+ function syncInstanceChildren(time) {
+ if (time >= instance.currentTime) {
+ for (var i = 0; i < childrenLength; i++) { children[i].seek(time - children[i].timelineOffset); }
} else {
- // Manual backward seeking requires looping in reverse
- for (var i$1 = childrenLength; i$1--;) { children[i$1].seek(time); }
+ for (var i$1 = childrenLength; i$1--;) { children[i$1].seek(time - children[i$1].timelineOffset); }
}
}
@@ -768,12 +876,12 @@ function anime(params) {
// Only check for keyframes if there is more than one tween
if (tweenLength) { tween = filterArray(tweens, function (t) { return (insTime < t.end); })[0] || tween; }
var elapsed = minMaxValue(insTime - tween.start - tween.delay, 0, tween.duration) / tween.duration;
- var eased = isNaN(elapsed) ? 1 : tween.easing(elapsed, tween.elasticity);
+ var eased = isNaN(elapsed) ? 1 : tween.easing(elapsed);
var strings = tween.to.strings;
var round = tween.round;
var numbers = [];
- var progress = (void 0);
var toNumbersLength = tween.to.numbers.length;
+ var progress = (void 0);
for (var n = 0; n < toNumbersLength; n++) {
var value = (void 0);
var toNumber = tween.to.numbers[n];
@@ -814,7 +922,6 @@ function anime(params) {
i++;
}
instance.currentTime = insTime;
- instance.progress = (insTime / instance.duration) * 100;
}
function setCallback(cb) {
@@ -827,35 +934,34 @@ function anime(params) {
}
}
- function setInstanceProgress(engineTime, manual) {
+ function setInstanceProgress(engineTime) {
var insDuration = instance.duration;
- var insOffset = instance.timeOffset;
- var insStart = insOffset + instance.delay;
- var insCurrentTime = instance.currentTime;
- var insReversed = instance.reversed;
+ var insDelay = instance.delay;
var insTime = adjustTime(engineTime);
- if (instance.children.length) { syncInstanceChildren(insTime, manual); }
- if (insTime >= insStart || !insDuration) {
+ instance.progress = minMaxValue((insTime / insDuration) * 100, 0, 100);
+ if (children) { syncInstanceChildren(insTime); }
+ if (insTime >= insDelay || !insDuration) {
if (!instance.began) {
instance.began = true;
setCallback('begin');
}
setCallback('run');
}
- if (insTime > insOffset && insTime < insDuration) {
+ if (insTime > insDelay && insTime < insDuration) {
setAnimationsProgress(insTime);
} else {
- if (insTime <= insOffset && insCurrentTime !== 0) {
+ if (insTime <= insDelay && instance.currentTime !== 0) {
setAnimationsProgress(0);
- if (insReversed) { countIteration(); }
+ if (instance.reversed) { countIteration(); }
}
- if ((insTime >= insDuration && insCurrentTime !== insDuration) || !insDuration) {
+ if ((insTime >= insDuration && instance.currentTime !== insDuration) || !insDuration) {
setAnimationsProgress(insDuration);
- if (!insReversed) { countIteration(); }
+ if (!instance.reversed) { countIteration(); }
}
}
setCallback('update');
if (engineTime >= insDuration) {
+ lastTime = 0;
if (instance.remaining) {
startTime = now;
if (instance.direction === 'alternate') { toggleInstanceDirection(); }
@@ -870,7 +976,6 @@ function anime(params) {
}
}
}
- lastTime = 0;
}
}
@@ -884,42 +989,37 @@ function anime(params) {
instance.completed = false;
instance.reversed = direction === 'reverse';
instance.remaining = direction === 'alternate' && loops === 1 ? 2 : loops;
+ children = instance.children;
+ childrenLength = children.length;
+ for (var i = childrenLength; i--;) { instance.children[i].reset(); }
setAnimationsProgress(0);
- for (var i = instance.children.length; i--; ){
- instance.children[i].reset();
- }
};
instance.tick = function(t) {
now = t;
if (!startTime) { startTime = now; }
- var engineTime = (lastTime + now - startTime) * anime.speed;
- setInstanceProgress(engineTime);
+ setInstanceProgress((now + (lastTime - startTime)) * anime.speed);
};
instance.seek = function(time) {
- setInstanceProgress(adjustTime(time), true);
+ setInstanceProgress(adjustTime(time));
};
instance.pause = function() {
instance.paused = true;
- var i = activeInstances.indexOf(instance);
- if (i > -1) { activeInstances.splice(i, 1); }
};
instance.play = function() {
if (!instance.paused) { return; }
instance.paused = false;
- startTime = 0;
- lastTime = adjustTime(instance.currentTime);
+ resetTime();
activeInstances.push(instance);
if (!raf) { engine(); }
};
instance.reverse = function() {
toggleInstanceDirection();
- startTime = 0;
- lastTime = adjustTime(instance.currentTime);
+ resetTime();
};
instance.restart = function() {
@@ -928,10 +1028,32 @@ function anime(params) {
instance.play();
};
+ instance.animateTo = function(stateName, paramsOverrides, bypassAnimation) {
+ if ( paramsOverrides === void 0 ) paramsOverrides = {};
+
+ var nextState = instance.states[stateName];
+ var defaultState = instance.states.default;
+ var params = mergeObjects(paramsOverrides, mergeObjects(nextState, defaultState));
+ var animation = anime(params);
+ if (!bypassAnimation) {
+ animation.play();
+ } else {
+ animation.seek(animation.duration);
+ }
+ };
+
+ instance.goTo = function(stateName, paramsOverrides) {
+ instance.animateTo(stateName, paramsOverrides, true);
+ };
+
instance.finished = promise;
instance.reset();
+ if (instance.startTime) {
+ setInstanceProgress(instance.startTime - (instance.duration * Math.floor(instance.startTime / instance.duration)));
+ }
+
if (instance.autoplay) { instance.play(); }
return instance;
@@ -940,17 +1062,28 @@ function anime(params) {
// Remove targets from animation
+function removeTargetsFromAnimations(targetsArray, animations) {
+ for (var a = animations.length; a--;) {
+ if (arrayContains(targetsArray, animations[a].animatable.target)) {
+ animations.splice(a, 1);
+ }
+ }
+}
+
function removeTargets(targets) {
var targetsArray = parseTargets(targets);
for (var i = activeInstances.length; i--;) {
var instance = activeInstances[i];
var animations = instance.animations;
- for (var a = animations.length; a--;) {
- if (arrayContains(targetsArray, animations[a].animatable.target)) {
- animations.splice(a, 1);
- if (!animations.length) { instance.pause(); }
- }
+ var children = instance.children;
+ removeTargetsFromAnimations(targetsArray, animations);
+ for (var c = children.length; c--;) {
+ var child = children[c];
+ var childAnimations = child.animations;
+ removeTargetsFromAnimations(targetsArray, childAnimations);
+ if (!childAnimations.length && !child.children.length) { children.splice(c, 1); }
}
+ if (!animations.length && !children.length) { instance.pause(); }
}
}
@@ -962,21 +1095,22 @@ function timeline(params) {
var tl = anime(params);
tl.pause();
tl.duration = 0;
- tl.add = function(instanceParams, timeOffset) {
- tl.children.forEach(function (i) { i.began = true; i.completed = true; });
+ tl.add = function(instanceParams, timelineOffset) {
+ function passThrough(ins) { ins.began = true; ins.completed = true; }
+ tl.children.forEach(passThrough);
var insParams = mergeObjects(instanceParams, replaceObjectProps(defaultTweenSettings, params));
insParams.targets = insParams.targets || params.targets;
var tlDuration = tl.duration;
insParams.autoplay = false;
insParams.direction = tl.direction;
- insParams.timeOffset = is.und(timeOffset) ? tlDuration : getRelativeValue(timeOffset, tlDuration);
- tl.began = true;
- tl.completed = true;
- tl.seek(insParams.timeOffset);
+ insParams.timelineOffset = is.und(timelineOffset) ? tlDuration : getRelativeValue(timelineOffset, tlDuration);
+ passThrough(tl);
+ tl.seek(insParams.timelineOffset);
var ins = anime(insParams);
- ins.began = true;
- ins.completed = true;
- if (ins.duration > tlDuration) { tl.duration = ins.duration; }
+ passThrough(ins);
+ var totalDuration = ins.duration + insParams.timelineOffset;
+ if (is.fnc(tl.delay)) { tl.delay = ins.delay; }
+ if (is.fnc(tlDuration) || totalDuration > tlDuration) { tl.duration = totalDuration; }
tl.children.push(ins);
tl.seek(0);
tl.reset();
@@ -990,13 +1124,11 @@ anime.version = '3.0.0';
anime.speed = 1;
anime.running = activeInstances;
anime.remove = removeTargets;
-anime.get = getOriginalTargetValue;
-anime.set = setTargetValue;
+anime.getValue = getOriginalTargetValue;
anime.path = getPath;
anime.setDashoffset = setDashoffset;
-anime.bezier = bezier;
-anime.easings = easings;
anime.timeline = timeline;
+anime.easings = easings;
anime.random = function (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; };
export default anime;
diff --git a/lib/anime.js b/lib/anime.js
index 0876ae57..cdbdbbc4 100644
--- a/lib/anime.js
+++ b/lib/anime.js
@@ -1,39 +1,52 @@
/*
- 2017 Julian Garnier
+ anime.js v3.0.0
+ © 2018 Julian Garnier
Released under the MIT license
+ animejs.com
*/
'use strict';
// Defaults
var defaultInstanceSettings = {
- update: undefined,
- begin: undefined,
- run: undefined,
- complete: undefined,
+ update: null,
+ begin: null,
+ run: null,
+ complete: null,
loop: 1,
direction: 'normal',
autoplay: true,
- timeOffset: 0
+ startTime: 0,
+ timelineOffset: 0
};
var defaultTweenSettings = {
duration: 1000,
delay: 0,
endDelay: 0,
- easing: 'easeOutElastic',
- elasticity: 500,
+ easing: 'easeOutElastic(1, .5)',
round: 0
};
var validTransforms = ['translateX', 'translateY', 'translateZ', 'rotate', 'rotateX', 'rotateY', 'rotateZ', 'scale', 'scaleX', 'scaleY', 'scaleZ', 'skew', 'skewX', 'skewY', 'perspective'];
+// Caching
+
+var cache = {
+ CSS: {},
+ springs: {}
+};
+
// Utils
function stringContains(str, text) {
return str.indexOf(text) > -1;
}
+function applyArguments(func, args) {
+ return func.apply(null, args);
+}
+
var is = {
arr: function (a) { return Array.isArray(a); },
obj: function (a) { return stringContains(Object.prototype.toString.call(a), 'Object'); },
@@ -49,6 +62,83 @@ var is = {
col: function (a) { return (is.hex(a) || is.rgb(a) || is.hsl(a)); }
};
+function parseEasingParameters(string) {
+ var match = /\(([^)]+)\)/.exec(string);
+ return match ? match[1].split(',').map(function (p) { return parseFloat(p); }) : [];
+}
+
+// Spring solver inspired by Webkit Copyright © 2016 Apple Inc. All rights reserved. https://webkit.org/demos/spring/spring.js
+
+function spring(string, duration) {
+
+ var params = parseEasingParameters(string);
+ var mass = minMaxValue(is.und(params[0]) ? 1 : params[0], .1, 100);
+ var stiffness = minMaxValue(is.und(params[1]) ? 100 : params[1], .1, 100);
+ var damping = minMaxValue(is.und(params[2]) ? 10 : params[2], .1, 100);
+ var velocity = minMaxValue(is.und(params[3]) ? 0 : params[3], .1, 100);
+ var w0 = Math.sqrt(stiffness / mass);
+ var zeta = damping / (2 * Math.sqrt(stiffness * mass));
+ var wd = zeta < 1 ? w0 * Math.sqrt(1 - zeta * zeta) : 0;
+ var a = 1;
+ var b = zeta < 1 ? (zeta * w0 + -velocity) / wd : -velocity + w0;
+
+ function solver(t) {
+ var progress = duration ? (duration * t) / 1000 : t;
+ if (zeta < 1) {
+ progress = Math.exp(-progress * zeta * w0) * (a * Math.cos(wd * progress) + b * Math.sin(wd * progress));
+ } else {
+ progress = (a + b * progress) * Math.exp(-progress * w0);
+ }
+ if (t === 0 || t === 1) { return t; }
+ return 1 - progress;
+ }
+
+ function getDuration() {
+ var cached = cache.springs[string];
+ if (cached) { return cached; }
+ var frame = 1/6;
+ var elapsed = 0;
+ var rest = 0;
+ while(true) {
+ elapsed += frame;
+ if (solver(elapsed) === 1) {
+ rest++;
+ if (rest >= 16) { break; }
+ } else {
+ rest = 0;
+ }
+ }
+ var duration = elapsed * frame * 1000;
+ cache.springs[string] = duration;
+ return duration;
+ }
+
+ return duration ? solver : getDuration;
+
+}
+
+// Elastic easing adapted from jQueryUI http://api.jqueryui.com/easings/
+
+function elastic(amplitude, period) {
+ if ( amplitude === void 0 ) amplitude = 1;
+ if ( period === void 0 ) period = .5;
+
+ var a = minMaxValue(amplitude, 1, 10);
+ var p = minMaxValue(period, .1, 2);
+ return function (t) {
+ return (t === 0 || t === 1) ? t :
+ -a * Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (p / (Math.PI * 2) * Math.asin(1 / a))) * (Math.PI * 2)) / p);
+ }
+}
+
+// Basic steps easing implementation https://developer.mozilla.org/fr/docs/Web/CSS/transition-timing-function
+
+function steps(steps) {
+ if ( steps === void 0 ) steps = 10;
+
+ return function (t) { return Math.round(t * steps) * (1 / steps); };
+}
+
// BezierEasing https://github.com/gre/bezier-easing
var bezier = (function () {
@@ -56,14 +146,14 @@ var bezier = (function () {
var kSplineTableSize = 11;
var kSampleStepSize = 1.0 / (kSplineTableSize - 1.0);
- function A (aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1 }
- function B (aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1 }
- function C (aA1) { return 3.0 * aA1 }
+ function A(aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1 }
+ function B(aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1 }
+ function C(aA1) { return 3.0 * aA1 }
- function calcBezier (aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT }
- function getSlope (aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1) }
+ function calcBezier(aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT }
+ function getSlope(aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1) }
- function binarySubdivide (aX, aA, aB, mX1, mX2) {
+ function binarySubdivide(aX, aA, aB, mX1, mX2) {
var currentX, currentT, i = 0;
do {
currentT = aA + (aB - aA) / 2.0;
@@ -73,7 +163,7 @@ var bezier = (function () {
return currentT;
}
- function newtonRaphsonIterate (aX, aGuessT, mX1, mX2) {
+ function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
for (var i = 0; i < 4; ++i) {
var currentSlope = getSlope(aGuessT, mX1, mX2);
if (currentSlope === 0.0) { return aGuessT; }
@@ -122,8 +212,7 @@ var bezier = (function () {
return function (x) {
if (mX1 === mY1 && mX2 === mY2) { return x; }
- if (x === 0) { return 0; }
- if (x === 1) { return 1; }
+ if (x === 0 || x === 1) { return x; }
return calcBezier(getTForX(x), mY1, mY2);
}
@@ -137,71 +226,75 @@ var easings = (function () {
var names = ['Quad', 'Cubic', 'Quart', 'Quint', 'Sine', 'Expo', 'Circ', 'Back', 'Elastic'];
- // Elastic easing adapted from jQueryUI http://api.jqueryui.com/easings/
-
- function elastic(t, p) {
- return t === 0 || t === 1 ? t :
- -Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (p / (Math.PI * 2.0) * Math.asin(1))) * (Math.PI * 2)) / p );
- }
-
// Approximated Penner equations http://matthewlein.com/ceaser/
- var equations = {
+ var penner = {
In: [
- [0.550, 0.085, 0.680, 0.530], /* InQuad */
- [0.550, 0.055, 0.675, 0.190], /* InCubic */
- [0.895, 0.030, 0.685, 0.220], /* InQuart */
- [0.755, 0.050, 0.855, 0.060], /* InQuint */
- [0.470, 0.000, 0.745, 0.715], /* InSine */
- [0.950, 0.050, 0.795, 0.035], /* InExpo */
- [0.600, 0.040, 0.980, 0.335], /* InCirc */
- [0.600, -0.280, 0.735, 0.045], /* InBack */
- elastic /* InElastic */
- ], Out: [
- [0.250, 0.460, 0.450, 0.940], /* OutQuad */
- [0.215, 0.610, 0.355, 1.000], /* OutCubic */
- [0.165, 0.840, 0.440, 1.000], /* OutQuart */
- [0.230, 1.000, 0.320, 1.000], /* OutQuint */
- [0.390, 0.575, 0.565, 1.000], /* OutSine */
- [0.190, 1.000, 0.220, 1.000], /* OutExpo */
- [0.075, 0.820, 0.165, 1.000], /* OutCirc */
- [0.175, 0.885, 0.320, 1.275], /* OutBack */
- function (t, f) { return 1 - elastic(1 - t, f); } /* OutElastic */
- ], InOut: [
- [0.455, 0.030, 0.515, 0.955], /* InOutQuad */
- [0.645, 0.045, 0.355, 1.000], /* InOutCubic */
- [0.770, 0.000, 0.175, 1.000], /* InOutQuart */
- [0.860, 0.000, 0.070, 1.000], /* InOutQuint */
- [0.445, 0.050, 0.550, 0.950], /* InOutSine */
- [1.000, 0.000, 0.000, 1.000], /* InOutExpo */
- [0.785, 0.135, 0.150, 0.860], /* InOutCirc */
- [0.680, -0.550, 0.265, 1.550], /* InOutBack */
- function (t, f) { return t < .5 ? elastic(t * 2, f) / 2 : 1 - elastic(t * -2 + 2, f) / 2; } /* InOutElastic */
+ [0.550, 0.085, 0.680, 0.530], /* inQuad */
+ [0.550, 0.055, 0.675, 0.190], /* inCubic */
+ [0.895, 0.030, 0.685, 0.220], /* inQuart */
+ [0.755, 0.050, 0.855, 0.060], /* inQuint */
+ [0.470, 0.000, 0.745, 0.715], /* inSine */
+ [0.950, 0.050, 0.795, 0.035], /* inExpo */
+ [0.600, 0.040, 0.980, 0.335], /* inCirc */
+ [0.600,-0.280, 0.735, 0.045], /* inBack */
+ elastic /* inElastic */
+ ],
+ Out: [
+ [0.250, 0.460, 0.450, 0.940], /* outQuad */
+ [0.215, 0.610, 0.355, 1.000], /* outCubic */
+ [0.165, 0.840, 0.440, 1.000], /* outQuart */
+ [0.230, 1.000, 0.320, 1.000], /* outQuint */
+ [0.390, 0.575, 0.565, 1.000], /* outSine */
+ [0.190, 1.000, 0.220, 1.000], /* outExpo */
+ [0.075, 0.820, 0.165, 1.000], /* outCirc */
+ [0.175, 0.885, 0.320, 1.275], /* outBack */
+ function (a, p) { return function (t) { return 1 - elastic(a, p)(1 - t); }; } /* outElastic */
+ ],
+ InOut: [
+ [0.455, 0.030, 0.515, 0.955], /* inOutQuad */
+ [0.645, 0.045, 0.355, 1.000], /* inOutCubic */
+ [0.770, 0.000, 0.175, 1.000], /* inOutQuart */
+ [0.860, 0.000, 0.070, 1.000], /* inOutQuint */
+ [0.445, 0.050, 0.550, 0.950], /* inOutSine */
+ [1.000, 0.000, 0.000, 1.000], /* inOutExpo */
+ [0.785, 0.135, 0.150, 0.860], /* inOutCirc */
+ [0.680,-0.550, 0.265, 1.550], /* inOutBack */
+ function (a, p) { return function (t) { return t < .5 ? elastic(a, p)(t * 2) / 2 : 1 - elastic(a, p)(t * -2 + 2) / 2; }; } /* inOutElastic */
]
};
- var functions = {
- linear: bezier(0.250, 0.250, 0.750, 0.750)
+ var eases = {
+ linear: [0.250, 0.250, 0.750, 0.750]
};
- var loop = function ( type ) {
- equations[type].forEach(function (f, i) {
- functions['ease' + type + names[i]] = is.fnc(f) ? f : bezier.apply(null, f);
+ var loop = function ( equation ) {
+ penner[equation].forEach(function (ease, i) {
+ eases['ease'+equation+names[i]] = ease;
});
};
- for (var type in equations) loop( type );
+ for (var equation in penner) loop( equation );
- return functions;
+ return eases;
})();
-// Strings
-
-function stringToHyphens(str) {
- return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
+function parseEasings(tween) {
+ var string = tween.easing;
+ var name = string.split('(')[0];
+ var args = parseEasingParameters(string);
+ var ease = easings[name];
+ switch (name) {
+ case 'spring' : return spring(string, tween.duration);
+ case 'cubicBezier' : return applyArguments(bezier, args);
+ case 'steps' : return applyArguments(steps, args);
+ default : return is.fnc(ease) ? applyArguments(ease, args) : applyArguments(bezier, ease);
+ }
}
+// Strings
+
function selectString(str) {
try {
var nodes = document.querySelectorAll(str);
@@ -336,15 +429,39 @@ function getFunctionValue(val, animatable) {
return val(animatable.target, animatable.id, animatable.total);
}
-function getCSSValue(el, prop) {
+function getAttribute(el, prop) {
+ return el.getAttribute(prop);
+}
+
+function convertPxToUnit(el, value, unit) {
+ var valueUnit = getUnit(value);
+ if (arrayContains([unit, 'deg', 'rad', 'turn'], valueUnit)) { return value; }
+ var cached = cache.CSS[value + unit];
+ if (!is.und(cached)) { return cached; }
+ var baseline = 100;
+ var tempEl = document.createElement(el.tagName);
+ var parentEl = el.parentNode || document.body;
+ parentEl.appendChild(tempEl);
+ tempEl.style.position = 'absolute';
+ tempEl.style.width = baseline + unit;
+ var factor = baseline / tempEl.offsetWidth;
+ parentEl.removeChild(tempEl);
+ var convertedUnit = factor * parseFloat(value);
+ cache.CSS[value + unit] = convertedUnit;
+ return convertedUnit;
+}
+
+function getCSSValue(el, prop, unit) {
if (prop in el.style) {
- return el.style[prop] || getComputedStyle(el).getPropertyValue(stringToHyphens(prop)) || '0';
+ var uppercasePropName = prop.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
+ var value = el.style[prop] || getComputedStyle(el).getPropertyValue(uppercasePropName) || '0';
+ return unit ? convertPxToUnit(el, value, unit) : value;
}
}
function getAnimationType(el, prop) {
+ if (is.dom(el) && (getAttribute(el, prop) || (is.svg(el) && el[prop]))) { return 'attribute'; }
if (is.dom(el) && arrayContains(validTransforms, prop)) { return 'transform'; }
- if (is.dom(el) && (el.getAttribute(prop) || (is.svg(el) && el[prop]))) { return 'attribute'; }
if (is.dom(el) && (prop !== 'transform' && getCSSValue(el, prop))) { return 'css'; }
if (el[prop] != null) { return 'object'; }
}
@@ -358,19 +475,19 @@ function getElementTransforms(el) {
return transforms;
}
-function getTransformValue(el, propName, animatable) {
+function getTransformValue(el, propName, animatable, unit) {
var defaultVal = stringContains(propName, 'scale') ? 1 : 0 + getTransformUnit(propName);
- var prop = getElementTransforms(el).get(propName) || defaultVal;
- animatable.transforms.list.set(propName, prop);
+ var value = getElementTransforms(el).get(propName) || defaultVal;
+ animatable.transforms.list.set(propName, value);
animatable.transforms['last'] = propName;
- return prop;
+ return unit ? convertPxToUnit(el, value, unit) : value;
}
-function getOriginalTargetValue(target, propName, animatable) {
+function getOriginalTargetValue(target, propName, unit, animatable) {
switch (getAnimationType(target, propName)) {
- case 'transform': return getTransformValue(target, propName, animatable);
- case 'css': return getCSSValue(target, propName);
- case 'attribute': return target.getAttribute(propName);
+ case 'transform': return getTransformValue(target, propName, animatable, unit);
+ case 'css': return getCSSValue(target, propName, unit);
+ case 'attribute': return getAttribute(target, propName);
default: return target[propName] || 0;
}
}
@@ -395,7 +512,7 @@ function validateValue(val, unit) {
return unit && !/\s/g.test(val) ? unitLess + unit : unitLess;
}
-// getTotalLength() equivalent for circle, rect, polyline, polygon and line shapes.
+// getTotalLength() equivalent for circle, rect, polyline, polygon and line shapes
// adapted from https://gist.github.com/SebLambla/3e0550c496c236709744
function getDistance(p1, p2) {
@@ -403,17 +520,17 @@ function getDistance(p1, p2) {
}
function getCircleLength(el) {
- return 2 * Math.PI * el.getAttribute('r');
+ return Math.PI * 2 * getAttribute(el, 'r');
}
function getRectLength(el) {
- return (el.getAttribute('width') * 2) + (el.getAttribute('height') * 2);
+ return (getAttribute(el, 'width') * 2) + (getAttribute(el, 'height') * 2);
}
function getLineLength(el) {
return getDistance(
- {x: el.getAttribute('x1'), y: el.getAttribute('y1')},
- {x: el.getAttribute('x2'), y: el.getAttribute('y2')}
+ {x: getAttribute(el, 'x1'), y: getAttribute(el, 'y1')},
+ {x: getAttribute(el, 'x2'), y: getAttribute(el, 'y2')}
);
}
@@ -514,6 +631,8 @@ function getAnimatables(targets) {
function normalizePropertyTweens(prop, tweenSettings) {
var settings = cloneObject(tweenSettings);
+ // Override duration if easing is a spring
+ if (/^spring/.test(settings.easing)) { settings.duration = spring(settings.easing); }
if (is.arr(prop)) {
var l = prop.length;
var isFromTo = (l === 2 && !is.obj(prop[0]));
@@ -527,24 +646,19 @@ function normalizePropertyTweens(prop, tweenSettings) {
}
var propArray = is.arr(prop) ? prop : [prop];
return propArray.map(function (v, i) {
+ var obj = (is.obj(v) && !is.pth(v)) ? v : {value: v};
// Default delay value should be applied only on the first tween
- var delay = !i ? tweenSettings.delay : 0;
- // Use path object as a tween value
- var obj = is.obj(v) && !is.pth(v) ? v : {value: v};
- // Set default delay value
- if (is.und(obj.delay)) { obj.delay = delay; }
+ if (is.und(obj.delay)) { obj.delay = !i ? tweenSettings.delay : 0; }
return obj;
}).map(function (k) { return mergeObjects(k, settings); });
}
function getProperties(instanceSettings, tweenSettings, params) {
var properties = [];
- var settings = mergeObjects(instanceSettings, tweenSettings);
for (var p in params) {
- if (!settings.hasOwnProperty(p) && p !== 'targets') {
+ if (!instanceSettings.hasOwnProperty(p) && !tweenSettings.hasOwnProperty(p) && p !== 'targets') {
properties.push({
name: p,
- offset: settings['timeOffset'],
tweens: normalizePropertyTweens(params[p], tweenSettings)
});
}
@@ -569,26 +683,23 @@ function normalizeTweenValues(tween, animatable) {
return t;
}
-function normalizeEasing(val) {
- return is.arr(val) ? bezier.apply(this, val) : easings[val];
-}
-
function normalizeTweens(prop, animatable) {
var previousTween;
return prop.tweens.map(function (t) {
var tween = normalizeTweenValues(t, animatable);
var tweenValue = tween.value;
- var originalValue = getOriginalTargetValue(animatable.target, prop.name, animatable);
+ var to = is.arr(tweenValue) ? tweenValue[1] : tweenValue;
+ var toUnit = getUnit(to);
+ var originalValue = getOriginalTargetValue(animatable.target, prop.name, toUnit, animatable);
var previousValue = previousTween ? previousTween.to.original : originalValue;
var from = is.arr(tweenValue) ? tweenValue[0] : previousValue;
- var to = getRelativeValue(is.arr(tweenValue) ? tweenValue[1] : tweenValue, from);
- var unit = getUnit(to) || getUnit(from) || getUnit(originalValue);
+ var fromUnit = getUnit(from) || getUnit(originalValue);
+ var unit = toUnit || fromUnit;
tween.from = decomposeValue(from, unit);
- tween.to = decomposeValue(to, unit);
- tween.start = previousTween ? previousTween.end : prop.offset;
+ tween.to = decomposeValue(getRelativeValue(to, from), unit);
+ tween.start = previousTween ? previousTween.end : 0;
tween.end = tween.start + tween.delay + tween.duration + tween.endDelay;
- tween.easing = normalizeEasing(tween.easing);
- tween.elasticity = (1000 - minMaxValue(tween.elasticity, 1, 999)) / 1000;
+ tween.easing = parseEasings(tween);
tween.isPath = is.pth(tweenValue);
tween.isColor = is.col(tween.from.original);
if (tween.isColor) { tween.round = 1; }
@@ -607,32 +718,14 @@ var setProgressValue = {
transforms.list.set(p, v);
if (p === transforms.last || manual) {
var str = '';
- for (var i = 0, list = transforms.list; i < list.length; i += 1) {
- var ref = list[i];
- var prop = ref[0];
- var value = ref[1];
-
+ transforms.list.forEach(function (value, prop) {
str += prop + "(" + value + ") ";
- }
+ });
t.style.transform = str;
}
}
};
-function setTargetValue(targets, properties) {
- var animatables = getAnimatables(targets);
- animatables.forEach(function (animatable) {
- for (var property in properties) {
- var value = properties[property];
- var originalValue = getOriginalTargetValue(animatable.target, property, animatable);
- var unit = getUnit(value) || getUnit(originalValue);
- var to = getRelativeValue(validateValue(value, unit), originalValue);
- var animType = getAnimationType(animatable.target, property);
- setProgressValue[animType](animatable.target, property, to, animatable.transforms, true);
- }
- });
-}
-
// Animations
function createAnimation(animatable, prop) {
@@ -660,12 +753,12 @@ function getAnimations(animatables, properties) {
// Create Instance
-function getInstanceTimings(type, animations, instanceSettings, tweenSettings) {
+function getInstanceTimings(type, animations, tweenSettings) {
var isDelay = (type === 'delay');
if (animations.length) {
return (isDelay ? Math.min : Math.max).apply(Math, animations.map(function (anim) { return anim[type]; }));
} else {
- return isDelay ? tweenSettings.delay : instanceSettings.timeOffset + tweenSettings.delay + tweenSettings.duration;
+ return isDelay ? tweenSettings.delay : tweenSettings.delay + tweenSettings.duration;
}
}
@@ -676,11 +769,12 @@ function createNewInstance(params) {
var properties = getProperties(instanceSettings, tweenSettings, params);
var animations = getAnimations(animatables, properties);
return mergeObjects(instanceSettings, {
+ states: {default: params},
children: [],
animatables: animatables,
animations: animations,
- duration: getInstanceTimings('duration', animations, instanceSettings, tweenSettings),
- delay: getInstanceTimings('delay', animations, instanceSettings, tweenSettings)
+ duration: getInstanceTimings('duration', animations, tweenSettings),
+ delay: getInstanceTimings('delay', animations, tweenSettings)
});
}
@@ -688,35 +782,48 @@ function createNewInstance(params) {
var activeInstances = [];
var pausedInstances = [];
-var raf = 0;
+var raf;
var engine = (function () {
- function play() { raf = requestAnimationFrame(step); }
+ function play() {
+ raf = requestAnimationFrame(step);
+ }
function step(t) {
- if (pausedInstances.length) {
- activeInstances = filterArray(activeInstances, function (ins) { return !pausedInstances.includes(ins); });
- pausedInstances = [];
- }
- if (activeInstances.length) {
+ var activeInstancesLength = activeInstances.length;
+ if (activeInstancesLength) {
var i = 0;
- while (i < activeInstances.length) {
+ while (i < activeInstancesLength) {
var activeInstance = activeInstances[i];
if (!activeInstance.paused) {
- activeInstances[i].tick(t);
+ activeInstance.tick(t);
} else {
- pausedInstances.push(activeInstance);
+ var instanceIndex = activeInstances.indexOf(activeInstance);
+ if (instanceIndex > -1) {
+ activeInstances.splice(instanceIndex, 1);
+ activeInstancesLength = activeInstances.length;
+ }
}
i++;
}
play();
} else {
- cancelAnimationFrame(raf);
- raf = 0;
+ raf = cancelAnimationFrame(raf);
}
}
return play;
})();
+function handleVisibilityChange() {
+ if (document.hidden) {
+ activeInstances.forEach(function (ins) { return ins.pause(); });
+ pausedInstances = activeInstances.slice(0);
+ activeInstances = [];
+ } else {
+ pausedInstances.forEach(function (ins) { return ins.play(); });
+ }
+}
+
+document.addEventListener('visibilitychange', handleVisibilityChange);
// Public Instance
@@ -724,8 +831,8 @@ function anime(params) {
if ( params === void 0 ) params = {};
- var now, startTime, lastTime = 0;
-
+ var startTime = 0, lastTime = 0, now = 0;
+ var children, childrenLength = 0;
var resolve = null;
function makePromise() {
@@ -738,22 +845,23 @@ function anime(params) {
function toggleInstanceDirection() {
instance.reversed = !instance.reversed;
- var children = instance.children;
children.forEach(function (child) { return child.reversed = instance.reversed; });
}
+ function resetTime() {
+ startTime = 0;
+ lastTime = adjustTime(instance.currentTime) * (1 / anime.speed);
+ }
+
function adjustTime(time) {
return instance.reversed ? instance.duration - time : time;
}
- function syncInstanceChildren(time, manual) {
- var children = instance.children;
- var childrenLength = children.length;
- if (!manual || time >= instance.currentTime) {
- for (var i = 0; i < childrenLength; i++) { children[i].seek(time); }
+ function syncInstanceChildren(time) {
+ if (time >= instance.currentTime) {
+ for (var i = 0; i < childrenLength; i++) { children[i].seek(time - children[i].timelineOffset); }
} else {
- // Manual backward seeking requires looping in reverse
- for (var i$1 = childrenLength; i$1--;) { children[i$1].seek(time); }
+ for (var i$1 = childrenLength; i$1--;) { children[i$1].seek(time - children[i$1].timelineOffset); }
}
}
@@ -770,12 +878,12 @@ function anime(params) {
// Only check for keyframes if there is more than one tween
if (tweenLength) { tween = filterArray(tweens, function (t) { return (insTime < t.end); })[0] || tween; }
var elapsed = minMaxValue(insTime - tween.start - tween.delay, 0, tween.duration) / tween.duration;
- var eased = isNaN(elapsed) ? 1 : tween.easing(elapsed, tween.elasticity);
+ var eased = isNaN(elapsed) ? 1 : tween.easing(elapsed);
var strings = tween.to.strings;
var round = tween.round;
var numbers = [];
- var progress = (void 0);
var toNumbersLength = tween.to.numbers.length;
+ var progress = (void 0);
for (var n = 0; n < toNumbersLength; n++) {
var value = (void 0);
var toNumber = tween.to.numbers[n];
@@ -816,7 +924,6 @@ function anime(params) {
i++;
}
instance.currentTime = insTime;
- instance.progress = (insTime / instance.duration) * 100;
}
function setCallback(cb) {
@@ -829,35 +936,34 @@ function anime(params) {
}
}
- function setInstanceProgress(engineTime, manual) {
+ function setInstanceProgress(engineTime) {
var insDuration = instance.duration;
- var insOffset = instance.timeOffset;
- var insStart = insOffset + instance.delay;
- var insCurrentTime = instance.currentTime;
- var insReversed = instance.reversed;
+ var insDelay = instance.delay;
var insTime = adjustTime(engineTime);
- if (instance.children.length) { syncInstanceChildren(insTime, manual); }
- if (insTime >= insStart || !insDuration) {
+ instance.progress = minMaxValue((insTime / insDuration) * 100, 0, 100);
+ if (children) { syncInstanceChildren(insTime); }
+ if (insTime >= insDelay || !insDuration) {
if (!instance.began) {
instance.began = true;
setCallback('begin');
}
setCallback('run');
}
- if (insTime > insOffset && insTime < insDuration) {
+ if (insTime > insDelay && insTime < insDuration) {
setAnimationsProgress(insTime);
} else {
- if (insTime <= insOffset && insCurrentTime !== 0) {
+ if (insTime <= insDelay && instance.currentTime !== 0) {
setAnimationsProgress(0);
- if (insReversed) { countIteration(); }
+ if (instance.reversed) { countIteration(); }
}
- if ((insTime >= insDuration && insCurrentTime !== insDuration) || !insDuration) {
+ if ((insTime >= insDuration && instance.currentTime !== insDuration) || !insDuration) {
setAnimationsProgress(insDuration);
- if (!insReversed) { countIteration(); }
+ if (!instance.reversed) { countIteration(); }
}
}
setCallback('update');
if (engineTime >= insDuration) {
+ lastTime = 0;
if (instance.remaining) {
startTime = now;
if (instance.direction === 'alternate') { toggleInstanceDirection(); }
@@ -872,7 +978,6 @@ function anime(params) {
}
}
}
- lastTime = 0;
}
}
@@ -886,42 +991,37 @@ function anime(params) {
instance.completed = false;
instance.reversed = direction === 'reverse';
instance.remaining = direction === 'alternate' && loops === 1 ? 2 : loops;
+ children = instance.children;
+ childrenLength = children.length;
+ for (var i = childrenLength; i--;) { instance.children[i].reset(); }
setAnimationsProgress(0);
- for (var i = instance.children.length; i--; ){
- instance.children[i].reset();
- }
};
instance.tick = function(t) {
now = t;
if (!startTime) { startTime = now; }
- var engineTime = (lastTime + now - startTime) * anime.speed;
- setInstanceProgress(engineTime);
+ setInstanceProgress((now + (lastTime - startTime)) * anime.speed);
};
instance.seek = function(time) {
- setInstanceProgress(adjustTime(time), true);
+ setInstanceProgress(adjustTime(time));
};
instance.pause = function() {
instance.paused = true;
- var i = activeInstances.indexOf(instance);
- if (i > -1) { activeInstances.splice(i, 1); }
};
instance.play = function() {
if (!instance.paused) { return; }
instance.paused = false;
- startTime = 0;
- lastTime = adjustTime(instance.currentTime);
+ resetTime();
activeInstances.push(instance);
if (!raf) { engine(); }
};
instance.reverse = function() {
toggleInstanceDirection();
- startTime = 0;
- lastTime = adjustTime(instance.currentTime);
+ resetTime();
};
instance.restart = function() {
@@ -930,10 +1030,32 @@ function anime(params) {
instance.play();
};
+ instance.animateTo = function(stateName, paramsOverrides, bypassAnimation) {
+ if ( paramsOverrides === void 0 ) paramsOverrides = {};
+
+ var nextState = instance.states[stateName];
+ var defaultState = instance.states.default;
+ var params = mergeObjects(paramsOverrides, mergeObjects(nextState, defaultState));
+ var animation = anime(params);
+ if (!bypassAnimation) {
+ animation.play();
+ } else {
+ animation.seek(animation.duration);
+ }
+ };
+
+ instance.goTo = function(stateName, paramsOverrides) {
+ instance.animateTo(stateName, paramsOverrides, true);
+ };
+
instance.finished = promise;
instance.reset();
+ if (instance.startTime) {
+ setInstanceProgress(instance.startTime - (instance.duration * Math.floor(instance.startTime / instance.duration)));
+ }
+
if (instance.autoplay) { instance.play(); }
return instance;
@@ -942,17 +1064,28 @@ function anime(params) {
// Remove targets from animation
+function removeTargetsFromAnimations(targetsArray, animations) {
+ for (var a = animations.length; a--;) {
+ if (arrayContains(targetsArray, animations[a].animatable.target)) {
+ animations.splice(a, 1);
+ }
+ }
+}
+
function removeTargets(targets) {
var targetsArray = parseTargets(targets);
for (var i = activeInstances.length; i--;) {
var instance = activeInstances[i];
var animations = instance.animations;
- for (var a = animations.length; a--;) {
- if (arrayContains(targetsArray, animations[a].animatable.target)) {
- animations.splice(a, 1);
- if (!animations.length) { instance.pause(); }
- }
+ var children = instance.children;
+ removeTargetsFromAnimations(targetsArray, animations);
+ for (var c = children.length; c--;) {
+ var child = children[c];
+ var childAnimations = child.animations;
+ removeTargetsFromAnimations(targetsArray, childAnimations);
+ if (!childAnimations.length && !child.children.length) { children.splice(c, 1); }
}
+ if (!animations.length && !children.length) { instance.pause(); }
}
}
@@ -964,21 +1097,22 @@ function timeline(params) {
var tl = anime(params);
tl.pause();
tl.duration = 0;
- tl.add = function(instanceParams, timeOffset) {
- tl.children.forEach(function (i) { i.began = true; i.completed = true; });
+ tl.add = function(instanceParams, timelineOffset) {
+ function passThrough(ins) { ins.began = true; ins.completed = true; }
+ tl.children.forEach(passThrough);
var insParams = mergeObjects(instanceParams, replaceObjectProps(defaultTweenSettings, params));
insParams.targets = insParams.targets || params.targets;
var tlDuration = tl.duration;
insParams.autoplay = false;
insParams.direction = tl.direction;
- insParams.timeOffset = is.und(timeOffset) ? tlDuration : getRelativeValue(timeOffset, tlDuration);
- tl.began = true;
- tl.completed = true;
- tl.seek(insParams.timeOffset);
+ insParams.timelineOffset = is.und(timelineOffset) ? tlDuration : getRelativeValue(timelineOffset, tlDuration);
+ passThrough(tl);
+ tl.seek(insParams.timelineOffset);
var ins = anime(insParams);
- ins.began = true;
- ins.completed = true;
- if (ins.duration > tlDuration) { tl.duration = ins.duration; }
+ passThrough(ins);
+ var totalDuration = ins.duration + insParams.timelineOffset;
+ if (is.fnc(tl.delay)) { tl.delay = ins.delay; }
+ if (is.fnc(tlDuration) || totalDuration > tlDuration) { tl.duration = totalDuration; }
tl.children.push(ins);
tl.seek(0);
tl.reset();
@@ -992,13 +1126,11 @@ anime.version = '3.0.0';
anime.speed = 1;
anime.running = activeInstances;
anime.remove = removeTargets;
-anime.get = getOriginalTargetValue;
-anime.set = setTargetValue;
+anime.getValue = getOriginalTargetValue;
anime.path = getPath;
anime.setDashoffset = setDashoffset;
-anime.bezier = bezier;
-anime.easings = easings;
anime.timeline = timeline;
+anime.easings = easings;
anime.random = function (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; };
module.exports = anime;
diff --git a/lib/anime.min.js b/lib/anime.min.js
index 5072dccf..f60e8e0f 100644
--- a/lib/anime.min.js
+++ b/lib/anime.min.js
@@ -1,5 +1,7 @@
/*
- 2017 Julian Garnier
+ anime.js v3.0.0
+ © 2018 Julian Garnier
Released under the MIT license
+ animejs.com
*/
-!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):t.animejs=r()}(this,function(){"use strict";var t={update:void 0,begin:void 0,run:void 0,complete:void 0,loop:1,direction:"normal",autoplay:!0,timeOffset:0},r={duration:1e3,delay:0,endDelay:0,easing:"easeOutElastic",elasticity:500,round:0},n=["translateX","translateY","translateZ","rotate","rotateX","rotateY","rotateZ","scale","scaleX","scaleY","scaleZ","skew","skewX","skewY","perspective"];function e(t,r){return t.indexOf(r)>-1}var a={arr:function(t){return Array.isArray(t)},obj:function(t){return e(Object.prototype.toString.call(t),"Object")},pth:function(t){return a.obj(t)&&t.hasOwnProperty("totalLength")},svg:function(t){return t instanceof SVGElement},dom:function(t){return t.nodeType||a.svg(t)},str:function(t){return"string"==typeof t},fnc:function(t){return"function"==typeof t},und:function(t){return void 0===t},hex:function(t){return/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(t)},rgb:function(t){return/^rgb/.test(t)},hsl:function(t){return/^hsl/.test(t)},col:function(t){return a.hex(t)||a.rgb(t)||a.hsl(t)}},i=function(){var t=11,r=1/(t-1);function n(t,r){return 1-3*r+3*t}function e(t,r){return 3*r-6*t}function a(t){return 3*t}function i(t,r,i){return((n(r,i)*t+e(r,i))*t+a(r))*t}function u(t,r,i){return 3*n(r,i)*t*t+2*e(r,i)*t+a(r)}return function(n,e,a,o){if(0<=n&&n<=1&&0<=a&&a<=1){var s=new Float32Array(t);if(n!==e||a!==o)for(var c=0;c<t;++c)s[c]=i(c*r,n,a);return function(t){return n===e&&a===o?t:0===t?0:1===t?1:i(f(t),e,o)}}function f(e){for(var o=0,c=1,f=t-1;c!==f&&s[c]<=e;++c)o+=r;var l=o+(e-s[--c])/(s[c+1]-s[c])*r,d=u(l,n,a);return d>=.001?function(t,r,n,e){for(var a=0;a<4;++a){var o=u(r,n,e);if(0===o)return r;r-=(i(r,n,e)-t)/o}return r}(e,l,n,a):0===d?l:function(t,r,n,e,a){var u,o,s=0;do{(u=i(o=r+(n-r)/2,e,a)-t)>0?n=o:r=o}while(Math.abs(u)>1e-7&&++s<10);return o}(e,o,o+r,n,a)}}}(),u=function(){var t=["Quad","Cubic","Quart","Quint","Sine","Expo","Circ","Back","Elastic"];function r(t,r){return 0===t||1===t?t:-Math.pow(2,10*(t-1))*Math.sin((t-1-r/(2*Math.PI)*Math.asin(1))*(2*Math.PI)/r)}var n={In:[[.55,.085,.68,.53],[.55,.055,.675,.19],[.895,.03,.685,.22],[.755,.05,.855,.06],[.47,0,.745,.715],[.95,.05,.795,.035],[.6,.04,.98,.335],[.6,-.28,.735,.045],r],Out:[[.25,.46,.45,.94],[.215,.61,.355,1],[.165,.84,.44,1],[.23,1,.32,1],[.39,.575,.565,1],[.19,1,.22,1],[.075,.82,.165,1],[.175,.885,.32,1.275],function(t,n){return 1-r(1-t,n)}],InOut:[[.455,.03,.515,.955],[.645,.045,.355,1],[.77,0,.175,1],[.86,0,.07,1],[.445,.05,.55,.95],[1,0,0,1],[.785,.135,.15,.86],[.68,-.55,.265,1.55],function(t,n){return t<.5?r(2*t,n)/2:1-r(-2*t+2,n)/2}]},e={linear:i(.25,.25,.75,.75)},u=function(r){n[r].forEach(function(n,u){e["ease"+r+t[u]]=a.fnc(n)?n:i.apply(null,n)})};for(var o in n)u(o);return e}();function o(t){try{return document.querySelectorAll(t)}catch(t){return}}function s(t,r){for(var n=t.length,e=arguments.length>=2?arguments[1]:void 0,a=[],i=0;i<n;i++)if(i in t){var u=t[i];r.call(e,u,i,t)&&a.push(u)}return a}function c(t){return t.reduce(function(t,r){return t.concat(a.arr(r)?c(r):r)},[])}function f(t){return a.arr(t)?t:(a.str(t)&&(t=o(t)||t),t instanceof NodeList||t instanceof HTMLCollection?[].slice.call(t):[t])}function l(t,r){return t.some(function(t){return t===r})}function d(t){var r={};for(var n in t)r[n]=t[n];return r}function v(t,r){var n=d(t);for(var e in t)n[e]=r.hasOwnProperty(e)?r[e]:t[e];return n}function p(t,r){var n=d(t);for(var e in r)n[e]=a.und(t[e])?r[e]:t[e];return n}function g(t){return a.rgb(t)?function(t){var r=/rgb\((\d+,\s*[\d]+,\s*[\d]+)\)/g.exec(t);return r?"rgba("+r[1]+",1)":t}(t):a.hex(t)?function(t){var r=t.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(t,r,n,e){return r+r+n+n+e+e}),n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(r);return"rgba("+parseInt(n[1],16)+","+parseInt(n[2],16)+","+parseInt(n[3],16)+",1)"}(t):a.hsl(t)?function(t){var r=/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.exec(t)||/hsla\((\d+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)/g.exec(t),n=parseInt(r[1],10)/360,e=parseInt(r[2],10)/100,a=parseInt(r[3],10)/100,i=r[4]||1;function u(t,r,n){return n<0&&(n+=1),n>1&&(n-=1),n<1/6?t+6*(r-t)*n:n<.5?r:n<2/3?t+(r-t)*(2/3-n)*6:t}var o,s,c;if(0==e)o=s=c=a;else{var f=a<.5?a*(1+e):a+e-a*e,l=2*a-f;o=u(l,f,n+1/3),s=u(l,f,n),c=u(l,f,n-1/3)}return"rgba("+255*o+","+255*s+","+255*c+","+i+")"}(t):void 0}function m(t){var r=/([\+\-]?[0-9#\.]+)(%|px|pt|em|rem|in|cm|mm|ex|ch|pc|vw|vh|vmin|vmax|deg|rad|turn)?$/.exec(t);if(r)return r[2]}function h(t,r,n){return Math.min(Math.max(t,r),n)}function y(t,r){return a.fnc(t)?t(r.target,r.id,r.total):t}function b(t,r){if(r in t.style)return t.style[r]||getComputedStyle(t).getPropertyValue((n=r,n.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()))||"0";var n}function x(t,r){return a.dom(t)&&l(n,r)?"transform":a.dom(t)&&(t.getAttribute(r)||a.svg(t)&&t[r])?"attribute":a.dom(t)&&"transform"!==r&&b(t,r)?"css":null!=t[r]?"object":void 0}function w(t){if(a.dom(t)){for(var r,n=t.style.transform||"",e=/(\w+)\(([^)]*)\)/g,i=new Map;r=e.exec(n);)i.set(r[1],r[2]);return i}}function M(t,r,n){var a,i=e(r,"scale")?1:0+(a=r,e(a,"translate")||"perspective"===a?"px":e(a,"rotate")||e(a,"skew")?"deg":void 0),u=w(t).get(r)||i;return n.transforms.list.set(r,u),n.transforms.last=r,u}function A(t,r,n){switch(x(t,r)){case"transform":return M(t,r,n);case"css":return b(t,r);case"attribute":return t.getAttribute(r);default:return t[r]||0}}function O(t,r){var n=/^(\*=|\+=|-=)/.exec(t);if(!n)return t;var e=m(t)||0,a=parseFloat(r),i=parseFloat(t.replace(n[0],""));switch(n[0][0]){case"+":return a+i+e;case"-":return a-i+e;case"*":return a*i+e}}function I(t,r){if(a.col(t))return g(t);var n=m(t),e=n?t.substr(0,t.length-n.length):t;return r&&!/\s/g.test(t)?e+r:e}function P(t,r){return Math.sqrt(Math.pow(r.x-t.x,2)+Math.pow(r.y-t.y,2))}function k(t){for(var r,n=t.points,e=0,a=0;a<n.numberOfItems;a++){var i=n.getItem(a);a>0&&(e+=P(r,i)),r=i}return e}function j(t){if(t.getTotalLength)return t.getTotalLength();switch(t.tagName.toLowerCase()){case"circle":return e=t,2*Math.PI*e.getAttribute("r");case"rect":return 2*(n=t).getAttribute("width")+2*n.getAttribute("height");case"line":return P({x:(r=t).getAttribute("x1"),y:r.getAttribute("y1")},{x:r.getAttribute("x2"),y:r.getAttribute("y2")});case"polyline":return k(t);case"polygon":return function(t){var r=t.points;return k(t)+P(r.getItem(r.numberOfItems-1),r.getItem(0))}(t)}var r,n,e}function L(t,r){function n(n){void 0===n&&(n=0);var e=r+n>=1?r+n:0;return t.el.getPointAtLength(e)}var e=n(),a=n(-1),i=n(1);switch(t.property){case"x":return e.x;case"y":return e.y;case"angle":return 180*Math.atan2(i.y-a.y,i.x-a.x)/Math.PI}}function T(t,r){var n=/-?\d*\.?\d+/g,e=I(a.pth(t)?t.totalLength:t,r)+"";return{original:e,numbers:e.match(n)?e.match(n).map(Number):[0],strings:a.str(t)||r?e.split(n):[]}}function C(t){return s(t?c(a.arr(t)?t.map(f):f(t)):[],function(t,r,n){return n.indexOf(t)===r})}function F(t){var r=C(t);return r.map(function(t,n){return{target:t,id:n,total:r.length,transforms:{list:w(t)}}})}function E(t,r){var n=d(r);if(a.arr(t)){var e=t.length;2===e&&!a.obj(t[0])?t={value:t}:a.fnc(r.duration)||(n.duration=r.duration/e)}return(a.arr(t)?t:[t]).map(function(t,n){var e=n?0:r.delay,i=a.obj(t)&&!a.pth(t)?t:{value:t};return a.und(i.delay)&&(i.delay=e),i}).map(function(t){return p(t,n)})}function N(t,r){var n;return t.tweens.map(function(e){var o=function(t,r){var n={};for(var e in t){var i=y(t[e],r);a.arr(i)&&1===(i=i.map(function(t){return y(t,r)})).length&&(i=i[0]),n[e]=i}return n.duration=parseFloat(n.duration),n.delay=parseFloat(n.delay),n}(e,r),s=o.value,c=A(r.target,t.name,r),f=n?n.to.original:c,l=a.arr(s)?s[0]:f,d=O(a.arr(s)?s[1]:s,l),v=m(d)||m(l)||m(c);return o.from=T(l,v),o.to=T(d,v),o.start=n?n.end:t.offset,o.end=o.start+o.delay+o.duration+o.endDelay,o.easing=function(t){return a.arr(t)?i.apply(this,t):u[t]}(o.easing),o.elasticity=(1e3-h(o.elasticity,1,999))/1e3,o.isPath=a.pth(s),o.isColor=a.col(o.from.original),o.isColor&&(o.round=1),n=o,o})}var $={css:function(t,r,n){return t.style[r]=n},attribute:function(t,r,n){return t.setAttribute(r,n)},object:function(t,r,n){return t[r]=n},transform:function(t,r,n,e,a){if(e.list.set(r,n),r===e.last||a){for(var i="",u=0,o=e.list;u<o.length;u+=1){var s=o[u];i+=s[0]+"("+s[1]+") "}t.style.transform=i}}};function S(t,r){return s(c(t.map(function(t){return r.map(function(r){return function(t,r){var n=x(t.target,r.name);if(n){var e=N(r,t);return{type:n,property:r.name,animatable:t,tweens:e,duration:e[e.length-1].end,delay:e[0].delay}}}(t,r)})})),function(t){return!a.und(t)})}function X(t,r,n,e){var a="delay"===t;return r.length?(a?Math.min:Math.max).apply(Math,r.map(function(r){return r[t]})):a?e.delay:n.timeOffset+e.delay+e.duration}function Y(n){var e=v(t,n),a=v(r,n),i=F(n.targets),u=S(i,function(t,r,n){var e=[],a=p(t,r);for(var i in n)a.hasOwnProperty(i)||"targets"===i||e.push({name:i,offset:a.timeOffset,tweens:E(n[i],r)});return e}(e,a,n));return p(e,{children:[],animatables:i,animations:u,duration:X("duration",u,e,a),delay:X("delay",u,e,a)})}var Z=[],q=[],D=0,Q=function(){function t(){D=requestAnimationFrame(r)}function r(r){if(q.length&&(Z=s(Z,function(t){return!q.includes(t)}),q=[]),Z.length){for(var n=0;n<Z.length;){var e=Z[n];e.paused?q.push(e):Z[n].tick(r),n++}t()}else cancelAnimationFrame(D),D=0}return t}();function V(t){void 0===t&&(t={});var r,n,e=0,a=null;function i(){return window.Promise&&new Promise(function(t){return a=t})}var u=i(),o=Y(t);function c(){o.reversed=!o.reversed;o.children.forEach(function(t){return t.reversed=o.reversed})}function f(t){return o.reversed?o.duration-t:t}function l(t){for(var r=0,n=o.animations,e=n.length;r<e;){var a=n[r],i=a.animatable,u=a.tweens,c=u.length-1,f=u[c];c&&(f=s(u,function(r){return t<r.end})[0]||f);for(var l=h(t-f.start-f.delay,0,f.duration)/f.duration,d=isNaN(l)?1:f.easing(l,f.elasticity),v=f.to.strings,p=f.round,g=[],m=void 0,y=f.to.numbers.length,b=0;b<y;b++){var x=void 0,w=f.to.numbers[b],M=f.from.numbers[b];x=f.isPath?L(f.value,d*w):M+d*(w-M),p&&(f.isColor&&b>2||(x=Math.round(x*p)/p)),g.push(x)}var A=v.length;if(A){m=v[0];for(var O=0;O<A;O++){v[O];var I=v[O+1],P=g[O];isNaN(P)||(m+=I?P+I:P+" ")}}else m=g[0];$[a.type](i.target,a.property,m,i.transforms),a.currentValue=m,r++}o.currentTime=t,o.progress=t/o.duration*100}function d(t){o[t]&&o[t](o)}function v(){o.remaining&&!0!==o.remaining&&o.remaining--}function p(t,s){var p=o.duration,g=o.timeOffset,m=g+o.delay,h=o.currentTime,y=o.reversed,b=f(t);o.children.length&&function(t,r){var n=o.children,e=n.length;if(!r||t>=o.currentTime)for(var a=0;a<e;a++)n[a].seek(t);else for(var i=e;i--;)n[i].seek(t)}(b,s),(b>=m||!p)&&(o.began||(o.began=!0,d("begin")),d("run")),b>g&&b<p?l(b):(b<=g&&0!==h&&(l(0),y&&v()),(b>=p&&h!==p||!p)&&(l(p),y||v())),d("update"),t>=p&&(o.remaining?(n=r,"alternate"===o.direction&&c()):(o.paused=!0,o.completed||(o.completed=!0,d("complete"),"Promise"in window&&(a(),u=i()))),e=0)}return o.reset=function(){var t=o.direction,r=o.loop;o.currentTime=0,o.progress=0,o.paused=!0,o.began=!1,o.completed=!1,o.reversed="reverse"===t,o.remaining="alternate"===t&&1===r?2:r,l(0);for(var n=o.children.length;n--;)o.children[n].reset()},o.tick=function(t){r=t,n||(n=r);p((e+r-n)*V.speed)},o.seek=function(t){p(f(t),!0)},o.pause=function(){o.paused=!0;var t=Z.indexOf(o);t>-1&&Z.splice(t,1)},o.play=function(){o.paused&&(o.paused=!1,n=0,e=f(o.currentTime),Z.push(o),D||Q())},o.reverse=function(){c(),n=0,e=f(o.currentTime)},o.restart=function(){o.pause(),o.reset(),o.play()},o.finished=u,o.reset(),o.autoplay&&o.play(),o}return V.version="3.0.0",V.speed=1,V.running=Z,V.remove=function(t){for(var r=C(t),n=Z.length;n--;)for(var e=Z[n],a=e.animations,i=a.length;i--;)l(r,a[i].animatable.target)&&(a.splice(i,1),a.length||e.pause())},V.get=A,V.set=function(t,r){F(t).forEach(function(t){for(var n in r){var e=r[n],a=A(t.target,n,t),i=O(I(e,m(e)||m(a)),a),u=x(t.target,n);$[u](t.target,n,i,t.transforms,!0)}})},V.path=function(t,r){var n=a.str(t)?o(t)[0]:t,e=r||100;return function(t){return{el:n,property:t,totalLength:j(n)*(e/100)}}},V.setDashoffset=function(t){var r=j(t);return t.setAttribute("stroke-dasharray",r),r},V.bezier=i,V.easings=u,V.timeline=function(t){void 0===t&&(t={});var n=V(t);return n.pause(),n.duration=0,n.add=function(e,i){n.children.forEach(function(t){t.began=!0,t.completed=!0});var u=p(e,v(r,t));u.targets=u.targets||t.targets;var o=n.duration;u.autoplay=!1,u.direction=n.direction,u.timeOffset=a.und(i)?o:O(i,o),n.began=!0,n.completed=!0,n.seek(u.timeOffset);var s=V(u);return s.began=!0,s.completed=!0,s.duration>o&&(n.duration=s.duration),n.children.push(s),n.seek(0),n.reset(),n.autoplay&&n.restart(),n},n},V.random=function(t,r){return Math.floor(Math.random()*(r-t+1))+t},V});
\ No newline at end of file
+!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.anime=n()}(this,function(){"use strict";var t={update:null,begin:null,run:null,complete:null,loop:1,direction:"normal",autoplay:!0,startTime:0,timelineOffset:0},n={duration:1e3,delay:0,endDelay:0,easing:"easeOutElastic(1, .5)",round:0},r=["translateX","translateY","translateZ","rotate","rotateX","rotateY","rotateZ","scale","scaleX","scaleY","scaleZ","skew","skewX","skewY","perspective"],e={CSS:{},springs:{}};function a(t,n){return t.indexOf(n)>-1}function i(t,n){return t.apply(null,n)}var u={arr:function(t){return Array.isArray(t)},obj:function(t){return a(Object.prototype.toString.call(t),"Object")},pth:function(t){return u.obj(t)&&t.hasOwnProperty("totalLength")},svg:function(t){return t instanceof SVGElement},dom:function(t){return t.nodeType||u.svg(t)},str:function(t){return"string"==typeof t},fnc:function(t){return"function"==typeof t},und:function(t){return void 0===t},hex:function(t){return/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(t)},rgb:function(t){return/^rgb/.test(t)},hsl:function(t){return/^hsl/.test(t)},col:function(t){return u.hex(t)||u.rgb(t)||u.hsl(t)}};function o(t){var n=/\(([^)]+)\)/.exec(t);return n?n[1].split(",").map(function(t){return parseFloat(t)}):[]}function s(t,n){var r=o(t),a=O(u.und(r[0])?1:r[0],.1,100),i=O(u.und(r[1])?100:r[1],.1,100),s=O(u.und(r[2])?10:r[2],.1,100),c=O(u.und(r[3])?0:r[3],.1,100),f=Math.sqrt(i/a),l=s/(2*Math.sqrt(i*a)),d=l<1?f*Math.sqrt(1-l*l):0,v=1,p=l<1?(l*f-c)/d:-c+f;function m(t){var r=n?n*t/1e3:t;return r=l<1?Math.exp(-r*l*f)*(v*Math.cos(d*r)+p*Math.sin(d*r)):(v+p*r)*Math.exp(-r*f),0===t||1===t?t:1-r}return n?m:function(){var n=e.springs[t];if(n)return n;for(var r=0,a=0;;)if(1===m(r+=1/6)){if(++a>=16)break}else a=0;var i=r*(1/6)*1e3;return e.springs[t]=i,i}}function c(t,n){void 0===t&&(t=1),void 0===n&&(n=.5);var r=O(t,1,10),e=O(n,.1,2);return function(t){return 0===t||1===t?t:-r*Math.pow(2,10*(t-1))*Math.sin((t-1-e/(2*Math.PI)*Math.asin(1/r))*(2*Math.PI)/e)}}function f(t){return void 0===t&&(t=10),function(n){return Math.round(n*t)*(1/t)}}var l=function(){var t=11,n=1/(t-1);function r(t,n){return 1-3*n+3*t}function e(t,n){return 3*n-6*t}function a(t){return 3*t}function i(t,n,i){return((r(n,i)*t+e(n,i))*t+a(n))*t}function u(t,n,i){return 3*r(n,i)*t*t+2*e(n,i)*t+a(n)}return function(r,e,a,o){if(0<=r&&r<=1&&0<=a&&a<=1){var s=new Float32Array(t);if(r!==e||a!==o)for(var c=0;c<t;++c)s[c]=i(c*n,r,a);return function(t){return r===e&&a===o?t:0===t||1===t?t:i(f(t),e,o)}}function f(e){for(var o=0,c=1,f=t-1;c!==f&&s[c]<=e;++c)o+=n;var l=o+(e-s[--c])/(s[c+1]-s[c])*n,d=u(l,r,a);return d>=.001?function(t,n,r,e){for(var a=0;a<4;++a){var o=u(n,r,e);if(0===o)return n;n-=(i(n,r,e)-t)/o}return n}(e,l,r,a):0===d?l:function(t,n,r,e,a){var u,o,s=0;do{(u=i(o=n+(r-n)/2,e,a)-t)>0?r=o:n=o}while(Math.abs(u)>1e-7&&++s<10);return o}(e,o,o+n,r,a)}}}(),d=function(){var t=["Quad","Cubic","Quart","Quint","Sine","Expo","Circ","Back","Elastic"],n={In:[[.55,.085,.68,.53],[.55,.055,.675,.19],[.895,.03,.685,.22],[.755,.05,.855,.06],[.47,0,.745,.715],[.95,.05,.795,.035],[.6,.04,.98,.335],[.6,-.28,.735,.045],c],Out:[[.25,.46,.45,.94],[.215,.61,.355,1],[.165,.84,.44,1],[.23,1,.32,1],[.39,.575,.565,1],[.19,1,.22,1],[.075,.82,.165,1],[.175,.885,.32,1.275],function(t,n){return function(r){return 1-c(t,n)(1-r)}}],InOut:[[.455,.03,.515,.955],[.645,.045,.355,1],[.77,0,.175,1],[.86,0,.07,1],[.445,.05,.55,.95],[1,0,0,1],[.785,.135,.15,.86],[.68,-.55,.265,1.55],function(t,n){return function(r){return r<.5?c(t,n)(2*r)/2:1-c(t,n)(-2*r+2)/2}}]},r={linear:[.25,.25,.75,.75]},e=function(e){n[e].forEach(function(n,a){r["ease"+e+t[a]]=n})};for(var a in n)e(a);return r}();function v(t){try{return document.querySelectorAll(t)}catch(t){return}}function p(t,n){for(var r=t.length,e=arguments.length>=2?arguments[1]:void 0,a=[],i=0;i<r;i++)if(i in t){var u=t[i];n.call(e,u,i,t)&&a.push(u)}return a}function m(t){return t.reduce(function(t,n){return t.concat(u.arr(n)?m(n):n)},[])}function h(t){return u.arr(t)?t:(u.str(t)&&(t=v(t)||t),t instanceof NodeList||t instanceof HTMLCollection?[].slice.call(t):[t])}function g(t,n){return t.some(function(t){return t===n})}function y(t){var n={};for(var r in t)n[r]=t[r];return n}function b(t,n){var r=y(t);for(var e in t)r[e]=n.hasOwnProperty(e)?n[e]:t[e];return r}function x(t,n){var r=y(t);for(var e in n)r[e]=u.und(t[e])?n[e]:t[e];return r}function w(t){return u.rgb(t)?function(t){var n=/rgb\((\d+,\s*[\d]+,\s*[\d]+)\)/g.exec(t);return n?"rgba("+n[1]+",1)":t}(t):u.hex(t)?function(t){var n=t.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(t,n,r,e){return n+n+r+r+e+e}),r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(n);return"rgba("+parseInt(r[1],16)+","+parseInt(r[2],16)+","+parseInt(r[3],16)+",1)"}(t):u.hsl(t)?function(t){var n=/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.exec(t)||/hsla\((\d+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)/g.exec(t),r=parseInt(n[1],10)/360,e=parseInt(n[2],10)/100,a=parseInt(n[3],10)/100,i=n[4]||1;function u(t,n,r){return r<0&&(r+=1),r>1&&(r-=1),r<1/6?t+6*(n-t)*r:r<.5?n:r<2/3?t+(n-t)*(2/3-r)*6:t}var o,s,c;if(0==e)o=s=c=a;else{var f=a<.5?a*(1+e):a+e-a*e,l=2*a-f;o=u(l,f,r+1/3),s=u(l,f,r),c=u(l,f,r-1/3)}return"rgba("+255*o+","+255*s+","+255*c+","+i+")"}(t):void 0}function M(t){var n=/([\+\-]?[0-9#\.]+)(%|px|pt|em|rem|in|cm|mm|ex|ch|pc|vw|vh|vmin|vmax|deg|rad|turn)?$/.exec(t);if(n)return n[2]}function O(t,n,r){return Math.min(Math.max(t,n),r)}function I(t,n){return u.fnc(t)?t(n.target,n.id,n.total):t}function T(t,n){return t.getAttribute(n)}function k(t,n,r){if(g([r,"deg","rad","turn"],M(n)))return n;var a=e.CSS[n+r];if(!u.und(a))return a;var i=document.createElement(t.tagName),o=t.parentNode||document.body;o.appendChild(i),i.style.position="absolute",i.style.width=100+r;var s=100/i.offsetWidth;o.removeChild(i);var c=s*parseFloat(n);return e.CSS[n+r]=c,c}function P(t,n,r){if(n in t.style){var e=n.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),a=t.style[n]||getComputedStyle(t).getPropertyValue(e)||"0";return r?k(t,a,r):a}}function C(t,n){return u.dom(t)&&(T(t,n)||u.svg(t)&&t[n])?"attribute":u.dom(t)&&g(r,n)?"transform":u.dom(t)&&"transform"!==n&&P(t,n)?"css":null!=t[n]?"object":void 0}function A(t){if(u.dom(t)){for(var n,r=t.style.transform||"",e=/(\w+)\(([^)]*)\)/g,a=new Map;n=e.exec(r);)a.set(n[1],n[2]);return a}}function E(t,n,r,e){var i,u=a(n,"scale")?1:0+(i=n,a(i,"translate")||"perspective"===i?"px":a(i,"rotate")||a(i,"skew")?"deg":void 0),o=A(t).get(n)||u;return r.transforms.list.set(n,o),r.transforms.last=n,e?k(t,o,e):o}function F(t,n,r,e){switch(C(t,n)){case"transform":return E(t,n,e,r);case"css":return P(t,n,r);case"attribute":return T(t,n);default:return t[n]||0}}function L(t,n){var r=/^(\*=|\+=|-=)/.exec(t);if(!r)return t;var e=M(t)||0,a=parseFloat(n),i=parseFloat(t.replace(r[0],""));switch(r[0][0]){case"+":return a+i+e;case"-":return a-i+e;case"*":return a*i+e}}function S(t,n){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))}function j(t){for(var n,r=t.points,e=0,a=0;a<r.numberOfItems;a++){var i=r.getItem(a);a>0&&(e+=S(n,i)),n=i}return e}function N(t){if(t.getTotalLength)return t.getTotalLength();switch(t.tagName.toLowerCase()){case"circle":return e=t,2*Math.PI*T(e,"r");case"rect":return 2*T(r=t,"width")+2*T(r,"height");case"line":return S({x:T(n=t,"x1"),y:T(n,"y1")},{x:T(n,"x2"),y:T(n,"y2")});case"polyline":return j(t);case"polygon":return function(t){var n=t.points;return j(t)+S(n.getItem(n.numberOfItems-1),n.getItem(0))}(t)}var n,r,e}function $(t,n){function r(r){void 0===r&&(r=0);var e=n+r>=1?n+r:0;return t.el.getPointAtLength(e)}var e=r(),a=r(-1),i=r(1);switch(t.property){case"x":return e.x;case"y":return e.y;case"angle":return 180*Math.atan2(i.y-a.y,i.x-a.x)/Math.PI}}function q(t,n){var r=/-?\d*\.?\d+/g,e=function(t,n){if(u.col(t))return w(t);var r=M(t),e=r?t.substr(0,t.length-r.length):t;return n&&!/\s/g.test(t)?e+n:e}(u.pth(t)?t.totalLength:t,n)+"";return{original:e,numbers:e.match(r)?e.match(r).map(Number):[0],strings:u.str(t)||n?e.split(r):[]}}function V(t){return p(t?m(u.arr(t)?t.map(h):h(t)):[],function(t,n,r){return r.indexOf(t)===n})}function X(t,n){var r=y(n);if(/^spring/.test(r.easing)&&(r.duration=s(r.easing)),u.arr(t)){var e=t.length;2===e&&!u.obj(t[0])?t={value:t}:u.fnc(n.duration)||(r.duration=n.duration/e)}return(u.arr(t)?t:[t]).map(function(t,r){var e=u.obj(t)&&!u.pth(t)?t:{value:t};return u.und(e.delay)&&(e.delay=r?0:n.delay),e}).map(function(t){return x(t,r)})}function Y(t,n){var r;return t.tweens.map(function(e){var a=function(t,n){var r={};for(var e in t){var a=I(t[e],n);u.arr(a)&&1===(a=a.map(function(t){return I(t,n)})).length&&(a=a[0]),r[e]=a}return r.duration=parseFloat(r.duration),r.delay=parseFloat(r.delay),r}(e,n),c=a.value,v=u.arr(c)?c[1]:c,p=M(v),m=F(n.target,t.name,p,n),h=r?r.to.original:m,g=u.arr(c)?c[0]:h,y=M(g)||M(m),b=p||y;return a.from=q(g,b),a.to=q(L(v,g),b),a.start=r?r.end:0,a.end=a.start+a.delay+a.duration+a.endDelay,a.easing=function(t){var n=t.easing,r=n.split("(")[0],e=o(n),a=d[r];switch(r){case"spring":return s(n,t.duration);case"cubicBezier":return i(l,e);case"steps":return i(f,e);default:return u.fnc(a)?i(a,e):i(l,a)}}(a),a.isPath=u.pth(c),a.isColor=u.col(a.from.original),a.isColor&&(a.round=1),r=a,a})}var Z={css:function(t,n,r){return t.style[n]=r},attribute:function(t,n,r){return t.setAttribute(n,r)},object:function(t,n,r){return t[n]=r},transform:function(t,n,r,e,a){if(e.list.set(n,r),n===e.last||a){var i="";e.list.forEach(function(t,n){i+=n+"("+t+") "}),t.style.transform=i}}};function D(t,n){return p(m(t.map(function(t){return n.map(function(n){return function(t,n){var r=C(t.target,n.name);if(r){var e=Y(n,t);return{type:r,property:n.name,animatable:t,tweens:e,duration:e[e.length-1].end,delay:e[0].delay}}}(t,n)})})),function(t){return!u.und(t)})}function Q(t,n,r){var e="delay"===t;return n.length?(e?Math.min:Math.max).apply(Math,n.map(function(n){return n[t]})):e?r.delay:r.delay+r.duration}function z(r){var e=b(t,r),a=b(n,r),i=function(t){var n=V(t);return n.map(function(t,r){return{target:t,id:r,total:n.length,transforms:{list:A(t)}}})}(r.targets),u=D(i,function(t,n,r){var e=[];for(var a in r)t.hasOwnProperty(a)||n.hasOwnProperty(a)||"targets"===a||e.push({name:a,tweens:X(r[a],n)});return e}(e,a,r));return x(e,{states:{default:r},children:[],animatables:i,animations:u,duration:Q("duration",u,a),delay:Q("delay",u,a)})}var B,G=[],H=[],W=function(){function t(){B=requestAnimationFrame(n)}function n(n){var r=G.length;if(r){for(var e=0;e<r;){var a=G[e];if(a.paused){var i=G.indexOf(a);i>-1&&(G.splice(i,1),r=G.length)}else a.tick(n);e++}t()}else B=cancelAnimationFrame(B)}return t}();document.addEventListener("visibilitychange",function(){document.hidden?(G.forEach(function(t){return t.pause()}),H=G.slice(0),G=[]):H.forEach(function(t){return t.play()})});function J(t){void 0===t&&(t={});var n,r=0,e=0,a=0,i=0,u=null;function o(){return window.Promise&&new Promise(function(t){return u=t})}var s=o(),c=z(t);function f(){c.reversed=!c.reversed,n.forEach(function(t){return t.reversed=c.reversed})}function l(){r=0,e=d(c.currentTime)*(1/J.speed)}function d(t){return c.reversed?c.duration-t:t}function v(t){for(var n=0,r=c.animations,e=r.length;n<e;){var a=r[n],i=a.animatable,u=a.tweens,o=u.length-1,s=u[o];o&&(s=p(u,function(n){return t<n.end})[0]||s);for(var f=O(t-s.start-s.delay,0,s.duration)/s.duration,l=isNaN(f)?1:s.easing(f),d=s.to.strings,v=s.round,m=[],h=s.to.numbers.length,g=void 0,y=0;y<h;y++){var b=void 0,x=s.to.numbers[y],w=s.from.numbers[y];b=s.isPath?$(s.value,l*x):w+l*(x-w),v&&(s.isColor&&y>2||(b=Math.round(b*v)/v)),m.push(b)}var M=d.length;if(M){g=d[0];for(var I=0;I<M;I++){d[I];var T=d[I+1],k=m[I];isNaN(k)||(g+=T?k+T:k+" ")}}else g=m[0];Z[a.type](i.target,a.property,g,i.transforms),a.currentValue=g,n++}c.currentTime=t}function m(t){c[t]&&c[t](c)}function h(){c.remaining&&!0!==c.remaining&&c.remaining--}function g(t){var l=c.duration,p=c.delay,g=d(t);c.progress=O(g/l*100,0,100),n&&function(t){if(t>=c.currentTime)for(var r=0;r<i;r++)n[r].seek(t-n[r].timelineOffset);else for(var e=i;e--;)n[e].seek(t-n[e].timelineOffset)}(g),(g>=p||!l)&&(c.began||(c.began=!0,m("begin")),m("run")),g>p&&g<l?v(g):(g<=p&&0!==c.currentTime&&(v(0),c.reversed&&h()),(g>=l&&c.currentTime!==l||!l)&&(v(l),c.reversed||h())),m("update"),t>=l&&(e=0,c.remaining?(r=a,"alternate"===c.direction&&f()):(c.paused=!0,c.completed||(c.completed=!0,m("complete"),"Promise"in window&&(u(),s=o()))))}return c.reset=function(){var t=c.direction,r=c.loop;c.currentTime=0,c.progress=0,c.paused=!0,c.began=!1,c.completed=!1,c.reversed="reverse"===t,c.remaining="alternate"===t&&1===r?2:r,n=c.children;for(var e=i=n.length;e--;)c.children[e].reset();v(0)},c.tick=function(t){a=t,r||(r=a),g((a+(e-r))*J.speed)},c.seek=function(t){g(d(t))},c.pause=function(){c.paused=!0},c.play=function(){c.paused&&(c.paused=!1,l(),G.push(c),B||W())},c.reverse=function(){f(),l()},c.restart=function(){c.pause(),c.reset(),c.play()},c.animateTo=function(t,n,r){void 0===n&&(n={});var e=J(x(n,x(c.states[t],c.states.default)));r?e.seek(e.duration):e.play()},c.goTo=function(t,n){c.animateTo(t,n,!0)},c.finished=s,c.reset(),c.startTime&&g(c.startTime-c.duration*Math.floor(c.startTime/c.duration)),c.autoplay&&c.play(),c}function K(t,n){for(var r=n.length;r--;)g(t,n[r].animatable.target)&&n.splice(r,1)}return J.version="3.0.0",J.speed=1,J.running=G,J.remove=function(t){for(var n=V(t),r=G.length;r--;){var e=G[r],a=e.animations,i=e.children;K(n,a);for(var u=i.length;u--;){var o=i[u],s=o.animations;K(n,s),s.length||o.children.length||i.splice(u,1)}a.length||i.length||e.pause()}},J.getValue=F,J.path=function(t,n){var r=u.str(t)?v(t)[0]:t,e=n||100;return function(t){return{el:r,property:t,totalLength:N(r)*(e/100)}}},J.setDashoffset=function(t){var n=N(t);return t.setAttribute("stroke-dasharray",n),n},J.timeline=function(t){void 0===t&&(t={});var r=J(t);return r.pause(),r.duration=0,r.add=function(e,a){function i(t){t.began=!0,t.completed=!0}r.children.forEach(i);var o=x(e,b(n,t));o.targets=o.targets||t.targets;var s=r.duration;o.autoplay=!1,o.direction=r.direction,o.timelineOffset=u.und(a)?s:L(a,s),i(r),r.seek(o.timelineOffset);var c=J(o);i(c);var f=c.duration+o.timelineOffset;return u.fnc(r.delay)&&(r.delay=c.delay),(u.fnc(s)||f>s)&&(r.duration=f),r.children.push(c),r.seek(0),r.reset(),r.autoplay&&r.restart(),r},r},J.easings=d,J.random=function(t,n){return Math.floor(Math.random()*(n-t+1))+t},J});
\ No newline at end of file
diff --git a/package.json b/package.json
index 78ec01b6..23a9699e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "animejs",
- "version": "2.2.0",
+ "version": "3.0.0",
"homepage": "http://animejs.com",
"repository": "juliangarnier/anime",
"description": "JavaScript animation engine",
diff --git a/src/index.js b/src/index.js
index 9e38e752..cd0a0aad 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,33 +1,44 @@
// Defaults
const defaultInstanceSettings = {
- update: undefined,
- begin: undefined,
- run: undefined,
- complete: undefined,
+ update: null,
+ begin: null,
+ run: null,
+ complete: null,
loop: 1,
direction: 'normal',
autoplay: true,
- timeOffset: 0
+ startTime: 0,
+ timelineOffset: 0
}
const defaultTweenSettings = {
duration: 1000,
delay: 0,
endDelay: 0,
- easing: 'easeOutElastic',
- elasticity: 500,
+ easing: 'easeOutElastic(1, .5)',
round: 0
}
const validTransforms = ['translateX', 'translateY', 'translateZ', 'rotate', 'rotateX', 'rotateY', 'rotateZ', 'scale', 'scaleX', 'scaleY', 'scaleZ', 'skew', 'skewX', 'skewY', 'perspective'];
+// Caching
+
+const cache = {
+ CSS: {},
+ springs: {}
+}
+
// Utils
function stringContains(str, text) {
return str.indexOf(text) > -1;
}
+function applyArguments(func, args) {
+ return func.apply(null, args);
+}
+
const is = {
arr: a => Array.isArray(a),
obj: a => stringContains(Object.prototype.toString.call(a), 'Object'),
@@ -43,6 +54,78 @@ const is = {
col: a => (is.hex(a) || is.rgb(a) || is.hsl(a))
}
+function parseEasingParameters(string) {
+ const match = /\(([^)]+)\)/.exec(string);
+ return match ? match[1].split(',').map(p => parseFloat(p)) : [];
+}
+
+// Spring solver inspired by Webkit Copyright © 2016 Apple Inc. All rights reserved. https://webkit.org/demos/spring/spring.js
+
+function spring(string, duration) {
+
+ const params = parseEasingParameters(string);
+ const mass = minMaxValue(is.und(params[0]) ? 1 : params[0], .1, 100);
+ const stiffness = minMaxValue(is.und(params[1]) ? 100 : params[1], .1, 100);
+ const damping = minMaxValue(is.und(params[2]) ? 10 : params[2], .1, 100);
+ const velocity = minMaxValue(is.und(params[3]) ? 0 : params[3], .1, 100);
+ const w0 = Math.sqrt(stiffness / mass);
+ const zeta = damping / (2 * Math.sqrt(stiffness * mass));
+ const wd = zeta < 1 ? w0 * Math.sqrt(1 - zeta * zeta) : 0;
+ const a = 1;
+ const b = zeta < 1 ? (zeta * w0 + -velocity) / wd : -velocity + w0;
+
+ function solver(t) {
+ let progress = duration ? (duration * t) / 1000 : t;
+ if (zeta < 1) {
+ progress = Math.exp(-progress * zeta * w0) * (a * Math.cos(wd * progress) + b * Math.sin(wd * progress));
+ } else {
+ progress = (a + b * progress) * Math.exp(-progress * w0);
+ }
+ if (t === 0 || t === 1) return t;
+ return 1 - progress;
+ }
+
+ function getDuration() {
+ const cached = cache.springs[string];
+ if (cached) return cached;
+ const frame = 1/6;
+ let elapsed = 0;
+ let rest = 0;
+ while(true) {
+ elapsed += frame;
+ if (solver(elapsed) === 1) {
+ rest++;
+ if (rest >= 16) break;
+ } else {
+ rest = 0;
+ }
+ }
+ const duration = elapsed * frame * 1000;
+ cache.springs[string] = duration;
+ return duration;
+ }
+
+ return duration ? solver : getDuration;
+
+}
+
+// Elastic easing adapted from jQueryUI http://api.jqueryui.com/easings/
+
+function elastic(amplitude = 1, period = .5) {
+ const a = minMaxValue(amplitude, 1, 10);
+ const p = minMaxValue(period, .1, 2);
+ return t => {
+ return (t === 0 || t === 1) ? t :
+ -a * Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (p / (Math.PI * 2) * Math.asin(1 / a))) * (Math.PI * 2)) / p);
+ }
+}
+
+// Basic steps easing implementation https://developer.mozilla.org/fr/docs/Web/CSS/transition-timing-function
+
+function steps(steps = 10) {
+ return t => Math.round(t * steps) * (1 / steps);
+}
+
// BezierEasing https://github.com/gre/bezier-easing
const bezier = (() => {
@@ -50,14 +133,14 @@ const bezier = (() => {
const kSplineTableSize = 11;
const kSampleStepSize = 1.0 / (kSplineTableSize - 1.0);
- function A (aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1 };
- function B (aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1 };
- function C (aA1) { return 3.0 * aA1 };
+ function A(aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1 };
+ function B(aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1 };
+ function C(aA1) { return 3.0 * aA1 };
- function calcBezier (aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT };
- function getSlope (aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1) };
+ function calcBezier(aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT };
+ function getSlope(aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1) };
- function binarySubdivide (aX, aA, aB, mX1, mX2) {
+ function binarySubdivide(aX, aA, aB, mX1, mX2) {
let currentX, currentT, i = 0;
do {
currentT = aA + (aB - aA) / 2.0;
@@ -67,7 +150,7 @@ const bezier = (() => {
return currentT;
}
- function newtonRaphsonIterate (aX, aGuessT, mX1, mX2) {
+ function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
for (let i = 0; i < 4; ++i) {
const currentSlope = getSlope(aGuessT, mX1, mX2);
if (currentSlope === 0.0) return aGuessT;
@@ -116,8 +199,7 @@ const bezier = (() => {
return x => {
if (mX1 === mY1 && mX2 === mY2) return x;
- if (x === 0) return 0;
- if (x === 1) return 1;
+ if (x === 0 || x === 1) return x;
return calcBezier(getTForX(x), mY1, mY2);
}
@@ -131,69 +213,73 @@ const easings = (() => {
const names = ['Quad', 'Cubic', 'Quart', 'Quint', 'Sine', 'Expo', 'Circ', 'Back', 'Elastic'];
- // Elastic easing adapted from jQueryUI http://api.jqueryui.com/easings/
-
- function elastic(t, p) {
- return t === 0 || t === 1 ? t :
- -Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (p / (Math.PI * 2.0) * Math.asin(1))) * (Math.PI * 2)) / p );
- }
-
// Approximated Penner equations http://matthewlein.com/ceaser/
- const equations = {
+ const penner = {
In: [
- [0.550, 0.085, 0.680, 0.530], /* InQuad */
- [0.550, 0.055, 0.675, 0.190], /* InCubic */
- [0.895, 0.030, 0.685, 0.220], /* InQuart */
- [0.755, 0.050, 0.855, 0.060], /* InQuint */
- [0.470, 0.000, 0.745, 0.715], /* InSine */
- [0.950, 0.050, 0.795, 0.035], /* InExpo */
- [0.600, 0.040, 0.980, 0.335], /* InCirc */
- [0.600, -0.280, 0.735, 0.045], /* InBack */
- elastic /* InElastic */
- ], Out: [
- [0.250, 0.460, 0.450, 0.940], /* OutQuad */
- [0.215, 0.610, 0.355, 1.000], /* OutCubic */
- [0.165, 0.840, 0.440, 1.000], /* OutQuart */
- [0.230, 1.000, 0.320, 1.000], /* OutQuint */
- [0.390, 0.575, 0.565, 1.000], /* OutSine */
- [0.190, 1.000, 0.220, 1.000], /* OutExpo */
- [0.075, 0.820, 0.165, 1.000], /* OutCirc */
- [0.175, 0.885, 0.320, 1.275], /* OutBack */
- (t, f) => 1 - elastic(1 - t, f) /* OutElastic */
- ], InOut: [
- [0.455, 0.030, 0.515, 0.955], /* InOutQuad */
- [0.645, 0.045, 0.355, 1.000], /* InOutCubic */
- [0.770, 0.000, 0.175, 1.000], /* InOutQuart */
- [0.860, 0.000, 0.070, 1.000], /* InOutQuint */
- [0.445, 0.050, 0.550, 0.950], /* InOutSine */
- [1.000, 0.000, 0.000, 1.000], /* InOutExpo */
- [0.785, 0.135, 0.150, 0.860], /* InOutCirc */
- [0.680, -0.550, 0.265, 1.550], /* InOutBack */
- (t, f) => t < .5 ? elastic(t * 2, f) / 2 : 1 - elastic(t * -2 + 2, f) / 2 /* InOutElastic */
+ [0.550, 0.085, 0.680, 0.530], /* inQuad */
+ [0.550, 0.055, 0.675, 0.190], /* inCubic */
+ [0.895, 0.030, 0.685, 0.220], /* inQuart */
+ [0.755, 0.050, 0.855, 0.060], /* inQuint */
+ [0.470, 0.000, 0.745, 0.715], /* inSine */
+ [0.950, 0.050, 0.795, 0.035], /* inExpo */
+ [0.600, 0.040, 0.980, 0.335], /* inCirc */
+ [0.600,-0.280, 0.735, 0.045], /* inBack */
+ elastic /* inElastic */
+ ],
+ Out: [
+ [0.250, 0.460, 0.450, 0.940], /* outQuad */
+ [0.215, 0.610, 0.355, 1.000], /* outCubic */
+ [0.165, 0.840, 0.440, 1.000], /* outQuart */
+ [0.230, 1.000, 0.320, 1.000], /* outQuint */
+ [0.390, 0.575, 0.565, 1.000], /* outSine */
+ [0.190, 1.000, 0.220, 1.000], /* outExpo */
+ [0.075, 0.820, 0.165, 1.000], /* outCirc */
+ [0.175, 0.885, 0.320, 1.275], /* outBack */
+ (a, p) => t => 1 - elastic(a, p)(1 - t) /* outElastic */
+ ],
+ InOut: [
+ [0.455, 0.030, 0.515, 0.955], /* inOutQuad */
+ [0.645, 0.045, 0.355, 1.000], /* inOutCubic */
+ [0.770, 0.000, 0.175, 1.000], /* inOutQuart */
+ [0.860, 0.000, 0.070, 1.000], /* inOutQuint */
+ [0.445, 0.050, 0.550, 0.950], /* inOutSine */
+ [1.000, 0.000, 0.000, 1.000], /* inOutExpo */
+ [0.785, 0.135, 0.150, 0.860], /* inOutCirc */
+ [0.680,-0.550, 0.265, 1.550], /* inOutBack */
+ (a, p) => t => t < .5 ? elastic(a, p)(t * 2) / 2 : 1 - elastic(a, p)(t * -2 + 2) / 2 /* inOutElastic */
]
}
- let functions = {
- linear: bezier(0.250, 0.250, 0.750, 0.750)
+ let eases = {
+ linear: [0.250, 0.250, 0.750, 0.750]
}
- for (let type in equations) {
- equations[type].forEach((f, i) => {
- functions['ease' + type + names[i]] = is.fnc(f) ? f : bezier.apply(null, f);
+ for (let equation in penner) {
+ penner[equation].forEach((ease, i) => {
+ eases['ease'+equation+names[i]] = ease;
});
}
- return functions;
+ return eases;
})();
-// Strings
-
-function stringToHyphens(str) {
- return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
+function parseEasings(tween) {
+ const string = tween.easing;
+ const name = string.split('(')[0];
+ const args = parseEasingParameters(string);
+ const ease = easings[name];
+ switch (name) {
+ case 'spring' : return spring(string, tween.duration);
+ case 'cubicBezier' : return applyArguments(bezier, args);
+ case 'steps' : return applyArguments(steps, args);
+ default : return is.fnc(ease) ? applyArguments(ease, args) : applyArguments(bezier, ease);
+ }
}
+// Strings
+
function selectString(str) {
try {
let nodes = document.querySelectorAll(str);
@@ -328,15 +414,39 @@ function getFunctionValue(val, animatable) {
return val(animatable.target, animatable.id, animatable.total);
}
-function getCSSValue(el, prop) {
+function getAttribute(el, prop) {
+ return el.getAttribute(prop);
+}
+
+function convertPxToUnit(el, value, unit) {
+ const valueUnit = getUnit(value);
+ if (arrayContains([unit, 'deg', 'rad', 'turn'], valueUnit)) return value;
+ const cached = cache.CSS[value + unit];
+ if (!is.und(cached)) return cached;
+ const baseline = 100;
+ const tempEl = document.createElement(el.tagName);
+ const parentEl = el.parentNode || document.body;
+ parentEl.appendChild(tempEl);
+ tempEl.style.position = 'absolute';
+ tempEl.style.width = baseline + unit;
+ const factor = baseline / tempEl.offsetWidth;
+ parentEl.removeChild(tempEl);
+ const convertedUnit = factor * parseFloat(value);
+ cache.CSS[value + unit] = convertedUnit;
+ return convertedUnit;
+}
+
+function getCSSValue(el, prop, unit) {
if (prop in el.style) {
- return el.style[prop] || getComputedStyle(el).getPropertyValue(stringToHyphens(prop)) || '0';
+ const uppercasePropName = prop.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
+ const value = el.style[prop] || getComputedStyle(el).getPropertyValue(uppercasePropName) || '0';
+ return unit ? convertPxToUnit(el, value, unit) : value;
}
}
function getAnimationType(el, prop) {
+ if (is.dom(el) && (getAttribute(el, prop) || (is.svg(el) && el[prop]))) return 'attribute';
if (is.dom(el) && arrayContains(validTransforms, prop)) return 'transform';
- if (is.dom(el) && (el.getAttribute(prop) || (is.svg(el) && el[prop]))) return 'attribute';
if (is.dom(el) && (prop !== 'transform' && getCSSValue(el, prop))) return 'css';
if (el[prop] != null) return 'object';
}
@@ -350,19 +460,19 @@ function getElementTransforms(el) {
return transforms;
}
-function getTransformValue(el, propName, animatable) {
+function getTransformValue(el, propName, animatable, unit) {
const defaultVal = stringContains(propName, 'scale') ? 1 : 0 + getTransformUnit(propName);
- const prop = getElementTransforms(el).get(propName) || defaultVal;
- animatable.transforms.list.set(propName, prop);
+ const value = getElementTransforms(el).get(propName) || defaultVal;
+ animatable.transforms.list.set(propName, value);
animatable.transforms['last'] = propName;
- return prop;
+ return unit ? convertPxToUnit(el, value, unit) : value;
}
-function getOriginalTargetValue(target, propName, animatable) {
+function getOriginalTargetValue(target, propName, unit, animatable) {
switch (getAnimationType(target, propName)) {
- case 'transform': return getTransformValue(target, propName, animatable);
- case 'css': return getCSSValue(target, propName);
- case 'attribute': return target.getAttribute(propName);
+ case 'transform': return getTransformValue(target, propName, animatable, unit);
+ case 'css': return getCSSValue(target, propName, unit);
+ case 'attribute': return getAttribute(target, propName);
default: return target[propName] || 0;
}
}
@@ -387,7 +497,7 @@ function validateValue(val, unit) {
return unit && !/\s/g.test(val) ? unitLess + unit : unitLess;
}
-// getTotalLength() equivalent for circle, rect, polyline, polygon and line shapes.
+// getTotalLength() equivalent for circle, rect, polyline, polygon and line shapes
// adapted from https://gist.github.com/SebLambla/3e0550c496c236709744
function getDistance(p1, p2) {
@@ -395,17 +505,17 @@ function getDistance(p1, p2) {
}
function getCircleLength(el) {
- return 2 * Math.PI * el.getAttribute('r');
+ return Math.PI * 2 * getAttribute(el, 'r');
}
function getRectLength(el) {
- return (el.getAttribute('width') * 2) + (el.getAttribute('height') * 2);
+ return (getAttribute(el, 'width') * 2) + (getAttribute(el, 'height') * 2);
}
function getLineLength(el) {
return getDistance(
- {x: el.getAttribute('x1'), y: el.getAttribute('y1')},
- {x: el.getAttribute('x2'), y: el.getAttribute('y2')}
+ {x: getAttribute(el, 'x1'), y: getAttribute(el, 'y1')},
+ {x: getAttribute(el, 'x2'), y: getAttribute(el, 'y2')}
);
}
@@ -504,6 +614,8 @@ function getAnimatables(targets) {
function normalizePropertyTweens(prop, tweenSettings) {
let settings = cloneObject(tweenSettings);
+ // Override duration if easing is a spring
+ if (/^spring/.test(settings.easing)) settings.duration = spring(settings.easing);
if (is.arr(prop)) {
const l = prop.length;
const isFromTo = (l === 2 && !is.obj(prop[0]));
@@ -517,24 +629,19 @@ function normalizePropertyTweens(prop, tweenSettings) {
}
const propArray = is.arr(prop) ? prop : [prop];
return propArray.map((v, i) => {
+ const obj = (is.obj(v) && !is.pth(v)) ? v : {value: v};
// Default delay value should be applied only on the first tween
- const delay = !i ? tweenSettings.delay : 0;
- // Use path object as a tween value
- let obj = is.obj(v) && !is.pth(v) ? v : {value: v};
- // Set default delay value
- if (is.und(obj.delay)) obj.delay = delay;
+ if (is.und(obj.delay)) obj.delay = !i ? tweenSettings.delay : 0;
return obj;
}).map(k => mergeObjects(k, settings));
}
function getProperties(instanceSettings, tweenSettings, params) {
let properties = [];
- const settings = mergeObjects(instanceSettings, tweenSettings);
for (let p in params) {
- if (!settings.hasOwnProperty(p) && p !== 'targets') {
+ if (!instanceSettings.hasOwnProperty(p) && !tweenSettings.hasOwnProperty(p) && p !== 'targets') {
properties.push({
name: p,
- offset: settings['timeOffset'],
tweens: normalizePropertyTweens(params[p], tweenSettings)
});
}
@@ -559,26 +666,23 @@ function normalizeTweenValues(tween, animatable) {
return t;
}
-function normalizeEasing(val) {
- return is.arr(val) ? bezier.apply(this, val) : easings[val];
-}
-
function normalizeTweens(prop, animatable) {
let previousTween;
return prop.tweens.map(t => {
- let tween = normalizeTweenValues(t, animatable);
+ const tween = normalizeTweenValues(t, animatable);
const tweenValue = tween.value;
- const originalValue = getOriginalTargetValue(animatable.target, prop.name, animatable);
+ const to = is.arr(tweenValue) ? tweenValue[1] : tweenValue;
+ const toUnit = getUnit(to);
+ const originalValue = getOriginalTargetValue(animatable.target, prop.name, toUnit, animatable);
const previousValue = previousTween ? previousTween.to.original : originalValue;
const from = is.arr(tweenValue) ? tweenValue[0] : previousValue;
- const to = getRelativeValue(is.arr(tweenValue) ? tweenValue[1] : tweenValue, from);
- const unit = getUnit(to) || getUnit(from) || getUnit(originalValue);
+ const fromUnit = getUnit(from) || getUnit(originalValue);
+ const unit = toUnit || fromUnit;
tween.from = decomposeValue(from, unit);
- tween.to = decomposeValue(to, unit);
- tween.start = previousTween ? previousTween.end : prop.offset;
+ tween.to = decomposeValue(getRelativeValue(to, from), unit);
+ tween.start = previousTween ? previousTween.end : 0;
tween.end = tween.start + tween.delay + tween.duration + tween.endDelay;
- tween.easing = normalizeEasing(tween.easing);
- tween.elasticity = (1000 - minMaxValue(tween.elasticity, 1, 999)) / 1000;
+ tween.easing = parseEasings(tween);
tween.isPath = is.pth(tweenValue);
tween.isColor = is.col(tween.from.original);
if (tween.isColor) tween.round = 1;
@@ -597,28 +701,14 @@ const setProgressValue = {
transforms.list.set(p, v);
if (p === transforms.last || manual) {
let str = '';
- for (let [prop, value] of transforms.list) {
+ transforms.list.forEach((value, prop) => {
str += `${prop}(${value}) `;
- }
+ });
t.style.transform = str;
}
}
}
-function setTargetValue(targets, properties) {
- const animatables = getAnimatables(targets);
- animatables.forEach(animatable => {
- for (var property in properties) {
- const value = properties[property];
- const originalValue = getOriginalTargetValue(animatable.target, property, animatable);
- const unit = getUnit(value) || getUnit(originalValue);
- const to = getRelativeValue(validateValue(value, unit), originalValue);
- const animType = getAnimationType(animatable.target, property);
- setProgressValue[animType](animatable.target, property, to, animatable.transforms, true);
- }
- });
-}
-
// Animations
function createAnimation(animatable, prop) {
@@ -646,12 +736,12 @@ function getAnimations(animatables, properties) {
// Create Instance
-function getInstanceTimings(type, animations, instanceSettings, tweenSettings) {
+function getInstanceTimings(type, animations, tweenSettings) {
const isDelay = (type === 'delay');
if (animations.length) {
return (isDelay ? Math.min : Math.max).apply(Math, animations.map(anim => anim[type]));
} else {
- return isDelay ? tweenSettings.delay : instanceSettings.timeOffset + tweenSettings.delay + tweenSettings.duration;
+ return isDelay ? tweenSettings.delay : tweenSettings.delay + tweenSettings.duration;
}
}
@@ -662,11 +752,12 @@ function createNewInstance(params) {
const properties = getProperties(instanceSettings, tweenSettings, params);
const animations = getAnimations(animatables, properties);
return mergeObjects(instanceSettings, {
+ states: {default: params},
children: [],
animatables: animatables,
animations: animations,
- duration: getInstanceTimings('duration', animations, instanceSettings, tweenSettings),
- delay: getInstanceTimings('delay', animations, instanceSettings, tweenSettings)
+ duration: getInstanceTimings('duration', animations, tweenSettings),
+ delay: getInstanceTimings('delay', animations, tweenSettings)
});
}
@@ -674,42 +765,55 @@ function createNewInstance(params) {
let activeInstances = [];
let pausedInstances = [];
-let raf = 0;
+let raf;
const engine = (() => {
- function play() { raf = requestAnimationFrame(step); };
+ function play() {
+ raf = requestAnimationFrame(step);
+ }
function step(t) {
- if (pausedInstances.length) {
- activeInstances = filterArray(activeInstances, ins => !pausedInstances.includes(ins));
- pausedInstances = [];
- }
- if (activeInstances.length) {
+ let activeInstancesLength = activeInstances.length;
+ if (activeInstancesLength) {
let i = 0;
- while (i < activeInstances.length) {
+ while (i < activeInstancesLength) {
const activeInstance = activeInstances[i];
if (!activeInstance.paused) {
- activeInstances[i].tick(t);
+ activeInstance.tick(t);
} else {
- pausedInstances.push(activeInstance);
+ const instanceIndex = activeInstances.indexOf(activeInstance);
+ if (instanceIndex > -1) {
+ activeInstances.splice(instanceIndex, 1);
+ activeInstancesLength = activeInstances.length;
+ }
}
i++;
}
play();
} else {
- cancelAnimationFrame(raf);
- raf = 0;
+ raf = cancelAnimationFrame(raf);
}
}
return play;
})();
+function handleVisibilityChange() {
+ if (document.hidden) {
+ activeInstances.forEach(ins => ins.pause());
+ pausedInstances = activeInstances.slice(0);
+ activeInstances = [];
+ } else {
+ pausedInstances.forEach(ins => ins.play());
+ }
+}
+
+document.addEventListener('visibilitychange', handleVisibilityChange);
// Public Instance
function anime(params = {}) {
- let now, startTime, lastTime = 0;
-
+ let startTime = 0, lastTime = 0, now = 0;
+ let children, childrenLength = 0;
let resolve = null;
function makePromise() {
@@ -722,22 +826,23 @@ function anime(params = {}) {
function toggleInstanceDirection() {
instance.reversed = !instance.reversed;
- const children = instance.children;
children.forEach(child => child.reversed = instance.reversed);
}
+ function resetTime() {
+ startTime = 0;
+ lastTime = adjustTime(instance.currentTime) * (1 / anime.speed);
+ }
+
function adjustTime(time) {
return instance.reversed ? instance.duration - time : time;
}
- function syncInstanceChildren(time, manual) {
- const children = instance.children;
- const childrenLength = children.length;
- if (!manual || time >= instance.currentTime) {
- for (let i = 0; i < childrenLength; i++) children[i].seek(time);
+ function syncInstanceChildren(time) {
+ if (time >= instance.currentTime) {
+ for (let i = 0; i < childrenLength; i++) children[i].seek(time - children[i].timelineOffset);
} else {
- // Manual backward seeking requires looping in reverse
- for (let i = childrenLength; i--;) children[i].seek(time);
+ for (let i = childrenLength; i--;) children[i].seek(time - children[i].timelineOffset);
}
}
@@ -754,12 +859,12 @@ function anime(params = {}) {
// Only check for keyframes if there is more than one tween
if (tweenLength) tween = filterArray(tweens, t => (insTime < t.end))[0] || tween;
const elapsed = minMaxValue(insTime - tween.start - tween.delay, 0, tween.duration) / tween.duration;
- const eased = isNaN(elapsed) ? 1 : tween.easing(elapsed, tween.elasticity);
+ const eased = isNaN(elapsed) ? 1 : tween.easing(elapsed);
const strings = tween.to.strings;
const round = tween.round;
const numbers = [];
- let progress;
const toNumbersLength = tween.to.numbers.length;
+ let progress;
for (let n = 0; n < toNumbersLength; n++) {
let value;
const toNumber = tween.to.numbers[n];
@@ -800,7 +905,6 @@ function anime(params = {}) {
i++;
}
instance.currentTime = insTime;
- instance.progress = (insTime / instance.duration) * 100;
}
function setCallback(cb) {
@@ -813,35 +917,34 @@ function anime(params = {}) {
}
}
- function setInstanceProgress(engineTime, manual) {
+ function setInstanceProgress(engineTime) {
const insDuration = instance.duration;
- const insOffset = instance.timeOffset;
- const insStart = insOffset + instance.delay;
- const insCurrentTime = instance.currentTime;
- const insReversed = instance.reversed;
+ const insDelay = instance.delay;
const insTime = adjustTime(engineTime);
- if (instance.children.length) syncInstanceChildren(insTime, manual);
- if (insTime >= insStart || !insDuration) {
+ instance.progress = minMaxValue((insTime / insDuration) * 100, 0, 100);
+ if (children) syncInstanceChildren(insTime);
+ if (insTime >= insDelay || !insDuration) {
if (!instance.began) {
instance.began = true;
setCallback('begin');
}
setCallback('run');
}
- if (insTime > insOffset && insTime < insDuration) {
+ if (insTime > insDelay && insTime < insDuration) {
setAnimationsProgress(insTime);
} else {
- if (insTime <= insOffset && insCurrentTime !== 0) {
+ if (insTime <= insDelay && instance.currentTime !== 0) {
setAnimationsProgress(0);
- if (insReversed) countIteration();
+ if (instance.reversed) countIteration();
}
- if ((insTime >= insDuration && insCurrentTime !== insDuration) || !insDuration) {
+ if ((insTime >= insDuration && instance.currentTime !== insDuration) || !insDuration) {
setAnimationsProgress(insDuration);
- if (!insReversed) countIteration();
+ if (!instance.reversed) countIteration();
}
}
setCallback('update');
if (engineTime >= insDuration) {
+ lastTime = 0;
if (instance.remaining) {
startTime = now;
if (instance.direction === 'alternate') toggleInstanceDirection();
@@ -856,7 +959,6 @@ function anime(params = {}) {
}
}
}
- lastTime = 0;
}
}
@@ -870,42 +972,37 @@ function anime(params = {}) {
instance.completed = false;
instance.reversed = direction === 'reverse';
instance.remaining = direction === 'alternate' && loops === 1 ? 2 : loops;
+ children = instance.children;
+ childrenLength = children.length;
+ for (let i = childrenLength; i--;) instance.children[i].reset();
setAnimationsProgress(0);
- for (let i = instance.children.length; i--; ){
- instance.children[i].reset();
- }
}
instance.tick = function(t) {
now = t;
if (!startTime) startTime = now;
- const engineTime = (lastTime + now - startTime) * anime.speed;
- setInstanceProgress(engineTime);
+ setInstanceProgress((now + (lastTime - startTime)) * anime.speed);
}
instance.seek = function(time) {
- setInstanceProgress(adjustTime(time), true);
+ setInstanceProgress(adjustTime(time));
}
instance.pause = function() {
instance.paused = true;
- const i = activeInstances.indexOf(instance);
- if (i > -1) activeInstances.splice(i, 1);
}
instance.play = function() {
if (!instance.paused) return;
instance.paused = false;
- startTime = 0;
- lastTime = adjustTime(instance.currentTime);
+ resetTime();
activeInstances.push(instance);
if (!raf) engine();
}
instance.reverse = function() {
toggleInstanceDirection();
- startTime = 0;
- lastTime = adjustTime(instance.currentTime);
+ resetTime();
}
instance.restart = function() {
@@ -914,10 +1011,30 @@ function anime(params = {}) {
instance.play();
}
+ instance.animateTo = function(stateName, paramsOverrides = {}, bypassAnimation) {
+ const nextState = instance.states[stateName];
+ const defaultState = instance.states.default;
+ const params = mergeObjects(paramsOverrides, mergeObjects(nextState, defaultState));
+ const animation = anime(params);
+ if (!bypassAnimation) {
+ animation.play();
+ } else {
+ animation.seek(animation.duration);
+ }
+ }
+
+ instance.goTo = function(stateName, paramsOverrides) {
+ instance.animateTo(stateName, paramsOverrides, true);
+ }
+
instance.finished = promise;
instance.reset();
+ if (instance.startTime) {
+ setInstanceProgress(instance.startTime - (instance.duration * Math.floor(instance.startTime / instance.duration)));
+ }
+
if (instance.autoplay) instance.play();
return instance;
@@ -926,17 +1043,28 @@ function anime(params = {}) {
// Remove targets from animation
+function removeTargetsFromAnimations(targetsArray, animations) {
+ for (let a = animations.length; a--;) {
+ if (arrayContains(targetsArray, animations[a].animatable.target)) {
+ animations.splice(a, 1);
+ }
+ }
+}
+
function removeTargets(targets) {
const targetsArray = parseTargets(targets);
for (let i = activeInstances.length; i--;) {
const instance = activeInstances[i];
const animations = instance.animations;
- for (let a = animations.length; a--;) {
- if (arrayContains(targetsArray, animations[a].animatable.target)) {
- animations.splice(a, 1);
- if (!animations.length) instance.pause();
- }
+ const children = instance.children;
+ removeTargetsFromAnimations(targetsArray, animations);
+ for (let c = children.length; c--;) {
+ const child = children[c];
+ const childAnimations = child.animations;
+ removeTargetsFromAnimations(targetsArray, childAnimations);
+ if (!childAnimations.length && !child.children.length) children.splice(c, 1);
}
+ if (!animations.length && !children.length) instance.pause();
}
}
@@ -946,21 +1074,22 @@ function timeline(params = {}) {
let tl = anime(params);
tl.pause();
tl.duration = 0;
- tl.add = function(instanceParams, timeOffset) {
- tl.children.forEach(i => { i.began = true; i.completed = true; });
+ tl.add = function(instanceParams, timelineOffset) {
+ function passThrough(ins) { ins.began = true; ins.completed = true; };
+ tl.children.forEach(passThrough);
let insParams = mergeObjects(instanceParams, replaceObjectProps(defaultTweenSettings, params));
insParams.targets = insParams.targets || params.targets;
const tlDuration = tl.duration;
insParams.autoplay = false;
insParams.direction = tl.direction;
- insParams.timeOffset = is.und(timeOffset) ? tlDuration : getRelativeValue(timeOffset, tlDuration);
- tl.began = true;
- tl.completed = true;
- tl.seek(insParams.timeOffset);
+ insParams.timelineOffset = is.und(timelineOffset) ? tlDuration : getRelativeValue(timelineOffset, tlDuration);
+ passThrough(tl);
+ tl.seek(insParams.timelineOffset);
const ins = anime(insParams);
- ins.began = true;
- ins.completed = true;
- if (ins.duration > tlDuration) tl.duration = ins.duration;
+ passThrough(ins);
+ const totalDuration = ins.duration + insParams.timelineOffset;
+ if (is.fnc(tl.delay)) tl.delay = ins.delay;
+ if (is.fnc(tlDuration) || totalDuration > tlDuration) tl.duration = totalDuration;
tl.children.push(ins);
tl.seek(0);
tl.reset();
@@ -974,13 +1103,11 @@ anime.version = '3.0.0';
anime.speed = 1;
anime.running = activeInstances;
anime.remove = removeTargets;
-anime.get = getOriginalTargetValue;
-anime.set = setTargetValue;
+anime.getValue = getOriginalTargetValue;
anime.path = getPath;
anime.setDashoffset = setDashoffset;
-anime.bezier = bezier;
-anime.easings = easings;
anime.timeline = timeline;
+anime.easings = easings;
anime.random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
export default anime;