/* * anime.js v3.1.0 * (c) 2019 julian garnier * released under the mit license * animejs.com */ 'use strict'; // defaults var defaultinstancesettings = { update: null, begin: null, loopbegin: null, changebegin: null, change: null, changecomplete: null, loopcomplete: null, complete: null, loop: 1, direction: 'normal', autoplay: true, timelineoffset: 0 }; var defaulttweensettings = { duration: 1000, delay: 0, enddelay: 0, 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 minmax(val, min, max) { return math.min(math.max(val, min), max); } 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'); }, pth: function (a) { return is.obj(a) && a.hasownproperty('totallength'); }, svg: function (a) { return a instanceof svgelement; }, inp: function (a) { return a instanceof htmlinputelement; }, dom: function (a) { return a.nodetype || is.svg(a); }, str: function (a) { return typeof a === 'string'; }, fnc: function (a) { return typeof a === 'function'; }, und: function (a) { return typeof a === 'undefined'; }, hex: function (a) { return /(^#[0-9a-f]{6}$)|(^#[0-9a-f]{3}$)/i.test(a); }, rgb: function (a) { return /^rgb/.test(a); }, hsl: function (a) { return /^hsl/.test(a); }, col: function (a) { return (is.hex(a) || is.rgb(a) || is.hsl(a)); }, key: function (a) { return !defaultinstancesettings.hasownproperty(a) && !defaulttweensettings.hasownproperty(a) && a !== 'targets' && a !== 'keyframes'; } }; // easings 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 = minmax(is.und(params[0]) ? 1 : params[0], .1, 100); var stiffness = minmax(is.und(params[1]) ? 100 : params[1], .1, 100); var damping = minmax(is.und(params[2]) ? 10 : params[2], .1, 100); var velocity = minmax(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; } // 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 () { 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 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) { var currentx, currentt, i = 0; do { currentt = aa + (ab - aa) / 2.0; currentx = calcbezier(currentt, mx1, mx2) - ax; if (currentx > 0.0) { ab = currentt; } else { aa = currentt; } } while (math.abs(currentx) > 0.0000001 && ++i < 10); return currentt; } 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; } var currentx = calcbezier(aguesst, mx1, mx2) - ax; aguesst -= currentx / currentslope; } return aguesst; } function bezier(mx1, my1, mx2, my2) { if (!(0 <= mx1 && mx1 <= 1 && 0 <= mx2 && mx2 <= 1)) { return; } var samplevalues = new float32array(ksplinetablesize); if (mx1 !== my1 || mx2 !== my2) { for (var i = 0; i < ksplinetablesize; ++i) { samplevalues[i] = calcbezier(i * ksamplestepsize, mx1, mx2); } } function gettforx(ax) { var intervalstart = 0; var currentsample = 1; var lastsample = ksplinetablesize - 1; for (; currentsample !== lastsample && samplevalues[currentsample] <= ax; ++currentsample) { intervalstart += ksamplestepsize; } --currentsample; var dist = (ax - samplevalues[currentsample]) / (samplevalues[currentsample + 1] - samplevalues[currentsample]); var guessfort = intervalstart + dist * ksamplestepsize; var initialslope = getslope(guessfort, mx1, mx2); if (initialslope >= 0.001) { return newtonraphsoniterate(ax, guessfort, mx1, mx2); } else if (initialslope === 0.0) { return guessfort; } else { return binarysubdivide(ax, intervalstart, intervalstart + ksamplestepsize, mx1, mx2); } } return function (x) { if (mx1 === my1 && mx2 === my2) { return x; } if (x === 0 || x === 1) { return x; } return calcbezier(gettforx(x), my1, my2); } } return bezier; })(); var penner = (function () { // based on jquery ui's implemenation of easing equations from robert penner (http://www.robertpenner.com/easing) var eases = { linear: function () { return function (t) { return t; }; } }; var functioneasings = { sine: function () { return function (t) { return 1 - math.cos(t * math.pi / 2); }; }, circ: function () { return function (t) { return 1 - math.sqrt(1 - t * t); }; }, back: function () { return function (t) { return t * t * (3 * t - 2); }; }, bounce: function () { return function (t) { var pow2, b = 4; while (t < (( pow2 = math.pow(2, --b)) - 1) / 11) {} return 1 / math.pow(4, 3 - b) - 7.5625 * math.pow(( pow2 * 3 - 2 ) / 22 - t, 2) }; }, elastic: function (amplitude, period) { if ( amplitude === void 0 ) amplitude = 1; if ( period === void 0 ) period = .5; var a = minmax(amplitude, 1, 10); var p = minmax(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); } } }; var baseeasings = ['quad', 'cubic', 'quart', 'quint', 'expo']; baseeasings.foreach(function (name, i) { functioneasings[name] = function () { return function (t) { return math.pow(t, i + 2); }; }; }); object.keys(functioneasings).foreach(function (name) { var easein = functioneasings[name]; eases['easein' + name] = easein; eases['easeout' + name] = function (a, b) { return function (t) { return 1 - easein(a, b)(1 - t); }; }; eases['easeinout' + name] = function (a, b) { return function (t) { return t < 0.5 ? easein(a, b)(t * 2) / 2 : 1 - easein(a, b)(t * -2 + 2) / 2; }; }; }); return eases; })(); function parseeasings(easing, duration) { if (is.fnc(easing)) { return easing; } var name = easing.split('(')[0]; var ease = penner[name]; var args = parseeasingparameters(easing); switch (name) { case 'spring' : return spring(easing, duration); case 'cubicbezier' : return applyarguments(bezier, args); case 'steps' : return applyarguments(steps, args); default : return applyarguments(ease, args); } } // strings function selectstring(str) { try { var nodes = document.queryselectorall(str); return nodes; } catch(e) { return; } } // arrays function filterarray(arr, callback) { var len = arr.length; var thisarg = arguments.length >= 2 ? arguments[1] : void 0; var result = []; for (var i = 0; i < len; i++) { if (i in arr) { var val = arr[i]; if (callback.call(thisarg, val, i, arr)) { result.push(val); } } } return result; } function flattenarray(arr) { return arr.reduce(function (a, b) { return a.concat(is.arr(b) ? flattenarray(b) : b); }, []); } function toarray(o) { if (is.arr(o)) { return o; } if (is.str(o)) { o = selectstring(o) || o; } if (o instanceof nodelist || o instanceof htmlcollection) { return [].slice.call(o); } return [o]; } function arraycontains(arr, val) { return arr.some(function (a) { return a === val; }); } // objects function cloneobject(o) { var clone = {}; for (var p in o) { clone[p] = o[p]; } return clone; } function replaceobjectprops(o1, o2) { var o = cloneobject(o1); for (var p in o1) { o[p] = o2.hasownproperty(p) ? o2[p] : o1[p]; } return o; } function mergeobjects(o1, o2) { var o = cloneobject(o1); for (var p in o2) { o[p] = is.und(o1[p]) ? o2[p] : o1[p]; } return o; } // colors function rgbtorgba(rgbvalue) { var rgb = /rgb\((\d+,\s*[\d]+,\s*[\d]+)\)/g.exec(rgbvalue); return rgb ? ("rgba(" + (rgb[1]) + ",1)") : rgbvalue; } function hextorgba(hexvalue) { var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; var hex = hexvalue.replace(rgx, function (m, r, g, b) { return r + r + g + g + b + b; } ); var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); var r = parseint(rgb[1], 16); var g = parseint(rgb[2], 16); var b = parseint(rgb[3], 16); return ("rgba(" + r + "," + g + "," + b + ",1)"); } function hsltorgba(hslvalue) { var hsl = /hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.exec(hslvalue) || /hsla\((\d+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)/g.exec(hslvalue); var h = parseint(hsl[1], 10) / 360; var s = parseint(hsl[2], 10) / 100; var l = parseint(hsl[3], 10) / 100; var a = hsl[4] || 1; function hue2rgb(p, q, t) { if (t < 0) { t += 1; } if (t > 1) { t -= 1; } if (t < 1/6) { return p + (q - p) * 6 * t; } if (t < 1/2) { return q; } if (t < 2/3) { return p + (q - p) * (2/3 - t) * 6; } return p; } var r, g, b; if (s == 0) { r = g = b = l; } else { var q = l < 0.5 ? l * (1 + s) : l + s - l * s; var p = 2 * l - q; r = hue2rgb(p, q, h + 1/3); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1/3); } return ("rgba(" + (r * 255) + "," + (g * 255) + "," + (b * 255) + "," + a + ")"); } function colortorgb(val) { if (is.rgb(val)) { return rgbtorgba(val); } if (is.hex(val)) { return hextorgba(val); } if (is.hsl(val)) { return hsltorgba(val); } } // units function getunit(val) { var split = /[+-]?\d*\.?\d+(?:\.\d+)?(?:[ee][+-]?\d+)?(%|px|pt|em|rem|in|cm|mm|ex|ch|pc|vw|vh|vmin|vmax|deg|rad|turn)?$/.exec(val); if (split) { return split[1]; } } function gettransformunit(propname) { if (stringcontains(propname, 'translate') || propname === 'perspective') { return 'px'; } if (stringcontains(propname, 'rotate') || stringcontains(propname, 'skew')) { return 'deg'; } } // values function getfunctionvalue(val, animatable) { if (!is.fnc(val)) { return val; } return val(animatable.target, animatable.id, animatable.total); } 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 && (el.parentnode !== document)) ? 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) { 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) && !is.inp(el) && (getattribute(el, prop) || (is.svg(el) && el[prop]))) { return 'attribute'; } if (is.dom(el) && arraycontains(validtransforms, prop)) { return 'transform'; } if (is.dom(el) && (prop !== 'transform' && getcssvalue(el, prop))) { return 'css'; } if (el[prop] != null) { return 'object'; } } function getelementtransforms(el) { if (!is.dom(el)) { return; } var str = el.style.transform || ''; var reg = /(\w+)\(([^)]*)\)/g; var transforms = new map(); var m; while (m = reg.exec(str)) { transforms.set(m[1], m[2]); } return transforms; } function gettransformvalue(el, propname, animatable, unit) { var defaultval = stringcontains(propname, 'scale') ? 1 : 0 + gettransformunit(propname); var value = getelementtransforms(el).get(propname) || defaultval; if (animatable) { animatable.transforms.list.set(propname, value); animatable.transforms['last'] = propname; } return unit ? convertpxtounit(el, value, unit) : value; } function getoriginaltargetvalue(target, propname, unit, animatable) { switch (getanimationtype(target, 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; } } function getrelativevalue(to, from) { var operator = /^(\*=|\+=|-=)/.exec(to); if (!operator) { return to; } var u = getunit(to) || 0; var x = parsefloat(from); var y = parsefloat(to.replace(operator[0], '')); switch (operator[0][0]) { case '+': return x + y + u; case '-': return x - y + u; case '*': return x * y + u; } } function validatevalue(val, unit) { if (is.col(val)) { return colortorgb(val); } if (/\s/g.test(val)) { return val; } var originalunit = getunit(val); var unitless = originalunit ? val.substr(0, val.length - originalunit.length) : val; if (unit) { return unitless + unit; } return unitless; } // gettotallength() equivalent for circle, rect, polyline, polygon and line shapes // adapted from https://gist.github.com/seblambla/3e0550c496c236709744 function getdistance(p1, p2) { return math.sqrt(math.pow(p2.x - p1.x, 2) + math.pow(p2.y - p1.y, 2)); } function getcirclelength(el) { return math.pi * 2 * getattribute(el, 'r'); } function getrectlength(el) { return (getattribute(el, 'width') * 2) + (getattribute(el, 'height') * 2); } function getlinelength(el) { return getdistance( {x: getattribute(el, 'x1'), y: getattribute(el, 'y1')}, {x: getattribute(el, 'x2'), y: getattribute(el, 'y2')} ); } function getpolylinelength(el) { var points = el.points; var totallength = 0; var previouspos; for (var i = 0 ; i < points.numberofitems; i++) { var currentpos = points.getitem(i); if (i > 0) { totallength += getdistance(previouspos, currentpos); } previouspos = currentpos; } return totallength; } function getpolygonlength(el) { var points = el.points; return getpolylinelength(el) + getdistance(points.getitem(points.numberofitems - 1), points.getitem(0)); } // path animation function gettotallength(el) { if (el.gettotallength) { return el.gettotallength(); } switch(el.tagname.tolowercase()) { case 'circle': return getcirclelength(el); case 'rect': return getrectlength(el); case 'line': return getlinelength(el); case 'polyline': return getpolylinelength(el); case 'polygon': return getpolygonlength(el); } } function setdashoffset(el) { var pathlength = gettotallength(el); el.setattribute('stroke-dasharray', pathlength); return pathlength; } // motion path function getparentsvgel(el) { var parentel = el.parentnode; while (is.svg(parentel)) { if (!is.svg(parentel.parentnode)) { break; } parentel = parentel.parentnode; } return parentel; } function getparentsvg(pathel, svgdata) { var svg = svgdata || {}; var parentsvgel = svg.el || getparentsvgel(pathel); var rect = parentsvgel.getboundingclientrect(); var viewboxattr = getattribute(parentsvgel, 'viewbox'); var width = rect.width; var height = rect.height; var viewbox = svg.viewbox || (viewboxattr ? viewboxattr.split(' ') : [0, 0, width, height]); return { el: parentsvgel, viewbox: viewbox, x: viewbox[0] / 1, y: viewbox[1] / 1, w: width / viewbox[2], h: height / viewbox[3] } } function getpath(path, percent) { var pathel = is.str(path) ? selectstring(path)[0] : path; var p = percent || 100; return function(property) { return { property: property, el: pathel, svg: getparentsvg(pathel), totallength: gettotallength(pathel) * (p / 100) } } } function getpathprogress(path, progress) { function point(offset) { if ( offset === void 0 ) offset = 0; var l = progress + offset >= 1 ? progress + offset : 0; return path.el.getpointatlength(l); } var svg = getparentsvg(path.el, path.svg); var p = point(); var p0 = point(-1); var p1 = point(+1); switch (path.property) { case 'x': return (p.x - svg.x) * svg.w; case 'y': return (p.y - svg.y) * svg.h; case 'angle': return math.atan2(p1.y - p0.y, p1.x - p0.x) * 180 / math.pi; } } // decompose value function decomposevalue(val, unit) { // const rgx = /-?\d*\.?\d+/g; // handles basic numbers // const rgx = /[+-]?\d+(?:\.\d+)?(?:[ee][+-]?\d+)?/g; // handles exponents notation var rgx = /[+-]?\d*\.?\d+(?:\.\d+)?(?:[ee][+-]?\d+)?/g; // handles exponents notation var value = validatevalue((is.pth(val) ? val.totallength : val), unit) + ''; return { original: value, numbers: value.match(rgx) ? value.match(rgx).map(number) : [0], strings: (is.str(val) || unit) ? value.split(rgx) : [] } } // animatables function parsetargets(targets) { var targetsarray = targets ? (flattenarray(is.arr(targets) ? targets.map(toarray) : toarray(targets))) : []; return filterarray(targetsarray, function (item, pos, self) { return self.indexof(item) === pos; }); } function getanimatables(targets) { var parsed = parsetargets(targets); return parsed.map(function (t, i) { return {target: t, id: i, total: parsed.length, transforms: { list: getelementtransforms(t) } }; }); } // properties 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])); if (!isfromto) { // duration divided by the number of tweens if (!is.fnc(tweensettings.duration)) { settings.duration = tweensettings.duration / l; } } else { // transform [from, to] values shorthand to a valid tween value prop = {value: prop}; } } 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 only be applied to the first tween if (is.und(obj.delay)) { obj.delay = !i ? tweensettings.delay : 0; } // default enddelay value should only be applied to the last tween if (is.und(obj.enddelay)) { obj.enddelay = i === proparray.length - 1 ? tweensettings.enddelay : 0; } return obj; }).map(function (k) { return mergeobjects(k, settings); }); } function flattenkeyframes(keyframes) { var propertynames = filterarray(flattenarray(keyframes.map(function (key) { return object.keys(key); })), function (p) { return is.key(p); }) .reduce(function (a,b) { if (a.indexof(b) < 0) { a.push(b); } return a; }, []); var properties = {}; var loop = function ( i ) { var propname = propertynames[i]; properties[propname] = keyframes.map(function (key) { var newkey = {}; for (var p in key) { if (is.key(p)) { if (p == propname) { newkey.value = key[p]; } } else { newkey[p] = key[p]; } } return newkey; }); }; for (var i = 0; i < propertynames.length; i++) loop( i ); return properties; } function getproperties(tweensettings, params) { var properties = []; var keyframes = params.keyframes; if (keyframes) { params = mergeobjects(flattenkeyframes(keyframes), params); } for (var p in params) { if (is.key(p)) { properties.push({ name: p, tweens: normalizepropertytweens(params[p], tweensettings) }); } } return properties; } // tweens function normalizetweenvalues(tween, animatable) { var t = {}; for (var p in tween) { var value = getfunctionvalue(tween[p], animatable); if (is.arr(value)) { value = value.map(function (v) { return getfunctionvalue(v, animatable); }); if (value.length === 1) { value = value[0]; } } t[p] = value; } t.duration = parsefloat(t.duration); t.delay = parsefloat(t.delay); return t; } function normalizetweens(prop, animatable) { var previoustween; return prop.tweens.map(function (t) { var tween = normalizetweenvalues(t, animatable); var tweenvalue = tween.value; 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 fromunit = getunit(from) || getunit(originalvalue); var unit = tounit || fromunit; if (is.und(to)) { to = previousvalue; } tween.from = decomposevalue(from, unit); 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 = parseeasings(tween.easing, tween.duration); tween.ispath = is.pth(tweenvalue); tween.iscolor = is.col(tween.from.original); if (tween.iscolor) { tween.round = 1; } previoustween = tween; return tween; }); } // tween progress var setprogressvalue = { css: function (t, p, v) { return t.style[p] = v; }, attribute: function (t, p, v) { return t.setattribute(p, v); }, object: function (t, p, v) { return t[p] = v; }, transform: function (t, p, v, transforms, manual) { transforms.list.set(p, v); if (p === transforms.last || manual) { var str = ''; transforms.list.foreach(function (value, prop) { str += prop + "(" + value + ") "; }); t.style.transform = str; } } }; // set value helper function settargetsvalue(targets, properties) { var animatables = getanimatables(targets); animatables.foreach(function (animatable) { for (var property in properties) { var value = getfunctionvalue(properties[property], animatable); var target = animatable.target; var valueunit = getunit(value); var originalvalue = getoriginaltargetvalue(target, property, valueunit, animatable); var unit = valueunit || getunit(originalvalue); var to = getrelativevalue(validatevalue(value, unit), originalvalue); var animtype = getanimationtype(target, property); setprogressvalue[animtype](target, property, to, animatable.transforms, true); } }); } // animations function createanimation(animatable, prop) { var animtype = getanimationtype(animatable.target, prop.name); if (animtype) { var tweens = normalizetweens(prop, animatable); var lasttween = tweens[tweens.length - 1]; return { type: animtype, property: prop.name, animatable: animatable, tweens: tweens, duration: lasttween.end, delay: tweens[0].delay, enddelay: lasttween.enddelay } } } function getanimations(animatables, properties) { return filterarray(flattenarray(animatables.map(function (animatable) { return properties.map(function (prop) { return createanimation(animatable, prop); }); })), function (a) { return !is.und(a); }); } // create instance function getinstancetimings(animations, tweensettings) { var animlength = animations.length; var gettloffset = function (anim) { return anim.timelineoffset ? anim.timelineoffset : 0; }; var timings = {}; timings.duration = animlength ? math.max.apply(math, animations.map(function (anim) { return gettloffset(anim) + anim.duration; })) : tweensettings.duration; timings.delay = animlength ? math.min.apply(math, animations.map(function (anim) { return gettloffset(anim) + anim.delay; })) : tweensettings.delay; timings.enddelay = animlength ? timings.duration - math.max.apply(math, animations.map(function (anim) { return gettloffset(anim) + anim.duration - anim.enddelay; })) : tweensettings.enddelay; return timings; } var instanceid = 0; function createnewinstance(params) { var instancesettings = replaceobjectprops(defaultinstancesettings, params); var tweensettings = replaceobjectprops(defaulttweensettings, params); var properties = getproperties(tweensettings, params); var animatables = getanimatables(params.targets); var animations = getanimations(animatables, properties); var timings = getinstancetimings(animations, tweensettings); var id = instanceid; instanceid++; return mergeobjects(instancesettings, { id: id, children: [], animatables: animatables, animations: animations, duration: timings.duration, delay: timings.delay, enddelay: timings.enddelay }); } // core var activeinstances = []; var pausedinstances = []; var raf; var engine = (function () { function play() { raf = requestanimationframe(step); } function step(t) { var activeinstanceslength = activeinstances.length; if (activeinstanceslength) { var i = 0; while (i < activeinstanceslength) { var activeinstance = activeinstances[i]; if (!activeinstance.paused) { activeinstance.tick(t); } else { var instanceindex = activeinstances.indexof(activeinstance); if (instanceindex > -1) { activeinstances.splice(instanceindex, 1); activeinstanceslength = activeinstances.length; } } i++; } play(); } else { raf = cancelanimationframe(raf); } } return play; })(); function handlevisibilitychange() { if (document.hidden) { activeinstances.foreach(function (ins) { return ins.pause(); }); pausedinstances = activeinstances.slice(0); anime.running = activeinstances = []; } else { pausedinstances.foreach(function (ins) { return ins.play(); }); } } if (typeof document !== 'undefined') { document.addeventlistener('visibilitychange', handlevisibilitychange); } // public instance function anime(params) { if ( params === void 0 ) params = {}; var starttime = 0, lasttime = 0, now = 0; var children, childrenlength = 0; var resolve = null; function makepromise(instance) { var promise = window.promise && new promise(function (_resolve) { return resolve = _resolve; }); instance.finished = promise; return promise; } var instance = createnewinstance(params); var promise = makepromise(instance); function toggleinstancedirection() { var direction = instance.direction; if (direction !== 'alternate') { instance.direction = direction !== 'normal' ? 'normal' : 'reverse'; } instance.reversed = !instance.reversed; children.foreach(function (child) { return child.reversed = instance.reversed; }); } function adjusttime(time) { return instance.reversed ? instance.duration - time : time; } function resettime() { starttime = 0; lasttime = adjusttime(instance.currenttime) * (1 / anime.speed); } function seekchild(time, child) { if (child) { child.seek(time - child.timelineoffset); } } function syncinstancechildren(time) { if (!instance.reverseplayback) { for (var i = 0; i < childrenlength; i++) { seekchild(time, children[i]); } } else { for (var i$1 = childrenlength; i$1--;) { seekchild(time, children[i$1]); } } } function setanimationsprogress(instime) { var i = 0; var animations = instance.animations; var animationslength = animations.length; while (i < animationslength) { var anim = animations[i]; var animatable = anim.animatable; var tweens = anim.tweens; var tweenlength = tweens.length - 1; var tween = tweens[tweenlength]; // 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 = minmax(instime - tween.start - tween.delay, 0, tween.duration) / tween.duration; var eased = isnan(elapsed) ? 1 : tween.easing(elapsed); var strings = tween.to.strings; var round = tween.round; var numbers = []; 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]; var fromnumber = tween.from.numbers[n] || 0; if (!tween.ispath) { value = fromnumber + (eased * (tonumber - fromnumber)); } else { value = getpathprogress(tween.value, eased * tonumber); } if (round) { if (!(tween.iscolor && n > 2)) { value = math.round(value * round) / round; } } numbers.push(value); } // manual array.reduce for better performances var stringslength = strings.length; if (!stringslength) { progress = numbers[0]; } else { progress = strings[0]; for (var s = 0; s < stringslength; s++) { var a = strings[s]; var b = strings[s + 1]; var n$1 = numbers[s]; if (!isnan(n$1)) { if (!b) { progress += n$1 + ' '; } else { progress += n$1 + b; } } } } setprogressvalue[anim.type](animatable.target, anim.property, progress, animatable.transforms); anim.currentvalue = progress; i++; } } function setcallback(cb) { if (instance[cb] && !instance.passthrough) { instance[cb](instance); } } function countiteration() { if (instance.remaining && instance.remaining !== true) { instance.remaining--; } } function setinstanceprogress(enginetime) { var insduration = instance.duration; var insdelay = instance.delay; var insenddelay = insduration - instance.enddelay; var instime = adjusttime(enginetime); instance.progress = minmax((instime / insduration) * 100, 0, 100); instance.reverseplayback = instime < instance.currenttime; if (children) { syncinstancechildren(instime); } if (!instance.began && instance.currenttime > 0) { instance.began = true; setcallback('begin'); } if (!instance.loopbegan && instance.currenttime > 0) { instance.loopbegan = true; setcallback('loopbegin'); } if (instime <= insdelay && instance.currenttime !== 0) { setanimationsprogress(0); } if ((instime >= insenddelay && instance.currenttime !== insduration) || !insduration) { setanimationsprogress(insduration); } if (instime > insdelay && instime < insenddelay) { if (!instance.changebegan) { instance.changebegan = true; instance.changecompleted = false; setcallback('changebegin'); } setcallback('change'); setanimationsprogress(instime); } else { if (instance.changebegan) { instance.changecompleted = true; instance.changebegan = false; setcallback('changecomplete'); } } instance.currenttime = minmax(instime, 0, insduration); if (instance.began) { setcallback('update'); } if (enginetime >= insduration) { lasttime = 0; countiteration(); if (!instance.remaining) { instance.paused = true; if (!instance.completed) { instance.completed = true; setcallback('loopcomplete'); setcallback('complete'); if (!instance.passthrough && 'promise' in window) { resolve(); promise = makepromise(instance); } } } else { starttime = now; setcallback('loopcomplete'); instance.loopbegan = false; if (instance.direction === 'alternate') { toggleinstancedirection(); } } } } instance.reset = function() { var direction = instance.direction; instance.passthrough = false; instance.currenttime = 0; instance.progress = 0; instance.paused = true; instance.began = false; instance.loopbegan = false; instance.changebegan = false; instance.completed = false; instance.changecompleted = false; instance.reverseplayback = false; instance.reversed = direction === 'reverse'; instance.remaining = instance.loop; children = instance.children; childrenlength = children.length; for (var i = childrenlength; i--;) { instance.children[i].reset(); } if (instance.reversed && instance.loop !== true || (direction === 'alternate' && instance.loop === 1)) { instance.remaining++; } setanimationsprogress(instance.reversed ? instance.duration : 0); }; // set value helper instance.set = function(targets, properties) { settargetsvalue(targets, properties); return instance; }; instance.tick = function(t) { now = t; if (!starttime) { starttime = now; } setinstanceprogress((now + (lasttime - starttime)) * anime.speed); }; instance.seek = function(time) { setinstanceprogress(adjusttime(time)); }; instance.pause = function() { instance.paused = true; resettime(); }; instance.play = function() { if (!instance.paused) { return; } if (instance.completed) { instance.reset(); } instance.paused = false; activeinstances.push(instance); resettime(); if (!raf) { engine(); } }; instance.reverse = function() { toggleinstancedirection(); resettime(); }; instance.restart = function() { instance.reset(); instance.play(); }; instance.reset(); if (instance.autoplay) { instance.play(); } return instance; } // 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; 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(); } } } // stagger helpers function stagger(val, params) { if ( params === void 0 ) params = {}; var direction = params.direction || 'normal'; var easing = params.easing ? parseeasings(params.easing) : null; var grid = params.grid; var axis = params.axis; var fromindex = params.from || 0; var fromfirst = fromindex === 'first'; var fromcenter = fromindex === 'center'; var fromlast = fromindex === 'last'; var isrange = is.arr(val); var val1 = isrange ? parsefloat(val[0]) : parsefloat(val); var val2 = isrange ? parsefloat(val[1]) : 0; var unit = getunit(isrange ? val[1] : val) || 0; var start = params.start || 0 + (isrange ? val1 : 0); var values = []; var maxvalue = 0; return function (el, i, t) { if (fromfirst) { fromindex = 0; } if (fromcenter) { fromindex = (t - 1) / 2; } if (fromlast) { fromindex = t - 1; } if (!values.length) { for (var index = 0; index < t; index++) { if (!grid) { values.push(math.abs(fromindex - index)); } else { var fromx = !fromcenter ? fromindex%grid[0] : (grid[0]-1)/2; var fromy = !fromcenter ? math.floor(fromindex/grid[0]) : (grid[1]-1)/2; var tox = index%grid[0]; var toy = math.floor(index/grid[0]); var distancex = fromx - tox; var distancey = fromy - toy; var value = math.sqrt(distancex * distancex + distancey * distancey); if (axis === 'x') { value = -distancex; } if (axis === 'y') { value = -distancey; } values.push(value); } maxvalue = math.max.apply(math, values); } if (easing) { values = values.map(function (val) { return easing(val / maxvalue) * maxvalue; }); } if (direction === 'reverse') { values = values.map(function (val) { return axis ? (val < 0) ? val * -1 : -val : math.abs(maxvalue - val); }); } } var spacing = isrange ? (val2 - val1) / maxvalue : val1; return start + (spacing * (math.round(values[i] * 100) / 100)) + unit; } } // timeline function timeline(params) { if ( params === void 0 ) params = {}; var tl = anime(params); tl.duration = 0; tl.add = function(instanceparams, timelineoffset) { var tlindex = activeinstances.indexof(tl); var children = tl.children; if (tlindex > -1) { activeinstances.splice(tlindex, 1); } function passthrough(ins) { ins.passthrough = true; } for (var i = 0; i < children.length; i++) { passthrough(children[i]); } 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.timelineoffset = is.und(timelineoffset) ? tlduration : getrelativevalue(timelineoffset, tlduration); passthrough(tl); tl.seek(insparams.timelineoffset); var ins = anime(insparams); passthrough(ins); children.push(ins); var timings = getinstancetimings(children, params); tl.delay = timings.delay; tl.enddelay = timings.enddelay; tl.duration = timings.duration; tl.seek(0); tl.reset(); if (tl.autoplay) { tl.play(); } return tl; }; return tl; } anime.version = '3.1.0'; anime.speed = 1; anime.running = activeinstances; anime.remove = removetargets; anime.get = getoriginaltargetvalue; anime.set = settargetsvalue; anime.convertpx = convertpxtounit; anime.path = getpath; anime.setdashoffset = setdashoffset; anime.stagger = stagger; anime.timeline = timeline; anime.easing = parseeasings; anime.penner = penner; anime.random = function (min, max) { return math.floor(math.random() * (max - min + 1)) + min; }; module.exports = anime;