Skip to content

Instantly share code, notes, and snippets.

@tylergaw
Created September 14, 2011 17:32
Show Gist options
  • Save tylergaw/1217185 to your computer and use it in GitHub Desktop.
Save tylergaw/1217185 to your computer and use it in GitHub Desktop.
Generate noise with canvas.
// Big up to Jeffrey Way for this noise generator (http://net.tutsplus.com/tutorials/javascript-ajax/how-to-generate-noise-with-canvas/)
function generateNoise(opacity) {
if (!!!document.createElement('canvas').getContext) {
return false;
}
var canvas = document.createElement("canvas"),
ctx = canvas.getContext('2d'),
x, y,
number,
opacity = opacity || .2;
canvas.width = 250;
canvas.height = 250;
for(x = 0; x < canvas.width; x++) {
for(y = 0; y < canvas.height; y++) {
number = Math.floor(Math.random() * 40);
ctx.fillStyle = "rgba(" + number + "," + number + "," + number + "," + opacity + ")";
ctx.fillRect(x, y, 1, 1);
}
}
return canvas.toDataURL("image/png");
}
@umbrae
Copy link

umbrae commented Sep 14, 2011

@tylergaw
Copy link
Author

Ahh, good thinkin'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment