最后一个demo,发现画图一闪而过,然后页面就空白了,什么鬼?代码哪里写错了嘛?求看

来源:8-2 globalAlpha和globalCompositeOperation

芒果屋里的猫

2016-11-19 11:07

<!DOCTYPE html>
<html>
<head>
    <title>躁动的小球</title>
    <meta charset="utf-8">
</head>
<body>
    <canvas id="canvas" style="display: block;margin:50px auto">
        <p>you browser not support canvas!</p>
    </canvas>
    <script type="text/javascript">
        var WIDTH = 900;
        var HEIGHT = 500;
        var balls = [];
        window.onload = function(){
            var canvas = document.getElementById('canvas'),
                context = canvas.getContext('2d');
            canvas.width = WIDTH;
            canvas.height = HEIGHT;
            for(var i=0;i<20;i++){
                var radius = Math.random()*25+25;
                aBall = {
                    color:"rgb("+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+")",
                    radius:radius,
                    x:Math.random()*(canvas.width-2*radius)+radius,
                    y:Math.random()*(canvas.height-2*radius)+radius,
                    vx:(Math.random()*5+5)*Math.pow(-1,Math.floor(Math.random()*100)),
                    vy:(Math.random()*5+5)*Math.pow(-1,Math.floor(Math.random()*100))
                }
                balls[i] = aBall;
            }

            setInterval(function(){
                draw(context);
                update(canvas.width,canvas.height);
            },50);
        }

        function draw(cxt){
            var canvas = cxt.canvas;
            cxt.clearRect(0,0,canvas.width,canvas.height);
            for(var i=0;i<balls.length;i++){
                cxt.fillStyle = balls[i].color;
                cxt.beginPath();
                cxt.arc(balls[i].x,balls[i].y,balls[i].radius,0,Math.PI*2);
                cxt.closePath();
                cxt.fill();
            }
        }
        function update(canvasWidth,canvasHeight){
            for (var i = 0; i < balls.length; i++) {
                balls[i].x += balls[i].vx;
                balls[i].y += balls[i].vy;

                if(balls[i].x - balls[i].radius <= 0){
                    balls[i].vx = -balls[i].vx;
                    balls[i].x = balls[i].radius;
                }
                if(balls[i].x + balls[i].radius >= canvasWidth){
                    balls[i].vx = -balls[i].vx;
                    balls[i].x = canvasWidth - balls[i].radius;
                }

                if(balls[i].y - balls[i].radius <= 0){
                    balls[i].vy = -balls[i].vy;
                    balls[i].y = balls[i].radius;
                }
                if(balls[i].y + balls[i].radius >= canvasHeight){
                    balls[i].vy = -balls[i].vy;
                    balls[i].y = canvasHeight - balls[i].radius;
                }
            }
        }
    </script>
</body>
</html>


写回答 关注

2回答

  • 网络摆渡客
    2016-11-20 18:12:05
    已采纳

    29行,写错了,vx应该是vy

    芒果屋里的猫

    谢谢,我还找了半天

    2016-11-21 13:26:07

    共 1 条回复 >

  • 芒果屋里的猫
    2016-11-21 13:27:24

    代码已没问题

Canvas绘图详解

Canvas系列教程第二课,详解Canvas各接口,让同学彻底掌握Canvas绘图

72881 学习 · 422 问题

查看课程

相似问题