UNPKG

pixelbutler

Version:

Low-res bitmap render engine for big screens

52 lines (51 loc) 1.68 kB
'use strict'; var FPS = (function () { function FPS(smoothFPS, smoothDelta) { if (typeof smoothFPS === "undefined") { smoothFPS = 30; } if (typeof smoothDelta === "undefined") { smoothDelta = 2; } this.tickHistory = [0]; this.deltaHistory = [0]; this.tickI = 0; this.deltaI = 0; this.smoothFPS = smoothFPS; this.smoothDelta = smoothDelta; this.previous = performance.now(); } FPS.prototype.begin = function () { var now = performance.now(); var delta = now - this.previous; this.tickHistory[this.tickI % this.smoothFPS] = delta; this.tickI++; this.previous = now; }; FPS.prototype.end = function () { var now = performance.now(); var delta = now - this.previous; this.deltaHistory[this.deltaI % this.smoothDelta] = delta; this.deltaI++; }; Object.defineProperty(FPS.prototype, "fps", { get: function () { var tot = 0; for (var i = 0; i < this.tickHistory.length; i++) { tot += this.tickHistory[i]; } return Math.ceil(1000 / (tot / this.tickHistory.length)); }, enumerable: true, configurable: true }); Object.defineProperty(FPS.prototype, "redraw", { get: function () { var tot = 0; for (var i = 0; i < this.deltaHistory.length; i++) { tot += this.deltaHistory[i]; } return Math.ceil(tot / this.deltaHistory.length); }, enumerable: true, configurable: true }); return FPS; })(); module.exports = FPS;