我如何从数组中的图像源创建 Canvas 图像?

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

我想从来自我的 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);

  });

  });

请帮忙!

尝试 images[key] 而不是 images.key

请注意,js 出现问题时,在 loadImages.drawImage 的第二、第三、第四个参数处,将每个图像绘制在先前绘制的图像上 canvas 上?

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

      });

另请注意 sources

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

是对象,不是数组


js 可以缩短为单个 .forEach() 循环;在 .forEach 回调

中调用 .drawImage 时根据需要调整 canvas 的位置

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>

@guest271314 提到的代码也很好,但它在 chrome 中引起了一个问题; chrome不保持顺序,图片一加载就排序; 所以我用下面的代码修复了这个问题

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