Skip to content

Instantly share code, notes, and snippets.

@kgust
Created March 13, 2014 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgust/9528688 to your computer and use it in GitHub Desktop.
Save kgust/9528688 to your computer and use it in GitHub Desktop.
Generate Noise on a Canvas
function generateNoise(opacity) {
if ( !!!document.createElement('canvas').getContext ) {
return false;
}
var canvas = document.createElement("canvas"),
ctx = canvas.getContext('2d'),
x, y,
number,
opacityopacity = opacity || .2;
canvas.width = 100;
canvas.height = 100;
for ( x = 0; x < canvas.width; x++ ) {
for ( y = 0; y < canvas.height; y++ ) {
number = Math.floor( (Math.random() * 60));
ctx.fillStyle = "rgba(" + number + "," + number + "," + number + "," + opacity + ")";
ctx.fillRect(x, y, 1, 1);
}
}
document.body.style.backgroundImage = "url(" + canvas.toDataURL("image/png") + ")";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment