同じキャンバスから同じコンテキストを介して取得する2つのimageDataオブジェクトがあります。しかし、それらを比較すると、同じデータが含まれているはずなので、同じだと思うと同じではありません。
var canv = document.createElement("canvas");
canv.setAttribute('width', 300);
canv.setAttribute('height', 300);
var context = canv.getContext('2d');
context.fillStyle = "#ff0000";
context.fillRect(0, 0, 300, 300);
var imageData = context.getImageData(0, 0, 300, 300);
var imageData2 = context.getImageData(0, 0, 300, 300);
if (imageData == imageData2) {
console.log("Test1: same");
} else {
console.log("Test1: different");
}
if (imageData.data == imageData2.data) {
console.log("Test2: same");
} else {
console.log("Test2: different");
}
if (imageData == imageData) {
console.log("Test3: same");
} else {
console.log("Test3: different");
}
if (imageData.data == imageData.data) {
console.log("Test4: same");
} else {
console.log("Test4: different");
}
このコードは以下を出力します:
Test1: different
Test2: different
Test3: same
Test4: same
したがって、オブジェクトをそれ自体と比較すると、期待どおりに機能し、imageDataオブジェクト内のデータを比較する場合は機能しますが、同一のコピーである必要があるものとは比較できません。
これはオブジェクトの比較に関する問題ですか、それとも、canvas要素からデータを取得する方法に欠けているものがありますか?
ありがとう