Is it possible to disable premultiplied alpha for the 2d canvas context?

4 views
Skip to first unread message

mechanic...@gmail.com

unread,
Dec 24, 2014, 12:32:12 PM12/24/14
to node-...@googlegroups.com
I'm working on a small project that uses canvas to replace exact colors on video game sprites and I've run into a problem that I'm not sure I can fix. Essentially whenever I load an image onto the canvas by using putImageData, any pixels with alpha may end up slightly different when read out with getImageData. A quick example of this is:

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');

var px = ctx.getImageData(0, 0, 1, 1);
px.data[0] = 253;
px.data[1] = 168;
px.data[2] = 55;
px.data[3] = 165;
ctx.putImageData(px, 0, 0);

var px2 = ctx.getImageData(0, 0, 1, 1);
console.log(px2.data); // Gives: [253, 168, 56, 165] Expected: [253, 168, 55, 165]

In my research into the problem I believe I've narrowed it down to the fact that Chromium seems to use premultiplied alpha for this context, which is a problem for me since I need to work with the exact colors of the image, and as soon as alpha is introduced it makes it uncertain whether or not the color I am using is really the exact color from the source image. This appears in every version of Node-Webkit as a result. I've looked around and this issue seems to have very little attention. I'm not an expert with Chromium but I was wondering if anyone knew of a way to disable premultiplied alpha when using Canvas in a 2d context?

Roger Wang

unread,
Dec 24, 2014, 11:38:12 PM12/24/14
to node-...@googlegroups.com, mechanic...@gmail.com
Hello,

What's the result with Chrome browser?

Roger

mechanic...@gmail.com

unread,
Dec 25, 2014, 12:34:17 AM12/25/14
to node-...@googlegroups.com, mechanic...@gmail.com
Hi, it's the exact same issue in the Chrome browser.
Reply all
Reply to author
Forward
0 new messages