skip if cell didn't change
This commit is contained in:
parent
bb8184f7e4
commit
7f8173df16
@ -50,12 +50,13 @@ export function picToBoard(pixels) {
|
|||||||
// convert board to ImageData
|
// convert board to ImageData
|
||||||
// TODO : different cell to color functions
|
// TODO : different cell to color functions
|
||||||
// (binary, intermediate states, camaieux, etc)
|
// (binary, intermediate states, camaieux, etc)
|
||||||
export function boardToPic(board) {
|
export function boardToPic(board, frame) {
|
||||||
const live = board.cellProperties.liveColor;
|
const live = board.cellProperties.liveColor;
|
||||||
const dead = board.cellProperties.deadColor;
|
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)];
|
const colors = [hexToRGB(live), hexToRGB(dead)];
|
||||||
board.grid.reduce((acc, cell, index) => {
|
board.grid.reduce((acc, cell, index) => {
|
||||||
|
if (cell.state === cell.prevState) return acc;
|
||||||
const color = colors[(cell.state === 1) & 1];
|
const color = colors[(cell.state === 1) & 1];
|
||||||
const i = index * 4;
|
const i = index * 4;
|
||||||
acc[i] = color[0];
|
acc[i] = color[0];
|
||||||
|
@ -9,18 +9,20 @@ class Renderer {
|
|||||||
this.height = null;
|
this.height = null;
|
||||||
this.ctx = null;
|
this.ctx = null;
|
||||||
this.refreshRate = 300;
|
this.refreshRate = 300;
|
||||||
|
this.prevFrame = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// draws the board representation on the canvas
|
// draws the board representation on the canvas
|
||||||
render(board) {
|
render(board) {
|
||||||
const d = board.cellProperties.size;
|
const d = board.cellProperties.size;
|
||||||
// bool to RGBA colors
|
// bool to RGBA colors
|
||||||
const img = boardToPic(board);
|
const img = boardToPic(board, this.prevFrame);
|
||||||
this.ctx.clearRect(0, 0, this.width, this.height);
|
this.ctx.clearRect(0, 0, this.width, this.height);
|
||||||
scaleAndApply(this.ctx, d, () => {
|
scaleAndApply(this.ctx, d, () => {
|
||||||
this.workCtx.putImageData(img, 0, 0);
|
this.workCtx.putImageData(img, 0, 0);
|
||||||
this.ctx.drawImage(this.workCanvas, 0, 0, this.width, this.height);
|
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
|
// draw image on canvas
|
||||||
|
Loading…
Reference in New Issue
Block a user