-
Notifications
You must be signed in to change notification settings - Fork 72
/
jstween.min.js
5 lines (4 loc) · 15.3 KB
/
jstween.min.js
1
2
3
4
5
/*!
* GIT: https://github.com/shrekshrek/jstween
**/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.JT=e()}(this,function(){"use strict";var u={};function e(t,e){if(t.length&&0<t.length)for(var n=0;n<t.length;n++)e.call(t[n],n,t[n]);else e.call(t,0,t)}function r(t){return t.replace(/([A-Z])/g,"-$1").toLowerCase()}function o(t){return Math.round(1e3*t)/1e3}Date.now=Date.now||function(){return(new Date).getTime()};var t=Date.now(),F=(u.now=function(){return Date.now()-t},function(){var t,e=document.createElement("div"),n=["Webkit","Moz","Ms","O"];for(t in n)if(n[t]+"Transform"in e.style)return n[t]}());function a(t){return F+(t?t.replace(/\b(\w)|\s(\w)/g,function(t){return t.toUpperCase()}):"")}var s=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(t){window.setTimeout(t,1e3/60)};function i(t){if(t)return"string"==typeof t?document.querySelectorAll(t):t;throw"el is undefined, can't tween!!!"}var h=["ease","delay","yoyo","repeat","repeatDelay","onStart","onStartScope","onStartParams","onRepeat","onRepeatScope","onRepeatParams","onEnd","onEndScope","onEndParams","onUpdate","onUpdateScope","onUpdateParams","interpolation","isReverse","timeScale","isFrom","isPlaying"],c=["rotation","scale","autoAlpha"];function m(t,e,n){for(var i=0,r=h.length;i<r;i++)if(e===h[i])return;if(n){for(i=0,r=c.length;i<r;i++)if(e===c[i])return e;if(void 0!==t._jt_obj[e])return e;if(void 0!==t.style[e])return e;if(e=a(e),void 0!==t.style[e])return e}else if("string"==typeof t[e]||"number"==typeof t[e])return e}function l(t,e){var n={};if(Array.isArray(e)){n.num=[t.num];for(var i=0,r=e.length;i<r;i++){var a=f(t,e[i]);n.num.push(a.num),n.unit=a.unit}}else n=f(t,e);return n}function f(t,e){var n,i=d(e);switch(i.unit){case"rem":switch(t.unit){case"rem":break;case"vw":var r=g(),a=S();t.num=o(t.num*a/r),t.unit="rem";break;case"vh":var r=g(),s=k();t.num=o(t.num*s/r),t.unit="rem";break;default:r=g();t.num=o(t.num/r),t.unit="rem"}break;case"vw":switch(t.unit){case"rem":r=g(),a=S();t.num=o(t.num*r/a),t.unit="vw";break;case"vw":break;case"vh":s=k(),a=S();t.num=o(t.num*s/a),t.unit="vw";break;default:a=S();t.num=o(t.num/a),t.unit="vw"}break;case"vh":switch(t.unit){case"rem":r=g(),s=k();t.num=o(t.num*r/s),t.unit="vh";break;case"vw":a=S(),s=k();t.num=o(t.num*a/s),t.unit="vh";break;case"vh":break;default:s=k();t.num=o(t.num/s),t.unit="vh"}break;default:switch(t.unit){case"rem":r=g();t.num=o(t.num*r),t.unit="px";break;case"vw":a=S();t.num=o(t.num*a),t.unit="px";break;case"vh":s=k();t.num=o(t.num*s),t.unit="px"}}switch(i.ext){case"+=":n=t.num+i.num;break;case"-=":n=t.num-i.num;break;default:n=i.num}return{num:n,unit:i.unit}}function p(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 d(t){t=/(\+=|-=|)(-|)(\d+\.\d+|\d+)(e[+-]?[0-9]{0,2}|)(rem|px|%|vw|vh|)/i.exec(t);return t?{num:o(t[2]+t[3]+t[4]),unit:t[5],ext:t[1]}:{num:0,unit:"px",ext:""}}function v(t,e){switch(e){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[e];case"rotation":return t._jt_obj.rotationZ;case"scale":return t._jt_obj.scaleX;case"autoAlpha":return n(t,"opacity");default:return n(t,e)}}function n(t,e){return t.style[e]||(e=r(e),(t=window.getComputedStyle(t,null))[e]||t.getPropertyValue(e))}var y,_,U={"column-count":1,columns:1,"font-weight":1,"line-height":1,opacity:1,"z-index":1,zoom:1};function b(t,e,n,i){switch(e){case"x":case"y":case"z":return t._jt_obj[e]=n+(i||"px"),1;case"rotationX":case"rotationY":case"rotationZ":case"skewX":case"skewY":return t._jt_obj[e]=n%360+"deg",1;case"scaleX":case"scaleY":case"scaleZ":return t._jt_obj[e]=n,1;case"rotation":return t._jt_obj.rotationZ=n%360+"deg",1;case"scale":return t._jt_obj.scaleX=n,t._jt_obj.scaleY=n,1;case"autoAlpha":return j(t,"opacity",n),void j(t,"display",0<n?"block":"none");default:return void j(t,e,n=void 0!==i?U[r(e)]?n+i:n+(i||"px"):n)}}function j(t,e,n){t.style[e]=n}function w(t){return"object"==typeof t&&1===t.nodeType}function T(t){var e="";(t._jt_obj.x||t._jt_obj.y||t._jt_obj.z)&&(e+="translate3d("+t._jt_obj.x+","+t._jt_obj.y+","+t._jt_obj.z+") "),t._jt_obj.rotationX&&(e+="rotateX("+t._jt_obj.rotationX+") "),t._jt_obj.rotationY&&(e+="rotateY("+t._jt_obj.rotationY+") "),t._jt_obj.rotationZ&&(e+="rotateZ("+t._jt_obj.rotationZ+") "),1===t._jt_obj.scaleX&&1===t._jt_obj.scaleY&&1===t._jt_obj.scaleZ||(e+="scale3d("+t._jt_obj.scaleX+", "+t._jt_obj.scaleY+", "+t._jt_obj.scaleZ+") "),(t._jt_obj.skewX||t._jt_obj.skewY)&&(e+="skew("+t._jt_obj.skewX+","+t._jt_obj.skewY+") "),t.style[a("transform")]=e}function g(){_||((_=document.createElement("div")).style.cssText="border:0 solid; position:absolute; line-height:0px;"),(y=y||document.getElementsByTagName("body")[0]).appendChild(_),_.style.borderLeftWidth="1rem";var t=parseFloat(_.offsetWidth);return y.removeChild(_),t}function S(){return window.innerWidth/100}function k(){return window.innerHeight/100}u.getRem=g,u.getVw=S,u.getVh=k;var M=[],P=[],R=!1,I=0;function z(){var t=M.length;if(0===t)R=!1;else{var e=u.now(),n=e-I;I=e,500<n&&(n=33),P=M.slice(0);for(var i=0;i<t;i++){var r=P[i];r&&r.isPlaying&&!r._update(n)&&r.pause()}s(z)}}function x(){this.initialize.apply(this,arguments)}function D(t){if(!t.isInited)for(var e in t.isInited=!0,t.fromVars){var n=d(t.isDom?v(t.el,e):t.el[e]);t.fromVars[e]=null===t.fromVars[e]?n:l(n,t.fromVars[e]),t.toVars[e]=null===t.toVars[e]?n:l(t.fromVars[e],t.toVars[e])}}function O(u,t,h,c,l){if("number"!=typeof h)throw"The second parameter must be a number!";Z(l);var t=i(t),f=[];return e(t,function(t,e){var n,i,r={},a={},s=w(e);switch(u){case"fromTo":(n=l).isFrom=!0;break;case"from":(n=c).isFrom=!0;break;case"to":(n=l).isFrom=!1}for(i in s&&p(e),n){var o=m(e,i,s);if(o)switch(u){case"fromTo":r[o]=c[i],a[o]=l[i];break;case"from":r[o]=c[i],a[o]=null;break;case"to":r[o]=null,a[o]=l[i]}else a[i]=n[i]}f.push(new x(e,h,r,a,s))}),0===f.length?null:1===f.length?f[0]:f}function E(t,r,a){var t=i(t),s=M.length;e(t,function(t,e){for(var n=s-1;0<=n;n--){var i=M[n];i.el===e&&i[r](a)}})}function A(t,e){for(var n=M.length-1;0<=n;n--)M[n][t](e)}function Z(t){t.bezier&&(Y(t,t.bezier),t.interpolation=K,delete t.bezier),t.through&&(Y(t,t.through),t.interpolation=B,delete t.through),t.linear&&(Y(t,t.linear),t.interpolation=q,delete t.linear)}function Y(t,e){for(var n=0,i=e.length;n<i;n++)for(var r in e[n])0===n?t[r]=[e[n][r]]:t[r].push(e[n][r])}function q(t,e){var n=t.length-1,i=n*e,r=Math.floor(i),a=X.Linear;return e<0?a(t[0],t[1],i):1<e?a(t[n],t[n-1],n-i):a(t[r],t[n<r+1?n:r+1],i-r)}function K(t,e){for(var n=0,i=t.length-1,r=Math.pow,a=X.Bernstein,s=0;s<=i;s++)n+=r(1-e,i-s)*r(e,s)*t[s]*a(i,s);return n}function B(t,e){var n=t.length-1,i=n*e,r=Math.floor(i),a=X.Through;return t[0]===t[n]?a(t[((r=e<0?Math.floor(i=n*(1+e)):r)-1+n)%n],t[r],t[(r+1)%n],t[(r+2)%n],i-r):e<0?t[0]-(a(t[0],t[0],t[1],t[1],-i)-t[0]):1<e?t[n]-(a(t[n],t[n],t[n-1],t[n-1],i-n)-t[n]):a(t[r?r-1:0],t[r],t[n<r+1?n:r+1],t[n<r+2?n:r+2],i-r)}Object.assign(x.prototype,{initialize:function(t,e,n,i,r){this.fromVars=n,this.curVars={},this.toVars=i,this.el=t,this.duration=1e3*Math.max(e,0),this.ease=i.ease||u.Linear.None,this.delay=1e3*Math.max(i.delay||0,0),this.yoyo=i.yoyo||!1,this.repeat=i.repeat||0,this.repeatDelay=1e3*Math.max(i.repeatDelay||0,0),this.onStart=i.onStart||null,this.onStartScope=i.onStartScope||this,this.onStartParams=i.onStartParams||[],this.onRepeat=i.onRepeat||null,this.onRepeatScope=i.onRepeatScope||this,this.onRepeatParams=i.onRepeatParams||[],this.onEnd=i.onEnd||null,this.onEndScope=i.onEndScope||this,this.onEndParams=i.onEndParams||[],this.onUpdate=i.onUpdate||null,this.onUpdateScope=i.onUpdateScope||this,this.onUpdateParams=i.onUpdateParams||[],this.isPlaying=!1,this.interpolation=i.interpolation||null,this.isReverse=i.isReverse||!1,this.timeScale=i.timeScale||1,this.isFrom=i.isFrom||!1,this.isInited=!1,this.isSeek=!1,this.isKeep=!1,this.isYoReverse=!1,this.isDom=r,this.repeat=this.repeat<0?999999999999:Math.floor(this.repeat),this.curRepeat=0,this.elapsed=null,this.startTime=this.delay,this.endTime=this.startTime+this.repeatDelay*this.repeat+this.duration*(this.repeat+1),this.curTime=null,this.lastTime=null,!1!==i.isPlaying&&this.play()},_update:function(t){this.isKeep=!1,t=(this.isReverse?-1:1)*t*this.timeScale;var e=this.curTime,t=Math.min(this.endTime,Math.max(0,e+t));if(t!==this.curTime){this.lastTime=e,this.curTime=t;e=Math.min(this.repeat,Math.max(0,Math.floor((this.curTime-this.startTime)/(this.duration+this.repeatDelay)))),t=!1;if(e!==this.curRepeat&&(this.curRepeat=e,this.yoyo&&(this.isYoReverse=this.curRepeat%2!=0),t=!0),this.isFrom&&(D(this),this._updateProp()),!(this.lastTime<this.startTime&&this.curTime<this.startTime))if(this.isFrom||(D(this),this._updateProp()),this.lastTime<this.curTime){if(this.lastTime<=this.startTime&&this.curTime>this.startTime&&!this.isSeek&&this.onStart&&this.onStart.apply(this.onStartScope,this.onStartParams),t&&!this.isSeek&&this.onRepeat&&this.onRepeat.apply(this.onRepeatScope,this.onRepeatParams),this.lastTime<this.endTime&&this.curTime>=this.endTime)return!this.isSeek&&this.onEnd&&this.onEnd.apply(this.onEndScope,this.onEndParams),this.isKeep}else if(this.lastTime>=this.endTime&&this.curTime<this.endTime&&!this.isSeek&&this.onEnd&&this.onEnd.apply(this.onEndScope,this.onEndParams),t&&!this.isSeek&&this.onRepeat&&this.onRepeat.apply(this.onRepeatScope,this.onRepeatParams),this.lastTime>this.startTime&&this.curTime<=this.startTime)return!this.isSeek&&this.onStart&&this.onStart.apply(this.onStartScope,this.onStartParams),this.isKeep}return!0},_updateProp:function(){var t=Math.min(1,Math.max(0,(this.curTime===this.endTime?this.duration:(this.curTime-this.startTime)%(this.duration+this.repeatDelay))/this.duration));if((t=this.isYoReverse?1-t:t)!==this.elapsed){this.elapsed=t;var e,n=this.ease(t),i=!1;for(e in this.fromVars){var r=this.fromVars[e],a=this.toVars[e],r=Array.isArray(a.num)?this.interpolation(a.num,n):r.num+(a.num-r.num)*n;r=o(r),this.curVars[e]={num:r,unit:a.unit},this.isDom?b(this.el,e,r,a.unit)&&(i=!0):this.el[e]=r+(a.unit||0)}i&&T(this.el),!this.isSeek&&this.onUpdate&&this.onUpdate.apply(this.onUpdateScope,this.onUpdateParams)}},_addSelf:function(){M.push(this),R||(I=u.now(),R=!0,s(z))},_removeSelf:function(){var t=M.indexOf(this);-1!==t&&M.splice(t,1)},play:function(t){if(this.isReverse=!1,void 0!==t&&this.seek(t,!0),this.curTime===this.endTime)return this.isKeep=!1;this.isKeep=!0,this.isPlaying||(this.isPlaying=!0,this._addSelf())},pause:function(){this.isKeep=!1,this.isPlaying&&(this.isPlaying=!1,this._removeSelf())},stop:function(){this.pause(),this.seek(0,!0)},reverse:function(t){if(this.isReverse=!0,void 0!==t&&this.seek(t,!0),0===this.curTime)return this.isKeep=!1;this.isKeep=!0,this.isPlaying||(this.isPlaying=!0,this._addSelf())},seek:function(t,e){t=Math.max(0,Math.min(this.endTime,1e3*t));this.curTime!==t&&(this.isSeek=e||!1,this._update((this.isReverse?-1:1)*(t-this.curTime)),this.isSeek=!1)},setTimeScale:function(t){this.timeScale=t},kill:function(t){this.pause(),t&&this.seek(this.endTime),this.duration=null,this.curTime=this.lastTime=this.startTime=this.endTime=null,this.el=this.onStart=this.onRepeat=this.onEnd=this.onUpdate=null}}),Object.assign(u,{get:function(t,e){var t=i(t),n=w(t=void 0!==t.length?t[0]:t);return n?(p(t),(n=m(t,e,n))?v(t,n):null):t[e]},set:function(t,u){e(i(t),function(t,e){var n,i=w(e);if(i){p(e);var r=!1;for(s in u){var a=m(e,s,i);a&&(n=u[s],/(,| |jpeg|jpg|png|gif|-3d)/g.test(n)||!/\d/g.test(n)?b(e,a,u[s])&&(r=!0):b(e,a,(o=l(d(v(e,a)),u[s])).num,o.unit)&&(r=!0))}r&&T(e)}else for(var s in u){var o=l(d(e[s]),u[s]);e[s]=o.num+(o.unit||0)}})},fromTo:function(t,e,n,i){return e||i.delay?O("fromTo",t,e,n,i):this.set(t,i)},from:function(t,e,n){return e||n.delay?O("from",t,e,n,{}):this.set(t,n)},to:function(t,e,n){return e||n.delay?O("to",t,e,{},n):this.set(t,n)},kill:function(t,r){e(i(t),function(t,e){for(var n=M.length-1;0<=n;n--){var i=M[n];i.el===e&&i.kill(r)}})},killAll:function(t){for(var e=M.length-1;0<=e;e--)M[e].kill(t)},play:function(t,e){E(t,"play",e)},playAll:function(t){A("play",t)},pause:function(t){E(t,"pause")},pauseAll:function(){A("pause")},stop:function(t){E(t,"stop")},stopAll:function(){A("stop")},reverse:function(t,e){E(t,"reverse",e)},reverseAll:function(t){A("reverse",t)},seek:function(t,e){E(t,"seek",e)},seekAll:function(t){A("seek",t)},setTimeScale:function(t,e){E(t,"setTimeScale",e)},setTimeScaleAll:function(t){A("setTimeScale",t)},isTweening:function(t){for(var e=(e=i(t))[0]||e,n=M.length-1;0<=n;n--)if(M[n].el===e)return!0;return!1},call:function(t,e,n,i){return t?new x({},Math.max(0,t),{},{onEnd:e,onEndParams:n,isPlaying:i},!1):e.apply(e,n)}}),Object.assign(u,{path:function(t){Z(t);for(var e=t.ease||u.Linear.None,n=t.step||1,i=[],r=0;r<=n;r++){var a,s=e(r/n),o={};for(a in t)Array.isArray(t[a])&&(o[a]=t.interpolation(t[a],s));i.push(o)}return i}});var V,X={Linear:function(t,e,n){return(e-t)*n+t},Bernstein:function(t,e){var n=X.Factorial;return n(t)/n(e)/n(t-e)},Factorial:(V=[1],function(t){var e,n=1;if(V[t])return V[t];for(e=t;1<e;e--)n*=e;return V[t]=n}),Through:function(t,e,n,i,r){var t=.5*(n-t),i=.5*(i-e),a=r*r;return(2*e-2*n+t+i)*(r*a)+(-3*e+3*n-2*t-i)*a+t*r+e}};return Object.assign(u,{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*(2-Math.pow(2,-10*(t-1)))}},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 e,n=.1;return 0===t?0:1===t?1:(e=!n||n<1?(n=1,.1):.4*Math.asin(1/n)/(2*Math.PI),-(n*Math.pow(2,10*--t)*Math.sin((t-e)*(2*Math.PI)/.4)))},Out:function(t){var e,n=.1;return 0===t?0:1===t?1:(e=!n||n<1?(n=1,.1):.4*Math.asin(1/n)/(2*Math.PI),n*Math.pow(2,-10*t)*Math.sin((t-e)*(2*Math.PI)/.4)+1)},InOut:function(t){var e,n=.1;return 0===t?0:1===t?1:(e=!n||n<1?(n=1,.1):.4*Math.asin(1/n)/(2*Math.PI),(t*=2)<1?n*Math.pow(2,10*--t)*Math.sin((t-e)*(2*Math.PI)/.4)*-.5:n*Math.pow(2,-10*--t)*Math.sin((t-e)*(2*Math.PI)/.4)*.5+1)}},Back:{In:function(t){return t*t*(2.70158*t-1.70158)},Out:function(t){return--t*t*(2.70158*t+1.70158)+1},InOut:function(t){var e=2.5949095;return(t*=2)<1?t*t*((1+e)*t-e)*.5:.5*((t-=2)*t*((1+e)*t+e)+2)}},Bounce:{In:function(t){return 1-u.Bounce.Out(1-t)},Out:function(t){return t<1/2.75?7.5625*t*t:t<2/2.75?7.5625*(t-=1.5/2.75)*t+.75:t<2.5/2.75?7.5625*(t-=2.25/2.75)*t+.9375:7.5625*(t-=2.625/2.75)*t+.984375},InOut:function(t){return t<.5?.5*u.Bounce.In(2*t):.5*u.Bounce.Out(2*t-1)+.5}}}),u});