|
|
@ -1,8 +1,8 @@ |
|
|
import { Universe, Cell, View3d, Color } from "wasm-game-of-life"; |
|
|
|
|
|
|
|
|
import { View3d } from "wasm-game-of-life"; |
|
|
import { memory } from "wasm-game-of-life/wasm_game_of_life_bg"; |
|
|
import { memory } from "wasm-game-of-life/wasm_game_of_life_bg"; |
|
|
|
|
|
|
|
|
// 3D canvas stuff
|
|
|
// 3D canvas stuff
|
|
|
const view3d = View3d.new(151, 151); |
|
|
|
|
|
|
|
|
const view3d = View3d.new(300, 300); |
|
|
|
|
|
|
|
|
const view3d_canvas = document.getElementById("view3d"); |
|
|
const view3d_canvas = document.getElementById("view3d"); |
|
|
view3d_canvas.width = view3d.width(); |
|
|
view3d_canvas.width = view3d.width(); |
|
|
@ -28,85 +28,7 @@ const drawView3d = () => { |
|
|
view3d_ctx.putImageData(view3d_image, 0, 0); |
|
|
view3d_ctx.putImageData(view3d_image, 0, 0); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// game of life stuff
|
|
|
|
|
|
const CELL_SIZE = 5; // px
|
|
|
|
|
|
const GRID_COLOR = "#CCCCCC"; |
|
|
|
|
|
const DEAD_COLOR = "#FFFFFF"; |
|
|
|
|
|
const ALIVE_COLOR = "#000000"; |
|
|
|
|
|
|
|
|
|
|
|
const universe = Universe.new(); |
|
|
|
|
|
const width = universe.width(); |
|
|
|
|
|
const height = universe.height(); |
|
|
|
|
|
|
|
|
|
|
|
const canvas = document.getElementById("game-of-life-canvas"); |
|
|
|
|
|
canvas.height = (CELL_SIZE + 1) * height + 1; |
|
|
|
|
|
canvas.width = (CELL_SIZE + 1) * width + 1; |
|
|
|
|
|
|
|
|
|
|
|
const ctx = canvas.getContext('2d'); |
|
|
|
|
|
|
|
|
|
|
|
const renderLoop = () => { |
|
|
|
|
|
universe.tick(); |
|
|
|
|
|
|
|
|
|
|
|
drawGrid(); |
|
|
|
|
|
drawCells(); |
|
|
|
|
|
|
|
|
|
|
|
requestAnimationFrame(renderLoop); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const drawGrid = () => { |
|
|
|
|
|
ctx.beginPath(); |
|
|
|
|
|
ctx.strokeStyle = GRID_COLOR; |
|
|
|
|
|
|
|
|
|
|
|
// vertical lines.
|
|
|
|
|
|
for (let i = 0; i <= width; i++) { |
|
|
|
|
|
ctx.moveTo(i * (CELL_SIZE + 1) + 1, 0); |
|
|
|
|
|
ctx.lineTo(i * (CELL_SIZE + 1) + 1, (CELL_SIZE + 1) * height + 1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// horizontal lines.
|
|
|
|
|
|
for (let j = 0; j <= height; j++) { |
|
|
|
|
|
ctx.moveTo( 0, j * (CELL_SIZE + 1) + 1); |
|
|
|
|
|
ctx.lineTo((CELL_SIZE + 1) * width + 1, j * (CELL_SIZE + 1) + 1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ctx.stroke(); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const getIndex = (row, col) => { |
|
|
|
|
|
return row * width + col; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const drawCells = () => { |
|
|
|
|
|
const cellsPtr = universe.cells() |
|
|
|
|
|
const cells = new Uint8Array(memory.buffer, cellsPtr, width * height); |
|
|
|
|
|
|
|
|
|
|
|
ctx.beginPath(); |
|
|
|
|
|
|
|
|
|
|
|
for (let row = 0; row < height; row++) { |
|
|
|
|
|
for (let col = 0; col < height; col++) { |
|
|
|
|
|
const idx = getIndex(row, col); |
|
|
|
|
|
|
|
|
|
|
|
ctx.fillStyle = cells[idx] === Cell.Dead |
|
|
|
|
|
? DEAD_COLOR |
|
|
|
|
|
: ALIVE_COLOR; |
|
|
|
|
|
|
|
|
|
|
|
ctx.fillRect( |
|
|
|
|
|
col * (CELL_SIZE + 1) + 1, |
|
|
|
|
|
row * (CELL_SIZE + 1) + 1, |
|
|
|
|
|
CELL_SIZE, |
|
|
|
|
|
CELL_SIZE |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ctx.stroke(); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// start everything ...
|
|
|
// start everything ...
|
|
|
//drawGrid();
|
|
|
|
|
|
//drawCells();
|
|
|
|
|
|
//requestAnimationFrame(renderLoop);
|
|
|
|
|
|
|
|
|
|
|
|
view3d.update(); |
|
|
view3d.update(); |
|
|
drawView3d(); |
|
|
drawView3d(); |
|
|
requestAnimationFrame(view3d_renderLoop); |
|
|
requestAnimationFrame(view3d_renderLoop); |