除了 Cerbrus 给出的好答案之外,这个 jsFiddle 还向您展示了如何使用easeljs 使用循环在它们自己的单独画布元素中显示您的图像:http: //jsfiddle.net/m1erickson/gxrSQ/
另请注意,在这行代码中,您的 can1 变量实际上是一个 Stage 对象——不是画布: can1=new Stage(canvas1);
下面是循环遍历 imageUrls 数组并使用 easeljs 将图像加载到单独的画布元素中的脚本:
var images=[];
var stages=[];
// create an array to hold the path to each image
images.push("yourImage1.png");
images.push("yourImage2.png");
images.push("yourImage3.png");
// call a function to load each of the images you pushed into images[]
loadImages();
function loadImages(){
// loop thru images
for(var i=0;i<images.length;i++){
// create a new canvas for this image
var canvas = document.createElement("canvas");
canvas.setAttribute("id", "can"+i);
document.body.appendChild(canvas);
stages.push(i);
// create a new image object
var img=new Image();
img.index=i;
// important! -- wait until the image has been loaded
img.onload=function(e){
// get the canvas element for this image
// and set the canvas.width and canvas.height
var c=document.getElementById("can"+this.index);
c.setAttribute("width",this.width);
c.setAttribute("height",this.height);
// create a new stage using this new canvas
var stage=new createjs.Stage(c);
// create a bitmap to feed into the new stage
var bmp=new createjs.Bitmap(this);
// add the bitmap to the stage
stage.addChild(bmp);
// update the stage (the image will display now)
stage.update();
// save this new stage in an array
// in case you need it later
stages[this.index]=stage;
}
// set the image source from the images array
img.src=images[i];
}
}