Skip to content

Instantly share code, notes, and snippets.

@nickjs
Created June 7, 2012 07:06
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 nickjs/2887072 to your computer and use it in GitHub Desktop.
Save nickjs/2887072 to your computer and use it in GitHub Desktop.
Infinite Picture Generator
(function() {
var canvas = document.getElementById('canvas');
var MAX_ROWS = canvas.height;
var MAX_COLS = canvas.width;
var code = "var context = document.getElementById('canvas').getContext('2d');var color;\n";
var magicNumber = 255/5;
var pixelCount = 0;
for (var row = 0; row < MAX_ROWS; row++) {
for (var col = 0; col < MAX_COLS; col++) {
code += "for(var p" + pixelCount + " = 0; p" + pixelCount + " < 5; p" + pixelCount + "++){\n";
code += "color = p" + pixelCount + " * parseInt(" + magicNumber + ");\n";
code += "context.fillStyle = 'rgb('+color+','+color+','+color+')';\n";
code += "context.fillRect("+col+","+row+",1,1);\n"
pixelCount++;
}
}
code += "debugger;"
for (var row = 0; row < MAX_ROWS; row++) {
for (var col = 0; col < MAX_COLS; col++) {
code += "}\n"
}
}
console.log(code);
new Function(code)();
})();
@nickjs
Copy link
Author

nickjs commented Jun 7, 2012

So, it turns out, by walking through a grid of pixels and giving every one every possible colour, you can create every possible image that could be photographed.

This is not that.

So, it turns out, even though we have a limited set of colours and thus aren't really using a truly infinite canvas, the amount of possible images is basically infinite for all intents and purposes and therefore takes a long time to generate. Like, a really long time. For example, flushing the image out at 60fps, to generate every possible image in a 10x10 grid using only 256 shades of grayscale, would take...

Wait for it...

2.11441351e233 YEARS.

That is not a small number of years. So here, we are using 6 shades of grayscale and a debugger statement for flow control. Zoom in, and hit continue continue continue to watch your image materialize.

Slowly.

Shut up, this is exactly how debugger is supposed to be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment