Framesync

6.1.2 · active · verified Sun Apr 19

Framesync is a JavaScript library designed for scheduling functions into a synchronized render loop, primarily to prevent layout thrashing and ensure predictable execution order within a browser frame. It provides discrete steps for `read`, `update`, `preRender`, `render`, and `postRender` operations, allowing developers to organize DOM interactions efficiently. The current stable version is 6.1.2, and as part of the Popmotion ecosystem, it maintains a steady, mature release cadence focusing on stability and performance. Its primary differentiator is the explicit segregation of frame steps, which is crucial for high-performance animations and UI updates, notably used by libraries like Framer Motion to manage complex transform animations independently. Functions scheduled with Framesync receive frame data including `delta` (time since last frame) and `timestamp`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates scheduling a function to run on the `update` step of the render loop, accessing frame data, and canceling the process once a condition is met. It also shows an immediate render call.

import sync, { cancelSync } from 'framesync';

let animationProgress = 0;
const targetProgress = 100;

console.log('Starting framesync animation...');

const updateFunction = ({ delta, timestamp }) => {
  // In a real app, 'delta' would be used to calculate frame-rate independent updates.
  // For this example, we'll just increment.
  animationProgress += 1; 

  // Simulate some DOM read operation (e.g., getBoundingClientRect)
  const elementWidth = document.body?.clientWidth || 0; 
  console.log(`[Frame ${timestamp.toFixed(2)}] Delta: ${delta.toFixed(2)}ms, Progress: ${animationProgress}, Width: ${elementWidth}`);

  if (animationProgress >= targetProgress) {
    cancelSync.update(updateFunction);
    console.log('Animation complete!');
  }
};

sync.update(updateFunction, true); // Schedule to run indefinitely on the update step

// Simulate an immediate render call for some initial setup
sync.render(() => {
  // This would typically update DOM properties
  // For example: document.body.style.transform = `translateX(${animationProgress}px)`;
  console.log('Initial render step executed immediately.');
}, false, true);

view raw JSON →