skip if cell didn't change

This commit is contained in:
Ali Gator 2023-01-02 22:37:57 +01:00
parent bb8184f7e4
commit 7f8173df16
2 changed files with 6 additions and 3 deletions

View File

@ -50,12 +50,13 @@ export function picToBoard(pixels) {
// convert board to ImageData
// TODO : different cell to color functions
// (binary, intermediate states, camaieux, etc)
export function boardToPic(board) {
export function boardToPic(board, frame) {
const live = board.cellProperties.liveColor;
const dead = board.cellProperties.deadColor;
const img = new ImageData(board.width, board.height);
const img = frame || new ImageData(board.width, board.height);
const colors = [hexToRGB(live), hexToRGB(dead)];
board.grid.reduce((acc, cell, index) => {
if (cell.state === cell.prevState) return acc;
const color = colors[(cell.state === 1) & 1];
const i = index * 4;
acc[i] = color[0];

View File

@ -9,18 +9,20 @@ class Renderer {
this.height = null;
this.ctx = null;
this.refreshRate = 300;
this.prevFrame = null;
}
// draws the board representation on the canvas
render(board) {
const d = board.cellProperties.size;
// bool to RGBA colors
const img = boardToPic(board);
const img = boardToPic(board, this.prevFrame);
this.ctx.clearRect(0, 0, this.width, this.height);
scaleAndApply(this.ctx, d, () => {
this.workCtx.putImageData(img, 0, 0);
this.ctx.drawImage(this.workCanvas, 0, 0, this.width, this.height);
});
this.prevFrame = this.workCtx.getImageData(0, 0, board.width, board.height);
}
// draw image on canvas