弹球bouncing balls 二 添加恶魔圈,创建balls(前端)

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>

    <link rel="stylesheet" type="text/css" href="css/ball.css"/>
    <style>
        footer{position:absolute;bottom:0;width:100%;height:5%;background-color: #ffc0cb; text-align: center;}
    </style>
</head>
<body>
    <div>

        <canvas></canvas>

    </div>

    <footer >

            <button id="create"> 创建ball</button>
            <button id="evilCircle">移动恶魔圈</button>

    </footer>
    <script src="js/main.js"></script>
    <script>
        var create = document.getElementById('create');
        create.onclick = function(){

        addBalls();


        }

        var create = document.getElementById('evilCircle');
        create.onclick = function(){

        evilCircle.move();


        }



    </script>
</body>
</html>

main.js

var canvas = document.querySelector('canvas');

var ctx = canvas.getContext('2d');

var width = canvas.width = window.innerWidth;
var height = canvas.height =  window.innerHeight*0.9;


function Ball(x,y,velX,velY,color,size){
    this.x  =x ;
    this.y = y;
    this.velX = velX;
    this.velY = velY;
    this.color = color;
    this.size = size;
}



//画出模型

var balls=[];

Ball.prototype.draw = function(){


    ctx.beginPath();
    ctx.fillStyle=this.color;
     ctx.arc(this.x,this.y ,this.size,0,Math.PI*2);
     ctx.fill();
};


Ball.prototype.sport = function(){
    if((this.x+this.velX)>width || (this.x+this.velX)<0){
        this.velX = -(this.velX);
    }

    if((this.y+this.velY)>height || (this.y +this.velY)<0){
        this.velY = -(this.velY);
    }

    this.x = this.x + this.velX;
    this.y = this.y + this.velY;

};

Ball.prototype.collison = function(){

    for (var j = 0; j < balls.length; j++) {
        if(!(this===balls[j])){
            var dx = Math.abs(this.x-balls[j].x);
            var dy = Math.abs(this.y-balls[j].y);
            var dis = Math.sqrt(dx*dx + dy*dy);
            var total =  this.size+balls[j].size;

            if(dis<total){

                    this.velX = -(this.velX) ;
                    balls[j].velX =- balls[j].velX;

                    this.velY = - (this.velY) ;
                    balls[j].velY = - balls[j].velY;

                this.color = balls[j].color ='rgb('+Math.random()*255+','
            +Math.random()*255+','
            +Math.random()*255
            +')';


            }
        }
    }

};

Ball.prototype.evil = function(i){
    //吸收路过的小圈圈



            var dx = Math.abs(this.x-evilWidth);
            var dy = Math.abs(this.y-evilHeight);
            var dis = Math.sqrt(dx*dx + dy*dy);
            var total =  this.size+50;

            if(dis<total){

             balls.splice(i,1);


    }

};

function loop(){

         ctx.fillStyle = 'rgba(0,0,0,0.25)';
        ctx.fillRect(0,0,width,height);

        while(balls.length<25){
            var ball = new Ball(Math.random()*width,Math.random()*height,
            Math.random()*5+Math.random()*(-5),
            Math.random()*5+Math.random()*(-5),
            'rgb('+Math.random()*255+','
            +Math.random()*255+','
            +Math.random()*255
            +')',Math.random()*20+5);

            balls.push(ball);
        }

        for (var i = 0; i < balls.length; i++) {
            balls[i].draw();
            balls[i].sport();
            balls[i].collison();
            balls[i].evil(i);

        }

  EvilCircle();

    requestAnimationFrame(loop);
}



loop();



function addBalls(){
    var  i=0;
    while(i<25){
            var ball = new Ball(Math.random()*width,Math.random()*height,
            Math.random()*5+Math.random()*(-5),
            Math.random()*5+Math.random()*(-5),
            'rgb('+Math.random()*255+','
            +Math.random()*255+','
            +Math.random()*255
            +')',Math.random()*20+5);

            balls.push(ball);
              i++;
        }
}   

var evilWidth = width/2;
var evilHeight = height/2;

function EvilCircle(){
    //画个圈

    ctx.beginPath();
    ctx.strokeStyle = 'crimson';
    ctx.arc(evilWidth,evilHeight,50,0,2*Math.PI);
    ctx.stroke();

}

EvilCircle.prototype.move=function(){
    evilWidth= Math.random()*(width/2)+50;
    evilHeight= Math.random()*(height/2) +50;

}


var evilCircle = new EvilCircle();

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值