1.0.2 • Published 6 months ago

@smoovy/ticker v1.0.2

Weekly downloads
72
License
MIT
Repository
github
Last release
6 months ago

@smoovy/ticker

Version Size

A simple ticker using requestAnimationFrame.

Installation

yarn add @smoovy/ticker

or

npm install --save @smoovy/ticker

Usage

Import the ticker as usual:

import { Ticker } from '@smoovy/ticker';

Creating a ticker

const ticker = new Ticker();

Listening for the tick

Everytime you start listening for the tick, the ticker creates a new "thread", which you can control:

const thread = ticker.add(delta => {
  // Animate with delta value
});

The delta value is the time difference to the tick before in milliseconds

To introduce the last two parameters passed to the callback, here's an example on how to kill a thread after 2 seconds:

const thread = ticker.add((delta, time, kill) => {
  if (time >= 2000) {
    kill();
  }
});

You can also kill it like this:

setTimeout(() => thread.kill(), 2000);

Once the thread is marked as dead it will be removed on the next animation frame. After that, the reference will be removed.

Overriding the tick automation

If you want to execute the tick function manually, you can enable the override flag. This prevents the internal ticking, after at least one thread is available:

ticker.override = true;

You can then call the tick method manually:

function tick(time) {
  ticker.tick(
    1    /** A static delta value */,
    time /** Optional time value */
  );

  requestAnimationFrame(tick);
};

requestAnimationFrame(tick);

License

See the LICENSE file for license rights and limitations (MIT).

1.0.2

6 months ago

1.0.1

1 year ago

1.0.0

1 year ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago