tempus
Version:
one rAF to rule them all
2 lines • 3.85 kB
JavaScript
var a=class{_elapsed=0;_currentTime=0;_startTime=void 0;_lastTime=void 0;_isPlaying=!1;_deltaTime=0;play(){this._isPlaying||(this._currentTime=0,this._startTime=void 0,this._isPlaying=!0)}pause(){this._isPlaying&&(this._deltaTime=0,this._isPlaying=!1)}reset(){this._elapsed=0,this._deltaTime=0,this._currentTime=0,this._lastTime=void 0,this._isPlaying=!1}update(e){this._isPlaying&&(this._startTime||(this._startTime=e),this._lastTime===void 0?(this._lastTime=this._startTime,this._currentTime=0,this._deltaTime=0):(this._lastTime=this._currentTime,this._currentTime=e-this._startTime,this._deltaTime=this._currentTime-this._lastTime,this._elapsed+=this._deltaTime))}get time(){return this._elapsed}get isPlaying(){return this._isPlaying}get deltaTime(){return this._deltaTime}};var d=0;function h(){return d++}var s=typeof window<"u",c=s&&window.requestAnimationFrame,f=s&&window.cancelAnimationFrame,u=class{callbacks=[];fps;time=0;lastTickDate=performance.now();framesCount=0;constructor(e=Number.POSITIVE_INFINITY){this.fps=e}get isRelativeFps(){return typeof this.fps=="string"&&this.fps.endsWith("%")}get maxFramesCount(){return this.isRelativeFps?Math.max(1,Math.round(100/Number(this.fps.replace("%","")))):1}get executionTime(){return this.isRelativeFps?0:1e3/this.fps}dispatch(e,i){for(let t=0;t<this.callbacks.length;t++){let r=performance.now();this.callbacks[t]?.callback(e,i);let n=performance.now()-r;this.callbacks[t].samples?.push(n),this.callbacks[t].samples=this.callbacks[t].samples?.slice(-9)}}raf(e,i){if(this.time+=i,this.isRelativeFps)this.framesCount===0&&this.dispatch(e,i),this.framesCount++,this.framesCount%=this.maxFramesCount;else if(this.fps===Number.POSITIVE_INFINITY)this.dispatch(e,i);else if(this.time>=this.executionTime){this.time=this.time%this.executionTime;let t=e-this.lastTickDate;this.lastTickDate=e,this.dispatch(e,t)}}add({callback:e,priority:i,label:t}){if(typeof e!="function"){console.warn("Tempus.add: callback is not a function");return}let r=h();return this.callbacks.push({callback:e,priority:i,uid:r,label:t,samples:[]}),this.callbacks.sort((n,m)=>n.priority-m.priority),()=>this.remove(r)}remove(e){this.callbacks=this.callbacks.filter(({uid:i})=>e!==i)}},o=class{framerates={};clock=new a;fps;usage=0;rafId;constructor(){s&&this.play()}restart(){this.rafId&&cancelAnimationFrame(this.rafId);for(let e of Object.values(this.framerates))e.framesCount=0,e.time=0,e.lastTickDate=performance.now();this.clock.reset(),this.play()}play(){!s||this.clock.isPlaying||(this.clock.play(),this.rafId=requestAnimationFrame(this.raf))}pause(){!s||!this.rafId||!this.clock.isPlaying||(cancelAnimationFrame(this.rafId),this.rafId=void 0,this.clock.pause())}get isPlaying(){return this.clock.isPlaying}add(e,{priority:i=0,fps:t=Number.POSITIVE_INFINITY,label:r=""}={}){if(s){if(typeof t=="number"||typeof t=="string"&&t.endsWith("%"))return this.framerates[t]||(this.framerates[t]=new u(t)),this.framerates[t].add({callback:e,priority:i,label:r});console.warn('Tempus.add: fps is not a number or a string ending with "%"')}}raf=e=>{if(!s)return;this.clock.update(e);let i=this.clock.time,t=this.clock.deltaTime;this.fps=1e3/t;let r=performance.now();for(let m of Object.values(this.framerates))m.raf(i,t);let n=performance.now()-r;this.usage=n/t,this.rafId=requestAnimationFrame(this.raf)};patch(){s&&(window.requestAnimationFrame=(e,{priority:i=0,fps:t=Number.POSITIVE_INFINITY}={})=>e===this.raf||!e.toString().includes("requestAnimationFrame(")?c(e):(e.__tempusPatched||(e.__tempusPatched=!0,e.__tempusUnsubscribe=this.add(e,{priority:i,fps:t,label:e.name})),e.__tempusUnsubscribe),window.cancelAnimationFrame=e=>{if(typeof e=="function"){e?.();return}return f(e)})}unpatch(){s&&(window.requestAnimationFrame=c,window.cancelAnimationFrame=f)}},p=new o;globalThis.Tempus=p;
//# sourceMappingURL=tempus.min.js.map