html5 getImageData then putImageData results in fading image

≡放荡痞女 提交于 2019-12-06 14:57:40
Mark Ivey

putImageData() and getImageData() can be lossy. There's a note in the spec about this:

Due to the lossy nature of converting to and from premultiplied alpha color values, pixels that have just been set using putImageData() might be returned to an equivalent getImageData() as different values.

See also this related question: Why does HTML Canvas getImageData() not return the exact same values that were just set?

I have tried also to apply this to my game where in im going to manipulate the selected pixels to have effect but It doesn't give me the expected result

here is some sample code that i used to manipulate the pixel to change

get image information and store

var img = context.getImageData(0,0, width, height)
var imgdata = img.data
var len = imgdata.length

loop to all data and manipulate pixel information

var i = 0;

for(i; i<leng; i++) {
    var red = imgdata[i]
    var green = imgadata[i+1]
    var blue = imgdata[i+2]
    var alpha = imgdata[i+3]

    imgdata[i] = new value
    imgdata[i+1] = new value
    imgdata[i+2] = new value
    imgdata[i+3] = new value
}

context.putImageData(img, 0,0)

then create animation frame to see effect

requestAnimationFrame is an experimental feature (june 2012) that uses time based frame access. The reason for this is avoid latency in animations. I suggest you take a look at this Moz article.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!