yy-loop
Simple and configurable game/main loop
features
- emits events (using eventemitter3)
- run every frame or after an interval
- control how many times each function runs
- loop pauses when app loses focus
- uses requestAnimationFrame()
rationale
This is a replacement for YY-Update. I use this in all my games, and as a base class for my other libraries, including pixi-ease and pixi-viewport.
installation
npm i yy-loop
simple example
const Loop = require('yy-loop')
const loop = new Loop()
// add a function to the loop that logs the elapsed time
loop.add((elapsed) => console.log(elapsed))
// add a function to the loop that runs ever second for 5 times
const entry = loop.add((elapsed) => console.log(elapsed), 1000, 5)
// run each time entry is called
entry.on('each', () => console.log('It has been one second.'))
// run after entry is complete
entry.on('done', () => console.log('completed'))
live example
https://davidfig.github.io/loop/
API
src/loop.js
/** * basic loop support * @param * @param * @param * * @event each(elapsed, Loop) * @event start(Loop) * @event stop(Loop) */ /** * start requestAnimationFrame() loop * @return */ start() /** * stop loop * @return */ stop() /** * loop through updates; can be called manually each frame, or called automatically as part of start() * @param */ update(elapsed) /** * adds a callback to the loop * @deprecated use add() instead * @param * @param * @param * @return */ interval(callback, time, count) /** * adds a callback to the loop * @param * @param * @param * @return */ add(callback, time, count) /** * adds a one-time callback to the loop * @param * @param * @return */ timeout(callback, time) /** * remove a callback from the loop * @param */ remove(entry) /** * removes all callbacks from the loop */ removeAll() /** * @type */ get count() /** * @type */ get countRunning()
src/entry.js
/** Entry class for Loop */ /** * create an entry in the update loop * used by Loop * @param * @param * @param */ /** * update checks time and runs the callback * @param * @return */ update(elapsed) /** * @type */ set pause(value)
License
MIT License
(c) 2017 YOPEY YOPEY LLC by David Figatner