Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

requestanimationframe

function animateFunction()
{
	//animate stuff
    window.requestAnimationFrame(animateFunction);
}
window.requestAnimationFrame(animateFunction);
Comment

requestanimationframe js

/* Tells the browser that you wish to perform an animation and 
 requests that the browser call a specified function 
 to update an animation before the next repaint. */

requestAnimationFrame(() => {
    // some animation-related/dependent code here
});

/* The callback function is automatically passed a timestamp 
 indicating the precise time requestAnimationFrame() was called at.
 requestAnimationFrame() returns a non-zero integer that can be passed into 
 cancelAnimationFrame() to cancel a requestAnimationFrame() call */

/* Advantages over setTimeout(..., 1000/60) approach:
 - The browser can optimize it, so animations will be smoother
 - Animations in inactive tabs will stop, allowing the CPU to chill
 - More battery-friendly */



Comment

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);
Comment

requestanimationframe in javascript

/* Request Animation Frame By Gourav Khurana */


https://flaviocopes.com/requestanimationframe/
https://glitch.com/edit/#!/flavio-requestanimationframe-example?path=script.js%3A1%3A0
https://glitch.com/edit/#!/flavio-settimeout-animation?path=script.js%3A54%3A4
Comment

PREVIOUS NEXT
Code Example
Javascript :: siwtch case javascript 
Javascript :: dynamic imports js 
Javascript :: how to change text color sweet alert IN JS 
Javascript :: js caps first letter 
Javascript :: how to emty an array in javascript 
Javascript :: javascript get query params from url 
Javascript :: react native navigation remove top header screen 
Javascript :: http requests in vue 3 
Javascript :: Vue minify images 
Javascript :: return promise in node js 
Javascript :: disable VirtualizedLists should never be nested inside 
Javascript :: angular configure routes 
Javascript :: github pages react route 
Javascript :: in text includes in aray of objects 
Javascript :: how to get date in footer javascript 
Javascript :: find object in json array 
Javascript :: flatten an array javascript 
Javascript :: change text in javascript 
Javascript :: jquery select the 3rd row of a table 
Javascript :: Javascript load at Window loading time 
Javascript :: get list of all attributes jqery 
Javascript :: jquery get element tag 
Javascript :: How to abreviate digits in js 
Javascript :: mongoose response to object 
Javascript :: javascript foreach url parameter 
Javascript :: js push in object 
Javascript :: vue add external script 
Javascript :: exec reges 
Javascript :: express cors specific origins 
Javascript :: how to clone an object 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =