UNPKG

ticker-service

Version:

A service used to create animation loops in a simple way and to keep everything in sync within an app.

3 lines (2 loc) 4.83 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e||self).tickerService=t()}(this,function(){var e=self.setTimeout,t=self.clearTimeout,n=self.setInterval,i=self.clearInterval,r=self.requestAnimationFrame,a=self.cancelAnimationFrame;return new(function(){function c(){var e=this,t=this;this.currentIndex=1e4,this.currentTime=0,this.delta=0,this.animationFrameRequest=null,this.useScopeFunctionsFlag=!0,this.running=!1,this.frameRateHistory=[],this.tickerCallbacks=new Map,this.getTime=function(){return self.performance?performance.now():Date.now()},this.clearTickerCallback=function(t){e.tickerCallbacks.delete(t)},this.createTickerCallback=function(t,n,i,r){if("number"!=typeof r||r<=0)throw new Error("Delay must be a positive number.");if("number"!=typeof i||i<=0)throw new Error("A callback must be executed at least once.");var a=e.currentIndex++;return e.tickerCallbacks.set(a,{callback:t,params:n,repeats:i,delay:r,count:0,executionTime:0,totalTime:e.currentTime-e.getTime()}),a},this.tick=function(t){e.tickerCallbacks.forEach(function(n,i){n.totalTime+=t;var r=n.totalTime-n.executionTime;r>=n.delay&&"function"==typeof n.callback&&(n.callback.apply(n,n.params.concat([r,n.count])),n.count++,n.executionTime=n.totalTime,n.count>=n.repeats&&e.clearTickerCallback(i))})},this.playAnimationFrame=function(){e.animationFrameRequest=r(function(){if(e.running){var t=e.getTime();e.delta=e.currentTime?t-e.currentTime:0,e.delta&&e.tick(e.delta),e.currentTime=t,e.storeFrameRateHistory(),e.playAnimationFrame()}})},this.storeFrameRateHistory=function(){if(e.delta>0){var t=Math.min(1e3/e.delta,e.maxFrameRate);e.frameRateHistory.unshift(t),e.frameRateHistory.length>120&&(e.frameRateHistory.length=120)}},self.setAnimationLoop=function(t,n){return e.setAnimationLoop(t,n)},self.clearAnimationLoop=function(t){return e.clearAnimationLoop(t)},self.setCounter=function(e,n,i){return t.setCounter.apply(t,[e,n,i].concat([].slice.call(arguments,3)))},self.clearCounter=function(t){return e.clearCounter(t)},self.sleep=function(t){return e.sleep(t)},self.frame=function(){return e.frame()},this.start()}var o,l=c.prototype;return l.setTimeout=function(e,t){return t=t||1,this.createTickerCallback(e,[].slice.call(arguments,2),1,t)},l.clearTimeout=function(e){this.clearTickerCallback(e)},l.setInterval=function(e,t){return this.createTickerCallback(e,[].slice.call(arguments,2),Infinity,t)},l.clearInterval=function(e){this.clearTickerCallback(e)},l.setCounter=function(e,t,n){return this.createTickerCallback(e,[].slice.call(arguments,3),n,t)},l.clearCounter=function(e){this.clearTickerCallback(e)},l.requestAnimationFrame=function(e){return this.createTickerCallback(e,[],1,1)},l.cancelAnimationFrame=function(e){this.clearTickerCallback(e)},l.setAnimationLoop=function(e,t){return this.createTickerCallback(e,[],Infinity,t?1e3/t:1)},l.clearAnimationLoop=function(e){this.clearTickerCallback(e)},l.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},l.frame=function(){return new Promise(function(e){return requestAnimationFrame(e)})},l.start=function(){return this.running||(this.useScopeFunctions=!1,this.running=!0,this.currentTime=this.getTime(),this.playAnimationFrame()),this},l.stop=function(){return this.running=!1,a(this.animationFrameRequest),this.useScopeFunctions=!0,this},(o=[{key:"frameRate",get:function(){return this.delta?Math.min(1e3/this.delta,this.maxFrameRate):this.maxFrameRate}},{key:"maxFrameRate",get:function(){return this.delta?30*Math.round(1e3/this.delta/30):60}},{key:"averageFrameRate",get:function(){return this.frameRateHistory.length?this.frameRateHistory.reduce(function(e,t){return e+t})/this.frameRateHistory.length:this.frameRate}},{key:"score",get:function(){return Math.round(this.averageFrameRate/this.maxFrameRate*100)}},{key:"isRunning",get:function(){return this.running}},{key:"useScopeFunctions",get:function(){return this.useScopeFunctionsFlag},set:function(c){var o=this,l=this;(c=Boolean(c))!==this.useScopeFunctionsFlag&&(self.setTimeout=c?e:function(e,t){return l.setTimeout.apply(l,[e,t].concat([].slice.call(arguments,2)))},self.clearTimeout=c?t:function(e){return o.clearTimeout(e)},self.setInterval=c?n:function(e,t){return l.setInterval.apply(l,[e,t].concat([].slice.call(arguments,2)))},self.clearInterval=c?i:function(e){return o.clearInterval(e)},self.requestAnimationFrame=c?r:function(e){return o.requestAnimationFrame(e)},self.cancelAnimationFrame=c?a:function(e){return o.cancelAnimationFrame(e)},this.useScopeFunctionsFlag=c)}}])&&function(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}(c.prototype,o),c}())}); //# sourceMappingURL=index.umd.js.map