Skip to content

Instantly share code, notes, and snippets.

@usefulthink
Created April 24, 2013 14:42
Show Gist options
  • Save usefulthink/5452653 to your computer and use it in GitHub Desktop.
Save usefulthink/5452653 to your computer and use it in GitHub Desktop.
var fs=require('fs'),
Canvas = require('Canvas');
/**
* algorithm impudently stolen from http://www.noisetexturegenerator.com/
*/
var generateNoise = function(width, height, opacity, density) {
var canvas = new Canvas(width, height),
ctx = canvas.getContext('2d');
var x, y,
r, g, b,
opacity = opacity,
density = Math.floor(density) || 1;
for ( x = 0; x < canvas.width; x += density ) {
for ( y = 0; y < canvas.height; y += density ) {
number = Math.floor( Math.random() * 256 );
ctx.fillStyle = 'rgba(' + number + ',' + number + ',' + number + ',' + opacity + ')';
ctx.fillRect(x, y, 1, 1);
}
}
return canvas.pngStream();
}
generateNoise(100, 100, 0.7, 10).pipe(fs.createWriteStream('test.png'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment