Basic cell drawing

This commit is contained in:
Dan Ballard 2011-06-16 07:15:21 -07:00
parent 698fc9c66a
commit f84c84fc1a
1 changed files with 44 additions and 3 deletions

View File

@ -6,6 +6,7 @@
var speed;
var height;
var width;
var cells;
function btn_control_set_name(name) {
@ -13,19 +14,60 @@
btn.value = name;
}
function init_cells() {
var hcells = height/size;
var wcells = width/size;
cells = new Array(hcells);
for(var i = 0; i < hcells; i++) {
cells[i] = new Array(wcells);
}
for (var y = 0; y < hcells; y++) {
for (var x = 0; x < wcells; x++) {
cells[y][x] = false;
}
}
}
function board_init() {
var canvas = document.getElementById('board');
width = canvas.width;
height = canvas.height;
btn_control_set_name('Start');
size = 10;
speed = 50;
init_cells();
redraw();
}
function redraw() {
var canvas = document.getElementById('board');
width = canvas.width;
height = canvas.height;
var c = canvas.getContext('2d');
draw_grid(c);
c.stroke();
cells[1][2] = true;
cells[3][4] = true;
cells[5][5] = true;
draw_cells(c);
c.stroke();
c.fill();
}
function draw_cells(c) {
var hcells = height/size;
var wcells = width/size;
for (var y = 0; y < hcells; y++) {
for (var x =0; x < wcells; x ++) {
if (cells[y][x] == true) {
c.moveTo(x*size, y*size);
c.lineTo(x*size+ size, y*size);
c.lineTo(x*size+ size, y*size + size);
c.lineTo(x*size, y*size + size);
// auto return home?
}
}
}
}
function draw_grid(c) {
@ -39,7 +81,6 @@
c.moveTo(x, 0);
c.lineTo(x, height);
}
c.stroke();
}