HTML5 打印矩形数组错误
HTML5 print array of rectangles error
HTML5,JavaScript 和在画布中绘制多个矩形
所以我在过去的 2 个小时里困惑自己为什么这不起作用,我已经遵循了上述问题中的所有代码,但它仍然对我不起作用,我做错了什么?
我添加了一个正方形,以确保它到达该行并实际工作
http://jsfiddle.net/Wh5YX/
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);
}
}
这似乎是Shape
对象中的一个简单的错别字:
function Shape(a,b,w,h,fill){
this.x = a; /// change to this.x
this.y = b; /// change to this.y
this.w = w;
this.h = h;
this.fill = fill;
}
在这里更新小提琴
或者,您可以通过提供正确的变量名称来降低拼写错误的风险(通常),例如:
function Shape(x, y, w, h, fill){
this.x = x; /// now we see the link more clearly
this.y = y;
this.w = w;
this.h = h;
this.fill = fill;
}
相关文章:
- JavaScript数组值设置错误
- jQuery Ajax数组序列化错误
- 嵌套的 Jquery 每个 2D 数组错误地重复表单元格
- Angular JS ng重复/添加到数组错误
- HTML5 打印矩形数组错误
- Javascript多维数组错误
- 数组错误如何使其工作
- 更改Img属性时出现填充数组错误
- 试图使用javascript将文本区域中的链接替换为超链接会导致数组错误
- javascript数组错误
- 简单的Javascript游戏:可能的对象数组错误
- Js:多维数组错误嵌套,而这仍然工作的一半
- Javascript数组中出现数组错误
- js空数据数组错误处理
- JS/JQuery在某些函数中出现全局数组错误
- Json数组错误无法设置属性'model'的定义
- 排序日期对象数组错误javascript
- 推送关联数组错误
- 未定义的数组错误(javascript)使用php/ajax/json等
- JSON数组错误