【发布时间】:2013-12-13 16:11:40
【问题描述】:
我正在尝试从 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);
}
请问我该怎么做?
谢谢
【问题讨论】:
标签: javascript jquery canvas load