2021-06-22 21:05:22 +01:00
|
|
|
import { Universe, Cell, init_game } from "gameoflife";
|
2021-06-21 23:39:44 +01:00
|
|
|
import { memory } from "gameoflife/gameoflife_bg.wasm";
|
2020-10-11 21:46:15 +01:00
|
|
|
|
2020-10-12 00:10:54 +01:00
|
|
|
// let PLAY = true;
|
2020-10-11 21:46:15 +01:00
|
|
|
// let PLAY = false;
|
2021-06-22 21:05:22 +01:00
|
|
|
init_game();
|
2020-10-12 00:10:54 +01:00
|
|
|
const randSlider = document.getElementById("randThreshold");
|
|
|
|
const randSliderLabel = document.getElementById("randThreshold-label");
|
2020-10-11 21:46:15 +01:00
|
|
|
|
2021-06-22 19:25:32 +01:00
|
|
|
const CELL_SIZE = 4; // px
|
2020-10-11 21:46:15 +01:00
|
|
|
const GRID_COLOR = "#BBBBBB";
|
|
|
|
const DEAD_COLOR = "#FFFFFF";
|
|
|
|
const ALIVE_COLOR = "#FF55AA";
|
|
|
|
|
2020-10-12 00:10:54 +01:00
|
|
|
let universe = Universe.new(100, 100, randSlider.value, new Date().getTime() / 1000);
|
|
|
|
let width = universe.width();
|
|
|
|
let height = universe.height();
|
2021-06-22 16:45:07 +01:00
|
|
|
let play = false;
|
2020-10-11 21:46:15 +01:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Draw grid onto canvas prior to painting cells
|
|
|
|
*/
|
2020-10-11 21:46:15 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Get linear index from row and column indices
|
|
|
|
* @param {*} row Row index
|
|
|
|
* @param {*} column Column index
|
|
|
|
* @returns Linear index
|
|
|
|
*/
|
2020-10-11 21:46:15 +01:00
|
|
|
const getIndex = (row, column) => {
|
|
|
|
return row * width + column;
|
|
|
|
};
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Paint alive cells onto grid
|
|
|
|
*/
|
2020-10-11 21:46:15 +01:00
|
|
|
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 < width; 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();
|
|
|
|
};
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Single frame/step of game, tick universe, refresh UI
|
|
|
|
*/
|
2020-10-11 21:46:15 +01:00
|
|
|
const renderSingle = () => {
|
2021-06-22 15:28:31 +01:00
|
|
|
// fps.render(); //new
|
2020-10-11 21:46:15 +01:00
|
|
|
universe.tick();
|
|
|
|
|
|
|
|
drawGrid();
|
|
|
|
drawCells();
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Start interval timer to periodically iterate frames
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const start = () => {
|
|
|
|
if(loop != null) clearInterval(loop);
|
|
|
|
loop = setInterval(renderSingle, frameInterval);
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Clear interval timer to stop animation loop
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const stop = () => {
|
|
|
|
if(loop != null) clearInterval(loop);
|
|
|
|
loop = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var frameInterval = 50;
|
|
|
|
// var loop = setInterval(renderSingle, frameInterval);
|
|
|
|
var loop = null;
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
// SLIDERS
|
|
|
|
|
2020-10-12 00:10:54 +01:00
|
|
|
const frameSlider = document.getElementById("frameRate");
|
|
|
|
const frameSliderLabel = document.getElementById("frameRate-label");
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Handler for frame interval slider change, stop, change interval, start
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const onFrameSlider = () => {
|
|
|
|
stop();
|
|
|
|
|
|
|
|
frameInterval = frameSlider.value;
|
2021-06-22 16:45:07 +01:00
|
|
|
frameSliderLabel.innerHTML = `Frame Interval: ${frameSlider.value}ms`;
|
2020-10-12 00:10:54 +01:00
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
if(play) start();
|
2020-10-12 00:10:54 +01:00
|
|
|
}
|
|
|
|
frameSlider.onchange = onFrameSlider;
|
2021-06-22 16:45:07 +01:00
|
|
|
frameSlider.value = 100;
|
2020-10-12 00:10:54 +01:00
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Handler for random threshold slider change, get a new universe with new threshold
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const onRandSlider = () => {
|
|
|
|
stop();
|
|
|
|
|
|
|
|
universe = Universe.new(width, height, randSlider.value, new Date().getTime() / 1000);
|
|
|
|
refreshCanvas();
|
2021-06-22 16:45:07 +01:00
|
|
|
randSliderLabel.innerHTML = `Random Threshold: ${randSlider.value}%`;
|
2020-10-12 00:10:54 +01:00
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
if(play) start();
|
2020-10-12 00:10:54 +01:00
|
|
|
}
|
|
|
|
randSlider.onchange = onRandSlider;
|
2021-06-22 16:45:07 +01:00
|
|
|
randSlider.value = 50;
|
2020-10-12 00:10:54 +01:00
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Refresh existing canvas, calculate dimensions and draw
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const refreshCanvas = () => {
|
|
|
|
canvas.width = (CELL_SIZE + 1) * width + 1;
|
|
|
|
canvas.height = (CELL_SIZE + 1) * height + 1;
|
|
|
|
drawGrid();
|
|
|
|
drawCells();
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
// INPUT BOXES
|
|
|
|
|
2020-10-12 00:10:54 +01:00
|
|
|
const widthBox = document.getElementById("width");
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Handler for width input box change, get a new universe of given size
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const onWidth = () => {
|
|
|
|
// PLAY = false;
|
|
|
|
width = widthBox.value;
|
|
|
|
universe = Universe.new(width, height, randSlider.value, new Date().getTime() / 1000);
|
|
|
|
refreshCanvas();
|
|
|
|
// PLAY = true;
|
|
|
|
// requestAnimationFrame(renderLoop);
|
|
|
|
}
|
|
|
|
widthBox.onchange = onWidth;
|
2021-06-22 19:25:32 +01:00
|
|
|
widthBox.value = 100;
|
2020-10-12 00:10:54 +01:00
|
|
|
|
|
|
|
const heightBox = document.getElementById("height");
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Handler for height input box change, get a new universe of given size
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const onHeight = () => {
|
|
|
|
// PLAY = false;
|
|
|
|
height = heightBox.value;
|
|
|
|
universe = Universe.new(width, height, randSlider.value, new Date().getTime() / 1000);
|
|
|
|
refreshCanvas();
|
|
|
|
// PLAY = true;
|
|
|
|
// requestAnimationFrame(renderLoop);
|
|
|
|
}
|
|
|
|
heightBox.onchange = onHeight;
|
2021-06-22 19:25:32 +01:00
|
|
|
heightBox.value = 100;
|
2020-10-12 00:10:54 +01:00
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
// BUTTONS
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Click handler for step button, make single move
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const onPlay = () => {
|
2021-06-22 16:45:07 +01:00
|
|
|
play = !play;
|
|
|
|
|
|
|
|
// console.log("play: " + play);
|
|
|
|
if(play) {
|
|
|
|
playButton.classList.remove("btn-success");
|
|
|
|
playButton.classList.add("btn-danger");
|
|
|
|
playButton.innerText = "Stop";
|
2020-10-12 00:10:54 +01:00
|
|
|
start();
|
|
|
|
}else {
|
2021-06-22 16:45:07 +01:00
|
|
|
playButton.classList.add("btn-success");
|
|
|
|
playButton.classList.remove("btn-danger");
|
|
|
|
playButton.innerText = "Play";
|
2020-10-12 00:10:54 +01:00
|
|
|
stop();
|
2020-10-11 21:46:15 +01:00
|
|
|
}
|
2020-10-12 00:10:54 +01:00
|
|
|
}
|
2021-06-22 16:45:07 +01:00
|
|
|
const playButton = document.getElementById("play");
|
|
|
|
playButton.onclick = onPlay;
|
2020-10-12 00:10:54 +01:00
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Click handler for step button, make single move
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const onStep = () => {
|
|
|
|
console.log("stepping");
|
|
|
|
renderSingle();
|
|
|
|
}
|
|
|
|
document.getElementById("step").onclick = onStep;
|
|
|
|
|
2021-06-22 16:45:07 +01:00
|
|
|
/**
|
|
|
|
* Click handler for reset button, generate a new universe and refresh the canvas
|
|
|
|
*/
|
2020-10-12 00:10:54 +01:00
|
|
|
const onReset = () => {
|
|
|
|
universe = Universe.new(width, height, randSlider.value, new Date().getTime() / 1000);
|
|
|
|
refreshCanvas();
|
|
|
|
}
|
|
|
|
document.getElementById("reset").onclick = onReset;
|
2020-10-11 21:46:15 +01:00
|
|
|
|
2020-10-12 00:10:54 +01:00
|
|
|
drawGrid();
|
2021-06-21 22:41:48 +01:00
|
|
|
drawCells();
|