Javascript !== 给出的结果与 === 的倒数不同
我已将色轮图像加载到画布上,并且在数组中有一个色调值列表。我循环遍历画布上的每个像素,并删除与相同色调值匹配的像素。
其代码是:
var element = document.getElementById("wheel-canvas");
var c = element.getContext("2d");
var image = c.getImageData(0, 0, 375, 375);
var imageData = image.data;
paletteList = this.collection.pluck('hsv');
for (var i = 0, n = imageData.length; i < n; i += 4) {
var hsv = this.model.convertRGBToHSV(imageData[i], imageData[i+1], imageData[i+2]);
var hue = hsv[0];
var sat = hsv[1];
var val = hsv[2];
$.each(paletteList, function(index, value) {
if (hue === value[0])
{
imageData[i] = '0';
imageData[i+1] = '0';
imageData[i+2] = '0';
}
});
}
c.putImageData(image, 0, 0);
现在我希望所有与色调不匹配的像素都变为黑色。我更改了代码:
if (hue !== value[0])
并得到以下结果:
为什么它看起来不像相反第一个圈子?
谢谢你!
I have loaded an image of a color wheel on to a canvas and I have a list of hue values in an array. I loop over each pixel on the canvas and remove the pixels that match the same hue values.
The code for that is:
var element = document.getElementById("wheel-canvas");
var c = element.getContext("2d");
var image = c.getImageData(0, 0, 375, 375);
var imageData = image.data;
paletteList = this.collection.pluck('hsv');
for (var i = 0, n = imageData.length; i < n; i += 4) {
var hsv = this.model.convertRGBToHSV(imageData[i], imageData[i+1], imageData[i+2]);
var hue = hsv[0];
var sat = hsv[1];
var val = hsv[2];
$.each(paletteList, function(index, value) {
if (hue === value[0])
{
imageData[i] = '0';
imageData[i+1] = '0';
imageData[i+2] = '0';
}
});
}
c.putImageData(image, 0, 0);
Now I want all pixels that DON'T match the hues to become black. I make a code change:
if (hue !== value[0])
and I get the following result:
Why doesn't it look like the inverse of the first circle?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您确定将哪些像素设置为黑色的逻辑是错误的。
请注意,在
each
循环中,每个像素的色调将不等于调色板列表中的某些色调,因此所有像素都将设置为黑色。您真正想要对每个像素执行的操作是检测其色调是否在调色板列表中。
Your logic for determining which pixels to set to black is wrong.
Note that in your
each
loop, the hue of each pixel will not be equal to some hues in the pallette list, thus all of them will be set to black.what you really want to do on each pixel is to detect whether its hue is in the pallet list or not.
您需要使用
!=
表示“不等于”,因为您只想比较值。检查一下: Javascript 比较
!==
还将比较身份/对象,它们是不一样的(正如基斯提到的)。You need to use
!=
for 'is not equal', since you only want to compare the values.Check this: Javascript comparison
!==
will also compare the identity / objects, which are not the same (as Kees mentioned).平等和身份之间是有区别的。
来源。 所以
!==
不能成为问题。There is a difference between equality and identity.
Source. So the
!==
can't be the problem.找不到您所指的运营商。我建议你使用 !( a=== b)
看看 w3school javascript 运算符列表
Couldn't find the operator you are referring to. I suggest you use !( a=== b)
have a look at w3school list of javascript operators
您使用 !== 比较字符串与整数,这将始终返回 false。
检查 http://jsfiddle.net/k4EAQ/
使用 != Javascript 不会检查类型,并且会给你你想要的。但如果您喜欢使用 !== 您始终可以将变量转换为整数或字符串,以便确保它们与类型匹配。
如何将字符串转换为整数JavaScript?
You are comparing a string with an integer using !== this will always return false.
Check on http://jsfiddle.net/k4EAQ/
Using != Javascript does not check the type and will give you what you want. But if you like to use !== you can always convert the variable to an integer or a string so you are sure they match the type.
How do I convert a string into an integer in JavaScript?