function mosaicEffect(){ //refer pixels var referImageData = contexta.getImageData(0,0,canvasa.width,canvasa.height); var referPixelData = referImageData.data; var imageData = contexta.getImageData(0,0,canvasa.width,canvasa.height); var pixelData = imageData.data; var size = 10;//参考周围像素正方形的边长 var totalNum = size*size; for(var i=0;i<canvasb.height;i+=size){ for(var j=0;j<canvasb.width;j+=size){ //find pixel around nine pixels //after all around pixels value == around pixels average value //第i行第j列的那个像素周围像素的所有RGB值 var totalR=0,totalG=0,totalB=0; for(var dx = 0;dx < size;dx++){ for(var dy = 0;dy < size;dy++){ //第i行第j列的那个像素周围像素的坐标值 var x = i+dx; var y = j+dy; var p = x*canvasb.width+y; totalR += referPixelData[4*p+0]; totalG += referPixelData[4*p+1]; totalB += referPixelData[4*p+2]; } } for(var dx = 0;dx < size;dx++){ for(var dy = 0;dy < size;dy++){ var p = x*canvasb.width+y; pixelData[4*i+0] = totalR / totalNum; pixelData[4*i+1] = totalG / totalNum; pixelData[4*i+2] = totalB / totalNum; } } } } contextb.putImageData(imageData,0,0,0,0,canvasb.width,canvasb.height); }
相关分类