我想從來自我的ajax請求的數組中的多個圖像創建一個Canvas圖像;爲此,我嘗試運行循環,但drawImage
不適用於循環。如何從數組中的圖像源創建畫布圖像?
然後我嘗試的功能,並在循環中調用該函數,但同樣的事情發生drawImage
下面我提到的所有代碼,一個與功能一個與循環不會與此
工作&一個靜態目前正在運行的drawImage
中的信息。
我真的很感激,如果你們中的任何人請指導我如何解決這個問題。
靜態的drawImage工作正常
function loadImages(sources, callback, imagesrc) {
var images = {};
var loadedImages = 0;
var numImages = 0;
for(var src in sources) {
numImages++;
}
for(var src in sources) {
images[src] = new Image();
images[src].onload = function() {
if(++loadedImages >= numImages) {
callback(images);
}
};
images[src].src = sources[src];
}
}
var canvas = document.getElementById('product-image');
canvas.height = (jQuery(window).height()) -120;
canvas.width = canvas.height * 0.75;
var heightscreen = (jQuery(window).height()) -120;
var canvasheight = heightscreen;
var canvaswidth = canvas.height * 0.75;
canvaswidthdiv4 = 0;
var widthNeeded = canvasheight * 0.75;
var context = canvas.getContext('2d');
// THIS IS A DUMMY ARRAY SAME AS COME IN AJAX RESPONSE
var sources =
{
Slim_Fit: "http://localhost/plugindev/wp-content/uploads/2015/08/slimFit.png",
Inside_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/maincolar.png",
Outside_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/outer_collar1.png",
Main_Colar: "http://localhost/plugindev/wp-content/uploads/2015/08/inner_collar11.png"
};
loadImages(sources, function(images)
{
context.drawImage(images.Slim_Fit, canvaswidthdiv4, 55, widthNeeded, canvasheight);
context.drawImage(images.Inside_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
context.drawImage(images.Outside_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
context.drawImage(images.Main_Colar, canvaswidthdiv4, 55, widthNeeded, canvasheight);
});
下面的代碼是我使用的功能的修改,但不工作
loadImages(sources, function(images)
{
jQuery.each(sources, function(key, value) {
DrawImage(key, images);
});
});
function DrawImage(keyname,images){
context.drawImage(images.keyname, canvaswidthdiv4, 55, widthNeeded, canvasheight);
}
下面是修正當我使用環畫但這不工作
loadImages(sources, function(images)
{
jQuery.each(sources, function(key, value) {
context.drawImage(images.key, canvaswidthdiv4, 55, widthNeeded, canvasheight);
});
});
請幫忙!
謝謝你,讓我試試我的代碼。 – mobi001
你岩石蓓蕾,非常感謝 – mobi001