Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place an image behind other drawings on Canvas?

Tags:

html

canvas

I am using Canvas in HTML5. I load some images and 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?

like image 900
user963936 Avatar asked Feb 02 '26 20:02

user963936


1 Answers

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/

like image 156
Simon Sarris Avatar answered Feb 04 '26 12:02

Simon Sarris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!