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

tickloop

v0.5.0

Published

Simple and performant wrapper around Request AnimationFrame

Downloads

54

Readme

tickloop

Simple and performant animation frame handler. The API and implementation were inspired by the GreenSock Animation Platform (GSAP) library.

Installation & Setup

tickloop is available on npm and can be installed with your favorite package manager:

$ yarn add tickloop
# or
$ npm i tickloop

Then import the creator from the package in your code:

import { createTicker } from 'tickloop'

const ticker = createTicker()

Usage

Configuring

The createTicker function accepts a configuration object with the following properties:

  • fps: The target frames per second (fps). The ticker will make its best effort to reach this framerate. Note that you are effectively capped to 60 because the browser's own requestAnimationFrame will not fire above this rate.
  • lagThreshold: The number of milliseconds the ticker should consider "lag". If the elapsed time between two ticks exceeds this value, the ticker will apply a lag ease. See Lag Easing below.
  • adjustedLag: The number of milliseconds the ticker should advance by when easing a lag spike. When a lag spike occurs, the ticker will advance time by this amount. See Lag Easing below.
  • stopped: Whether this ticker should not start() itself immediately.

Lag Easing

This effect is borrowed from GSAP. Should the ticker encounter a lag spike, instead of advancing that time all at once or skipping it entirely, it advances time by a fixed easing amount.

By default, the lagThreshold is set to 500ms and the adjustedLag is set to 33ms. This means that if the ticker observes that the elapsed time between two ticks exceeds 500ms, it will advance time by only 33ms.

Setting lagThreshold to 0 disables this feature.

Working with Tick Callbacks

Add a tick callback that you wish to execute every frame with the add method:

ticker.add((time, delta, frame) => {
  // ...
})

The callback receives, as arguments:

  • time: The current time in milliseconds
  • delta: The milliseconds since the previous tick
  • frame: The number of frames that have passed

Remove a tick callback with the remove method:

const callback = (time, delta, frame) => {
  // ...
}

ticker.add(callback)

// later

ticker.remove(callback)

If you only want to hook into one frame, as opposed to every frame, you can send your callback to the once method:

ticker.once((time, delta, frame) => {
  // ...
})

Getting General Information

The ticker itself makes the same time, delta, and frame information sent to callbacks available on the instance. Additionally, it exposes a deltaRatio which is the ratio of the current delta value to the expected delta value of the target fps:

// the time in milliseconds this ticker has been running
ticker.time

// the time in milliseconds since the last frame this ticker observed
ticker.delta

// the ratio between the current delta and the expected delta for the target frame rate
ticker.deltaRatio

// the number of frames that have passed
ticker.frames