简体   繁体   English

HTML5 canvas数组创建错误

[英]HTML5 canvas array creation error

I am using the following code to try out the canvas element 我正在使用以下代码尝试canvas元素

    Shape = function(x,y){
        this.x = x;
        this.y = y; 
    };

    shapes = new array();

    shapes.push(new Shape(50,50,10,10));
    shapes.push(new Shape(100,100,10,10));
    shapes.push(new Shape(150,150,10,10));

    function animate(){
        context.clearRect(0,0, canvas.width(),canvas.height());
        var shapesLength = shapes.length;
        for(var i = 0; i < shapesLength; i++){
            var tmpShape = shapes[i];
            tmpShape.x++;
            context.fillRect(tmpShape.x,thmpShape.y,10,10);
        }
        context.fillStyle = 'yellow';
        context.fill();
        if(playAnimation){
            setTimeout(animate, 1)
        }
    }

However, when I run this in the browser I get the following error - 但是,当我在浏览器中运行此命令时,出现以下错误-

ReferenceError: array is not defined    

shapes = new array();

I have tried making it a global and local variable I just cant see where I am going wrong? 我尝试将其设置为全局变量和局部变量,但我看不到哪里出错了?

array should be capitalized, as that is the name of the constructor: array应大写,因为这是构造函数的名称:

shapes = new Array();

In addition it's better to use the square bracket notation to create an array. 另外,最好使用方括号表示法创建数组。 Like this: 像这样:

shapes = [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM