DeltaFrame
DeltaFrame is a lightweight animation and game loop manager.
Note: Extra features like tasks will now go into deltaframe-extra. This package, deltaframe, will remain a small and fast game loop manager while extra features like tasks will go into deltaframe-extra for those who want the extra features.
Installation
To install this module through npm, simply use the following command:
$ npm install deltaframe
and to use it, you can import it as an ES6 module:
// Webpack; // Browser;
Initialization
After installing Deltaframe, it can be initialized like so:
const deltaframe = ;
Deltaframe also accepts an options object at initialization with the following options available:
param | type | description | default |
---|---|---|---|
minFps | number | The minimum number of frames per second to run deltaframe at. If frames per second drop below this, deltaframe will attempt to restart. | 5 |
targetFps | number | The number of frames per second that deltaframe should achieve. | 60 |
maxRestartAttempts | number | The number of times deltaframe will attempt to restart before stopping entirely. | Infinity |
runTime | number | Specify a value in milliseconds to have Deltaframe automatically stop after the specified amount of time has passed. | Infinity |
forceSetTimeout | boolean | Indicates whether setTimeout should be used instead of requestAnimationFrame even if requestAnimation is supported by the user's browser | false |
So an example of initializating Deltaframe with options is:
const options = minFps: 40 maxRestartAttempts: 10; const deltaframe = options;
From here you can use any of the conversion features available.
API
Deltaframe all revolves around a function you pass to it and this function should contain your drawing code.
For example, we'll take a simple game loop that moves a circle.
Traditionally, with requestAnimationFrame
it would be accomplished like so:
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; ;} ;
This example uses drawCircle
to just draw a circle centered horizontally and near the bottom of the canvas
and then it uses requestAnimationFrame
recursively to keep the loop going and it makes the ball go diagonally.
To change this so that it uses deltaframe instead, use the following:
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;} deltaframestartdraw;
The biggest difference here is removing all calls to requestAnimationFrame
and just using deltaframe.start(draw)
instead. You'll also notice now that the draw function now accepts two more parameters because deltaframe returns three values which can be used inside of your drawing function. The time
parameter is the same as it is in the first example, its the DOMHighResTimestamp as returned from requestAnimationFrame
or (window.performance.now()
in the case of setTimeout
). The delta
parameter is the change in time from the last frame to this frame and deltaAverage
is the mean of the most recent 10 delta values. These values can all be used inside of the draw
function however you deem fit.
start
The start method takes a function and begins running the deltaframe loop on it.
param | type | description | default |
---|---|---|---|
fn | Function | The function to call on a drawing loop. |
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;} deltaframestartdraw;
pause
The pause method temprarily stops the deltaframe loop. This should only be used if you plan on resuming it at some point and not just stopping entirely.
Note that deltaframe automatically pauses when the user switches tabs.
// Start the drawing loop.deltaframestartdraw; ;
resume
The resume method resumes the deltaframe loop after a paused state. Everything will be where it was before it was paused so it will seem like it was never even paused.
Note that deltaframe automatically resumes from being paused when the user switches back to the tab containing deltaframe.
// Start the drawing loop.deltaframestartdraw; ;
stop
The stop method stops the game and resets all deltaframe values back to their original values.
Note that this should only be used when you are done with the animation or drawing. In order to start the loop again you have to call start and provide the drawing function.
// Start the drawing loop.deltaframestartdraw; // If some imaginary condition is met, stop deltaframe from running any more.if someConditionThatEndsTheAnimation deltaframe;
Tests
The tests for Deltaframe 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 Deltaframe.
License
MIT