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 sample test page showing the problem. Boiled down, the problem is:
var id = someContext.getImageData(0,0,1,1);
id.data[0]=id.data[3]=64; // 25% red, 25% alpha
id.data[1]=id.data[2]=0; // No blue or green
someContext.putImageData(id,0,0);
var newData = someContext.getImageData(0,0,1,1);
console.log( newData.data[0] );
On Chrome v8, the red value comes back as 63
; on Firefox v3.6, Safari v5, and IE9 the red value comes back as 67
(all on Windows). On OS X, Chrome v7, Safari v5, and Firefox v3.6 also come back as 67
. None of them come back as the 64
value originally set!
Using setTimeout
to delay between setting and re-fetching makes no difference. Changing the background of the page makes no difference. Using save()
and restore()
on the context (per this unlikely article) makes no difference.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…