Created
September 18, 2012 21:28
-
-
Save joehakimrahme/3746040 to your computer and use it in GitHub Desktop.
Incremental random matrix
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 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