开发者

HTML 5 Canvas draw image over other image

开发者 https://www.devze.com 2023-02-06 10:15 出处:网络
I have two images I wan开发者_StackOverflowt to draw on one canvas. The problem is that the first image I draw might take longer to load than the second one. Since the pictures are drawn on the onload

I have two images I wan开发者_StackOverflowt to draw on one canvas. The problem is that the first image I draw might take longer to load than the second one. Since the pictures are drawn on the onload event it could occur that the first image is drawn on top of the second picture.

This is not what I want, I always want the second image to be drawn on top of the first image. Any ideas?


var imgSrcs = ['url1', 'url2']; // <- put image URLs here

var imgs = [];
var loaded = 0;
var loadCallback = function () {
    loaded++;
    if (loaded == imgSrcs.length) {
        // draw imgs in correct order
    }
};

for (var i = 0; i < imgSrcs.length; i++) {
    imgs[i] = new Image();
    imgs[i].addEventListener('load', loadCallback, false);
    imgs[i].src = imgSrcs[i];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消