๐น.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
Was this helpful?