利用canvas、对象知识做弹跳小球

loop函数中的相关说明
1、 requestAnimationFrame(loop); 让函数每隔一段时间运行一次
2、如何让小球避免长蛇轨迹,并稍微有一点之前的运动轨迹?
可以在下一次小球出现的时候重绘画布,让他变为黑色,再给一点透明度,就可以显示之前的一点运动轨迹了

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>弹跳小球</title>
</head>

<body>
    <!-- 1、利用canvas画布画画布 -->
    <canvas></canvas>
    <script>
        const canvas = document.querySelector('canvas');
        const ctx = canvas.getContext('2d');
        const width = canvas.width = window.innerWidth;
        const height = canvas.height = window.innerHeight;
        //让小球动起来
        const balls = []; //存放小球

        //画小球 小球颜色
        //生成随机数
        function random(min, max) {
            return Math.floor(Math.random() * (max - min)) + min;
        }
        //随机颜色
        function randomColor() {
            return 'rgb(' + random(0, 255) + ', ' + random(0, 255) + ', ' + random(0, 255) + ')';
        }
        //小球属性设置  构造函数
        function bbb(x, y, r, color, vx, vy) {
            this.x = x;
            this.y = y;
            this.r = r;
            this.color = color;
            this.vx = vx;
            this.vy = vy;
        }


        bbb.prototype.draw = function () {
            // ctx.save();
            ctx.beginPath();
            ctx.fillStyle = this.color;
            ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
            ctx.fill(); //必须有这个
            // ctx.restore();
        };

        //小球运动轨迹  x,y,vx,vy
        bbb.prototype.run = function () {
            if ((this.x + this.r) >= width) {
                this.vx = -(this.vx);
            }
            if ((this.x - this.r) <= 0) {
                this.vx = -(this.vx);
            }
            if ((this.y + this.r) >= height) {
                this.vy = -(this.vy);
            }
            if ((this.y - this.r) <= 0) {
                this.vy = -(this.vy);
            }
            this.x += this.vx;
            this.y += this.vy;
        };

        //小球碰撞
        bbb.prototype.click=function (){
            for(var i=0;i<balls.length;i++){
                if(!(this===balls[i])){
                    var dx=this.x-balls[i].x;
                    var dy=this.y-balls[i].y;
                    var l=Math.sqrt(dx*dx+dy*dy);
                    if(l=this.r+balls[i].r){
                        balls[i].color=this.color=randomColor();
                    }
                }
            }
        };


        //循环动画
        function loop() {
            ctx.fillStyle = 'rgba(0, 0, 0, 0.24)';
            ctx.fillRect(0, 0, width, height);

            while (balls.length < 25) {
                const r = random(10,20);
                const ball = new bbb(
                    random(r, width - r),
                    random(r, height - r),
                    r,
                    randomColor(),
                    random(-7, 7),
                    random(-7, 7));
                    balls.push(ball);
            };
            for (let i = 0; i < balls.length; i++) {
                balls[i].draw();
                balls[i].run();
                balls[i].click();
            }
            // console.log(balls)
            requestAnimationFrame(loop);
        }
        loop();
    </script>
</body>

</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值