Answers for "requestanimationframe();"

2

requestanimationframe

function animateFunction()
{
	//animate stuff
    window.requestAnimationFrame(animateFunction);
}
window.requestAnimationFrame(animateFunction);
Posted by: Guest on August-05-2021
1

requestAnimationframe

const element = document.getElementById('some-element-you-want-to-animate');
let start, previousTimeStamp;

function step(timestamp) {
  if (start === undefined)
    start = timestamp;
  const elapsed = timestamp - start;

  if (previousTimeStamp !== timestamp) {
    // Math.min() is used here to make sure the element stops at exactly 200px
    const count = Math.min(0.1 * elapsed, 200);
    element.style.transform = 'translateX(' + count + 'px)';
  }

  if (elapsed < 2000) { // Stop the animation after 2 seconds
    previousTimeStamp = timestamp
    window.requestAnimationFrame(step);
  }
}

window.requestAnimationFrame(step);
Posted by: Guest on November-13-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language