简体   繁体   English

加载到数组中后在画布上绘制图像

[英]draw image on canvas after load into array

I tried to create an array of Image to be displayed on a canvas, after each image is loaded. 加载每个图像后,我试图创建一个图像数组以显示在画布上。 No errors, no draw... 没有错误,没有抽奖...

    var x=...
var y=...
var canvas = document.getElementById(sCanvasName);
var context = canvas.getContext('2d');

var imageCardObj = [];

//vCards contains the images file names
for (var k=0;k<vCards.length;k++){
    imageCardObj[k] = new Image();

    var func = function(){
            var c = arguments[3];
            try{                
                c.drawImage(arguments[0], arguments[1], arguments[2]);
            }catch(e){
                alert(e.message)
            }
        }

        imageCardObj[k].onload = func(imageCardObj[k], x, y, context);

        imageCardObj[k].src = "res/img/"+vCards[k].trim()+".png";   

        x+=40;
    }

You are calling the func() handler and gives the result to it to the image's onload handler. 您正在调用func()处理程序,并将结果提供给图像的onload处理程序。 That won't work so well.. and you cannot pass arguments that way to a handler function. 那将不能很好地工作..并且您不能以这种方式将参数传递给处理函数。

Try this: 尝试这个:

var func = function(){

    // "this" will be the current image in here

    var c = arguments[3];
    try{                
        c.drawImage(this, x, y); // you need to reference x and y
    }catch(e){
        alert(e.message)
    }
}

imageCardObj[k].onload = func;  // only a reference here

If you need different x and y's then you need to maintain those on the side, either in an additional array or use objects to embed the image, its intended x and y and use the url to identify the image in question inside the func() callback. 如果您需要不同的x和y,则需要将它们保持在侧面,或者放置在其他数组中,或者使用对象将图像,其预期的x和y嵌入图像,并使用url在func()内部标识出相关图像打回来。

Also note that load order may vary as the last image loaded could finish before the first one so when you draw the image they may not appear in the same order. 还要注意,加载顺序可能会有所不同,因为最后加载的图像可能会在第一个图像之前完成,因此当您绘制图像时,它们可能不会以相同的顺序出现。

You may want to do this instead: 您可能要这样做:

var files = [url1, url2, url, ...],
    images = [],
    numOfFiles = files.length,
    count = numOfFiles;

// function to load all images in one go
function loadImages() {

    // go through array of file names
    for(var i = 0; i < numOfFiles; i++) {

        // create an image element
        var img = document.createElement('img');

        // use common loader as we need to count files
        img.onload = imageLoaded;
        //img.onerror = ... handle errors too ...
        //img.onabort = ... handle errors too ...

        img.src = files[i];

        // push image onto array in the same order as file names
        images.push(img);
    }
}
function imageLoaded(e) {

    // for each successful load we count down
    count--;
    if (count === 0) draw(); //start when all images are loaded
}

Then you can start the drawing after the images has loaded - the images are now in the same order as the original array: 然后,您可以在图像加载后开始绘图-图像现在与原始数组的顺序相同:

function draw() {
    for(var i = 0, img; img = images[i++];)
        ctx.drawImage(img, x, y); // or get x and y from an array
}

Hope this helps! 希望这可以帮助!

This is the final (working) version 这是最终版本(工作中)

var x=...
var y=...
var canvas = document.getElementById(sCanvasName);
var context = canvas.getContext('2d');
var imageCardObj = [];

    for (var k=0;k<vCards.length;k++){

        imageCardObj[k] = new Image();
         imageCardObj[k].xxx=x;

        var func = function(){              

            try{                
                context.drawImage(this, this.xxx, yStreet);                 
            }catch(e){
                alert(e.message)
            }
        }

        imageCardObj[k].onload = func;      
        imageCardObj[k].src = 'res/img/'+vCards[k].trim()+".png";   
        x +=40;         

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

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