diff --git a/index.js b/index.js index 928c78c..5ab68d5 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ import { Universe, Cell } from "wasm-game-of-life"; // Import wasm memory import { memory } from "wasm-game-of-life/wasm_game_of_life_bg"; -const CELL_SIZE = 5; +const CELL_SIZE = 5; // px const GRID_COLOR = "#CCCCCC"; const DEAD_COLOR = "#FFFFFF"; const ALIVE_COLOR = "#000000"; @@ -17,11 +17,21 @@ canvas.width = (CELL_SIZE + 1) * width + 1; const ctx = canvas.getContext('2d'); -const renderLoop = () => { +const PERIODE = 100; // ms +let start; + +const renderLoop = (timestamp) => { drawGrid(); drawCells(); - universe.tick(); + if (start === undefined) + start = timestamp; + const elapsed = timestamp - start; + + if (elapsed > PERIODE) { + universe.tick(); + start = timestamp; + } requestAnimationFrame(renderLoop); };