add frequence
This commit is contained in:
parent
e36fb20761
commit
b6e45e24cb
1 changed files with 13 additions and 3 deletions
16
index.js
16
index.js
|
@ -2,7 +2,7 @@ import { Universe, Cell } from "wasm-game-of-life";
|
||||||
// Import wasm memory
|
// Import wasm memory
|
||||||
import { memory } from "wasm-game-of-life/wasm_game_of_life_bg";
|
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 GRID_COLOR = "#CCCCCC";
|
||||||
const DEAD_COLOR = "#FFFFFF";
|
const DEAD_COLOR = "#FFFFFF";
|
||||||
const ALIVE_COLOR = "#000000";
|
const ALIVE_COLOR = "#000000";
|
||||||
|
@ -17,11 +17,21 @@ canvas.width = (CELL_SIZE + 1) * width + 1;
|
||||||
|
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
const renderLoop = () => {
|
const PERIODE = 100; // ms
|
||||||
|
let start;
|
||||||
|
|
||||||
|
const renderLoop = (timestamp) => {
|
||||||
drawGrid();
|
drawGrid();
|
||||||
drawCells();
|
drawCells();
|
||||||
|
|
||||||
universe.tick();
|
if (start === undefined)
|
||||||
|
start = timestamp;
|
||||||
|
const elapsed = timestamp - start;
|
||||||
|
|
||||||
|
if (elapsed > PERIODE) {
|
||||||
|
universe.tick();
|
||||||
|
start = timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
requestAnimationFrame(renderLoop);
|
requestAnimationFrame(renderLoop);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue