Détail du package

framesync

Popmotion9.8mMIT6.1.2

A frame-synced render loop for JavaScript

animation, raf

readme

Framesync

A tiny frame scheduler for performantly batching reads, updates and renders.

Segregating actions that read and write to the DOM will avoid layout thrashing.

It's also a way of ensuring order of execution across a frame. For instance, Framer Motion batches animations on the update step, and renders on the render step, allowing independent animation of transform properties.

Install

npm install framesync

Usage

Framesync splits a frame into discrete read, update, preRender, render and postRender steps.

Scheduling functions

Functions can be scheduled to different parts of the render loop with sync.

import sync from 'framesync';

It provides four functions, one for scheduling a function to run on each part of the frame:

sync.update(() => {});

Frame data

Each function is provided data about the current frame:

sync.update(({ delta, timestamp }) => {});
  • delta: Time since last frame (in milliseconds)
  • timestamp: Timestamp of the current frame.

This object is recycled across frames, so values should be destructured if intended to be used asynchronously.

Keep alive

We can run a function as an ongoing process by passing true as the second parameter:

let count = 0;

sync.update(() => count++, true);

This will keep the process running until it's actively cancelled.

Run immediately

The third parameter, immediate, can be used to sync a function on the current frame step.

By default, Framesync will schedule functions to run the next time that frame step is fired:

sync.update(({ timestamp }) => {
  // The following function will run on the subsequent frame:
  sync.update((frame) => frame.timestamp !== timestamp);
});

By setting immediate to true, we can add this at the end of the current step:

sync.update(({ timestamp }) => {
  // The following function will run on the **current** frame:
  sync.update(
    (frame) => frame.timestamp === timestamp,
    false,
    true
  );
});

Cancelling

Synced processes can be cancelled with the cancelSync function:

import sync, { cancelSync } from 'framesync';

let count = 0;

const process = sync.render(() => {
  count++;
  if (count >= 10) cancelSync.render(process);
}, true);

changelog

Changelog

Framesync adheres to Semantic Versioning.

[6.1.2] 2022-08-15

Update

  • tslib and typescript.

[6.1.1] 2022-08-10

Update

  • Adding types to exports field.

[6.1.0] 2021-11-24

Update

  • Updating tslib.

[6.0.1] 2021-10-22

Fixed

  • Fixing bug where flushSync being called from an existing process would boot keep-alive processes off the thread.

[6.0.0] 2021-09-23

Fixed

  • Fixing exports and module in package.json. This will break (unsupported) direct file imports.

[5.3.0] 2021-03-30

Added

  • Adding flushSync API for manual flushing of job queues.

[5.2.3] 2021-03-19

Fixed

  • Fixing main entry point.

[5.2.2] 2021-03-19

Fixed

  • Fixing main entry point.

[5.2.1] 2021-03-19

Added

  • Adding exports to package.json.

Updated

  • tslib to latest.

[5.2.0] 2021-03-01

Fixed

  • Unbundling ES code to facilitate code-splitting in Webpack.

[5.1.0] 2021-02-22

Fixed

  • Adding polyfill for performance.now for better compatibility with Node environments.

[5.0.0] 2021-01-01

Changed

  • Using performance.now to measure polyfilled elapsed time.

[4.1.0] 2020-8-24

Added

  • preRender step.

[4.0.2] 2019-02-05

Fixed

  • Fixing rescheduling keepAlive method.

[4.0.1] 2018-09-24

Changed

  • Updated README.

[4.0.0] 2018-09-24

Changed

  • New API.
    • onFrameUpdate(callback, immediate) -> sync.update(callback, keepAlive, immediate)
    • cancelOnFrameUpdate(callback) -> cancelSync.update(callback)

Added

  • keepAlive parameter.

[3.1.9] 2018-05-16

Fixed

  • Fixing Rollup config.

[3.1.8] 2018-05-13

Added

  • Rollup support for outputting ES modules and UMD bundles.

[3.1.7] 2018-01-04

Fixed

  • Fixing illegal invocation errors.

[3.1.6] 2018-01-04

Changed

  • Using previous frame duration as default duration (for instance between active cycles).

[3.1.5] 2018-01-04

Changed

  • Cleaning polyfill.

[3.1.4] 2018-01-04

Changed

  • Max permitted time elapsed is now 40ms to permit 30fps max.
  • When startRenderLoop is fired, and the loop is not active, we set a new currentTime.

[3.1.3] 2017-11-08

Fixed

  • Actually pointing to new declaration file.

[3.1.2] 2017-11-08

Fixed

  • Pointing to new declaration file.

[3.1.1] 2017-11-08

Fixed

  • Automatically exporting declaration file.

[3.1.0] 2017-11-08

Added

  • Added optional true flag to schedule. This will schedule a job to run at the end of the current frame step.

[3.0.0] 2017-08-28

  • currentFrameTimestamp becomes currentFrameTime for symmetry with currentTime.

[2.0.1] 2017-08-26

Added

  • Changelog and Readme.

[2.0.0] 2017-08-26

Added

  • First publish.