当前位置:网站首页>JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
2022-07-25 10:03:00 【Su Yiheng】
FPS Frames per second , We have regular fps It's usually 60, Next use requestAnimationFrame To achieve fps Tested demo
Let's talk about the principle first :
requestAnimationFrame The number of callback function executions usually matches the number of browser screen refreshes , And using this API The principle of realizing animation is to call again in the callback function requestAnimationFrame, So when the page is constantly redrawn , And then test 1 Seconds requestAnimationFrame Number of calls , It's the current FPS
window.requestAnimationFrame() Tell the browser —— You want to perform an animation , It also requires the browser to invoke the specified callback function before updating the animation next time . This method needs to pass in a callback function as a parameter , This callback function will execute before the next redraw of the browser
Be careful : If you want to update the next frame of animation before the browser redraw next time , Then the callback function itself must call... Again window.requestAnimationFrame()
——MDN
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background: #1890ff;
border: 1px solid crimson;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div id="div"></div>
</body>
<script>
let num = 0
let height = 0
let frame = 0
window.onload = () => {
let animationHeight = () => {
document.getElementById('div').style.height = (++height) + 'px'
if (height < 1000) {
frame++
requestAnimationFrame(animationHeight)
}
}
// Output current frames per second
setInterval(() => {
console.log(frame)
frame = 0
}, 1000)
requestAnimationFrame(animationHeight)
}
</script>
</html>

It's just a simple DEMO The idea of , In fact, there are many details that have not been taken into account , Specifically, it should be improved according to your business scenario .
边栏推荐
猜你喜欢
随机推荐
SD/SDIO/EMMC
ECO简介
ARM预备知识
CCF 201512-4 送货
App lifecycle and appledelegate, scenedelegate
概率论与数理统计 3 Discrete Random Variables and Probability Distributions(离散随机变量与概率分布) (下篇)
Mlx90640 infrared thermal imaging sensor temperature measurement module development notes (III)
C函数不加括号的教训
深度学习 段错误(Segment Core/ Exit code 139)情况记录
ISP image signal processing
CCF 201503-3 节日
手持振弦采集仪对振弦传感器激励方法和激励电压
工程仪器振弦传感器无线采集仪的采集数据发送方式及在线监测系统
T5论文总结
【成长必备】我为什么推荐你写博客?愿你多年以后成为你想成为的样子。
Data viewing and parameter modification of multi-channel vibrating wire, temperature and analog sensing signal acquisition instrument
Introduction to armv8 general timer
ARMV8 datasheet学习
Introduction to Verdi Foundation
预测2021年:加速实现RPA以外的超自动化成果








