/
motio.min.js
8 lines (8 loc) · 3.27 KB
/
motio.min.js
1
2
3
4
5
6
7
8
/*! Motio 2.0.0 - 5th Apr 2013 | https://github.com/Darsain/motio */
(function(n){function z(p,m){function H(c){b.reversed=c;t||(a.isPaused=0,A("play"),E())}function v(){w=0;F=B?Math.round(d.x)+"px "+Math.round(d.y)+"px":q[h];F!==I&&(p.style.backgroundPosition=I=F);A("frame");b.finite&&b.to===h&&(a.pause(),"function"===j(b.callback)&&b.callback.call(a))}function E(){b.finite&&b.to===h||(B?(d.x+=g.speedX/g.fps,d.y+=g.speedY/g.fps,g.bgWidth&&Math.abs(d.x)>g.bgWidth&&(d.x%=g.bgWidth),g.bgHeight&&Math.abs(d.y)>g.bgHeight&&(d.y%=g.bgHeight)):(b.finite?h=b.immediate?b.to:
h+(h>b.to?-1:1):b.reversed?0>=--h&&(h=q.length-1):++h>=q.length&&(h=0),a.frame=h),b.immediate||(t=60<=g.fps?C(E):setTimeout(E,1E3/g.fps)),w||(w=C(v)))}function y(a,f){r=0;for(D=k[a].length;r<D;r++)if(k[a][r]===f)return r;return-1}function A(c,f){if(k[c]){r=0;for(D=k[c].length;r<D;r++)k[c][r].call(a,c,f)}}function G(a){return n.getComputedStyle?n.getComputedStyle(p,null)[a]:p.currentStyle[a]}var g,s=m,x={},s="object"===j(s)?s:{},l;for(l in z.defaults)x[l]=(s.hasOwnProperty(l)?s:z.defaults)[l];g=x;
var a=this,B=!g.frames,q=[],k={},b={},h=0,s=p.clientWidth,x=p.clientHeight,d,F,I,t,w,r,D;a.width=s;a.height=x;a.options=g;a.isPaused=1;a.pause=function(){t&&(a.isPaused=1,u(t),t=clearTimeout(t),w=u(w),A("pause"));return a};a.play=function(c){b.finite=0;b.callback=0;H(c);return a};a.toggle=function(){A("toggle");a[t?"pause":"play"]();return a};a.toStart=function(c,f){return a.to(0,c,f)};a.toEnd=function(c,f){return a.to(q.length-1,c,f)};a.to=function(c,f,e){if(B||isNaN(parseFloat(c))||!isFinite(c)||
0>c||c>=q.length)return a;"function"===j(f)&&(e=f,f=!1);if(c===h)if(0===c)h=q.length;else if(c===q.length-1)h=-1;else return"function"===j(e)&&e.call(a),a.pause(),a;b.finite=1;b.to=c;b.immediate=f;b.callback=e;H();return a};a.set=function(c,f){g[c]=f;return a};a.on=function(c,f){if("object"===j(c))for(var e in c){if(c.hasOwnProperty(e))a.on(e,c[e])}else if("function"===j(f)){e=c.split(" ");for(var b=0,d=e.length;b<d;b++)k[e[b]]=k[e[b]]||[],-1===y(e[b],f)&&k[e[b]].push(f)}else if("array"===j(f)){e=
0;for(b=f.length;e<b;e++)a.on(c,f[e])}return a};a.off=function(c,b){if(b instanceof Array)for(var e=0,d=b.length;e<d;e++)a.off(c,b[e]);else for(var e=c.split(" "),d=0,g=e.length;d<g;d++)if(k[e[d]]=k[e[d]]||[],"undefined"===j(b))k[e[d]].length=0;else{var h=y(e[d],b);-1!==h&&k[e[d]].splice(h,1)}return a};a.destroy=function(){a.pause();p.style.backgroundPosition="";return a};l=(G("backgroundPosition")||G("backgroundPositionX")+" "+G("backgroundPositionY")).replace(/left|top/gi,0).split(" ");d={x:0|parseInt(l[0],
10),y:0|parseInt(l[1],10)};if(B)a.pos=d;else{for(l=q.length=0;l<g.frames;l++)g.vertical?d.y=l*-x:d.x=l*-s,q.push(d.x+"px "+d.y+"px");a.frames=q.length;a.frame=0}}function j(p){return Object.prototype.toString.call(p).match(/\s([a-z]+)/i)[1].toLowerCase()}for(var u=n.cancelAnimationFrame||n.cancelRequestAnimationFrame,C=n.requestAnimationFrame,v=["moz","webkit","o"],y=0,m=0,J=v.length;m<J&&!u;++m)C=(u=n[v[m]+"CancelAnimationFrame"]||n[v[m]+"CancelRequestAnimationFrame"])&&n[v[m]+"RequestAnimationFrame"];
u||(C=function(p){var j=+new Date,m=Math.max(0,16-(j-y));y=j+m;return n.setTimeout(function(){p(j+m)},m)},u=function(j){clearTimeout(j)});n.Motio=z;z.defaults={fps:15,frames:0,vertical:0,speedX:0,speedY:0,bgWidth:0,bgHeight:0}})(window);