Created
December 7, 2010 07:08
-
-
Save shazow/731548 to your computer and use it in GitHub Desktop.
My ghetto frame counter (for tracking fps), now replaced with mrdoob's Stats.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function FrameCounter(sample_length) { | |
this.sample_length = sample_length || 500; // In milliseconds | |
this.n = 0; | |
this.last_n = 0; | |
this.time_created = this.time_sampled = this.time_updated = new Date(); | |
this.last_rate = 0; | |
} | |
FrameCounter.prototype = { | |
tick: function() { | |
this.n++; | |
this.time_updated = +new Date(); | |
}, | |
get_rate: function() { | |
var now = +new Date(); | |
var delta = now - this.time_sampled; | |
if(delta < this.sample_length) return this.last_rate; | |
this.last_rate = (this.n - this.last_n) / delta; | |
this.last_n = this.n; | |
this.time_sampled = now; | |
return this.last_rate * 1000; | |
}, | |
get_time_elapsed: function() { | |
return new Date() - this.time_created; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment