I'm trying to duotone an image and plaster it onto a canvas.
Works in desktop browsers:
- Chrome
- Firefox
- Safari
- Internet Explorer
Fails in mobile browsers:
- Android
Workable demo on JSFiddle, this example works in Chrome but fails in Android's default browser.
The code is:
<style>
body {
background-color: gray;
}
</style>
<canvas id="mycanvas" width="64" height="64"></canvas>
<script>
var image = new Image();
image.src = 'image.png';
image.onload = function () { //once the image finishes loading
var context = document.getElementById("mycanvas").getContext("2d");
context.drawImage(image, 0, 0);
var imageData = context.getImageData(0, 0, 64, 64);
var pixels = imageData.data;
var numPixels = pixels.length;
for (var i = 0; i < numPixels; i++) { //for every pixel in the image
var index = i * 4;
var average = (pixels[index] + pixels[index + 1] + pixels[index + 2]) / 3;
pixels[index] = average + 255; //red is increased
pixels[index + 1] = average; //green
pixels[index + 2] = average; //blue
//pixels[index + 3] = pixels[index + 3]; //no changes to alpha
}
context.clearRect(0, 0, 64, 64); //clear the image
context.putImageData(imageData, 0, 0); //places the modified image instead
}
</script>
The summary is:
- set the background color to gray so alpha can be observed easier
- create a canvas 64 by 64
- load a image of a smiley face on a transparent background
- draw the image onto the canvas
- get the image's data
- for every pixel, make the red stronger
- replace the altered image on the canvas
The smiley face looks like this (block-quoted so you can tell it's transparent):
However, in comparison with the chrome and android browser,
The background of the android drawing is reddish while the chrome drawing is completely transparent.
So...
- What happened here?
- How can I change the code so that the android drawing matches with the chrome drawing?
Note: I already tried if (pixels[index + 3] == 0) continue;
, and I'm aware of this, but it won't work for images with varying opacity.