board module

This commit is contained in:
Gator
2022-12-23 12:32:59 +01:00
parent f351f37c46
commit faee8f61d5
6 changed files with 128 additions and 73 deletions

View File

@ -2,9 +2,6 @@
import { onMounted, watch } from "vue";
import { globalStore } from "../stores/index.js";
import {
create1dState,
create1dStateOneCell,
create2dState,
createBoard,
conwayRules,
overpopulationRules,
@ -13,8 +10,11 @@
highLifeRules,
servietteRules,
evolve2d,
} from "../modules/automata.js";
import { getRandomInt } from "../modules/common.js";
} from "../modules/core.js";
import {
create1dInitialState,
create2dRandomGrid,
} from "../modules/board.js";
import { boardToPic, picToBoard } from "../modules/picture.js";
const store = globalStore();
@ -36,7 +36,7 @@
// used to determine the dimensions of the board
const max = () => {
return Math.max(store.boardWidth, store.boardHeight);
return Math.max(store.board.width, store.board.height);
};
const selectedRules = () => {
@ -92,10 +92,10 @@
});
// draws the board on the canvas
const drawCanvas = async (board, width, height) => {
const d = store.cellProperties.size;
const drawCanvas = async (board) => {
const d = board.cellProperties.size;
// bool to RGBA colors
const img = await boardToPic(board, width, height, store.cellProperties);
const img = await boardToPic(board);
// rescale and draw
ctx.save();
ctx.clearRect(0, 0, store.canvasWidth, store.canvasHeight);
@ -106,39 +106,22 @@
ctx.restore();
};
// create a first state, either a single living cell
// at the center or random ones
const compute1dInitialState = () => {
if (store.initial1dState === "onecell")
return create1dStateOneCell(store.boardWidth);
return create1dState(store.boardWidth, getRandomInt, [0, 2]);
};
// initialize board with random cells
const randomInitialState = () => {
return create2dState(
store.boardWidth,
store.boardHeight,
getRandomInt,
[0, 2]
);
};
// draw elementary automaton on the canvas based on selected ruleset
const draw1d = () => {
const initialState = compute1dInitialState();
const initialState = create1dInitialState(
store.board,
store.initial1dState
);
const board = createBoard(initialState, store.ruleset1d.rules, max());
store.lastBoard = Object.freeze(board);
// TODO: the board clearly could be an object
drawCanvas(store.lastBoard, store.boardWidth, store.boardHeight);
store.board.grid = Object.freeze(board);
drawCanvas(store.board);
store.toggleStop();
};
// draw 2D automaton on the canvas in a loop
const draw2d = (board) => {
const newBoard = evolve2d(board, selectedRules());
drawCanvas(newBoard, store.boardWidth, store.boardHeight);
store.lastBoard = Object.freeze(newBoard);
store.board.grid = Object.freeze(evolve2d(board.grid, selectedRules()));
drawCanvas(store.board);
};
// draw 2d automaton in a loop, starting from passed state
@ -147,7 +130,7 @@
requestAnimationFrame(() => {
if (!store.canDraw) return;
draw2d(board);
return draw2dNext(store.lastBoard);
return draw2dNext(store.board);
});
}, store.refreshRate);
};
@ -155,18 +138,21 @@
// draw 2d automaton from a new state
const draw2dNew = async () => {
if (!store.canDraw) return;
const initialState = randomInitialState();
const board = evolve2d(initialState, selectedRules());
if (store.loop) return draw2dNext(board);
else draw2d(board);
const initialGrid = create2dRandomGrid(
store.board.width,
store.board.height
);
store.board.grid = Object.freeze(evolve2d(initialGrid, selectedRules()));
if (store.loop) return draw2dNext(store.board);
else draw2d(store.board);
store.toggleStop();
};
// draw 2d automaton from the last known generated board
const draw2dLast = async () => {
if (!store.canDraw) return;
if (store.loop) return draw2dNext(store.lastBoard);
else draw2d(store.lastBoard);
if (store.loop) return draw2dNext(store.board);
else draw2d(store.board);
store.toggleStop();
};
@ -185,19 +171,25 @@
);
// draw the image back on the work canvas with the dimensions of the board
workCtx.drawImage(store.picture, 0, 0, store.boardWidth, store.boardHeight);
workCtx.drawImage(
store.picture,
0,
0,
store.board.width,
store.board.height
);
// get the resized image data from work canvas
const resized = workCtx.getImageData(
0,
0,
store.boardWidth,
store.boardHeight
store.board.width,
store.board.height
);
// convert the image into a 2D board of boolean based on pixel value
store.lastBoard = Object.freeze(
picToBoard(resized.data, store.boardWidth, store.boardHeight)
store.board.grid = Object.freeze(
picToBoard(resized.data, store.board.width, store.board.height)
);
store.toggleStop();
@ -206,7 +198,7 @@
// stop drawing routines and clear the canvas
const reset = () => {
store.toggleStop();
store.lastBoard = null;
store.board.grid = null;
ctx.clearRect(0, 0, store.canvasWidth, store.canvasHeight);
store.reset = false;
};