从HTML画布中获取Pixel是否可以查询HTML画布对象以获得特定位置的颜色?
3 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
// Get the CanvasPixelArray from the given coordinates and dimensions.var imgd = context.getImageData(x, y, width, height); var pix = imgd.data;// Loop over each pixel and invert the color.for (var i = 0, n = pix.length; i < n; i += 4) { pix[i ] = 255 - pix[i ]; // red pix[i+1] = 255 - pix[i+1]; // green pix[i+2] = 255 - pix[i+2]; // blue // i+3 is alpha (the fourth element)}// Draw the ImageData at the given (x,y) coordinates.context.putImageData(imgd, x, y);
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
var data = context.getImageData(x, y, 1, 1).data;var rgb = [ data[0], data[1], data[2] ];
繁华开满天机
TA贡献1816条经验 获得超4个赞
var imgData = context.getImageData(0,0,canvas.width,canvas.height);// { data: [r,g,b,a,r,g,b,a,r,g,..], ... }function getPixel(imgData, index) { var i = index*4, d = imgData.data; return [d[i],d[i+1],d[i+2],d[i+3]] // returns array [R,G,B,A]}// AND/ORfunction getPixelXY(imgData, x, y) { return getPixel(imgData, y*imgData.width+x);}
添加回答
举报
0/150
提交
取消