Skip to content

Instantly share code, notes, and snippets.

@joehakimrahme
Created September 18, 2012 21:28
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 joehakimrahme/3746040 to your computer and use it in GitHub Desktop.
Save joehakimrahme/3746040 to your computer and use it in GitHub Desktop.
Incremental random matrix
function draw_matrix(context, matrix, startx, starty, cellsize)
{
var x = startx;
var y = starty;
var colors = ['yellow', 'red'];
for (var i=0; i<matrix.length; ++i) {
line = matrix[i];
for (var j=0; j<line.length; ++j) {
context.beginPath();
context.rect(x, y, cellsize, cellsize);
context.fillStyle = colors[line[j]];
context.fill();
x += cellsize;
}
x = startx;
y += cellsize;
}
}
function random_matrix(width, length)
{
matrix = [];
for (var i=0; i<length; ++i) {
line = [];
for (var j=0; j<width; ++j)
line.push(Math.floor(Math.random()*2))
matrix.push(line);
}
return matrix;
}
window.onload = function () {
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
for (var n=0; n<10; ++n) {
context.clearRect(0,0,canvas.width, canvas.height);
setTimeout(draw_matrix(context, random_matrix(3, 3), 50, 50, 20), 1000);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment