Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JS performance and fix draw order bug #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,14 @@ const drawCells = () => {
const cells = new Uint8Array(memory.buffer, cellsPtr, width * height);

ctx.beginPath();

// Alive cells.
ctx.fillStyle = ALIVE_COLOR;
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const idx = getIndex(row, col);
if (cells[idx] !== Cell.Alive) {
continue;
}

ctx.fillRect(
col * (CELL_SIZE + 1) + 1,
row * (CELL_SIZE + 1) + 1,
CELL_SIZE,
CELL_SIZE
);
}
}

// Dead cells.
ctx.fillStyle = DEAD_COLOR;

for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const idx = getIndex(row, col);
if (cells[idx] !== Cell.Dead) {
continue;
if (cells[idx] === Cell.Alive) {
ctx.fillStyle = ALIVE_COLOR;
} else if(cells[idx] === Cell.Dead) {
ctx.fillStyle = DEAD_COLOR;
}

ctx.fillRect(
Expand Down Expand Up @@ -183,9 +165,9 @@ canvas.addEventListener("click", event => {
const col = Math.min(Math.floor(canvasLeft / (CELL_SIZE + 1)), width - 1);

universe.toggle_cell(row, col);

drawCells();

drawGrid();
drawCells();
});

play();