Skip to content

Instantly share code, notes, and snippets.

@paddykontschak
Created September 12, 2012 19:25
Show Gist options
  • Save paddykontschak/3709275 to your computer and use it in GitHub Desktop.
Save paddykontschak/3709275 to your computer and use it in GitHub Desktop.
Generate noise with canvas
generateNoise = (opacity) ->
return false unless !!document.createElement("canvas").getContext
canvas = document.createElement("canvas")
ctx = canvas.getContext("2d")
x = undefined
y = undefined
number = undefined
opacity = opacity or .2
canvas.width = 45
canvas.height = 45
x = 0
while x < canvas.width
y = 0
while y < canvas.height
number = Math.floor(Math.random() * 60)
ctx.fillStyle = "rgba(" + number + "," + number + "," + number + "," + opacity + ")"
ctx.fillRect x, y, 1, 1
y++
x++
document.body.style.backgroundImage = "url(" + canvas.toDataURL("image/png") + ")"
generateNoise .1 # default opacity is .2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment