Javascript !== 给出的结果与 === 的倒数不同

发布于 2024-12-25 03:16:39 字数 1030 浏览 1 评论 0原文

我已将色轮图像加载到画布上,并且在数组中有一个色调值列表。我循环遍历画布上的每个像素,并删除与相同色调值匹配的像素。

其代码是:

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);

enter image description here

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:

enter image description here

Why doesn't it look like the inverse of the first circle?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

穿透光 2025-01-01 03:16:39

您确定将哪些像素设置为黑色的逻辑是错误的。

请注意,在 each 循环中,每个像素的色调将不等于调色板列表中的某些色调,因此所有像素都将设置为黑色。

您真正想要对每个像素执行的操作是检测其色调是否在调色板列表中。

var matched = false;
$.each(paletteList, function(index, value) {
    if (hue === value[0]) {
        matched = true;    
        return false;
    }
}
if (!matched) {
    imageData[i] = '0';
    imageData[i+1] = '0';
    imageData[i+2] = '0';
}

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.

var matched = false;
$.each(paletteList, function(index, value) {
    if (hue === value[0]) {
        matched = true;    
        return false;
    }
}
if (!matched) {
    imageData[i] = '0';
    imageData[i+1] = '0';
    imageData[i+2] = '0';
}
赠我空喜 2025-01-01 03:16:39

您需要使用 != 表示“不等于”,因为您只想比较值。
检查一下: 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).

冷心人i 2025-01-01 03:16:39

平等身份之间是有区别的。

相等(==,!=)

  • 如果两个表达式的类型不同,则尝试转换
    它们为字符串、数字或布尔值。
  • NaN 不等于任何值(包括其自身)。
  • 负零等于正零。
  • null 等于 null 和 undefined。
  • 如果值在数字上是相同的字符串,则认为它们相等
    相等的数字、相同的对象、相同的布尔值,或者(如果
    不同类型)他们可能会被迫进入其中一种情况。
  • 所有其他比较都被视为不平等。

身份(===.!==)

这些运算符的行为与相等运算符相同,除了 no
类型转换完成,类型必须相同才可以
被视为平等。

来源。 所以 !== 不能成为问题。

There is a difference between equality and identity.

Equality (==, !=)

  • If the types of the two expressions are different, attempt to convert
    them to string, number, or Boolean.
  • NaN is not equal to anything including itself.
  • Negative zero equals positive zero.
  • null equals both null and undefined.
  • Values are considered equal if they are identical strings, numerically
    equivalent numbers, the same object, identical Boolean values, or (if
    different types) they can be coerced into one of these situations.
  • Every other comparison is considered unequal.

Identity (===. !==)

These operators behave identically to the equality operators except no
type conversion is done, and the types must be the same to be
considered equal.

Source. So the !== can't be the problem.

迷雾森÷林ヴ 2025-01-01 03:16:39

找不到您所指的运营商。我建议你使用 !( 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

霊感 2025-01-01 03:16:39

您使用 !== 比较字符串与整数,这将始终返回 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文