torque/dist/torque.js

2 lines
125 KiB
JavaScript
Raw Normal View History

2019-01-22 01:00:17 +08:00
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.torque=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){var AnimatorStepsRange=function(start,end){if(start<0)throw new Error("start must be a positive number");if(start>=end)throw new Error("start must be smaller than end");this.start=start;this.end=end};AnimatorStepsRange.prototype={diff:function(){return this.end-this.start},isLast:function(step){return(step|0)===this.end}};module.exports=AnimatorStepsRange},{}],2:[function(require,module,exports){(function(global){var torque=require("./");var AnimatorStepsRange=require("./animator-steps-range");var requestAnimationFrame=global.requestAnimationFrame||global.mozRequestAnimationFrame||global.webkitRequestAnimationFrame||global.msRequestAnimationFrame||function(callback){return global.setTimeout(callback,1e3/60)};var cancelAnimationFrame=global.cancelAnimationFrame||global.mozCancelAnimationFrame||global.webkitCancelAnimationFrame||global.msCancelAnimationFrame||function(id){clearTimeout(id)};function Animator(callback,options){if(!options.steps){throw new Error("steps option missing")}this.options=options;this.running=false;this._tick=this._tick.bind(this);this._t0=+new Date;this.callback=callback;this._time=0;this.itemsReady=false;this.options=torque.extend({animationDelay:0,maxDelta:.2,loop:options.loop===undefined?true:options.loop},this.options);this.steps(options.steps)}Animator.prototype={start:function(){this.running=true;requestAnimationFrame(this._tick);this.options.onStart&&this.options.onStart();if(this.stepsRange().diff()===1){this.running=false}},isRunning:function(){return this.running},stop:function(){this.pause();this.time(this.stepsRange().start);this.options.onStop&&this.options.onStop()},time:function(_){if(!arguments.length)return this._time;this._time=_;var t=this.range(this.domain(this._time));this.callback(t)},toggle:function(){if(this.running){this.pause()}else{this.start()}},rescale:function(){this.domainInv=torque.math.linear(this.options.animationDelay,this.options.animationDelay+this.options.animationDuration);this.domain=this.domainInv.invert();this.range=torque.math.linear(0,this._defaultStepsRange.end);this.rangeInv=this.range.invert();this.time(this._time);this.running?this.start():this.pause();return this},duration:function(_){if(!arguments.length)return this.options.animationDuration;this.options.animationDuration=_;if(this.time()>_){this.time(0)}this.rescale();return this},steps:function(_){this.options.steps=_;this._defaultStepsRange=new AnimatorStepsRange(0,_);return this.rescale()},stepsRange:function(start,end){if(arguments.length===2){if(start<this._defaultStepsRange.start)throw new Error("start must be within default steps range");if(end>this._defaultStepsRange.end)throw new Error("end must be within default steps range");this._customStepsRange=new AnimatorStepsRange(start,end);this.options.onStepsRange&&this.options.onStepsRange();var step=this.step()|0;if(step<start||step>end){this.step(start)}}return this._customStepsRange||this._defaultStepsRange},removeCustomStepsRange:function(){this._customStepsRange=undefined;this.options.onStepsRange&&this.options.onStepsRange()},step:function(s){if(arguments.length===0)return this.range(this.domain(this._time));this._time=this.domainInv(this.rangeInv(s))},pause:function(){this.running=false;cancelA