简体   繁体   English

在Kinetic.js中,如何在画布上一次加载一个图像的情况下加载多个图像

[英]In Kinetic.js how can I load multiple images with one image loaded on canvas at a time

I need generic code for whole images in the sources. 我需要通用代码来处理源代码中的整个图像。 It should still work even if image size increases. 即使图像尺寸增加,它仍然应该起作用。 For loading I am using this code: 对于加载,我使用以下代码:

var loadImages = function(sources, callback) {
    var images = {};
    var loadedImages = 0;
    var numImages = 0;
    for(var src in sources) {
       numImages++;
    }
    //document.getElementById('ImageIndex').value
    document.getElementById('TotalIndex').value="/"+numImages;
    for(var src in sources) {
       alert(src);
       images[src] = new Image();
       images[src].onload = function() {
       if(++loadedImages >= numImages) {
           callback(images);
       }
       };
    images[src].src = sources[src];
    }
}

I don't know what to do next. 我不知道下一步该怎么做。

In the middle of the code, you can see image index and total index. 在代码中间,您可以看到图像索引和总索引。 These variables indicate the current image index and the total number of images in the source, respectively. 这些变量分别指示当前图像索引和源中图像的总数。

This is the first time I am posting any mistakes please excuse me. 这是我第一次发布任何错误,请原谅。

Thank you for your help. 谢谢您的帮助。

Try : 尝试:

var loadImages = function(sources, callback) {
var images = {};

//A second array for storing Kinetic Objects
var images2 = new Array();

var loadedImages = 0;
var numImages = 0;
for(var src in sources) {
   numImages++;
}
//document.getElementById('ImageIndex').value
document.getElementById('TotalIndex').value="/"+numImages;
for(var src in sources) {
   alert(src);
   images[src] = new Image();

   images2[src] = new Kinetic.Image({
       //your config and source here
   });

   images[src].onload = function() {
   if(++loadedImages >= numImages) {
       callback(images);
   }
   };
images[src].src = sources[src];
}

//Then draw each image, one at a time:
for( var src in sources ){
    layer.add(images2[src]);
    layer.draw();  //this draws each picture one at a time
}
// or you could just redraw the whole stage after adding all the items to the layer. 
stage.draw();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM