frame-interval

Execute a function n-times per second, on requestAnimationFrame

MIT License

Downloads
249
Stars
0
Committers
3

frame-interval

What is this?

A library that allows you to limit the execution of requestAnimationFrame to n-frames per second for some value of n.

Why should I use this?

  • You want a requestAnimationFrame interface which runs at a lower speed or is bounded at some upper limit.

Installation

yarn add frame-interval

Usage

import { FrameInterval } from "frame-interval";

const FPS = 24;

// constructor FrameInterval(fps: number, callback: FrameIntervalCallback): FrameInterval

// type FrameIntervalCallback = (
//   props?:
//     | {
//         time: number;
//         frame: number;
//       }
//     | undefined
// ) => void;

const fi = new FrameInterval(FPS, ({ frame }) => {
  document.body.innerHTML = `${Math.floor(frame / FPS)} ${frame}`;
});

fi.start();
// ...
fi.stop();