refresh rate, some attempts at optimizing

This commit is contained in:
Ali Gator 2022-12-20 16:27:04 +01:00
parent 44d749ac4e
commit c825752405
3 changed files with 28 additions and 25 deletions

View File

@ -89,8 +89,8 @@
getDraw2d(value) { getDraw2d(value) {
if (value == true) this.draw2dNew(); if (value == true) this.draw2dNew();
}, },
getDraw2dLast(value) { async getDraw2dLast(value) {
if (value == true) this.draw2dLast(); if (value == true) await this.draw2dLast();
}, },
getDraw2dPicture(value) { getDraw2dPicture(value) {
if (value == true) this.draw2dPicture(); if (value == true) this.draw2dPicture();
@ -118,10 +118,10 @@
"setBoardHeight", "setBoardHeight",
]), ]),
// draws the board on the canvas // draws the board on the canvas
drawCanvas(board, width, height) { async drawCanvas(board, width, height) {
const d = this.cellProperties.size; const d = this.cellProperties.size;
// bool to RGBA colors // bool to RGBA colors
const img = boardToPic(board, width, height, this.cellProperties); const img = await boardToPic(board, width, height, this.cellProperties);
// rescale and draw // rescale and draw
this.ctx.save(); this.ctx.save();
this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight); this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
@ -168,14 +168,14 @@
this.lastBoard = Object.freeze(newBoard); this.lastBoard = Object.freeze(newBoard);
}, },
// draw 2d automaton in a loop, starting from passed state // draw 2d automaton in a loop, starting from passed state
async draw2dNext(board) { async draw2dNext(board, time) {
requestAnimationFrame(() => { setTimeout(() => {
if (!this.canDraw) return; requestAnimationFrame(() => {
const newBoard = evolve2d(board, this.selectedRules); if (!this.canDraw) return;
this.draw2d(board); this.draw2d(board);
this.lastBoard = Object.freeze(newBoard); return this.draw2dNext(this.lastBoard);
return this.draw2dNext(newBoard); });
}); }, this.refreshRate);
}, },
// draw 2d automaton from a new state // draw 2d automaton from a new state
async draw2dNew() { async draw2dNew() {

View File

@ -1,14 +1,14 @@
// handles negative index and index bigger than its array length // handles negative index and index bigger than its array length
function guard(index, array) { function guard(index, arrayLength) {
if (index > array.length - 1) return 0; if (index > arrayLength - 1) return 0;
if (index < 0) return array.length - 1; if (index < 0) return arrayLength - 1;
return index; return index;
} }
// get the next evolution of a 1D CA initial state // get the next evolution of a 1D CA initial state
function evolve1d(state, rules) { function evolve1d(state, rules) {
function getCell(index) { function getCell(index) {
const safeIndex = guard(index, state); const safeIndex = guard(index, state.length);
return state[safeIndex]; return state[safeIndex];
} }
const newState = state.map((_, x) => { const newState = state.map((_, x) => {
@ -55,8 +55,8 @@ function getCellNeighbors(board, cellCoordinates) {
// handles board edges where the cell is missing neighbors // handles board edges where the cell is missing neighbors
function getCell(xx, yy) { function getCell(xx, yy) {
const safeX = guard(xx, board); const safeX = guard(xx, board.length);
const safeY = guard(yy, rowLength); const safeY = guard(yy, rowLength.length);
return board[safeX][safeY]; return board[safeX][safeY];
} }

View File

@ -54,13 +54,16 @@ export function picToBoard(pixels, width, height) {
export function boardToPic(board, width, height, cellProperties) { export function boardToPic(board, width, height, cellProperties) {
const live = cellProperties.liveColor; const live = cellProperties.liveColor;
const dead = cellProperties.deadColor; const dead = cellProperties.deadColor;
return board.flat().reduce((acc, cell, index) => { const img = new ImageData(width, height);
const color = cell === 1 ? hexToRGB(live) : hexToRGB(dead); const colors = [hexToRGB(live), hexToRGB(dead)];
board.flat().reduce((acc, cell, index) => {
const color = colors[cell];
const i = index * 4; const i = index * 4;
acc.data[i] = color[0]; acc[i] = color[0];
acc.data[i + 1] = color[1]; acc[i + 1] = color[1];
acc.data[i + 2] = color[2]; acc[i + 2] = color[2];
acc.data[i + 3] = 255; acc[i + 3] = 255;
return acc; return acc;
}, new ImageData(width, height)); }, img.data);
return img;
} }