canvas - javascript ImageData typed array read whole pixel? -
so there lot of examples on how write entire pixel uint32array view of imagedata object. possible read entire pixel without incrementing counter 4 times? hacks.mozilla.org, writing rgba pixels looks this.
var imagedata = ctx.getimagedata(0, 0, canvaswidth, canvasheight); var buf = new arraybuffer(imagedata.data.length); var buf8 = new uint8clampedarray(buf); var data = new uint32array(buf); (var y = 0; y < canvasheight; ++y) { (var x = 0; x < canvaswidth; ++x) { var value = x * y & 0xff; data[y * canvaswidth + x] = (255 << 24) | // alpha (value << 16) | // blue (value << 8) | // green value; // red } } imagedata.data.set(buf8); ctx.putimagedata(imagedata, 0, 0); but, how can read entire pixel single offset in 32-bit view of imagedata? here's i'm finding confusing, shouldn't buf32 below have length of 256/4 = 64?
// 8x8 image var imgd = ctx.getimagedata(0, 0, canvaswidth, canvasheight), buf32 = new uint32array(imgd.data); console.log(imgd.data.length); // 256 console.log(buf32.length); // 256 shouldn't 256/4 ? thanks!
figured out, need pass buffer uint32array constructor, not bufferview.
var buf32 = new uint32array(imgd.data.buffer); console.log(buf32.length) // 64 yay!
Comments
Post a Comment