Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Border Dynamically to an Image on an HTML5 Canvas

I've got a canvas that includes images, I'm re-drawing 1 pixel lower each time to give the effect of falling. I've got the images in an array and I just place them 1 pixel lower without recreating the image.

Is it possible to add a border dynamically to images that reach a certain point and if so, how?

like image 756
Dexter Avatar asked Dec 13 '22 11:12

Dexter


2 Answers

Yes, all you have to do is draw a path outside the image and call ctx.stroke() to make the border.

So say the image has the coordinates x and y, with a width and height of w and h, you just do:

ctx.rect(x, y, w, h);
ctx.stroke();

Want a different colored border?

ctx.strokeStyle = 'blue';

Thicker?

ctx.lineWidth = 5;
like image 179
Simon Sarris Avatar answered Dec 14 '22 23:12

Simon Sarris


If you know your images' size and location and as you draw them you probably do, You can use the .rect canvas method to draw a rectangle around the image.

like image 41
Variant Avatar answered Dec 14 '22 23:12

Variant