This is a simple JavaScript engine built around request animation frame that features a small api for usage and a queue system
https://codepen.io/zephyr/pen/OzeQJO - Bare bones example
https://codepen.io/zephyr/pen/OOeywo - Animating a snow background
// Have a variable to track frames outside of the engine
let frame = 0;
// Make a new engine
let engine = new Engine();
// Register a function to the onStart queue (multiple can be made)
engine.onStart.register(() => {
console.log('Engine started');
});
// Register a function to the onRun queue (multiple can be made)
// This function will be executed every single frame
engine.onRun.register(() => {
console.log(`New frame: ${frame}`);
// Change the tracking variable every frame
frame++;
});
// Register a function to the onPause queue (multiple can be made)
engine.onPause.register(() => {
console.log('Engine is paused');
});
// Register a function to the onUnpause queue (multiple can be made)
engine.onUnpause.register(() => {
console.log('Engine is unpaused and running');
});
// Register a function to the onStop queue (multiple can be made)
engine.onStop.register(() => {
console.log('Engine is stopped');
// Reset the tracking variable
frame = 0;
});
// Small helpers to see the result of the engine in the console
document.addEventListener('keyup', e => {
if(e.which === 13){ // Enter key
engine.start();
}
if(e.which === 32){ // Space key
engine.pause();
}
if(e.which === 8 || e.which === 46){ // Backspace/Delete key
engine.stop();
}
});