我试图在画布上绘制图像数组,但是我的onload方法有问题。在CANVAS上绘制图像阵列
我认为这可能是一个问题,因为图像不能在画布上完成的onload之前绘制的,因为内幕它可能是我失去了所有的参考...
这里是我的代码:
// Grab the Canvas and Drawing Context
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
// Create an image element
var bg = document.createElement('IMG');
// When the image is loaded, draw it
img.onload = function() {
ctx.drawImage(bg, 0, 0);
}
// Specify the src to load the image
fondo.src = "../img/auxfinal.png";
img.src = "../img/bolso.png";
// var res is is my array where i have on each position (img name, x, y)
var arrayLength = res.length;
for (var i = 0; i < arrayLength; i++) {
var aux = document.createElement('IMG');
aux.onload = function() {
ctx.drawImage(aux, res2[1].substr(0,res2[1].length-2), res2[2].substr(0,res2[2].length-2));
}
}
它看起来像在循环你不分配'辅助。 SRC ='。关于失去参考你是对的。你只会得到绘制的数组中的最后一项。将'.onload'放在它自己的闭包上。就像'function drawImage(aux){aux.onload ...}'并且从循环中调用它。 – gtramontina