所以我花了最后 2 个小时来困惑自己为什么这不起作用,我已经遵循了上述问题中的所有代码,但它仍然对我不起作用我做错了什么?
我添加了一个正方形以确保它到达那条线并实际工作
function Shape(a,b,w,h,fill){
this.a = a;
this.b = b;
this.w = w;
this.h = h;
this.fill = fill;
}
var canvas = document.getElementById("canvas");
if (canvas.getContext){
var myRect = [];
myRect.push(new Shape(100, 0, 25, 25, "#333"));
myRect.push(new Shape(0, 40, 39, 25, "#333"));
myRect.push(new Shape(0, 80, 100, 25, "#333"));
ctx = canvas.getContext("2d");
for (i in myRect) {
oblock = myRect[i];
ctx.fillStyle = oblock.fill;
ctx.fillRect(oblock.x,oblock.y,oblock.w,oblock.h);
ctx.fillStyle="#CC00FF";
ctx.fillRect(100,100,20,20);
}
}