Share the post "How To Change Color Of An Image Drawn In An HTML Canvas"
User Jarrod from the StackOverflow forum posted a very useful function to change the color of the image drawn in an HTML Canvas.
I modified his function to take into account where the image’s current position is in the Canvas.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function changeColorOfDrawnImage(context, image, color, x, y, w, h, rotate) { context.save(); context.translate(x, y); context.clearRect(0, 0, w, h); if (rotate) context.rotate(rotate); context.drawImage(image, 0, 0, w, h); context.fillStyle = color; context.globalCompositeOperation = "source-in"; context.fillRect(0, 0, w, h); context.restore(); } |