GameStats
GameStats binds to your game loop and provides helpful statistics about your game's performance.
Installation
To install this module through npm, simply use:
$ npm install game-stats
and to use it, you can import it as an ES6 module:
// Webpack; // Browser;
Initialization
After installing GameStats, it can be initialized like so:
const gameStats = ;
GameStats also accepts an options object at initialization with the following options available:
param | type | description | default |
---|---|---|---|
historyLimit | number | The amount of items that will be kept in history to be used in calculations. The more history you keep the more accurate the calculations but the longer they will take. | 10 |
So an example of initializating game-stats with options is:
const options = historyLimit: 15; const gameStats = options;
Usage
GameStats works with your requestAnimationFrame loop to give you more detailed data about the performance of your game. For example, a typical game loop using requestAnimationFrame would look like this:
const canvas = document;const ctx = canvas; let x = canvaswidth / 2;let y = canvasheight - 30; { ctx; ctx; ctxfillStyle = '#0095DD'; ctx; ctx;} { ctx; ; x += 2; y += -2; ;} ;
To start gathering data about this game loop, you simply call GameStat's record method after the call to requestAnimationFrame like so:
const canvas = document;const ctx = canvas; let x = canvaswidth / 2;let y = canvasheight - 30; { ctx; ctx; ctxfillStyle = '#0095DD'; ctx; ctx;} // Timestamp parameter automatically passed by requestAnimationFrame. { ctx; ; x += 2; y += -2; ; // Record statistics here, notice the timestamp parameter passed to record. gameStats;} // Timestamp is passed to draw automatically without us having to do anything.;
You'll notice that record takes a parameter, timestamp, which matches the parameter taken by the draw function. If this is new to you, it's a parameter passed by requestAnimationFrame to any function you give it and GameStats uses it to gather information so it must be provided as a parameter to record.
API
After you start recording, the following methods are available:
stats
Gets all of the statistics about the game loop at the current point in time.
The object returned by this method contains the following data:
param | type | description |
---|---|---|
browser | Object | Information about the browser being used to run the game loop. |
browser.name | string | The name of the browser being used. |
browser.version | string | The version of the browser being used. |
timestamp | DOMHighResTimeStamp | The current timestamp of the game loop. |
prevTimestamp | DOMHighResTimeStamp | The previous timestamp of the game loop. |
predictedNextTimestamp | number | The predicted next timestamp of the game loop. Note: This is predicted from the average of previous timestamps but is in no way dependable. |
delta | number | The time in between the previous and current timestamp. |
deltaAverage | number | The average time in between previous and current timestamps. This uses a collection of deltas to compute the average. |
frame | number | The current frame that the game is on. |
fps | number | The number of frames per second that the game is running at. |
fpsAverage | number | The average number of frames per second that the game is running at. This uses a collection of fps values to compute the average. |
history | Object | A collection of the histories of certain statistics. |
history.timestamps | Array | The history of timestamps. |
history.deltas | Array | The history of delta values. |
const stats = gameStats; console; // 60, hopefully.
Tests
The tests for game-stats are browser based so to run them you will first need to start the local testing server like so:
$ npm run test
then you will need to navigate to https://localhost/test/index.html in your browser to run all of the available tests for game-stats.
License
MIT