1.4.1 • Published 7 years ago

browser-game-loop v1.4.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Build Status npm version Dependency Status semantic-release Commitizen friendly GitHub license

browser-game-loop

A very minimal game loop for browser games.

It's working with fixed update time step and variable rendering.

Loop execution model

FRAME

|---<<<--------------------<<<------------------------<<<---------------------<<<---|
|                         |---<<<------------------<<<---|                          |
|                         |                              |                          |
|         |---------|     |         |----------|         |     |----------|         |
|         |         |     |--->>>---|          |--->>>---|     |          |         |
|--->>>---| input() |------>>>------| update() |------>>>------| render() |--->>>---|
          |         |               |          |               |          |
          |---------|               |----------|               |----------|
TIMELINE

input  : |    |   |    |  |   |    |    |   |  |   |  |    |  |    |   |  
update :  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
render :   |    |   |    |  |   |    |    |   |  |   |  |    |  |    |   |

API

Factory function

createGameLoop(options:Object) => Object

Options object

var loop = createGameLoop({
    updateTimeStep: 1000 / 30,
    fpsFilterStrength: 20,
    slow: 1,
    input: function(){},
    update: function(step){},
    render: function(interp){}
});
PropertyTypeDefaultDesc
updateTimeStepNumber1000/30 ~33msSets update time step to a fixed value
fpsFilterStrengthNumber20How often should FPS measurement change (1 means every frame)
slowNumber1Slow motion coefficient (the bigger the slower)
inputFunctionN/AThis function is responsible for processing input
updateFunction(step:Number)N/AThis function is responsible for updating game objects' properties, physics etc...
renderFunction(interpolation:Number)N/AThis function is responsible for drawing game objects

Returned object

loop.start();
loop.stop();
loop.getFps();
loop.getElapsedTime();
loop.setSlow(2);
loop.getSlow();
PropertyTypeDesc
startFunctionStarts the loop
stopFunctionStops the loop
getFpsFunctionGet FPS
getElapsedTimeFunctionGet elapsed time
setSlowFunction(slow:Number)Set slow motion coefficient
getSlowFunctionGet slow motion coefficient

Inspiring blogs, posts

Javascript Game Foundations - The Game Loop

deWiTTERS Game Loop

The Curious Case of Casey and The Clearly Deterministic Contraptions

Fix Your Timestep!

A Detailed Explanation of JavaScript Game Loops and Timing

Game Programming Patterns - Game Loop

Interpolated Physics Rendering

GAME TIMERS: ISSUES AND SOLUTIONS.

Game loops - Basic theory

To multiply with delta time or not to multiply with delta time?

1.4.1

7 years ago

1.4.0

8 years ago

1.3.3

8 years ago

1.3.2

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.5

8 years ago

1.2.4

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago