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 :: js add function to array 
Javascript :: js remove key from object 
Javascript :: jquery do something if toggle open and close 
Javascript :: how to use useeffect 
Javascript :: jquery checkbox group selected value 
Javascript :: connected-react-router error could not find router reducer in state tree 
Javascript :: document fragment 
Javascript :: javascript replace spaces with br 
Javascript :: how to change html element in javascript 
Javascript :: gitignore subfolders 
Javascript :: react.createelement 
Javascript :: placeholder text disappear when click in react 
Javascript :: find items from array of ids mongoose 
Javascript :: refresh div after ajax success 
Javascript :: reverse array js 
Javascript :: how to create json file in c# 
Javascript :: how to watch for changes within a prop in vue 
Javascript :: nmapscript location 
Javascript :: array find 
Javascript :: react native dotenv 
Javascript :: jquery get custom attribute 
Javascript :: js how to convert vh to pixel 
Javascript :: local storage react 
Javascript :: lodash compare array without order 
Javascript :: javascript array contains 
Javascript :: pattern alphabet and space 
Javascript :: find an object from array of objects javascript 
Javascript :: react buffer to image 
Javascript :: react detect page width 
Javascript :: how to console in node js 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =