开发者

How to place an image behind other drawings on Canvas?

开发者 https://www.devze.com 2023-04-12 08:06 出处:网络
I am using Canvas in HTML5. I load some images a开发者_Python百科nd then would like to draw some lines and such on top of them.

I am using Canvas in HTML5. I load some images a开发者_Python百科nd then would like to draw some lines and such on top of them.

But the images are always positioned on top, regardless of the order in which I execute these commands. Is there a way to force them to be drawn on top?


You have to draw everything after the image loads

var img = new Image();
img.onload = function() {
    ctx.drawImage(img, 0, 0);   
    // then draw other stuff 
    ctx.fillRect(50,50,150,150);
}
img.src = 'http://placekitten.com/500/500';

Live example:

http://jsfiddle.net/XkYN7/

0

精彩评论

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

关注公众号