Life-Canvas/life.html

194 lines
4.2 KiB
HTML

<html>
<head>
<title>The Game of Life: HTML Canvas Demo</title>
<script language="javascript">
var size;
var hertz;
var height_px;
var width_px;
var height_c;
var width_c;
var cells;
var timer;
function btn_control_set_name(name) {
var btn = document.getElementById('control-btn');
btn.value = name;
}
function gen_cells() {
var new_cells = new Array(height_c);
for(var i = 0; i < height_c; i++) {
new_cells[i] = new Array(width_c);
}
for (var y = 0; y < height_c; y++) {
for (var x = 0; x < width_c; x++) {
new_cells[y][x] = false;
}
}
return new_cells;
}
function init_cells() {
cells = gen_cells();
}
function board_init() {
var canvas = document.getElementById('board');
size = 10;
width_px = canvas.width;
width_c = width_px/ size;
height_px = canvas.height;
height_c = height_px/size;
btn_control_set_name('Start');
hertz = 4;
init_cells();
redraw();
}
function redraw() {
console.log('redraw()');
var canvas = document.getElementById('board');
var c = canvas.getContext('2d');
c.globalCompositeOperation = 'destination-over';
c.clearRect(0, 0, width_px, height_px);
c.fillStyle='rgba(0,0,0,0.)';
c.strokeStyle= 'rgba(192,192,192,1)';
c.save();
c.beginPath();
draw_grid(c);
c.stroke();
c.strokeStyle= 'rgba(0,0,0,1)';
c.fillStyle = c.strokeStyle;
c.beginPath();
draw_cells(c);
c.stroke();
c.fill();
}
function draw_cells(c) {
for (var y = 0; y < height_c; y++) {
for (var x =0; x < width_c; x ++) {
if (cells[y][x] == true) {
c.moveTo(x*size +1, y*size+1);
c.lineTo(x*size+ size-1, y*size+1);
c.lineTo(x*size+ size-1, y*size + size-1);
c.lineTo(x*size+1, y*size + size-1);
// auto return home?
}
}
}
}
function draw_grid(c) {
// Horizontal lines
for (var y = 0; y <= height_px; y += size) {
c.moveTo(0, y);
c.lineTo(width_px, y);
}
// Vertical lines
for (var x = 0; x <= width_px; x += size) {
c.moveTo(x, 0);
c.lineTo(x, height_px);
}
}
window.onload = board_init;
function cell_value(x, y) {
//console.log('(x:' + x + ',y:' + y + ') w/s:' + (width/size) + ' h/s:' + (height/size) );
if (x < 0 || x >= width_c || y < 0 || y >= height_c) {
return 0;
} else {
return cells[y][x];
}
}
function num_neighbours(x, y) {
var n = 0;
n += cell_value(x-1, y-1);
n += cell_value(x, y-1);
n += cell_value(x+1, y-1);
n += cell_value(x-1, y);
n += cell_value(x+1, y);
n += cell_value(x-1, y+1);
n += cell_value(x, y+1);
n += cell_value(x+1, y+1);
return n;
}
function step() {
var new_cells = gen_cells();
for(var y=0; y < height_c; y++) {
for(var x =0 ; x < width_c; x++) {
var n = num_neighbours(x, y);
if (cells[y][x] == true) {
if (n < 2 || n > 3) {
new_cells[y][x] = false;
} else {
console.log('ALIVE');
new_cells[y][x] = true;
}
} else if (n == 3) {
new_cells[y][x] = true;
} else {
new_cells[y][x] = false;
}
}
}
//new_cells[Math.floor(Math.random()*8)][ Math.floor(Math.random()*8) ] = true;
cells = new_cells;
}
function control_click() {
var btn = document.getElementById('control-btn');
if (btn.value == 'Start') {
tick();
btn.value = 'Stop';
} else {
clearTimeout(timmer);
btn.value = 'Start';
}
}
function tick() {
step();
redraw();
timmer = setTimeout('tick();', 1000/hertz);
}
function board_click(e) {
var canvas = document.getElementById('board');
var x = Math.floor((e.pageX-canvas.offsetLeft)/size);
var y = Math.floor((e.pageY-canvas.offsetTop)/size);
console.log(e.pageX + '/' + size + ' = ' + x + ' && ' + e.pageY + '/' + size + ' = ' +y);
cells[y][x] = ! cells[y][x];
redraw();
}
</script>
</head>
<body>
<input type="button" id="control-btn" onClick="control_click(); return false;" />
<br/>
<canvas width="800" height="400" id="board" onClick="board_click(event);"></canvas>
</body>
</html>