Skip to content

Instantly share code, notes, and snippets.

@ihatecsv
Created December 2, 2017 23:54
Show Gist options
  • Save ihatecsv/a0b15d7d2dd775fbe7ce1d98ea3e2c60 to your computer and use it in GitHub Desktop.
Save ihatecsv/a0b15d7d2dd775fbe7ce1d98ea3e2c60 to your computer and use it in GitHub Desktop.
Randomness visualizer
var dim = 256;
var offset = 5;
var randomSources = [
{
rBool: function(){
return Math.random() >= 0.5;
//return 1;
}
},
{
rBool: function(){
var array = new Uint8Array(1);
window.crypto.getRandomValues(array);
return array[0] > 127;
}
}
];
for(var i = 0; i < randomSources.length; i++){
console.log("pi");
randomSources[i].canvas = document.createElement('canvas');
randomSources[i].canvas.id = "canvas-"+i;
randomSources[i].canvas.width = (dim + (offset*2));
randomSources[i].canvas.height = (dim + (offset*2));
randomSources[i].ctx = randomSources[i].canvas.getContext('2d');
document.body.appendChild(randomSources[i].canvas);
}
var draw = function(){
for(var i = 0; i < randomSources.length; i++){
randomSources[i].ctx.clearRect(0, 0, randomSources[i].canvas.width, randomSources[i].canvas.height);
for(var y = 0; y < dim; y++){
for(var x = 0; x < dim; x++){
if(randomSources[i].rBool()){
//randomSources[i].ctx.fillStyle = "rgb(" + Math.random()*50 + "," + Math.random()*50 + "," + Math.random()*50 + ")";
randomSources[i].ctx.fillRect((x + offset), (y + offset),1,1);
}
}
}
}
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment