Calvin-LL / delay-by-animation-frame

A Promise wrapper around requestAnimationFrame

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

delay-by-animation-frame

npm License: MIT

This library exports () => new Promise((resolve) => requestAnimationFrame(resolve))

Resolves to a DOMHighResTimeStamp which is a double

Install

Install with npm:

npm install delay-by-animation-frame

Install with yarn:

yarn add delay-by-animation-frame

Example

import delayByAnimationFrame from "delay-by-animation-frame";

async function animate() {
  const element = document.getElementById("element-id");
  let start;

  while (true) {
    const timestamp = await delayByAnimationFrame();

    if (start === undefined) start = timestamp;

    const elapsed = timestamp - start;

    if (elapsed > 2000) break;

    element.style.transform =
      "translateX(" + Math.min(0.1 * elapsed, 200) + "px)";
  }
}

animate();

About

A Promise wrapper around requestAnimationFrame

License:MIT License


Languages

Language:TypeScript 56.3%Language:JavaScript 43.7%