npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

frame-loop

v1.2.1

Published

timing for simulations and games for node and the browser

Downloads

19

Readme

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
  • opts.now - function to use for timing in milliseconds like Date. now(). By default, this is window.performance.now() in browsers and a wrapper for process.hrtime() that returns floating point milliseconds in node.

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