Skip to content
Browse files

Add Timer

  • Loading branch information...
1 parent eca8dd7 commit 7f7da2e353702afbe2ea013f4f48a5dab3e7dfd5 @stagas committed Apr 20, 2012
Showing with 57 additions and 0 deletions.
  1. +57 −0 lib/timer.js
View
57 lib/timer.js
@@ -0,0 +1,57 @@
+/**
+ * Timer
+ */
+
+function Timer () {
+ this.now = Date.now()
+ this.before = this.now
+ this.delta = 0
+ this.accumulator = 0
+ this.precision = global.FLOAT_PRECISION || Math.pow(10, 4)
+ this.fps = 60
+}
+
+Timer.prototype.__defineGetter__('fps', function () {
+ return this._fps
+})
+
+Timer.prototype.__defineSetter__('fps', function (fps) {
+ this._fps = fps
+ this.ms = fixFloat(100 / fps, this.precision)
+})
+
+Timer.prototype.tick = function () {
+ this.now = Date.now()
+ this.delta = this.now - this.before
+ this.accumulator += this.delta
+ this.before = this.now
+ return this.delta
+}
+
+Timer.prototype.overflow = function () {
+ if (this.accumulator >= this.ms) {
+ this.accumulator -= this.ms
+ return true
+ }
+ return false
+}
+
+Timer.prototype.alpha = function () {
+ return fixFloat(this.accumulator / this.ms, this.precision)
+}
+
+/**
+ * Main exports
+ * @type {Function} Timer ctor
+ */
+module.exports = Timer
+
+/**
+ * Fix floating point precision
+ * @param {Number} i The Number we want to fix
+ * @param {Number} precision Precision scalar
+ * @returns {Number} The Number with a fixed decimal digit length
+ */
+function fixFloat (i, precision) {
+ return Math.round(i * precision) / precision
+}

0 comments on commit 7f7da2e

Please sign in to comment.
Something went wrong with that request. Please try again.