当前位置:网站首页>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的思路,其实还有很多细节情况没有考虑到,具体还是要根据你的业务场景进行改进。
边栏推荐
猜你喜欢

Mixed supervision for surface-defect detection: from weakly to fully supervised learning:表面缺陷检测的混合监督

ARMV8体系结构简介

CCF 201503-4 网络延时

Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 1)

Swift simple implementation of to-do list

How to import a large amount of data in MATLAB

Defect detection network -- hybrid supervision (kolektor defect data set reproduction)

Get to know opencv4.x for the first time --- add Gaussian noise to the image

Connection and data reading of hand-held vibrating wire vh501tc collector sensor

First acquaintance with opencv4.x --- ROI interception
随机推荐
深度估计自监督模型monodepth2在自己数据集的实战——单卡/多卡训练、推理、Onnx转换和量化指标评估
CCF 201509-3 模板生成系统
ADC介绍
SD/SDIO/EMMC
First knowledge of opencv4.x --- image template matching
ARM GIC简介
[dimension reduction strike] Hilbert curve
T5论文总结
阿里MQTT物联网平台“云产品流转”实战——两片ESP32通过物联网平台实现远程互操作
Fundamentals of C language
Creation of adjacency matrix of undirected connected graph output breadth depth traversal
App lifecycle and appledelegate, scenedelegate
基于PackNet的演进——丰田研究院(TRI)深度估计文章盘点(下)
工程监测无线中继采集仪和无线网络的优势
ADC introduction
【RNN】剖析RNN 之 从RNN-(Simple|LSTM) 到 序列生成 再到 seq2seq框架(encoder-decoder,或称为seq2seq)
Segmentation based deep learning approach for surface defect detection
目标检测与分割之MaskRCNN代码结构流程全面梳理+总结
Swift simple implementation of to-do list
NLM5系列无线振弦传感采集仪的工作模式及休眠模式下状态