drawgrid = function(ctx, grid, f) {
for (let i = 0; i+1 < grid.length; i++) {
for (let j = 0; j+1 < grid[i].length; j++) {
let p = [grid[i][j],grid[i][j+1],grid[i+1][j+1],grid[i+1][j]];
let q = p.map(f);
ctx.fillStyle = (i+j)%2 ? '#CCC' : '#888';
ctx.beginPath();
ctx.moveTo(q[0][0],q[0][1]);
ctx.lineTo(q[1][0],q[1][1]);
ctx.lineTo(q[2][0],q[2][1]);
ctx.lineTo(q[3][0],q[3][1]);
ctx.fill();
}
}
}