forked from lvivski/animatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anima.min.js
2 lines (2 loc) · 16.2 KB
/
anima.min.js
1
2
(function(){function t(t){var i=document.createElement("div").style,n=t[0].toUpperCase()+t.slice(1);return i.transform===void 0?h.filter(function(t){return i[t+n]!==void 0})[0]+n:t}function i(t,i){!document.styleSheets.length&&this.createStyleSheet(),this.stylesheet=document.styleSheets[0],this.item=t,this.animations=i,this.total=this.animations.map(function(t){return t.delay+t.duration}).reduce(function(t,i){return t+i}),this.style()}function n(){this.handlers={}}function e(t,i,e,s,r){n.call(this),this.item=t,this.translate=i.translate&&i.translate.map(parseFloat),this.rotate=i.rotate&&i.rotate.map(parseFloat),this.scale=i.scale,this.opacity=i.opacity,this.start=null,this.diff=null,this.duration=parseInt(i.duration||e,10)||500,this.delay=parseInt(i.delay||r,10)||0,this.ease=b[i.ease]||b[s]||b.linear,this.easeName=s||"linear"}function s(t,i,s,r,a){n.call(this),this.item=t,this.animations=i.map(function(i){return new e(t,{translate:i.translate,rotate:i.rotate,scale:i.scale,opacity:i.opacity},i.duration||s,i.ease||r,i.delay||a)}),this.start=null,this.delay=0,this.easeName=r||"linear",this.duration=Math.max.apply(null,this.animations.map(function(t){return t.duration+t.delay}))}function r(t){n.call(this),this.items=[],this._frame=null,t&&this.init()}function a(){r.call(this,!0),this.currentTime=0,this.start=0}function o(t){n.call(this),this.dom=t,this.animations=[],this.init()}var u=window.anima=window.a={};u.js=function(){return new r(!0)},u.css=function(){return new r},u.timeline=function(){return new a};for(var h=["webkit","Moz","O","ms"],c=0,p=window.requestAnimationFrame,m=window.cancelAnimationFrame;!p&&h.length>c;){var f=h[c++].toLowerCase();p=window[f+"RequestAnimationFrame"],m=window[f+"CancelAnimationFrame"]||window[f+"CancelRequestAnimationFrame"]}window.chrome&&!f&&(f=h[0]);var l=f?"-"+f+"-":"",y=t("transform"),d=t("animation"),v=t("transition"),b=function(){var t={quad:function(t){return Math.pow(t,2)},cubic:function(t){return Math.pow(t,3)},quart:function(t){return Math.pow(t,4)},quint:function(t){return Math.pow(t,5)},expo:function(t){return Math.pow(t,6)},sine:function(t){return 1-Math.cos(t*Math.PI/2)},circ:function(t){return 1-Math.sqrt(1-t*t)},back:function(t){return t*t*(3*t-2)}},i={linear:function(t){return t}};return Object.keys(t).forEach(function(n){var e=t[n];i["ease-in-"+n]=e,i["ease-out-"+n]=function(t){return 1-e(1-t)},i["ease-in-out-"+n]=function(t){return.5>t?e(2*t)/2:1-e(-2*t+2)/2}}),i.css={linear:"cubic-bezier(0.000, 0.000, 1.000, 1.000)","ease-in-quad":"cubic-bezier(0.550, 0.085, 0.680, 0.530)","ease-in-cubic":"cubic-bezier(0.550, 0.055, 0.675, 0.190)","ease-in-quart":"cubic-bezier(0.895, 0.030, 0.685, 0.220)","ease-in-quint":"cubic-bezier(0.755, 0.050, 0.855, 0.060)","ease-in-sine":"cubic-bezier(0.470, 0.000, 0.745, 0.715)","ease-in-expo":"cubic-bezier(0.950, 0.050, 0.795, 0.035)","ease-in-circ":"cubic-bezier(0.600, 0.040, 0.980, 0.335)","ease-in-back":"cubic-bezier(0.600, -0.280, 0.735, 0.045)","ease-out-quad":"cubic-bezier(0.250, 0.460, 0.450, 0.940)","ease-out-cubic":"cubic-bezier(0.215, 0.610, 0.355, 1.000)","ease-out-quart":"cubic-bezier(0.165, 0.840, 0.440, 1.000)","ease-out-quint":"cubic-bezier(0.230, 1.000, 0.320, 1.000)","ease-out-sine":"cubic-bezier(0.390, 0.575, 0.565, 1.000)","ease-out-expo":"cubic-bezier(0.190, 1.000, 0.220, 1.000)","ease-out-circ":"cubic-bezier(0.075, 0.820, 0.165, 1.000)","ease-out-back":"cubic-bezier(0.175, 0.885, 0.320, 1.275)","ease-in-out-quad":"cubic-bezier(0.455, 0.030, 0.515, 0.955)","ease-in-out-cubic":"cubic-bezier(0.645, 0.045, 0.355, 1.000)","ease-in-out-quart":"cubic-bezier(0.770, 0.000, 0.175, 1.000)","ease-in-out-quint":"cubic-bezier(0.860, 0.000, 0.070, 1.000)","ease-in-out-sine":"cubic-bezier(0.445, 0.050, 0.550, 0.950)","ease-in-out-expo":"cubic-bezier(1.000, 0.000, 0.000, 1.000)","ease-in-out-circ":"cubic-bezier(0.785, 0.135, 0.150, 0.860)","ease-in-out-back":"cubic-bezier(0.680, -0.550, 0.265, 1.550)"},i}();i.prototype.createStyleSheet=function(){var t=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(t)},i.prototype.pause=function(){return this.item.dom.style[d+"PlayState"]="paused",this},i.prototype.resume=function(){return this.item.dom.style[d+"PlayState"]="running",this},i.prototype.stop=function(){var t=getComputedStyle(this.item.dom),i=t[y],n=t.opacity,e=this.item.dom.style;return e[d]="",e[v]="",this.item.state=M.decompose(M.parse(i)),this.item.state.opacity=n,this.item.style(),this},i.prototype.handle=function(t){var i=function(){this.stop(),this.item.dom.removeEventListener(f+t,i,!1)}.bind(this);this.item.dom.addEventListener(f+t,i,!1)},i.prototype.style=function(){var t="a"+Date.now()+"r"+Math.floor(1e3*Math.random());if(this.item.animations[0]instanceof e&&1==this.item.animations.length){var i=this.item.animations[0];i.init(),this.item.dom.style[v]="all "+i.duration+"ms "+b.css[i.easeName]+" "+i.delay+"ms",i.transform(1),this.handle("TransitionEnd"),this.item.style()}else this.stylesheet.insertRule(this.keyframes(t),0),this.handle("AnimationEnd"),this.item.dom.style[d]=t+" "+this.total+"ms"+(this.item.infinite?" infinite ":" ")+"forwards";this.item.animations=[]},i.prototype.keyframes=function(t){for(var i=0,n=["@"+l+"keyframes "+t+"{"],s=0;this.animations.length>s;++s){var r=this.animations[s],a=this.animations[s+1];if(r.init(),r instanceof e)0===s&&n.push(this.frame(0,b.css[r.easeName])),r.delay&&n.push(this.frame(i+=r.delay)),r.transform(1),n.push(this.frame(i+=r.duration,a&&b.css[a.easeName]));else{var o=[];r.animations.forEach(function(t){t.delay&&-1===o.indexOf(t.delay)&&o.push(t.delay),t.duration&&-1===o.indexOf(t.delay+t.duration)&&o.push(t.delay+t.duration)}),o=o.sort(function(t,i){return t-i});for(var u=0;o.length>u;++u){for(var h=o[u],c=0;r.animations.length>c;++c){var p=r.animations[c];p.delay>=h||h>p.delay+p.duration||p.transform(p.ease((h-p.delay)/p.duration))}n.push(this.frame(i+=h))}}}return n.push("}"),n.join("")},i.prototype.percent=function(t){return(100*t/this.total).toFixed(3)},i.prototype.frame=function(t,i){var n=this.percent(t);return n+"% {"+(n?l+"transform:"+this.item.transform()+";":"")+(n?"opacity:"+this.item.opacity()+";":"")+(i?l+"animation-timing-function:"+i+";":"")+"}"},n.prototype.on=function(t,i){return(this.handlers[t]=this.handlers[t]||[]).push(i),this},n.prototype.off=function(t,i){var n=this.handlers[t];return i?n.splice(n.indexOf(i),1):this.handlers[t]=[],this},n.prototype.emit=function(t){var i=Array.prototype.slice.call(arguments,1),n=this.handlers[t];if(n)for(var e=0;n.length>e;++e)n[e].apply(this,i);return this},e.prototype=new n,e.prototype.init=function(t,i){if(null===this.start||i){this.start=t+this.delay;var n=this.item.state;this.initial={translate:n.translate.slice(),rotate:n.rotate.slice(),scale:n.scale.slice(),opacity:n.opacity},this.emit("start")}},e.prototype.animate=function(){return this.item.animate.apply(this.item,arguments)},e.prototype.css=function(){return this.item.css()},e.prototype.infinite=function(){return this.item.infinite=!0,this},e.prototype.run=function(t){if(!(this.start>t)){var i=(t-this.start)/this.duration;i=this.ease(i),this.transform(i)}},e.prototype.pause=function(){this.diff=Date.now()-this.start},e.prototype.resume=function(){this.start=Date.now()-this.diff},e.prototype.set=function(t,i){var n=this.item.state,e=this.initial;if(Array.isArray(this[t]))for(var s=0;3>s;++s)this[t][s]&&(n[t][s]=e[t][s]+this[t][s]*i);else this[t]!==void 0&&(n[t]=e[t]+(this[t]-e[t])*i)},e.prototype.transform=function(t){this.set("translate",t),this.set("rotate",t),this.set("scale",t),this.set("opacity",t)},e.prototype.end=function(t){!t&&this.transform(1),this.start=null,this.emit("end")},s.prototype=new n,s.prototype.init=function(t,i){if(null===this.start||i){this.start=t;for(var n=0;this.animations.length>n;++n)this.animations[n].init(t,i);this.emit("start")}},s.prototype.animate=function(){return this.item.animate.apply(this.item,arguments)},s.prototype.css=function(){return this.item.css()},s.prototype.infinite=function(){return this.item.infinite=!0,this},s.prototype.run=function(t){for(var i=0;this.animations.length>i;++i){var n=this.animations[i];t>=n.start+n.duration?(this.animations.splice(i--,1),n.end()):n.run(t)}},s.prototype.pause=function(){for(var t=0;this.animations.length>t;++t)this.animations[t].pause()},s.prototype.resume=function(){for(var t=0;this.animations.length>t;++t)this.animations[t].resume()},s.prototype.end=function(t){for(var i=0;this.animations.length>i;++i)this.animations[i].end(t);this.emit("end")},r.prototype=new n,r.prototype.init=function(){function t(n){i.update(n),i._frame=p(t)}var i=this;this._frame=p(t)},r.prototype.update=function(t){for(var i=0;this.items.length>i;++i)this.items[i].update(t)},r.prototype.add=function(t){var i=new o(t);return this.items.push(i),i},r.prototype.cancel=function(){this._frame&&m(this._frame),this._frame=0},r.prototype.stop=function(){this.cancel();for(var t=0;this.items.length>t;++t)this.items[t].stop()},r.prototype.pause=function(){this.cancel();for(var t=0;this.items.length>t;++t)this.items[t].pause()},r.prototype.resume=function(){for(var t=0;this.items.length>t;++t)this.items[t].resume();this.init()},a.prototype=new r,a.prototype.init=function(){function t(n){i.running&&(i.currentTime=n-i.start),i.update(i.currentTime),i._frame=p(t)}this._frame=p(t);var i=this},a.prototype.update=function(t){for(var i=0;this.items.length>i;++i)this.items[i].timeline(t);this.emit("update",t)},a.prototype.play=function(){this.running=!0,this.start=Date.now()-this.currentTime},a.prototype.pause=function(){this.running=!1},a.prototype.stop=function(){this.currentTime=0,this.running=!1},a.prototype.seek=function(t){this.currentTime=t};var g={set:function(t,i,n){return[t,i,n]},length:function(t,i,n){return Array.isArray(t)&&(i=t[1],n=t[2],t=t[0]),Math.sqrt(t*t+i*i+n*n)},add:function(t,i){return[t[0]+i[0],t[1]+i[1],t[2]+i[2]]},sub:function(t,i){return[t[0]-i[0],t[1]-i[1],t[2]-i[2]]},norm:function(t,i,n){Array.isArray(t)&&(i=t[1],n=t[2],t=t[0]);var e=this.length(t,i,n);return 0!==e?(t/=e,i/=e,n/=e):(t=0,i=0,n=0),[t,i,n]},cross:function(t,i){var n=t[1]*i[2]-t[2]*i[1],e=t[2]*i[0]-t[0]*i[2],s=t[1]*i[1]-t[1]*i[0];return[n,e,s]},copy:function(t){return[t[0],t[1],t[2]]}},w=Math.PI/180,M={identity:function(){return[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]},multiply:function z(t,i){var n=this.identity();return n[0]=t[0]*i[0]+t[1]*i[4]+t[2]*i[8],n[1]=t[0]*i[1]+t[1]*i[5]+t[2]*i[9],n[2]=t[0]*i[2]+t[1]*i[6]+t[2]*i[10],n[4]=t[4]*i[0]+t[5]*i[4]+t[6]*i[8],n[5]=t[4]*i[1]+t[5]*i[5]+t[6]*i[9],n[6]=t[4]*i[2]+t[5]*i[6]+t[6]*i[10],n[8]=t[8]*i[0]+t[9]*i[4]+t[10]*i[8],n[9]=t[8]*i[1]+t[9]*i[5]+t[10]*i[9],n[10]=t[8]*i[2]+t[9]*i[6]+t[10]*i[10],n[12]=t[12]*i[0]+t[13]*i[4]+t[14]*i[8]+i[12],n[13]=t[12]*i[1]+t[13]*i[5]+t[14]*i[9]+i[13],n[14]=t[12]*i[2]+t[13]*i[6]+t[14]*i[10]+i[14],2>=arguments.length?n:z.apply(this,[n].concat(Array.prototype.slice.call(arguments,2)))},translate:function(t,i,n){return t||i||n?(t||(t=0),i||(i=0),n||(n=0),[1,0,0,0,0,1,0,0,0,0,1,0,t,i,n,1]):this.identity()},scale:function(t,i,n){return t||i||n?(t||(t=1),i||(i=1),n||(n=1),[t,0,0,0,0,i,0,0,0,0,n,0,0,0,0,1]):this.identity()},rotate:function(t,i,n){if(!(t||i||n))return this.identity();t||(t=0),i||(i=0),n||(n=0),t*=w,i*=w,n*=w;var e=Math.sin(t),s=Math.cos(t),r=Math.sin(i),a=Math.cos(i),o=Math.sin(n),u=Math.cos(n);return[a*u,s*o+e*r*u,e*o-s*r*u,0,-a*o,s*u-e*r*o,e*u+s*r*o,0,r,-e*a,s*a,0,0,0,0,1]},rotate3d:function(t,i,n,e){e||(e=0),e*=w;var s=Math.sin(e),r=Math.cos(e),a=g.norm(t,i,n);t=a[0],i=a[1],n=a[2];var o=t*t,u=i*i,h=n*n,c=1-r;return[o+(1-o)*r,t*i*c+n*s,t*n*c-i*s,0,t*i*c-n*s,u+(1-u)*r,i*n*c+t*s,0,t*n*c+i*s,i*n*c-t*s,h+(1-h)*r,0,0,0,0,1]},skew:function(t,i){return t||i?(t||(t=0),i||(i=0),t*=w,i*=w,[1,Math.tan(i),0,0,Math.tan(t),1,0,0,0,0,1,0,0,0,0,1]):this.identity()},perspective:function(t){return t=-1/t,[1,0,0,0,0,1,0,0,0,0,1,t,0,0,0,1]},parse:function(t){var i=t.match(/\((.+)\)/)[1].split(/,\s?/);return 6===i.length&&(i.splice(2,0,"0","0"),i.splice(6,0,"0","0"),i.splice(8,0,"0","0","1","0"),i.push("0","1")),i},inverse:function(t){var i=this.identity(),n=t[5]*t[10]-t[6]*t[9],e=t[1]*t[10]-t[2]*t[9],s=t[1]*t[6]-t[2]*t[5],r=t[4]*t[10]-t[6]*t[8],a=t[0]*t[10]-t[2]*t[8],o=t[0]*t[6]-t[2]*t[4],u=t[4]*t[9]-t[5]*t[8],h=t[0]*t[9]-t[1]*t[8],c=t[0]*t[5]-t[1]*t[4],p=1/(t[0]*n-t[1]*r+t[2]*u);return i[0]=p*n,i[1]=-p*e,i[2]=p*s,i[4]=-p*r,i[5]=p*a,i[6]=-p*o,i[8]=p*u,i[9]=-p*h,i[10]=p*c,i[12]=-t[12]*i[0]-t[13]*i[4]-t[14]*i[8],i[13]=-t[12]*i[1]-t[13]*i[5]-t[14]*i[9],i[14]=-t[12]*i[2]-t[13]*i[6]-t[14]*i[10],i},compose:function(t,i,n){t||(t=[]),i||(i=[]),n||(n=[]);var e=this.rotate(i[0],i[1],i[2]);return n.length&&(e[0]*=n[0],e[1]*=n[0],e[2]*=n[0],e[4]*=n[1],e[5]*=n[1],e[6]*=n[1],e[8]*=n[2],e[9]*=n[2],e[10]*=n[2]),t.length&&(e[12]=t[0],e[13]=t[1],e[14]=t[2]),e},decompose:function(t){var i=g.length(t[0],t[1],t[2]),n=g.length(t[4],t[5],t[6]),e=g.length(t[8],t[9],t[10]),s=Math.atan2(-t[9]/e,t[10]/e)/w,r=Math.asin(t[8]/e)/w,a=Math.atan2(-t[4]/n,t[0]/i)/w;(1===t[4]||-1===t[4])&&(s=0,r=t[4]*-Math.PI/2,a=t[4]*Math.atan2(t[6]/n,t[5]/n)/w);var o=t[12],u=t[13],h=t[14];return{translate:[o,u,h],rotate:[s,r,a],scale:[i,n,e]}},transpose:function(t){var i;return i=t[1],t[1]=t[4],t[4]=i,i=t[2],t[2]=t[8],t[8]=i,i=t[6],t[6]=t[9],t[9]=i,i=t[3],t[3]=t[12],t[12]=i,i=t[7],t[7]=t[13],t[13]=i,i=t[11],t[11]=t[14],t[14]=i,t},lookAt:function(t,i,n){var e=g.sub(t,i);e=g.norm(e),0===g.length(e)&&(e[2]=1);var s=g.cross(n,e);0===g.length(s)&&(e[0]+=1e-4,s=g.norm(g.cross(n,e)));var r=g.cross(e,s),a=this.identity();return a[0]=s[0],a[1]=s[1],a[2]=s[2],a[4]=r[0],a[5]=r[1],a[6]=r[2],a[8]=e[0],a[9]=e[1],a[10]=e[2],a},stringify:function(t){for(var i=0;t.length>i;++i)1e-6>Math.abs(t[i])&&(t[i]=0);return"matrix3d("+t.join()+")"},toTestString:function(t){function i(t){return t.toFixed(6)}function n(t){return 0===t[2]&&0===t[3]&&0===t[6]&&0===t[7]&&0===t[8]&&0===t[9]&&1===t[10]&&0===t[11]&&0===t[14]&&1===t[15]}function e(t,i){return-1!==[0,1,4,5,12,13].indexOf(i)}return n(t)?"matrix("+t.filter(e).map(i).join(", ")+")":"matrix3d("+t.map(i).join(", ")+")"}};o.prototype=new n,o.prototype.init=function(){this.infinite=!1,this.running=!0,this.state={translate:[0,0,0],rotate:[0,0,0],scale:[1,1,1],opacity:1}},o.prototype.update=function(t){this.animation(t),this.style()},o.prototype.timeline=function(t){this.seek(t),this.style()},o.prototype.pause=function(){this.running&&(this.animations.length&&this.animations[0].pause(),this.running=!1)},o.prototype.resume=function(){this.running||(this.animations.length&&this.animations[0].resume(),this.running=!0)},o.prototype.style=function(){this.dom.style[y]=this.transform(),this.dom.style.opacity=this.opacity()},o.prototype.transform=function(){return M.stringify(this.matrix())},o.prototype.matrix=function(){var t=this.state;return M.compose(t.translate,t.rotate,t.scale)},o.prototype.center=function(){return M.decompose(M.inverse(this.matrix()))},o.prototype.lookAt=function(t){var i=M.decompose(M.lookAt(t,this.state.translate,[0,1,0]));this.state.rotate=i.rotate},o.prototype.opacity=function(){return this.state.opacity},o.prototype.add=function(t,i){return this.state[t][0]+=i[0],this.state[t][1]+=i[1],this.state[t][2]+=i[2],this},o.prototype.set=function(t,i){return this.state[t]=i,this},o.prototype.translate=function(t){return this.add("translate",t)},o.prototype.rotate=function(t){return this.add("rotate",t)},o.prototype.scale=function(t){return this.add("scale",t)},o.prototype.clear=function(){this.state.translate=[0,0,0],this.state.rotate=[0,0,0],this.state.scale=[1,1,1],this.state.opacity=1},o.prototype.animate=function(t,i,n,r){var a=Array.isArray(t)?s:e,o=new a(this,t,i,n,r);return this.animations.push(o),o},o.prototype.animation=function(t){if(this.running&&0!==this.animations.length)for(;0!==this.animations.length;){var i=this.animations[0];i.init(t);{if(!(t>=i.start+i.duration)){i.run(t);break}this.infinite&&this.animations.push(i),this.animations.shift(),i.end()}}},o.prototype.seek=function(t){if(0!==this.animations.length){this.clear();for(var i=0,n=0;this.animations.length>n;++n){var e=this.animations[n];e.init(i,!0);{if(!(t>=e.start+e.duration)){e.run(t);break}e.end(),i+=e.delay+e.duration}}}},o.prototype.finish=function(t){if(0===this.animations.length)return this;for(var i=0;this.animations.length>i;++i){var n=this.animations[i];n.end(t)}return this.animations=[],this.infinite=!1,this},o.prototype.stop=function(){return this.finish(!0)},o.prototype.css=function(){return new i(this,this.animations)}})();
//@ sourceMappingURL=anima.min.js.map