-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathjstimeline.min.js
5 lines (4 loc) · 5.74 KB
/
jstimeline.min.js
1
2
3
4
5
/*!
* GIT: https://github.com/shrekshrek/jstween
**/
!function(i,t){if("object"==typeof exports&&"undefined"!=typeof module){var e=require("jstween");module.exports=t(e)}else"function"==typeof define&&define.amd?define(["jstween"],t):i.JTL=t(i.JT)}(this,function(i){"use strict";function t(i){var t=/(^[a-zA-Z]\w*|)(\+=|-=|)(\d*\.\d*|\d*)/,e=t.exec(i);return{label:e[1],ext:e[2],num:parseFloat(e[3])}}function e(){var t=a.length;if(0===t)return void(r=!1);var s=i.now(),l=s-o;o=s,l>500&&(l=33),h=a.slice(0);for(var u=0;u<t;u++){var m=h[u];m&&m.isPlaying&&!m._update(l)&&m.pause()}n(e)}function s(){this.initialize.apply(this,arguments)}var n=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(i){window.setTimeout(i,1e3/60)},a=[],h=[],r=!1,o=0;return Object.assign(s.prototype,{initialize:function(i){i=i||{},this.duration=0,this.delay=1e3*Math.max(i.delay||0,0),this.onStart=i.onStart||null,this.onStartScope=i.onStartScope||this,this.onStartParams=i.onStartParams||[],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.isReverse=i.isReverse||!1,this.timeScale=i.timeScale||1,this.isSeek=!1,this.isKeep=!1,this.startTime=this.delay,this._updateEndTime(),this.curTime=null,this.lastTime=null,this.labels=[],this.tweens=[],this.calls=[]},_updateEndTime:function(){this.endTime=this.startTime+this.duration},_update:function(i){this.isKeep=!1,i=(this.isReverse?-1:1)*i*this.timeScale;var t=this.curTime,e=Math.min(this.endTime,Math.max(0,t+i));if(e===this.curTime)return!0;if(this.lastTime=t,this.curTime=e,this._updateProp(),this.lastTime<this.startTime&&this.curTime<this.startTime)return!0;if(this.lastTime<this.curTime){if(this.lastTime<=this.startTime&&this.curTime>this.startTime&&!this.isSeek&&this.onStart&&this.onStart.apply(this.onStartScope,this.onStartParams),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),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(){this._checkTween(),this._checkCall(),!this.isSeek&&this.onUpdate&&this.onUpdate.apply(this.onUpdateScope,this.onUpdateParams)},_addSelf:function(){a.push(this),r||(o=i.now(),r=!0,n(e))},_removeSelf:function(){var i=a.indexOf(this);-1!==i&&a.splice(i,1)},_parsePosition:function(i){if(void 0===i)return this.duration;var e=t(i),s=0;if(e.label)s=this.getLabelTime(e.label);else if(e.ext){switch(e.ext){case"+=":s=this.duration+1e3*e.num;break;case"-=":s=this.duration-1e3*e.num}this._updateEndTime()}else e.num&&(s=1e3*e.num);return s},addCall:function(i,t){var e=this._parsePosition(t);this.duration=Math.max(this.duration,e),this.calls.push({time:e,call:i}),this._updateEndTime()},_checkCall:function(){for(var i=0,t=this.calls.length;i<t;i++){var e=this.calls[i],s=this.lastTime-this.startTime,n=this.curTime-this.startTime,a=e.time;this.isSeek||(s<n?(0===a&&0===s&&n>0||s<a&&n>=a)&&e.call():(s>a&&n<=a||a===this.endTime&&s===this.endTime&&n<this.endTime)&&e.call())}},addTween:function(i,t){var e=this._parsePosition(t);this.duration=Math.max(this.duration,e+i.endTime),this.tweens.push({time:e,tween:i}),this._updateEndTime()},_checkTween:function(){for(var i=0,t=this.tweens.length;i<t;i++){var e=this.tweens[i],s=this.lastTime-this.startTime,n=this.curTime-this.startTime,a=e.time;(null!==e.tween.curTime||s>=a||n>=a)&&e.tween.seek((n-a)/1e3,this.isSeek)}},fromTo:function(t,e,s,n,a){return n.isPlaying=!1,this.addTween(i.fromTo(t,e,s,n),a),this},from:function(t,e,s,n){return s.isPlaying=!1,this.addTween(i.from(t,e,s),n),this},to:function(t,e,s,n){return s.isPlaying=!1,this.addTween(i.to(t,e,s),n),this},add:function(i,t){switch(typeof i){case"object":this.addTween(i,t);break;case"function":this.addCall(i,t);break;case"string":this.addLabel(i,t);break;default:throw"add action is wrong!!!"}return this},remove:function(i){for(var t=this._parsePosition(i),e=this.tweens.length,s=e-1;s>=0;s--){this.tweens[s].time==t&&this.tweens.splice(s,1)}return this},addLabel:function(i,t){this.removeLabel(i);var e=this._parsePosition(t);this.labels.push({name:i,time:e})},removeLabel:function(i){for(var t=0,e=this.labels.length;t<e;t++){if(i==this.labels[t].name)return void this.labels.splice(t,1)}},getLabelTime:function(i){for(var t=0,e=this.labels.length;t<e;t++){var s=this.labels[t];if(i==s.name)return s.time}return null},totalTime:function(){return this.duration},play:function(i){if(this.isReverse=!1,void 0!==i&&this.seek(i,!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(i){if(this.isReverse=!0,void 0!==i&&this.seek(i,!0),0===this.curTime)return this.isKeep=!1;this.isKeep=!0,this.isPlaying||(this.isPlaying=!0,this._addSelf())},seek:function(i,t){var e=Math.max(0,Math.min(this.endTime,this._parsePosition(i)));this.curTime!==e&&(this.isSeek=t||!1,this._update((this.isReverse?-1:1)*(e-this.curTime)),this.isSeek=!1)},kill:function(i){this.pause(),i&&this.seek(this.endTime),this.labels=[],this.tweens=[],this.calls=[],this.duration=null,this.curTime=this.lastTime=this.startTime=this.endTime=null,this.onStart=this.onEnd=this.onUpdate=null}}),{create:function(i){return new s(i)},kill:function(i){i.kill()}}});