requestAnimationFrame

window.requestAnimationFrame()方法告诉浏览器您希望执行动画并请求浏览器在下一次重绘之前调用指定的函数来更新动画。该方法使用一个回调函数作为参数,这个回调函数会在浏览器重绘之前调用。

例:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>requestAnimationFrame</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            width: 500px;
            height:500px;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <div class="box">
        <canvas></canvas>
    </div>
    <script>
        (function() {
            var oBox = document.querySelector(".box");
            var canvas = document.querySelector("canvas");
            canvas.width = oBox.offsetWidth;
            canvas.height = oBox.offsetHeight;
            ctx = canvas.getContext('2d');
            function Ball(x,y,r) {
                this.x = x;
                this.y = y;
                this.r = r;
                this.vx = random(-2,2);
                this.vy = random(-3,3);
                this.colors = ['#FF69B4','#C71585','#FF00FF','#800080','#9400D3','#90EE90'];
                this.color = this.colors[Math.floor(random(0,6))]
                this.a = 1;
            }
            var ball = new Ball();
            Ball.prototype = {
                drawBall:function() {
                    ctx.beginPath();
                    ctx.fillStyle = this.color;
                    ctx.globalAlpha = this.a;
                    ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
                    ctx.fill();
                    ctx.closePath();
                    this.updata();
                },
                updata: function() {
                    this.x += this.vx;
                    this.y += this.vy;
                    this.a -= 0.05;
                }
            }
            function random(min,max) {
                return min+(max-min)*Math.random();
            }
            var flag = false;
            var ballList = [];
            oBox.addEventListener("mousedown",function() {
                flag = true;
            })
            oBox.addEventListener("mousemove",function(e) {
                if(!flag) {
                    return;
                }
                var ball = new Ball(e.x,e.y,10);
                ballList.push(ball);
            })
            oBox.addEventListener("mouseup",function() {
                console.log(ballList)
                flag = false;
            })
            function render() {
                ctx.clearRect(0,0,oBox.offsetWidth,oBox.offsetWidth)
                ballList.forEach(function(item,index) {
                    console.log(item.a)
                    if(item.a<0) {
                        ballList.splice(index,1);
                        return;
                    }
                    item.drawBall();
                });
                if(ballList.length>10) {
                    var s = ballList.length-100
                    ballList.splice(0,s);
                }
                requestAnimationFrame(render);
            }
            render();
        })();
    </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值