/ Published in: JavaScript

This allows you to add image noise to the body of your website. simply call generateNoise(), pass an optional opacity parameter, and then apply a background color to the body element in your stylesheet.
It's not ideal, and maybe isn't practical -- but it works. :)
Adapted from: http://mitchj.info/blog/2010/09/generate-background-noise-with-jquery/
It's not ideal, and maybe isn't practical -- but it works. :)
Adapted from: http://mitchj.info/blog/2010/09/generate-background-noise-with-jquery/
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 = 45; canvas.height = 45; 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") + ")"; } generateNoise(.1); // default opacity is .2
URL: http://nettuts.s3.amazonaws.com/854_noiseWithCanvas/noise.html
Comments
