当前位置:网站首页>js利用requestAnimationFrame实时检测当前动画的FPS帧率
js利用requestAnimationFrame实时检测当前动画的FPS帧率
2022-07-25 09:24:00 【苏一恒】
FPS每秒传输帧数,我们常规的fps一般是60,接下来利用requestAnimationFrame来实现fps检测的demo
先说下原理:
requestAnimationFrame的回调函数执行次数通常与浏览器屏幕刷新次数相匹配,而利用这个API实现动画的原理就是回调函数内再次调用requestAnimationFrame,所以页面不断重绘时,然后检测1秒内requestAnimationFrame调用的次数,就是当前的FPS
window.requestAnimationFrame() 告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。该方法需要传入一个回调函数作为参数,该回调函数会在浏览器下一次重绘之前执行
注意:若你想在浏览器下次重绘之前继续更新下一帧动画,那么回调函数自身必须再次调用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)
}
}
// 每秒钟输出当前帧数
setInterval(() => {
console.log(frame)
frame = 0
}, 1000)
requestAnimationFrame(animationHeight)
}
</script>
</html>

这只是个简单的DEMO的思路,其实还有很多细节情况没有考虑到,具体还是要根据你的业务场景进行改进。
边栏推荐
- ¥ 1-2 example 2.2 put the union of two sets into the linear table
- MLX90640 红外热成像传感器测温模块开发笔记(二)
- ¥ 1-1 SWUST OJ 941: implementation of consolidation operation of ordered sequence table
- 低功耗和UPF介绍
- Introduction to low power consumption and UPF
- 从鱼眼到环视到多任务王炸——盘点Valeo视觉深度估计经典文章(从FisheyeDistanceNet到OmniDet)(下)
- ADC简介
- Linked list -- basic operation
- I2C也可总线取电!
- 【RNN】剖析RNN 之 从RNN-(Simple|LSTM) 到 序列生成 再到 seq2seq框架(encoder-decoder,或称为seq2seq)
猜你喜欢

File -- first acquaintance

工程监测多通道振弦传感器无线采集仪外接数字传感器过程

Data viewing and parameter modification of multi-channel vibrating wire, temperature and analog sensing signal acquisition instrument

SD/SDIO/EMMC

无向连通图邻接表的创建输出广度深度遍历

CCF 201509-4 高速公路

无线中继采集仪的常见问题

CCF 201509-3 模板生成系统

Get to know opencv4.x for the first time --- add salt and pepper noise to the image

MLX90640 红外热成像仪测温模块开发说明
随机推荐
ARM预备知识
CCF 201604-2 俄罗斯方块
手持振弦采集仪对振弦传感器激励方法和激励电压
BSP3 电力监控仪(功率监控仪)端子定义和接线
ADC介绍
Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 2)
Server CUDA toolkit multi version switching
AMD EPYC 9664旗舰规格曝光:96核192线程 480MB缓存 3.8GHz频率
¥ 1-1 SWUST OJ 941: implementation of consolidation operation of ordered sequence table
C函数不加括号的教训
CCF 201503-4 网络延时
MLX90640 红外热成像传感器测温模块开发笔记(三)
SystemVerilog syntax
无线振弦采集仪的使用常见问题
Terminal definition and wiring of bsp3 power monitor (power monitor)
SD/SDIO/EMMC
工程监测多通道振弦传感器无线采集仪外接数字传感器过程
How to install pytorch—— A most simple and effective method!
深度估计自监督模型monodepth2论文总结和源码分析【理论部分】
Mlx90640 infrared thermal imager temperature measurement module development notes (I)