Giter Club home page Giter Club logo

rafps's Introduction

rafps codecov

A tiny (178B) helper for playing, pausing, and setting requestAnimationFrame frame rates.

This module is delivered as:

Install

$ npm install --save rafps

Usage

import rafps from 'rafps';

// Run at 30fps
const ctx = rafps(frame => {
  console.log('Current frame:', frame);

  // Do animation things...

  // target hit, pause animation
  // .. or can run indefinitely
  if (frame >= 1e3) ctx.pause();
}, 30);

// Begin playback
ctx.play();

API

rafps(draw, fps?)

Returns: RAFPS

Returns a new RAFPS instance which can toggle between play/pause states using the same draw and fps values.

draw

Type: Function
Required: true

The callback function to invoke at every "frame" tick.

It will receive one argument, frame (number), which is the current frame count since playback began.
The frame count starts at 0 and will increment indefinitely until paused. Once paused, frame will start from 0 the next time playback begins.

fps

Type: Number
Default: 60

The number of frames per second* to target.
Put differently, this is the maximum number of times draw will be called per second.

Important: This is a time-based calculation, so it's a best guess solution!
Also, your true frame rate will be at the mercy of your draw callback.
You should aim for lightweight operations that complete within 1ms.

rafps.play()

Returns: void

Begin playback.
Your draw function will begin invocations, receiving a new frame (number) parameter. See draw for info.

rafps.pause()

Returns: void

Pause playback.

License

MIT © Luke Edwards

rafps's People

Contributors

lukeed avatar

Watchers

 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.