Path: blob/main/projects/HexGL/bkcore.coffee/Timer.js
2602 views
// Generated by CoffeeScript 1.4.012/*3new Date().getTime() wrapper to use as timer.45@class bkcore.Timer6@author Thibaut 'BKcore' Despoulain <http://bkcore.com>7*/8910(function() {11var Timer, exports;1213Timer = (function() {14/*15Creates a new timer, inactive by default.16Call Timer.start() to activate.17*/1819function Timer() {20this.time = {21start: 0,22current: 0,23previous: 0,24elapsed: 0,25delta: 026};27this.active = false;28}2930/*31Starts/restarts the timer.32*/333435Timer.prototype.start = function() {36var now;37now = (new Date).getTime();38this.time.start = now;39this.time.current = now;40this.time.previous = now;41this.time.elapsed = 0;42this.time.delta = 0;43return this.active = true;44};4546/*47Pauses(true)/Unpauses(false) the timer.4849@param bool Do pause50*/515253Timer.prototype.pause = function(doPause) {54return this.active = !doPause;55};5657/*58Update method to be called inside a RAF loop59*/606162Timer.prototype.update = function() {63var now;64if (!this.active) {65return;66}67now = (new Date).getTime();68this.time.current = now;69this.time.elapsed = this.time.current - this.time.start;70this.time.delta = now - this.time.previous;71return this.time.previous = now;72};7374/*75Returns a formatted version of the current elapsed time using msToTime().76*/777879Timer.prototype.getElapsedTime = function() {80return this.constructor.msToTime(this.time.elapsed);81};8283/*84Formats a millisecond integer into a h/m/s/ms object8586@param x int In milliseconds87@return Object{h,m,s,ms}88*/899091Timer.msToTime = function(t) {92var h, m, ms, s;93ms = t % 1000;94s = Math.floor((t / 1000) % 60);95m = Math.floor((t / 60000) % 60);96h = Math.floor(t / 3600000);97return {98h: h,99m: m,100s: s,101ms: ms,102ms: ms103};104};105106/*107Formats a millisecond integer into a h/m/s/ms object with prefix zeros108109@param x int In milliseconds110@return Object<string>{h,m,s,ms}111*/112113114Timer.msToTimeString = function(t) {115var time;116time = this.msToTime(t);117time.h = this.zfill(time.h, 2);118time.m = this.zfill(time.m, 2);119time.s = this.zfill(time.s, 2);120time.ms = this.zfill(time.ms, 4);121return time;122};123124/*125Convert given integer to string and fill with heading zeros126127@param num int Number to convert/fill128@param size int Desired string size129*/130131132Timer.zfill = function(num, size) {133var len;134len = size - num.toString().length;135if (len > 0) {136return new Array(len + 1).join('0') + num;137} else {138return num.toString();139}140};141142return Timer;143144})();145146/*147Exports148@package bkcore149*/150151152exports = exports != null ? exports : this;153154exports.bkcore || (exports.bkcore = {});155156exports.bkcore.Timer = Timer;157158}).call(this);159160161