如何统计web fps变化

本文介绍如何使用requestAnimationFrame API来精确统计页面渲染的帧数,并通过实例演示如何计算Frames Per Second (FPS)。关键在于实时更新 fps 指标,帮助开发者监控和优化网页动画效果的流畅度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

核心知识点

  • requestAnimationFrame
  • 统计一秒内的frame次数
  • fps → Frames Per Second

示例代码

在线地址 https://jsfiddle.net/zhangchi/1gm9a84L/14/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stat</title>
    <style>
        .fps-panel {
            background: #333;
            color: #fff;
            text-align: center;
            width: 100px;
            height: 120px;
            line-height: 120px;
        }
    </style>
</head>

<body>
    <div class="fps-panel" id="stat"></div>

    <div contenteditable="true" style="overflow:hidden;margin-top:30px; width:400px;height: 200px;border: 1px #333 solid;"/>

    <script>

        let beignTime;
        let framesCount = 0;
        let prevTime;
        function runTestUI(){
          
        }
        function stat() {

            beignTime = new Date().getTime();//启动时间
            if (!prevTime) {
                prevTime = beignTime;
            }
            //测试渲染的的代码
            runTestUI();

            framesCount++
            if (new Date().getTime() - prevTime > 1000) {
                document.getElementById('stat').innerHTML = framesCount;
                framesCount = 0;
                prevTime = new Date().getTime();
            }
            window.requestAnimationFrame(stat)
        }


        window.requestAnimationFrame(stat)
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰瑞_

知识源于创作热情,感谢你的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值