Path: blob/main/projects/HexGL/libs/Editor_files/Timer.js
4627 views
/*!1* @class bkcore.Timer2*3* new Date().getTime() wrapper to use as timers.4*5* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>6*/78/**9* RAF shim10*/11window.requestAnimFrame = (function(){12return window.requestAnimationFrame ||13window.webkitRequestAnimationFrame ||14window.mozRequestAnimationFrame ||15window.oRequestAnimationFrame ||16window.msRequestAnimationFrame ||17function( callback ){18window.setTimeout(callback, 1000 / 60);19};20})();2122/*!23* @package bkcore24*/25var bkcore = bkcore || {};2627/*!28* Creates a new timer, inactive by default.29* Call Timer.start() to activate.30*/31bkcore.Timer = function()32{33this.time = {34start: 0,35current: 0,36previous: 0,37elapsed: 0,38delta: 039}4041this.active = false;42}4344/*!45* Starts/restarts the timer.46*/47bkcore.Timer.prototype.start = function()48{49var now = new Date().getTime();5051this.time.start = now;52this.time.current = now;53this.time.previous = now;54this.time.elapsed = 0;55this.time.delta = 0;5657this.active = true;58}5960/*!61* Restarts timer, returning last ms tick62*/63bkcore.Timer.prototype.restart = function()64{65var now = new Date().getTime();66var e = now - this.time.start;6768this.time.start = now;69this.time.current = now;70this.time.previous = now;71this.time.elapsed = 0;72this.time.delta = 0;7374this.active = true;7576return e;77}7879/*!80* Pauses(true)/Unpauses(false) the timer.81*82* @param bool Do pause83*/84bkcore.Timer.prototype.pause = function(bool)85{86this.active = !bool;87}8889/*!90* Update method to be called inside a RAF loop91*/92bkcore.Timer.prototype.update = function()93{94if(!this.active) return;9596var now = new Date().getTime();9798this.time.current = now;99this.time.elapsed = this.time.current - this.time.start;100this.time.delta = now - this.time.previous;101this.time.previous = now;102103return this.time.elapsed;104}105106/*!107* Returns elapsed milliseconds108*/109bkcore.Timer.prototype.getElapsed = function()110{111return this.time.elapsed;112}113114/*!115* Returns a formatted version of the current elapsed time using msToTime().116*117*118*/119bkcore.Timer.prototype.getElapsedTime = function()120{121return bkcore.Timer.msToTime(this.time.elapsed);122}123124/*!125* Formats a millisecond integer into a h/m/s/ms object126*127* @param x int In milliseconds128* @return Object{h,m,s,ms}129*/130bkcore.Timer.msToTime = function(t)131{132var ms, s, m, h;133134ms = t%1000;135136s = Math.floor((t/1000)%60);137138m = Math.floor((t/60000)%60);139h = Math.floor((t/3600000));140141return {h:h, m:m, s:s, ms:ms};142}143144/*!145* Formats a millisecond integer into a h/m/s/ms object with prefix zeros146*147* @param x int In milliseconds148* @return Object<string>{h,m,s,ms}149*/150bkcore.Timer.msToTimeString = function(t)151{152var ms, s, m, h;153154ms = t%1000;155if(ms < 10) ms = "00"+ms;156else if(ms < 100) ms = "0"+ms;157158s = Math.floor((t/1000)%60);159if(s < 10) s = "0"+s;160161m = Math.floor((t/60000)%60);162h = Math.floor((t/3600000));163164return {h:h, m:m, s:s, ms:ms};165}166167