Preface
Kind of weird when using canvas
, but it’s true. Let’s take a look.
Main
canvas would lost accurate data when opacity isn’t equal to 255
sample code:
<canvas id="canvas"></canvas> |
let canvas = document.getElementById('canvas') |
imageData.data
wouldn’t be equal to debugImgData.data
because opacity isn’t equal to 255. If we set opacity to 255. They would be the same.
data[3] = 255 |
imageData.data is readonly
You can modify data
but you can’t reassign it like imageData.data=[1,22,3,5]
. It will fail silently.
copy imageData
Because imageData.data
is readonly. So, you can’t copy it like code below:
let newImgData = cxt.createImageData(debugImgData) |
According to mdn, return value of createImageData is
A new ImageData object with the specified width and height. The new object is filled with transparent black pixels.
which is [0,0,0,0,,,,]
. And assignment of newImgData.data = debugImgData.data
fail silently. So, if you want to copy imageData
the right way is:
let newImgData = cxt.createImageData(debugImgData) |
That is because ImageData.data
is a Uint8ClampedArray
. So we can find the API ofUint8ClampedArray.prototype.set()
which stores multiple values in the typed array, reading input values from a specified array.