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

kang-timeline

v1.1.0

Published

A Javascript Library for the window.requestAnimationFrame() API

Downloads

11

Readme

Kang Timeline

Disclamer : This little project has been made for my personal use but I thougth it could be interesting to share it.

Presentation

Timeline is a Javascript Library for the window.requestAnimationFrame() API. It provides some classes which can give you more controle on the recursives calls made by a requestAnimationFrame (or rAF for short) loop.

Installation

$ npm i -S kang-timeline

Import

You can import the library as an es module :

import Timeline from 'kang-timeline'

Or via the exposed varible Timeline :

<script src="/path/to/timeline.min.js"></script>
const timer = new Timeline()

Classes and Methods

The Provider TimeProvider

TimeProvider represents the global time of our system. It is basically a recursive loop made with rAFs and to which are attached utilitary methods.

// Basic rAF loop

const callback = ts => {
	// do something
	return window.requestAnimationFrame(callback)
}

const loop = window.requestAnimationFrame(callback)

All methods are static and so utilitary. To consume the TimeProvider, you need to create a new Timeline.

| Methods | Description | | --------- | ----------------------------------------------------- | | subscribe | Add Timelines to the timeSubscribers private property | | start | Start the loop | | loop | The loop callback |

The Timeline class new Timeline()

The Timeline class is actually a subscriber to the TimeProvider. All Timelines share the same loop provided by the TimeProvider class but also have their own current timestamp.

You can add some options to your Timeline by passing an object as argument, or by setting those options afterward via methods.

// by argument
const timer = new Timeline({
	task: () => {
		/* do something */
	}
})

// OR by method
const timer = new Timeline()
timer.setTask(() => {
	/* do something */
})

id

| Name | Type | Default | | ------- | ------- | ------------ | | Number | String | Date.now() |

All Timelines have a unique id which is basically a simple Date.now() in milliseconds but you can specify a one.

const timer = new Timeline({ id: 'timer1' }))

speed

| Name | Type | Default | | ----- | -------- | ------- | | speed | Number | 1 |

The speed define how fast the time should pass. It can be less than 0 or greater than 1 but the current timestamp will obviously be negative and less precise respectively.

const timer = new Timeline({ speed: 0.5 }))

task

| Name | Type | default | | ---- | ---------------------------- | ------- | | task | Function \| Object \| null | null |

Task is executed at each loop iterration. It can be a simple function or an object with a function to execute at a given frequency.

const timer = new Timeline({
	task: ts => {
		/* do something */
	}
})
const timer = new Timeline({
    task: {
        frequency: 1000, // each second
        run: timestamp => { /* do something */ }
    }
}))

As you can see, you can retrive the current timestamp and other informations about your Timeline as the argument described further below.

range

| Name | Type | default | | ----- | ------------------------------------ | ------- | | range | number \| [number, number] \| null | null |

A range between which the timeline will be effective, with the first index being the minimum and the second being the maximum. If set as a number, the range become [0, number] with 0 as minimum.

The Timestamp class Timestamp

Because each Timeline are based on a provided time, you can access to some informations about your Timeline throught the unique parameter of a task.

| Properties | Type | Description | | ----------- | -------- | --------------------------------------------------- | | currentTime | Number | The current timestamp of your Timeline | | globalTime | Number | The global timestamp, offered by the TimeProvider |

Control Methods

You can control each Timeline by simple methods, which all can take a delay and a callback as parameters.

| Methods | Description | | ------- | ---------------------------------------------------------------------------- | | start | Start the Timeline | | stop | Stop the Timeline | | reset | Reset the Timeline, by stopping it firstly and then reseting all its values. |

// Create a new Timeline
const timer = new Timeline()

// One by one :
// 1. start the Timeline
timer.start()
// 2. stop the Timeline after 5 seconds
timer.stop(5000)
// 3. reset the Timeline after 1 second
timer.reset(1000)

// Or in chain :
timer.start().stop(5000).reset(1000)

Usually, the targeted timestamp and the actual execution timestamp have a delta of 17ms. So you can use those same methods under the sync property.

Key Times methods

Timestamp.addKeytime([ Object | Array ])

A key time trigger a callback at a specifed timestamp. You can multiple key times by passing an array to the method.

| Properties | Type | Description | | ---------- | ------------------ | ---------------------- | | id | Number \| String | A unique id | | timestamp | Number | The targeted timestamp | | task | Function | The callback to run |

// We create our Timeline instance
const timer = new Timeline()

// And we add a keytime
timer.addKeytime({
  id: 'log-0',
  timestamp: 5000
  task: timestamp => {
    console.log(timestamp)
  }
})

Timestamp.removeKeytimes([ Number | String | Array ])

Remove key times by their id.

// Our "log-0" key time is still there
// Let's remove it
timer.removeKeytime('log-0')

Timestamp.listKeytimes()

Also you can list all your keytimes by this method. They will be listed in order to their timestamp.

const list = timer.listKeytimes()

console.log(list)
/* Output
[
  {
    id: "log-1",
    timestamp: 5000,
    run: ts => { console.log(ts) }
  },
  {
    id: "log-3",
    timestamp: 8000,
    ...
  }
]
*/

Todolist

  • [ ] Add a demo page
  • [ ] Add a record solution (method) to extract our timelines and replay, pause, modify them on demand. This could be a good feature for animators
  • [x] Add types