-_-#【Canvas】转成黑白

-_-#【Canvas】转成黑白_第1张图片

function drawInBlackAndWhite() {
   var imagedata = context.getImageData(0, 0, canvas.width, canvas.height)
   var data = imagedata.data
   
   for (var i = 0, l = data.length - 4; i < l; i+=4) {
      var average = (data[i] + data[i+1] + data[i+2]) / 3
      data[i]   =
      data[i+1] =
      data[i+2] = average
   }

   context.putImageData(imagedata, 0, 0)
}

 

你可能感兴趣的:(canvas)