diff --git a/src/modules/picture.js b/src/modules/picture.js index 41933b9..f65a504 100644 --- a/src/modules/picture.js +++ b/src/modules/picture.js @@ -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]; diff --git a/src/modules/renderer.js b/src/modules/renderer.js index a720ff8..a49c9d3 100644 --- a/src/modules/renderer.js +++ b/src/modules/renderer.js @@ -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