我想使用image = new Image();
我用三个不同的图像定义了新变量。
当我运行代码时,我收到错误“Uncaught TypeError: Type error”
如何正确修复代码以绘制包含在可变图像中的所有图像。
非常感谢。
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = 1000;
canvas.height = 500;
var BACKGROUND = {
p1: { url : "http://flora4me.com/im/b/1149.jpg",x: 5, y: 5, w: 1280, h: 480 , dx:0 ,dy:0 ,dw:500 ,dh:500 },
p2: { url : "http://flora4me.com/im/b/1119.jpg",x: 5, y: 495, w: 1280, h: 480 , dx:100 ,dy:100 ,dw:500 ,dh:500},
p3: { url : "http://vladfilippov.com/blog/2012-12-07/intro.jpg",x: 5, y: 985, w: 1280, h: 480 , dx:200 ,dy:200 ,dw:500 ,dh:500 }
};
var image;
for (var n in BACKGROUND)
{
image = new Image();
image.src = BACKGROUND[n].url;
image.onload = function() {
ctx.drawImage(image, BACKGROUND[n].x,BACKGROUND[n].y, BACKGROUND[n].w, BACKGROUND[n].h, BACKGROUND[n].dx, BACKGROUND[n].dy, BACKGROUND[n].dw, BACKGROUND[n].dh );
};
}