GithubHelp home page GithubHelp logo

frame-loop's Introduction

frame-loop

timing for simulations and games for node and the browser

testling badge

build status

example

Here is a simple game where you can drive a purple square with the keyboard:

var loop = require('frame-loop');
var keyname = require('keynames');

var player = document.querySelector('#player');
var pos = { x: 200, y: 200 };
var vel = { x: 0, y: 0 };

var engine = loop(function (dt) {
    pos.x += vel.x * dt / 5;
    pos.y += vel.y * dt / 5;
    player.style.left = pos.x;
    player.style.top = pos.y;
});
engine.run();

engine.on('fps', function (fps) {
    console.log('fps=', fps);
});

window.addEventListener('keydown', function (ev) {
    var name = keyname[ev.which];
    if (name === 'left') vel.x = -1;
    if (name === 'right') vel.x = 1;
    if (name === 'up') vel.y = -1;
    if (name === 'down') vel.y = 1;
});
window.addEventListener('keyup', function (ev) {
    var name = keyname[ev.which];
    if (name === 'left' || name === 'right') vel.x = 0;
    if (name === 'up' || name === 'down') vel.y = 0;
});

and the html:

<!doctype html5>
<html>
  <head>
    <style>
      #player {
        position: absolute;
        background-color: purple;
        width: 100px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <div id="player"></div>
    <script src="bundle.js"></script>
  </body>
</html>

Compile with browserify:

$ browserify loop.js > bundle.js

methods

var loop = require('frame-loop')

var engine = loop(opts, fn)

If a function fn(dt) is given, it will be registered as a listener for the 'tick' event.

opts are:

  • opts.fps - the target fps to aim for, default: 60
  • opts.fpsWindow - how often to emit the 'fps' event in milliseconds, default: 1000
  • opts.requestFrame - the function to use to request a frame

If opts.requestFrame isn't provided, it will be detected dynamically.

In the browser, opts.requestFrame defaults to window.requestAnimationFrame or self.requestAnimationFrame.

On the server, opts.requestFrame defaults to setImmediate or setTimeout(fn, 0).

engine.run()

Start or unpause the engine.

engine.pause()

Stop the engine.

engine.toggle()

If the engine was running, pause it. Otherwise, run the engine.

var to = engine.setTimeout(fn, time)

Schedule an event fn to happen in time milliseconds of game time.

engine.clearTimeout(to)

Clear a timeout to created with engine.setTimeout().

var iv = engine.setInterval(fn, time)

Schedule an event fn to happen every time milliseconds of game time.

engine.clearInterval(iv)

Clear a timeout iv created with engine.setInterval().

properties

engine.running

A boolean indicating whether the engine is paused or running.

engine.time

The monotonically-increasing game time in milliseconds.

events

engine.on('tick', function (dt) {})

Whenever a frame is rendered, a tick event fires with the time difference between frames, dt in milliseconds.

engine.on('fps', function (fps) {})

Every fpsWindow (default: 1000ms), this event fires with the calculated frames per second fps.

install

With npm do:

npm install frame-loop

license

MIT

frame-loop's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.