๐Ÿ”น.requestAnimationFrame()

๐Ÿ”ฐ JS โŸฉ browser โŸฉ DOM โŸฉ type โŸฉ Window โŸฉ .requestAnimationFrame()

  • run the animation

// โญ๏ธ 1. request first frame
requestAnimationFrame(animate);

// render loop
// - t1: current timestamp (in ms)
// - t0: last timestamp
function animate(t1, t0) {
  if (!t0) t0 = t1;
  // โญ๏ธ 2. update the animation ...
  // ...
  // โญ๏ธ 3. request next frame if necessary
  if (!done) requestAnimationFrame(t2 => animate(t2, t1));
}
  • stop the animation

let id = requestAnimationFrame(animate);
// ...
cancelAnimationFrame(id);

โญ๏ธ ๆณจๆ„๏ผš

requestAnimationFrame() ็š„ timestamp ๆ˜ฏๅพžใ€Œ็ถฒ้ ไธ€ๆ‰“้–‹ใ€ๅฐฑ้–‹ๅง‹่จˆ็ฎ—๏ผŒไธฆไธๆ˜ฏๅพžๅŸท่กŒ requestAnimationFrame(animate) ๆ‰้–‹ๅง‹โ—

Last updated