forked from shrekshrek/jstween
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjstween.min.js
More file actions
9 lines (8 loc) · 14 KB
/
jstween.min.js
File metadata and controls
9 lines (8 loc) · 14 KB
1
2
3
4
5
6
7
8
9
/*!
* VERSION: 0.6.0
* DATE: 2016-6-26
* GIT:https://github.com/shrekshrek/jstween
*
* @author: Shrek.wang, shrekshrek@gmail.com
**/
!function(t){var n="object"==typeof self&&self.self==self&&self||"object"==typeof global&&global.global==global&&global;"function"==typeof define&&define.amd?define(["exports"],function(e){n.JT=t(n,e)}):"undefined"!=typeof exports?t(n,exports):n.JT=t(n,{})}(function(t,n){function e(t,n){for(var e in n)t[e]=n[e]}function r(t,n){if("function"==typeof t)n.call(t,0,t);else if(void 0===t.length)n.call(t,0,t);else for(var e=0;e<t.length;e++)n.call(t[e],e,t[e])}function i(t){return t.replace(/([A-Z])/g,"-$1").toLowerCase()}function a(t){return t.replace(/\b(\w)|\s(\w)/g,function(t){return t.toUpperCase()})}function o(t){return Math.round(100*t)/100}function u(t){return t?D+a(t):D}function s(t){if(!t)throw"target is undefined, can't tween!!!";return"string"==typeof t?"undefined"==typeof document?t:document.querySelectorAll?document.querySelectorAll(t):document.getElementById("#"===t.charAt(0)?t.substr(1):t):t}function l(t,n){return"rotation"==n||"scale"==n?n:void 0!==t._jt_obj[n]?n:void 0!==t.style[n]?n:(n=u(n),void 0!==t.style[n]?n:void 0)}function c(t,n,e,r){var i={};if(n instanceof Array){i.num=[];for(var a in n){var o=f(t,n[a]);i.num[a]=o.num,i.unit=o.unit}void 0!=e&&(r?i.num.push(e.num):i.num.unshift(e.num))}else i=f(t,n);return i}function f(t,n){var e=p(n);"rem"==t.unit&&"rem"!=e.unit?(w(),t.num=o(t.num*Z),t.unit="px"):"rem"!=t.unit&&"rem"==e.unit&&(w(),t.num=o(t.num/Z),t.unit="rem");var r;switch(e.ext){case"+=":r=t.num+e.num;break;case"-=":r=t.num-e.num;break;default:r=e.num}return{num:r,unit:e.unit||t.unit}}function h(t){void 0==t._jt_obj&&(t._jt_obj={x:0,y:0,z:0,rotationX:0,rotationY:0,rotationZ:0,scaleX:1,scaleY:1,scaleZ:1,skewX:0,skewY:0})}function p(t){var n=/(\+=|-=|)(-|)(\d+\.\d+|\d+)(e[+-]?[0-9]{0,2}|)(rem|px|)/i,e=n.exec(t);return e?{num:o(e[2]+e[3]+e[4]),unit:e[5],ext:e[1]}:{num:0,unit:"px",ext:""}}function d(t){var n=/\S\s+\S/g;return n.test(t)}function m(t,n){switch(n){case"x":case"y":case"z":case"rotationX":case"rotationY":case"rotationZ":case"scaleX":case"scaleY":case"scaleZ":case"skewX":case"skewY":return t._jt_obj[n];case"rotation":return t._jt_obj.rotationZ;case"scale":return t._jt_obj.scaleX;default:return v(t,n)}}function v(t,n){if(t.style[n])return t.style[n];if(document.defaultView&&document.defaultView.getComputedStyle){var e=i(n),r=document.defaultView.getComputedStyle(t,"");return r&&r.getPropertyValue(e)}return t.currentStyle?t.currentStyle[n]:null}function y(t,n,e){switch(n){case"x":case"y":case"z":case"rotationX":case"rotationY":case"rotationZ":case"scaleX":case"scaleY":case"scaleZ":case"skewX":case"skewY":return t._jt_obj[n]=e,!0;case"rotation":return t._jt_obj.rotationZ=e,!0;case"scale":return t._jt_obj.scaleX=e,t._jt_obj.scaleY=e,!0;default:return _(t,n,e),!1}}function _(t,n,e){t.style[n]=e}function g(t){return"object"==typeof t&&1===t.nodeType}function j(t){var n="";(t._jt_obj.x||t._jt_obj.y||t._jt_obj.z)&&(n+="translate3d("+b(t._jt_obj.x)+","+b(t._jt_obj.y)+","+b(t._jt_obj.z)+") "),t._jt_obj.rotationX&&(n+="rotateX("+t._jt_obj.rotationX%360+"deg) "),t._jt_obj.rotationY&&(n+="rotateY("+t._jt_obj.rotationY%360+"deg) "),t._jt_obj.rotationZ&&(n+="rotateZ("+t._jt_obj.rotationZ%360+"deg) "),(1!=t._jt_obj.scaleX||1!=t._jt_obj.scaleY||1!=t._jt_obj.scaleZ)&&(n+="scale3d("+t._jt_obj.scaleX+", "+t._jt_obj.scaleY+", "+t._jt_obj.scaleZ+") "),(t._jt_obj.skewX||t._jt_obj.skewY)&&(n+="skew("+t._jt_obj.skewX+"deg,"+t._jt_obj.skewY+"deg) "),t.style[u("transform")]=n}function b(t){return t+("number"==typeof t?"px":"")}function w(){C||(C=document.createElement("div"),C.style.cssText="border:0 solid; position:absolute; line-height:0px;"),z||(z=document.getElementsByTagName("body")[0]),z.appendChild(C),C.style.borderLeftWidth="1rem",Z=parseFloat(C.offsetWidth),z.removeChild(C)}function P(){B=!0;var t,n,e=V.length,r=L.length;if(0===e&&0===r)return void(B=!1);var i=Y(),a=i-F;for(F=i,t=e-1;t>=0;t--)if(V[t]&&!V[t]._update(a)){var o=V.splice(t,1)[0];o.onUpdate&&o.onUpdate.apply(o,o.onUpdateParams),o.onEnd&&o.onEnd.apply(o,o.onEndParams),o.target=null}for(n=r-1;n>=0;n--)if(L[n]&&!L[n]._update(a)){var u=L.splice(n,1)[0];u.onEnd&&u.onEnd.apply(u,u.onEndParams)}q(P)}function M(){this.initialize.apply(this,arguments)}function E(t,n){var e=s(t),i=V.length;r(e,function(t,e){for(var r=i-1;r>=0;r--){var a=V[r];a.target===e&&a[n]()}})}function T(t){for(var n=V.length,e=n-1;e>=0;e--){var r=V[e];r[t]()}}function I(){this.initialize.apply(this,arguments)}function x(t,n){var e=t,i=L.length;r(e,function(t,e){for(var r=i-1;r>=0;r--){var a=L[r];a.onEnd===e&&a[n]()}})}function O(t){for(var n=L.length,e=n-1;e>=0;e--){var r=L[e];r[t]()}}function k(t){t.bezier&&(R(t,t.bezier),t.interpolation=A,delete t.bezier),t.through&&(R(t,t.through),t.interpolation=X,delete t.through),t.linear&&(R(t,t.linear),t.interpolation=S,delete t.linear)}function R(t,n){for(var e in n)for(var r in n[e])0==e?t[r]=[n[e][r]]:t[r].push(n[e][r])}function S(t,n){var e=t.length-1,r=e*n,i=Math.floor(r),a=N.Linear;return 0>n?a(t[0],t[1],r):n>1?a(t[e],t[e-1],e-r):a(t[i],t[i+1>e?e:i+1],r-i)}function A(t,n){var e,r=0,i=t.length-1,a=Math.pow,o=N.Bernstein;for(e=0;i>=e;e++)r+=a(1-n,i-e)*a(n,e)*t[e]*o(i,e);return r}function X(t,n){var e=t.length-1,r=e*n,i=Math.floor(r),a=N.Through;return t[0]===t[e]?(0>n&&(i=Math.floor(r=e*(1+n))),a(t[(i-1+e)%e],t[i],t[(i+1)%e],t[(i+2)%e],r-i)):0>n?t[0]-(a(t[0],t[0],t[1],t[1],-r)-t[0]):n>1?t[e]-(a(t[e],t[e],t[e-1],t[e-1],r-e)-t[e]):a(t[i?i-1:0],t[i],t[i+1>e?e:i+1],t[i+2>e?e:i+2],r-i)}Date.now=Date.now||function(){return(new Date).getTime()};var U=Date.now(),Y=function(){return Date.now()-U},D="";!function(){var t=document.createElement("div"),n=["Webkit","Moz","Ms","O"];for(var e in n)if(n[e]+"Transform"in t.style){D=n[e];break}}();var z,C,Z,q=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){window.setTimeout(t,1e3/60)},V=[],B=!1,F=0;e(M.prototype,{initialize:function(t,e,r,i,a){this.fromVars=r,this.curVars={},this.toVars=i,this.target=t,this.duration=1e3*Math.max(e,0),this.ease=i.ease||n.Linear.None,this.delay=1e3*Math.max(i.delay||0,0),this.yoyo=i.yoyo||!1,this.repeat=this.curRepeat=Math.floor(i.repeat||0),this.repeatDelay=1e3*Math.max(i.repeatDelay||0,0),this.onStart=i.onStart||null,this.onStartParams=i.onStartParams||[],this.onRepeat=i.onRepeat||null,this.onRepeatParams=i.onRepeatParams||[],this.onEnd=i.onEnd||null,this.onEndParams=i.onEndParams||[],this.onUpdate=i.onUpdate||null,this.onUpdateParams=i.onUpdateParams||[],this.isPlaying=i.isPlaying||!0,this.interpolation=i.interpolation||null,this.isReverse=!1,this.isDom=a,this.curTime=0,this.isStart=!1,this.startTime=this.delay,this.endTime=this.startTime+this.repeatDelay+this.duration,0!=this.delay&&(this._updateProp(),this.onUpdate&&this.onUpdate.apply(this,this.onUpdateParams)),V.push(this),B||(F=Y(),P())},_update:function(t){if(!this.isPlaying)return!0;if(this.curTime+=t,this.curTime<this.startTime)return!0;if(this.isStart||(this.isStart=!0,this.onStart&&this.onStart.apply(this,this.onStartParams),this.repeat==this.curRepeat&&(this.curTime+=this.repeatDelay)),this.curTime<this.startTime+this.repeatDelay)return!0;if(this.curTime<this.endTime)this._updateProp(),this.onUpdate&&this.onUpdate.apply(this,this.onUpdateParams);else{if(0==this.curRepeat)return this._updateProp(),!1;this.yoyo&&(this.isReverse=!this.isReverse),0==this.repeatDelay?(this.curTime=this.curTime-this.duration-this.repeatDelay,this._updateProp()):(this._updateProp(),this.curTime=this.curTime-this.duration-this.repeatDelay),this.onUpdate&&this.onUpdate.apply(this,this.onUpdateParams),this.onRepeat&&this.onRepeat.apply(this,this.onRepeatParams),this.curRepeat>0&&this.curRepeat--}return!0},_updateProp:function(){var t=0==this.duration?1:(this.curTime-this.startTime-this.repeatDelay)/this.duration;t=Math.max(0,Math.min(1,t)),this.isReverse&&(t=1-t);var n=this.ease(t),e=!1;for(var r in this.fromVars){var i,a=this.fromVars[r],u=this.toVars[r];i=u.num instanceof Array?this.interpolation(u.num,n):a.num+(u.num-a.num)*n,i=o(i),this.curVars[r]={num:i,unit:u.unit},this.isDom?(Math.abs(u.num-a.num)>20&&(i=Math.round(i)),y(this.target,r,i+(u.unit||0))&&(e=!0)):this.target[r]=i+(u.unit||0)}e&&j(this.target)},play:function(){this.target&&(this.isPlaying=!0)},pause:function(){this.target&&(this.isPlaying=!1)},destroy:function(t){if(this.target){var n=V.indexOf(this);if(-1!==n){var e=V.splice(n,1)[0];t&&e.onEnd&&e.onEnd.apply(e,e.onEndParams),this.target=null}}}}),e(n,{get:function(t,n){var e=s(t);if(void 0!==e.length&&(e=e[0]),g(e)){h(e);var r=l(e,n);return r?m(e,r):null}return e[n]},set:function(t,n){var e=s(t);r(e,function(t,e){if(g(e)){h(e);var r=!1;for(var i in n){var a=l(e,i);if(a)if(d(n[i]))y(e,a,n[i]);else{var o=c(p(m(e,a)),n[i]);y(e,a,o.num+(o.unit||0))&&(r=!0)}}r&&j(e)}else for(var u in n){var o=c(p(e[u]),n[u]);e[u]=o.num+(o.unit||0)}})},fromTo:function(t,n,e,i){k(i);var a=s(t),o=[];return r(a,function(t,r){var a={},u={},s=g(r);if(s){h(r);for(var f in i){var d=l(r,f);if(d){var v=p(m(r,d));a[d]=c(v,e[f]),u[d]=c(v,i[f],a[d],!1)}else u[f]=i[f]}}else for(var f in i)if(void 0!==r[f]){var v=p(r[f]);a[f]=c(v,e[f]),u[f]=c(v,i[f],a[f],!1)}else u[f]=i[f];var y=new M(r,n,a,u,s);o.push(y)}),1==o.length?o[0]:o},from:function(t,n,e){k(e);var i=s(t),a=[];return r(i,function(t,r){var i={},o={},u=g(r);if(u){h(r);for(var s in e){var f=l(r,s);if(f){var d=p(m(r,f));i[f]=c(d,e[s],d,!0),o[f]=d}else o[s]=e[s]}}else for(var s in e)if(void 0!==r[s]){var d=p(r[s]);i[s]=c(d,e[s],d,!0),o[s]=d}else o[s]=e[s];var v=new M(r,n,i,o,u);a.push(v)}),1==a.length?a[0]:a},to:function(t,n,e){k(e);var i=s(t),a=[];return r(i,function(t,r){var i={},o={},u=g(r);if(u){h(r);for(var s in e){var f=l(r,s);if(f){var d=p(m(r,f));i[f]=d,o[f]=c(d,e[s],d,!1)}else o[s]=e[s]}}else for(var s in e)if(void 0!==r[s]){var d=p(r[s]);i[s]=d,o[s]=c(d,e[s],d,!1)}else o[s]=e[s];var v=new M(r,n,i,o,u);a.push(v)}),1==a.length?a[0]:a},kill:function(t,n){var e=s(t);r(e,function(t,e){for(var r=V.length,i=r-1;i>=0;i--){var a=V[i];a.target===e&&(V.splice(i,1),n&&a.onEnd&&a.onEnd.apply(a,a.onEndParams),a.target=null)}})},killAll:function(t){if(!t)return void(V=[]);for(var n=V.length,e=n-1;e>=0;e--){var r=V.splice(e,1);t&&r.onEnd&&r.onEnd.apply(r,r.onEndParams),r.target=null}},play:function(t){E(t,"play")},playAll:function(){T("play")},pause:function(t){E(t,"pause")},pauseAll:function(){T("pause")}});var L=[];e(I.prototype,{initialize:function(t,n,e,r){this.delay=1e3*t,this.onEnd=n||null,this.onEndParams=e||[],this.curTime=0,this.endTime=this.delay,this.isPlaying=r||!0,L.push(this),B||(F=Y(),P())},_update:function(t){return this.isPlaying?(this.curTime+=t,this.curTime<this.endTime?!0:!1):!0},play:function(){this.isPlaying=!0},pause:function(){this.isPlaying=!1},destroy:function(t){var n=L.indexOf(this);if(-1!==n){var e=L.splice(n,1)[0];t&&e.onEnd&&e.onEnd.apply(e,e.onEndParams)}}}),e(n,{call:function(t,n,e){return new I(t,n,e)},killCall:function(t,n){var e=t,i=L.length;r(e,function(t,e){for(var r=i-1;r>=0;r--){var a=L[r];a.onEnd===e&&(L.splice(r,1),n&&a.onEnd&&a.onEnd.apply(a,a.onEndParams))}})},killAllCalls:function(t){if(!t)return void(L=[]);for(var n=L.length,e=n-1;e>=0;e--){var r=L.splice(e,1);t&&r.onEnd&&r.onEnd.apply(r,r.onEndParams)}},playCall:function(t){x(t,"play")},playAllCalls:function(){O("play")},pauseCall:function(t){x(t,"pause")},pauseAllCalls:function(){O("pause")}}),e(n,{path:function(t){k(t);for(var e,r=t.ease||n.Linear.None,i=t.step||1,a=[],o=0;i>=o;o++){e=r(o/i);var u={};for(var s in t)t[s]instanceof Array&&(u[s]=t.interpolation(t[s],e));a.push(u)}return a}});var N={Linear:function(t,n,e){return(n-t)*e+t},Bernstein:function(t,n){var e=N.Factorial;return e(t)/e(n)/e(t-n)},Factorial:function(){var t=[1];return function(n){var e,r=1;if(t[n])return t[n];for(e=n;e>1;e--)r*=e;return t[n]=r}}(),Through:function(t,n,e,r,i){var a=.5*(e-t),o=.5*(r-n),u=i*i,s=i*u;return(2*n-2*e+a+o)*s+(-3*n+3*e-2*a-o)*u+a*i+n}};return e(n,{Linear:{None:function(t){return t}},Quad:{In:function(t){return t*t},Out:function(t){return t*(2-t)},InOut:function(t){return(t*=2)<1?.5*t*t:-.5*(--t*(t-2)-1)}},Cubic:{In:function(t){return t*t*t},Out:function(t){return--t*t*t+1},InOut:function(t){return(t*=2)<1?.5*t*t*t:.5*((t-=2)*t*t+2)}},Quart:{In:function(t){return t*t*t*t},Out:function(t){return 1- --t*t*t*t},InOut:function(t){return(t*=2)<1?.5*t*t*t*t:-.5*((t-=2)*t*t*t-2)}},Quint:{In:function(t){return t*t*t*t*t},Out:function(t){return--t*t*t*t*t+1},InOut:function(t){return(t*=2)<1?.5*t*t*t*t*t:.5*((t-=2)*t*t*t*t+2)}},Sine:{In:function(t){return 1-Math.cos(t*Math.PI/2)},Out:function(t){return Math.sin(t*Math.PI/2)},InOut:function(t){return.5*(1-Math.cos(Math.PI*t))}},Expo:{In:function(t){return 0===t?0:Math.pow(1024,t-1)},Out:function(t){return 1===t?1:1-Math.pow(2,-10*t)},InOut:function(t){return 0===t?0:1===t?1:(t*=2)<1?.5*Math.pow(1024,t-1):.5*(-Math.pow(2,-10*(t-1))+2)}},Circ:{In:function(t){return 1-Math.sqrt(1-t*t)},Out:function(t){return Math.sqrt(1- --t*t)},InOut:function(t){return(t*=2)<1?-.5*(Math.sqrt(1-t*t)-1):.5*(Math.sqrt(1-(t-=2)*t)+1)}},Elastic:{In:function(t){var n,e=.1,r=.4;return 0===t?0:1===t?1:(!e||1>e?(e=1,n=r/4):n=r*Math.asin(1/e)/(2*Math.PI),-(e*Math.pow(2,10*(t-=1))*Math.sin(2*(t-n)*Math.PI/r)))},Out:function(t){var n,e=.1,r=.4;return 0===t?0:1===t?1:(!e||1>e?(e=1,n=r/4):n=r*Math.asin(1/e)/(2*Math.PI),e*Math.pow(2,-10*t)*Math.sin(2*(t-n)*Math.PI/r)+1)},InOut:function(t){var n,e=.1,r=.4;return 0===t?0:1===t?1:(!e||1>e?(e=1,n=r/4):n=r*Math.asin(1/e)/(2*Math.PI),(t*=2)<1?-.5*e*Math.pow(2,10*(t-=1))*Math.sin(2*(t-n)*Math.PI/r):e*Math.pow(2,-10*(t-=1))*Math.sin(2*(t-n)*Math.PI/r)*.5+1)}},Back:{In:function(t){var n=1.70158;return t*t*((n+1)*t-n)},Out:function(t){var n=1.70158;return--t*t*((n+1)*t+n)+1},InOut:function(t){var n=2.5949095;return(t*=2)<1?.5*t*t*((n+1)*t-n):.5*((t-=2)*t*((n+1)*t+n)+2)}},Bounce:{In:function(t){return 1-n.Bounce.Out(1-t)},Out:function(t){return 1/2.75>t?7.5625*t*t:2/2.75>t?7.5625*(t-=1.5/2.75)*t+.75:2.5/2.75>t?7.5625*(t-=2.25/2.75)*t+.9375:7.5625*(t-=2.625/2.75)*t+.984375},InOut:function(t){return.5>t?.5*n.Bounce.In(2*t):.5*n.Bounce.Out(2*t-1)+.5}}}),n.now=Y,n});