Giter VIP home page Giter VIP logo

dom-loop's Introduction

Build Status codecov Wallaby.js

DOM Animation Loop

Batches DOM read/write operations within a constant animation loop to prevent layout thrashing and allow for smooth animation of DOM elements.


Basic usage

Install using npm install dom-loop --save

Queue an action to run in either the read or write phase of the next animation frame using read() and write()

import { loop } from "dom-loop";

const myElement1 = document.querySelector("#myElement1");
const myElement2 = document.querySelector("#myElement2");

let height;

loop.read(() => {
    // Read from the DOM
    height = myElement1.clientHeight;
});

loop.write(() => {
    // Write to the DOM
    myElement.style.height = `${height}px`;
});

Read or write every frame

By default, actions queued in the read or write phases only run once on the next frame. You can have those actions run every frame indefinitely, by specifying false as the second parameter

// This value will be read and updated every frame until stopped
loop.read(() => {
    height = myElement.clientHeight;
}, false);

To stop the action from running any further, store the action's ID and call removeEventHandler():

const id = loop.read(() => height = myElement.clientHeight, false);

loop.removeEventHandler(id);

Read or write every nth frame

The third parameter in read()`` and write()``` controls which frames to run on. The following example runs every 2nd frame (using frameCount modulo 2);

// Runs every other frame
loop.read(() => {
    height = myElement.clientHeight;
}, false, 2);

Custom phases

By default, the loop batches operations in a read phase and a write phase. You can add any number of custom phases as needed.

// Adds a new phase called "calc" before the write phase
loop.addPhaseBefore("calc", "write");

// Use add() to queue an action into a custom-defined phase
loop.add("calc", () => {
    height = height * 2;
});

dom-loop's People

Contributors

stephenjjbrown avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.