为什么函数在画布中返回错误的颜色? [英] Why function returns wrong color in canvas?

查看:62
本文介绍了为什么函数在画布中返回错误的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的画布颜色是50 255 50155。执行代码时:

My canvas color is 50 255 50 155. When I do a code:

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]);
}
返回颜色;
}

返回49255 49155
为什么?

It returns 49 255 49 155 Why is that?

推荐答案

针对此类情况的getImageData方法规范:


由于转换为和从预乘的alpha
颜色值中,刚刚使用putImageData()
设置的像素可能会以不同的值返回到等效的getImageData()。

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.

它可以解释为什么您会在

It can explain why you see such difference in

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/

这篇关于为什么函数在画布中返回错误的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆