0
我試圖創建一個圖像對象的數組,並在窗口加載到畫布後加載圖像。Javascript - 爲畫布創建一個圖像對象數組?
這裏是我的代碼:
var canvasObj = document.getElementById('myCanvas');
var ctx = canvasObj.getContext('2d');
var imgsrcs = ["1.png", "2.png", "3.png"];
var imgs = [];
for(var i=0; i<imgsrcs.length; i++){
imgs[i] = new Image();
imgs[i].onload = function() {
ctx.drawImage(imgs[i], xb,yb);
}
imgs[i].src = imgsrcs[i];
}
不過,我得到這個錯誤控制檯:
TypeError: Argument 1 of CanvasRenderingContext2D.drawImage could not be converted to any of: HTMLImageElement, HTMLCanvasElement, HTMLVideoElement, ImageBitmap.
ctx.drawImage(imgs[i], xb,yb);
我在做什麼錯?
預先感謝
感謝您的回答和洞察力。有用。 – ponir