I have been having a bear of a time trying to figure out why my colorization of a PNG won't work. I want to:
- create a canvas.
- draw a rectangle in it.
- clip the rect, so that subsequent drawing happens INSIDE the rect.
- bring in a couple of shapes INSIDE the rect.
- bring in a PNG (with transparent areas) inside the rect as well.
- Colorize the Png, in which the shapes underneath shows through the transparent areas and the PNG can togle thru any color.
I got so frustrated that I kept simpifying to get to the root of the problem, until I just copied the SIMPLEST piece of code from the W3schools site, and this simple image colorization doesn't work! Why does the simple example work when I look at it on the W3site but when I copy it VERBATIM onto my computer (change image name and src) it doesn't work?
I got the latest Chrome. Here's the code from W3schools.
<!DOCTYPE html>
<html>
<body>
<img id="scream" src="supportArt/scream.jpg" alt="The Scream" width="220" height="277">
<canvas id="myCanvas" width="220" height="277" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
document.getElementById("scream").onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 0, 0);
var imgData = ctx.getImageData(0, 0, c.width, c.height);
// invert colors
var i;
for (i = 0; i < imgData.data.length; i += 4) {
imgData.data[i] = 255 - imgData.data[i];
imgData.data[i+1] = 255 - imgData.data[i+1];
imgData.data[i+2] = 255 - imgData.data[i+2];
imgData.data[i+3] = 150;
}
ctx.putImageData(imgData, 0, 0);
};
</script>
</body>
</html>