Skip to content

A simple api used for calculating the FPS or function call per second

License

Notifications You must be signed in to change notification settings

peterczg/fps-calculation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FPS calculation

A simple api used for calculate the FPS or function call per second when doing loop jobs.

Installation

npm install fps-calculation --save
# or
yarn add fps-calculation

Usage

/**
 * calculate the fps or function call per sec.
 * @param {function} cb primary function
 * @param {object} context function context
 * @param {boolean} enableConsole log the current FPS
 */
function calcFrames(cb, context, enableConsole = false) {}
import { calcFrames } from "fps-calculation";

// Assume this is a loop call engine
// when call the start method
// cb will be called at every requestAnimationFrame hook
const engine = {
  start(cb) {},
};

// Assume this is a loop function
function cb() {
  // ...
  // Get current FPS
  const currentFps = cb.__frames__;
  // ...
}

// Wrap the function and then return a closure
const wrapCb = calcFrames(cb, this, true);
engine.start(wrapCb);

// Hook function registration below is optional

// Register a callback when refreshing (end of a function call)
cb.__onRefresh__ = function () {
  console.log("End of the function call");
};

// Register a callback when FPS change (end of a function call)
cb.__onFPSChange__ = function (curFPS, prevFPS) {
  console.log("FPS changed from " + prevFPS + " to" + curFPS);
};

License

MIT

About

A simple api used for calculating the FPS or function call per second

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published