npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

frame-ticker

v1.0.3

Published

Frame ticker with easy events for animation or games in JavaScript or TypeScript

Downloads

93,708

Readme

FrameTicker

npm Build Status Coverage Status Dependency Status

FrameTicker creates an object that continuously loops ("ticks") on every rendering frame, dispatching callbacks every time it does so.

It works on top of requestAnimationFrame, but with features of its own. It does not provide a polyfill for browsers that don't support requestAnimationFrame.

FrameTicker is written in TypeScript, but can be used both in JavaScript and TypeScript. In TypeScript, you get the benefit of automatic declarations (auto-completion, "intellisense").

Installation

Simply install FrameTicker as a module dependency using NPM:

npm install frame-ticker --save

Usage

Import:

// Import (JavaScript ES5)
var FrameTicker = require('frame-ticker').default;

// Import (JavaScript ES6 and TypeScript)
import FrameTicker from 'frame-ticker';

Create an instance:

let ticker = new FrameTicker(); // Create and start, at current browser FPS (60)
let ticker = new FrameTicker(30); // Create at 30fps and start
let ticker = new FrameTicker(30, 15); // Create at 30fps, but with a minimum of 15 calls per second, and start
let ticker = new FrameTicker(NaN, NaN, true); // Creates at paused state

Then, add callbacks using simplesignal events:

looper.onTick.add((timeSeconds, tickDeltaSeconds, currentFrame) => {
	console.log(`Executing for ${timeSeconds} seconds, delta since last tick is ${tickDeltaSeconds}, current frame is ${currentFrame}.`);
});

A typical implementation in games is moving things at a certain speed per seconds. Example:

private function onTick(currentTimeSeconds:number, tickDeltaTimeSeconds:number, currentTick:number):void {
    let speed = 10; // Speed of the box, in pixels per seconds
    box.x += speed * tickDeltaTimeSeconds;
}

looper.onTick.add(onTick);

You can also pause/resume the looper to pause/resume the game loop:

looper.pause();
looper.resume();

Ot change the time scale to make time go "faster" (reflected in timeSeconds and tickDeltaSeconds):

looper.timeScale = 2; // 2x original speed (faster motion)
looper.timeScale = 0.5; // 0.5x original speed (slower motion)

When the maxFPS parameter is used, the looper is not dispatched more that number of times per second:

// Max 30 fps dispatching; will only fire once every other visual frame on a 60fps browser
let looper = new FrameTicker(30);

When the minFPS parameter is used, the looper is always dispatched at least that amount of times per second, regardless of the number of frames:

// Max 120 fps dispatching; will fire twice per visual frame on a 60fps browser
let looper = new FrameTicker(NaN, 120);

Full reference

new FrameTicker(maxFPS:number = NaN, minFPS:number = NaN, paused:boolean = false):FrameTicker

Generate a new FrameTicker instance.

Parameters:

  • maxFPS: Desired maximum framerate. Cause onTick calls to be skipped if the desired maximum framerate is lower than the current environment maximum. NaN means no maximum.
  • minFPS: Desired minimum framerate. Cause onTick calls to be repeated more than once per frame if the desired minimum framerate is higher than the current environment maximum. Careful when using this, since it can create performance problems in low-performance situations; it can't magically makes thing faster. NaN means no minimum.
  • paused: Whether it's paused at start or not.

Return:

  • A new FrameTicker instance.

updateOnce(callback:(timeSeconds:number, tickDeltaSeconds:number, currentFrame:number) => void)

resume()

pause()

dispose()

currentTick:number

currentTimeSeconds:number

tickDeltaTimeSeconds:number

timeScale:number

onResume:SimpleSignal

Can use .add(f), .resume(f), .removeAll

onPause:SimpleSignal

onTick:SimpleSignal

onTickOncePerFrame:SimpleSignal

License

MIT.