这个问题在这里已有答案:
这更像是一个逻辑问题。我无法弄清楚我的功能有什么问题
// This function is meant to check if 3 pixels
// are colored and therefore if the canvas is full
function checkIfFull() {
let emptyPixel = [0, 0, 0, 0];
let pixel1 = get(1, 1);
//let pixel2 = get(599,599);
//let pixel3 = get(400, 50);
console.log(pixel1);
if (pixel1 === emptyPixel) {
console.log(true);
} else {
console.log(false);
}
}
我正在运行P5.js库。 get()给我一个数组[a,b,c,d],我试图测试这两者之间的相等或差异(emptyPixel / pixel1)
这些是我尝试过的:测试数组中的每个位置。
谢谢!
如果一个空像素是一个数组[0,0,0,0],你可以用这样的东西测试它
function checkIfEmpty(pixel) {
return pixel.every(x => x === 0)
}
您的数组可以简单地转换为字符串以检查相等性
const emptyPixel = "0,0,0,0";
function checkIfFull() {
const pixel1 = get(1, 1);
return pixel1.toString() === emptyPixel
}
console.log(checkIfFull)