Skip to content

Instantly share code, notes, and snippets.

@darkhelmet
Created July 20, 2012 05:11
Show Gist options
  • Save darkhelmet/3148811 to your computer and use it in GitHub Desktop.
Save darkhelmet/3148811 to your computer and use it in GitHub Desktop.
grayscale = (canvas) ->
context = canvas.getContext('2d')
width = canvas.width
height = canvas.height
img = context.getImageData(0, 0, width, height)
pixels = img.data
for i in [0...height]
for j in [0...width]
index = (i * 4) * width + (j * 4)
[r, g, b, a] = [index, index + 1, index + 2, index + 3]
average = (pixels[r] + pixels[g] + pixels[b]) / 3
pixels[r] = average
pixels[g] = average
pixels[b] = average
context.putImageData(img, 0, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment