为什么在 canvas 中函数 returns 颜色错误?

Why function returns wrong color in canvas?

我的canvas颜色是50 255 50 155。当我做一个代码时:

function getClickedAreaColor(x, y) { var data = ctx.getImageData(x, y, 1, 1).data, color = []; for (var i = 0; i < data.length; i++) { color.push(data[i]); } return color; }

它returns 49 255 49 155 这是为什么?

specs for the getImageData method 中有针对此类情况的注释:

Due to the lossy nature of converting to and from premultiplied alpha color values, pixels that have just been set using putImageData() might be returned to an equivalent getImageData() as different values.

它可以解释为什么你在

中看到如此不同
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle="rgba(50, 255, 50, 0.607843137254902)"
ctx.rect(0, 0, 100, 100);
ctx.fill();

console.log(ctx.getImageData(0, 0, 1, 1).data);

http://jsfiddle.net/jtav1dm6/2/