@thmsdmcrt_/libraf
v1.3.1
Published
Lightweight requestAnimationFrame wrapper.
Downloads
4
Maintainers
Readme
libraf
Lightweight requestAnimationFrame wrapper.
Table of contents
Core Concepts
Frame is an object available as a global "singleton" when you import it. There is no instantiation required.
Loop (Private)
With Frame, you will run a unique window.requestAnimationFrame
based loop. It is the Frame Loop. You always want to start it with Frame.start()
and stop it with Frame.stop()
.
Actions (Private)
A Map
reference containing the Frame Action to call inside the Frame Loop
Stack (Private)
An Array
containing the Frame Actions, but ordered by weight. The higher the weight is, the lower is the action's priority execution inside the Frame Loop. Sorting Actions is executed each time an action in added to the Frame Stack.
Usage
Install
npm install --save @thmsdmcrt_/libraf
yarn add @thmsdmcrt_/libraf
Import
// ES
import { Frame } from "@thmsdmcrt_/libraf";
// UMD
const { Frame } = require("@thmsdmcrt_/libraf");
Methods
Adding an Action to the Stack
/**
* Add an action in the Frame Actions & Frame Stack.
* @param {*} identifier Map key to identify a Frame Action.
* @param {function} action Frame Action.
* @param {number} weight Frame Action's priority. Greater number equals lower priority.
* @returns {*} Frame
*/
Frame.add(
"identifier",
function(time) {
/* Logic */
},
0
);
The action accept the current loop time
since the last Frame.start
call as first argument.
Removing an Action from the Stack
/**
* Remove an action from the Frame Actions & Frame Stack.
* @param {*} identifier Map key reference.
* @returns {*} Frame
*/
Frame.remove("identifier");
Clearing Stack & registered Actions
/**
* Clear Frame Actions & Frame Stack.
* @returns {*} Frame
*/
Frame.clear();
Start Loop
Stopping the loop will silently fail if is running (where Frame.active() === true
).
/**
* Start the Frame Loop.
* @returns {*} Frame
*/
Frame.start();
Stop Loop
Stopping the loop will silently fail if is not running (where Frame.active() === false
).
/**
* Stop the Frame Loop.
* @returns {*} Frame
*/
Frame.stop();
Get elpased time
/**
* Frame current Time since the last start call.
* @returns {number} Frame Time
*/
Frame.time();
Get the loop state
/**
* Return the active state of Frame.
* @returns {boolean} Frame's active state.
*/
Frame.active();
Trigger an action in the next Frame
/**
* Call an action once in the next tick of Frame Loop. If the Frame Loop is inactive, the methods act as shorthand for window.requestAnimationFrame().
* @param {function} action The next Frame action to execute.
* @returns {*} Frame
*/
Frame.next(function() {
/* Logic */
});
Build
npm run build
yarn build
Testing
npm run test
yarn test
Contributing
N/A
LICENSE
MIT License
Copyright (c) 2018 Thomas D.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.