Why does HTML Canvas getImageData() not return the exact same values that were just set?

后端 未结 3 685
暖寄归人
暖寄归人 2020-11-30 05:40

When writing pixels to an HTML Canvas context using putImageData I find that the pixel values are not exactly the same when I fetch them again. I have put up a

3条回答
  •  Happy的楠姐
    2020-11-30 06:39

    Looks like a rounding issue to me...

    64/255 = 0.2509... (rounded down to give 0.25)
    0.25 * 255 = 63.75 (rounded down to give 63)
    == OR ==
    64/255 = 0.2509... (rounded up to give 0.26)
    0.26 * 255 = 66.3  (rounded up to give 67)
    

    Remember that 255 is the maximum value, not 256 ;)

    EDIT: Of course, this wouldn't explain why the alpha channel is behaving...

提交回复
热议问题