I want to create a Canvas image from multiple images in an array coming from my ajax request; for that purpose i try to run the loop but drawImage does not works with the loop.
Then i try a function, and called that function in a loop but same thing happens drawImage does not works with this
below i have mentioned all the code one with the function one with the loop & one with static information in drawImage which is currently working.
I would really appreciate if any of you please guide me how can i fix this.
Static drawImage Code Which Works Fine
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);
});
Below is the amendments i use for function but does not works
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);
}
Below is the amendments when i use loop to draw but that is not working aswell
loadImages(sources, function(images)
{
jQuery.each( sources, function( key, value ) {
context.drawImage(images.key, canvaswidthdiv4, 55, widthNeeded, canvasheight);
});
});
Please Help!