美文网首页
直线运动

直线运动

作者: 洛洛kkkkkk | 来源:发表于2017-04-20 19:31 被阅读0次
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #kk{
                box-shadow: 0 0 10px 0 sandybrown;
            }
        </style>
    </head>
    <body>
        <canvas id="kk" width="500" height="500"></canvas>
    </body>
    <script type="text/javascript">
        var myCanvas = document.getElementById("kk");
        var context = myCanvas.getContext("2d");
        
//      context.beginPath();
//      context.fillStyle = "red";
//      context.arc(100,100,30,0,Math.PI*2,false);
//      context.fill();
        
//      var ball = {
//          r:30,
//          x:100,
//          y:100,
//          color:"red",
//          speedX:3,
//          speedY:5
//      }
        function rand (min,max) {
            return parseInt(Math.random()*(max-min+1)+min)
        }
        function Ball () {
            this.r = rand(10,30);
            this.x = rand(this.r,myCanvas.width-this.r);
            this.y = rand(this.r,myCanvas.height-this.r);
            this.color = `rgb(${rand(0,255)},${rand(0,255)},${rand(0,255)})`;
            var rX = rand(-4,4);
            var rY = rand(-4,4);
            this.speedX = rX == 0?2:rX;
            this.speedY = rY == 0?1:rY;
        }
        
        Ball.prototype.draw = function () {
            context.beginPath();
            context.fillStyle = this.color;
            context.arc(this.x,this.y,this.r,0,Math.PI*2,false);
            context.fill();
        }
        Ball.prototype.move = function () {
            this.x+=this.speedX;
            this.y+=this.speedY;
            if(this.x+this.r>500 || this.x-this.r<0){
                this.speedX*=-1;
            }
            if(this.y+this.r>500 || this.y-this.r<0){
                this.speedY*=-1;
            }
        }
        var balls = [];//用来存储所有产生的对象
        for(var i=0;i<20;i++){
            var ball = new Ball();
            balls.push(ball);
        }

        function animate () {
            context.clearRect(0,0,myCanvas.width,myCanvas.height);
            for(var j=0;j<balls.length;j++){
                balls[j].draw();
                balls[j].move();
            }
            window.requestAnimationFrame(animate);
        }
        animate();
        
    </script>
</html>

相关文章

  • 匀速直线运动

    定义:匀速直线运动 物体沿直线运动时,如果在任意相同时间内通过的路程都相等,这种运动叫匀速直线运动 匀速直线运动是...

  • 如诗般的物理运动

    匀速直线运动 不骄不躁 从一而不终 匀加速直线运动...

  • 2019-07-28

    物理直线运动part

  • 变速直线运动

    1.变速直线运动:物体沿直线运动,如果在相等时间内通过的路程不相等,这种运动就称为变速直线运动 2.平均速度 1....

  • 直线运动

  • 安全生产技术(总结四)

    机械危险部位及其防护措施(能辨识出危险部位及其对应防护措施) 转动、直线运动、转动+直线运动 (一)转动危险部位及...

  • 走直线运动

    走线的活动,除了培养孩子步行机能的平衡感觉、大肌肉协调之外,还培养孩子的意志力、独立性,更可以使孩子充分的集中注意...

  • 【物理动图】抛体运动

    平抛 水平方向匀速直线运动 竖直方向自由落体 斜抛 水平方向匀速直线运动 竖直方向竖直上抛

  • 成长第一季——《通往》:关于创业的选择

    检视阅读 一)阅读目的 如果人生保持匀速直线运动,已然不够;想要有所突破,那必须要做加速直线运动;然而只是匀加速也...

  • 【轴承】直线运动轴承运转中的维护对寿命的重要性!

    直线运动轴承运转中的维护对寿命的重要性! 直线运动轴承若得到良好的润滑,并且正确的阻隔杂物及湿气,表示油封应该没有...

网友评论

      本文标题:直线运动

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