美文网首页
帧率FPS控制

帧率FPS控制

作者: CODERLIHAO | 来源:发表于2020-07-13 20:30 被阅读0次
2020-07-13 20.28.39 (1).gif
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>帧率控制</title>
    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
<canvas id="target"></canvas>
<script>
    let canvas = document.getElementById("target");
    canvas.width = document.body.clientWidth;
    canvas.height = document.body.clientHeight;
    let ctx = canvas.getContext("2d");


    class Time {

        constructor(fps) {
            this.timeoutId = undefined;
            this.callBack = null;
            this.period = 0;
            if (fps > 0) {
                this.period = Math.round(1000 / fps);
            }
            this.delta = 0;
            this.fired_sys = NaN;
            this.firing = false;
            this.legacy = !(window.requestAnimationFrame);
        }

        setCallBack(callBack) {
            this.stopFiring();
            this.callBack = callBack;
        }

        timerCallback() {
            if (this.callBack == null) {
                return;
            }
            let now = Date.now();
            let elapsed = now - (this.fired_sys - this.delta);
            if (elapsed >= this.period) {
                this.callBack();
                this.fired_sys = now;
                this.delta = this.period > 0 ? elapsed % this.period : 0;
            }
            if (this.legacy) {
                let delay_ms = this.period > 0 ? Math.round(this.period) : 17;
                this.timeoutId = setTimeout(this.timerCallback.bind(this), delay_ms);
            } else {
                this.timeoutId = window.requestAnimationFrame(this.timerCallback.bind(this))
            }
        }

        startFiring() {
            if (!this.firing) {
                this.firing = true;
                this.delta = 0;
                this.fired_sys = Date.now()  - this.period;
                this.timerCallback();
            }
        }

        stopFiring() {
            this.firing = false;
            if (!this.timeoutId) {
                if (this.legacy) {
                    clearTimeout(this.timeoutId);
                } else {
                    cancelAnimationFrame(this.timeoutId)
                }
            }
            this.fired_sys = NaN;
            this.delta = 0;
        }
    }

    let time = new Time(30);
    time.setCallBack(draw);


    class Ball {
        constructor(cx, cy, r, color) {
            this.cx = cx;
            this.cy = cy;
            this.r = r;
            this.color = color;
        }
    }

    function randomColor() {
        const r = ~~(255 * Math.random());
        const g = ~~(255 * Math.random());
        const b = ~~(255 * Math.random());
        return 'rgba(' + r + ',' + g + ',' + b + ',1)';
    }

    const balls = [];
    for (let i = 0; i < 50; i++) {
        balls.push(new Ball(canvas.width * Math.random(), canvas.height * Math.random(), 20 + 30 * Math.random(), randomColor()))
    }

    let t = 0;

    function draw() {
        if(t == 0){
            t  =Date.now();
        }else{
            const  now = Date.now();

            console.log(now - t );
            t = now;
        }

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        for (let i = 0; i < balls.length; i++) {
            const ball = balls[i];
            ball.cx += -1 + Math.random() * 2;
            ball.cy += -1 + Math.random() * 2;
            ctx.beginPath();
            ctx.fillStyle = ball.color;
            ctx.arc(ball.cx, ball.cy, ball.r, 0, 2 * Math.PI);
            ctx.fill()
        }
    }
    time.startFiring();
</script>
</body>
</html>

相关文章

  • 帧率FPS控制

  • Web动画性能介绍

    在谈动画性能之前,先介绍一些概念。 帧率(FPS) 帧率(FPS):描述每秒播放的帧数,单位为 Hz 或者 fra...

  • VSyne 相关的概念

    帧率 即 Frame Rate,单位 fps,是指 gpu 生成帧的速率,如 33 fps,60fps,越高越好。...

  • threejs性能优化手段

    Three.js控制渲染帧率(FPS) 如果场景有动画效果,就必须周期性执行.render()更新canvas画布...

  • iOS性能(一) UI优化

    帧率-FPS UI的性能主要的体现就是帧率(Frames Per Second),当快速滚动页面时能够保持较高帧率...

  • AsyncDisplayKit 教程:达到 60 FPS 的滚动

    AsyncDisplayKit 教程:达到 60 FPS 的滚动帧率 AsyncDisplayKit 教程:达到 ...

  • 帧率、码率、分辨率与清晰度流畅度的关系

    一、什么是帧率、码率、分辨率 帧率 帧率:FPS(每秒钟要多少帧画面)以及Gop(表示多少秒一个I帧)。帧率就是在...

  • PPS

    前言 fps,是 frames per second 的简称,也就是我们常说的“帧率”。在游戏领域中,fps 作为...

  • app专项之流畅度测试

    一、几个概念 1.FrameTime:单帧渲染耗时 2.FPS:帧率 - 平均帧率:1s平均画面刷新次数 - 瞬时...

  • 对pts的计算记录

    pts = inc++ *(1000/fps);inc:第几帧,从0开始,每次加1fps为帧率:每秒显示多少针,1...

网友评论

      本文标题:帧率FPS控制

      本文链接:https://www.haomeiwen.com/subject/qgmzcktx.html