我試圖從URL加載多個圖像,然後將它們繪製到畫布元素中。但我不想爲每個必須加載的圖像重新創建相同的代碼。Jquery - 從URL加載圖像並在畫布上繪製
的的LoadImage功能(正常工作):
function loadImage(divId, imgId, path, width, height) {
var img = $("<img />").attr({ 'id': imgId, 'src': path , 'width': width, 'height': height, 'style': "display:none"})
.load(function() {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
alert('broken image: ' + imgId);
} else {
$("#" + divId).append(img);
}
});
}
但我想呼籲的LoadImage多次,然後在畫布上繪製所有圖片:
function myTheme()
{
loadImage("ContentBox", "bottom", "http://mydomain/images/bottom.jpg", 400, 391);
loadImage("ContentBox", "left", "http://mydomain/images/left.jpg", 400, 391);
loadImage("ContentBox", "right", "http://mydomain/images/right.jpg", 400, 391);
loadImage("ContentBox", "top", "http://mydomain/images/top.jpg", 400, 391);
// I need to wait ALL loads before using getElementById (these lines below don't work)
_imgBottom = document.getElementById("bottom");
_imgLeft = document.getElementById("left");
_imgRight = document.getElementById("right");
_imgTop = document.getElementById("top");
Context.drawImage(_imgBottom, 0, 0);
Context.drawImage(_imgLeft, 0, 0);
Context.drawImage(_imgRight, 0, 0);
Context.drawImage(_imgTop, 0, 0);
}
我怎麼能這樣做?
謝謝