简体   繁体   English

如何处理HTML5画布中的许多图像?

[英]How do I handle many images in my HTML5 canvas?

I'm very new to Html5 canvas and Javascript. 我对Html5 canvas和Javascript很陌生。 I'm trying this : 我正在尝试:

function animate() {    
   var image1 = new Image();
   image.src = /path
   var image2 = new Image();
   image2.src = /path
   for(;;)
   {
        //change value of x and y so that it looks like moving
        context.beginPath();
        context.drawImage(<image>, x, y );
        context.closePath();
        context.fill();
   }
}

EDIT: 编辑:

And I call the animate function each 33ms : 我每33ms调用一个animate函数:

if (playAnimation) {
            // Run the animation loop again in 33 milliseconds
            setTimeout(animate, 33);
        };

If I follow the answer given here, I get the image struck and its not moving any further. 如果按照此处给出的答案进行操作,则图像会被击打,并且图像不会进一步移动。

Update : Based on new information in the question, your problem (restated) is that you want to either 更新 :根据问题中的新信息,您的问题(重述)是您想要

  1. wait for all images to load first, and then start animating with them, or 等待所有图像首先加载, 然后开始对其进行动画处理,或者
  2. start animating and only use an image if it is available. 开始制作动画,并仅使用可用的图像。

Both are described below. 两者都在下面描述。

1. Loading many images and proceeding only when they are finished 1.加载许多图像并仅在完成后进行

With this technique we load all images immediately and when the last has loaded we run a custom callback. 使用此技术,我们可以立即加载所有图像,并在加载最后一个图像时运行自定义回调。

Demo: http://jsfiddle.net/3MPrT/1/ 演示: http//jsfiddle.net/3MPrT/1/

// Load images and run the whenLoaded callback when all have loaded;
// The callback is passed an array of loaded Image objects.
function loadImages(paths,whenLoaded){
  var imgs=[];
  paths.forEach(function(path){
    var img = new Image;
    img.onload = function(){
      imgs.push(img);
      if (imgs.length==paths.length) whenLoaded(imgs);
    }
    img.src = path;
  });
}

var imagePaths = [...]; // array of strings
loadImages(imagePaths,function(loadedImages){
  setInterval(function(){ animateInCircle(loadedImages) }, 30);
});

2. Keeping track of all images loaded so far 2.跟踪到目前为止加载的所有图像

With this technique we start animating immediately, but only draw images once they are loaded. 使用这种技术,我们可以立即开始动画处理,但是只有在加载图像后才绘制图像。 Our circle dynamically changes dimension based on how many images are loaded so far. 我们的圈子会根据到目前为止加载的图片数量动态更改尺寸。

Demo: http://jsfiddle.net/3MPrT/2/ 演示: http//jsfiddle.net/3MPrT/2/

var imagePaths = [...]; // array of strings
var loadedImages = [];  // array of Image objects loaded so far

imagePaths.forEach(function(path){
  // When an image has loaded, add it to the array of loaded images
  var img = new Image;
  img.onload = function(){ loadedImages.push(img); }
  img.src = path;
});

setInterval(function(){
  // Only animate the images loaded so far
  animateInCircle(loadedImages);
}, 100);

And, if you wanted the images to rotate in a circle instead of just move in a circle: 而且,如果您希望图像旋转一圈而不是仅仅旋转一圈:

Rotating images: http://jsfiddle.net/3MPrT/7/ 旋转图片: http//jsfiddle.net/3MPrT/7/

ctx.save();
ctx.translate(cx,cy); // Center of circle
ctx.rotate( (angleOffset+(new Date)/3000) % Math.TAU );
ctx.translate(radius-img.width/2,-img.height/2);
ctx.drawImage(img,0,0);
ctx.restore();

Original answer follows. 原始答案如下。

In general, you must wait for each image loading to complete: 通常,您必须等待每次图像加载完成:

function animate(){
  var img1 = new Image;
  img1.onload = function(){
    context.drawImage(img1,x1,y1);
  };
  img1.src = "/path";

  var img2 = new Image;
  img2.onload = function(){
    context.drawImage(img2,x2,y2);
  };
  img2.src = "/path";
}

You may want to make this code more DRY by using an object: 您可能想通过使用对象使此代码更干燥:

var imgLocs = {
  "/path1" : { x:17, y:42  },
  "/path2" : { x:99, y:131 },
  // as many as you want
};

function animate(){
  for (var path in imgLocs){
    (function(imgPath){
      var xy = imgLocs[imgPath];
      var img = new Image;
      img.onload = function(){
        context.drawImage( img, xy.x, xy.y );
      }
      img.src = imgPath;
    })(path);
  }
}

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

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