Created
January 31, 2012 19:43
-
-
Save stephenparish/1712490 to your computer and use it in GitHub Desktop.
Add a noise background to your site: Currently working for better non-grayscale support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addNoise(canvas) { | |
var ctx = canvas.getContext('2d'); | |
// get canvas pixels | |
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
var pixels = imageData.data; | |
for (var i = 0, il = pixels.length; i < il; i += 4) { | |
// var color = Math.round(Math.random() * 255); | |
var M = 100; | |
var N = 120; | |
var color = Math.floor(M + (1 + N - M) * Math.random()); | |
// because the noise is monochromatic, | |
// we should put the same value in the R, G and B channels | |
pixels[i] = pixels[i + 1] = pixels[i + 2] = color; | |
// make sure pixels are opaque | |
pixels[i + 3] = 255; | |
} | |
// put pixels back into canvas | |
ctx.putImageData(imageData, 0, 0); | |
} | |
// set up canvas | |
var canvas = document.createElement('canvas'); | |
canvas.width = canvas.height = 1000; | |
$('body').append(canvas); | |
addNoise(canvas); | |
canvasUrl = canvas.toDataURL('image/png'); | |
$('body').css('background', 'url(' + canvasUrl + ')'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment