帧率FPS控制

在这里插入图片描述

<!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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值