itay-game-loop
Version:
A game loop written in typescript.
35 lines (34 loc) • 1.24 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class FpsMonitor {
constructor() {
this.fps = 60;
this.sampleIntervalMillisec = 500;
this.timestamp = 0;
this.framesThisInterval = 0;
this.sampleStartTimestamp = 0;
}
countFrame(deltaMillisec) {
this.timestamp += deltaMillisec;
if (this.hasSampleTimeEnded()) {
this.sampleStartTimestamp = this.timestamp;
this.fps = this.framesThisInterval * (1000 / this.sampleIntervalMillisec);
this.framesThisInterval = 0;
}
this.framesThisInterval++;
}
hasSampleTimeEnded() {
return this.timestamp > this.sampleStartTimestamp + this.sampleIntervalMillisec;
}
}
exports.FpsMonitor = FpsMonitor;
});