mobi001
mobi001

Reputation: 537

How can i create Canvas Image from image sources in an array?

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!

Upvotes: 0

Views: 159

Answers (3)

mobi001
mobi001

Reputation: 537

mentioned code by @guest271314 is fine also, but it causes one problem in chrome; chrome does not maintain the sequence , it sort the image as soon as imaged loaded; so i fixed that with below code

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');
obj = JSON.parse(imagesrc);

obj = JSON.parse(obj);
var sources = obj;


var images = Object.keys(sources).map(function(k) { return sources[k] });

var returnedImages = loadImages(images);


for (i = 0; i < returnedImages.length; i++) { 
context.drawImage(returnedImages[i], canvaswidthdiv4, 55, widthNeeded, canvasheight);
}

// Image loading global variables
var loadcount = 0;
var loadtotal = 0;
var preloaded = false;

// Load images
function loadImages(imagefiles) {
// Initialize variables
loadcount = 0;
loadtotal = imagefiles.length;
preloaded = false;

// Load the images
var loadedimages = [];
for (var i=0; i<imagefiles.length; i++) {
    // Create the image object
    var image = new Image();

    // Add onload event handler
    image.onload = function () {
        loadcount++;
        if (loadcount == loadtotal) {
            // Done loading
            preloaded = true;
        }
    };

    // Set the source url of the image
    image.src = imagefiles[i];

    // Save to the image array
    loadedimages[i] = image;
   }

// Return an array of images
return loadedimages;
}

Upvotes: 0

guest271314
guest271314

Reputation: 1

Note, js at Question appear drawing each image over previously drawn image onto canvas at second, third, fourth arguments to .drawImage within loadImages ?

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);

      });

Note also sources at

// 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"
        };

is object , not array


js could be shortened to single .forEach() loop ; adjusting position on canvas as needed at call to .drawImage within .forEach callback

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');

var images = ["http://lorempixel.com/50/50/cats"
              , "http://lorempixel.com/50/50/nature"
              , "http://lorempixel.com/50/50/animals"
              , "http://lorempixel.com/50/50/sports"
];

images.forEach(function(src, index) {
  var img = new Image;
  img.onload = function() {
    context.drawImage(this, index * this.width, index * this.width)
  }
  img.src = src
})
<canvas id="product-image" width="400px" height="400px"></canvas>

Upvotes: 1

raduation
raduation

Reputation: 792

Try images[key] instead of images.key.

Upvotes: 0

Related Questions