Skip to content

Commit 890ef72

Browse files
mschwarclaude
andcommitted
Fix JS timing: defer DOM access to DOMContentLoaded
Scripts were accessing elements before DOM was ready, causing null errors. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 674efce commit 890ef72

2 files changed

Lines changed: 25 additions & 30 deletions

File tree

docs/js/simulations.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@
44
(function() {
55
const FPS = 30;
66
const FRAME_TIME = 1000 / FPS;
7-
8-
// ========== GAME OF LIFE ==========
9-
const lifeCanvas = document.getElementById('life-canvas');
10-
const lifeCtx = lifeCanvas.getContext('2d');
11-
const lifeStartBtn = document.getElementById('life-start');
12-
const lifeResetBtn = document.getElementById('life-reset');
13-
147
const LIFE_GRID_SIZE = 80;
8+
9+
let lifeCanvas, lifeCtx, lifeStartBtn, lifeResetBtn;
1510
let lifeGrid, lifeNext, lifeRunning, lifeAnimId, lifeLastTime;
1611

1712
function initLife() {
1813
lifeCanvas.width = lifeCanvas.offsetWidth;
1914
lifeCanvas.height = lifeCanvas.offsetHeight;
2015
lifeGrid = new Uint8Array(LIFE_GRID_SIZE * LIFE_GRID_SIZE);
2116
lifeNext = new Uint8Array(LIFE_GRID_SIZE * LIFE_GRID_SIZE);
22-
// Random seed
2317
for (let i = 0; i < lifeGrid.length; i++) {
2418
lifeGrid[i] = Math.random() < 0.3 ? 1 : 0;
2519
}
@@ -47,10 +41,8 @@
4741
const idx = y * LIFE_GRID_SIZE + x;
4842
const neighbors = countNeighbors(x, y);
4943
if (lifeGrid[idx] === 1) {
50-
// Alive: survive with 2-3 neighbors
5144
lifeNext[idx] = (neighbors === 2 || neighbors === 3) ? 1 : 0;
5245
} else {
53-
// Dead: birth with exactly 3 neighbors
5446
lifeNext[idx] = (neighbors === 3) ? 1 : 0;
5547
}
5648
}
@@ -83,31 +75,34 @@
8375
lifeAnimId = requestAnimationFrame(loopLife);
8476
}
8577

86-
lifeStartBtn.addEventListener('click', () => {
87-
if (lifeRunning) {
78+
window.addEventListener('DOMContentLoaded', () => {
79+
lifeCanvas = document.getElementById('life-canvas');
80+
lifeCtx = lifeCanvas.getContext('2d');
81+
lifeStartBtn = document.getElementById('life-start');
82+
lifeResetBtn = document.getElementById('life-reset');
83+
84+
lifeStartBtn.addEventListener('click', () => {
85+
if (lifeRunning) {
86+
lifeRunning = false;
87+
lifeStartBtn.textContent = 'Start';
88+
} else {
89+
lifeRunning = true;
90+
lifeStartBtn.textContent = 'Pause';
91+
lifeAnimId = requestAnimationFrame(loopLife);
92+
}
93+
});
94+
95+
lifeResetBtn.addEventListener('click', () => {
8896
lifeRunning = false;
8997
lifeStartBtn.textContent = 'Start';
90-
} else {
91-
lifeRunning = true;
92-
lifeStartBtn.textContent = 'Pause';
93-
lifeAnimId = requestAnimationFrame(loopLife);
94-
}
95-
});
96-
97-
lifeResetBtn.addEventListener('click', () => {
98-
lifeRunning = false;
99-
lifeStartBtn.textContent = 'Start';
100-
cancelAnimationFrame(lifeAnimId);
101-
initLife();
102-
});
98+
cancelAnimationFrame(lifeAnimId);
99+
initLife();
100+
});
103101

104-
// Initialize on load
105-
window.addEventListener('load', () => {
106102
initLife();
107103
});
108104

109-
// Handle resize
110105
window.addEventListener('resize', () => {
111-
initLife();
106+
if (lifeCanvas) initLife();
112107
});
113108
})();

docs/js/tutorial.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
lifeClearBtn.addEventListener('click', clearLife);
141141

142142
// Initialize both grids on load
143-
window.addEventListener('load', () => {
143+
window.addEventListener('DOMContentLoaded', () => {
144144
buildAntGrid();
145145
buildLifeGrid();
146146
});

0 commit comments

Comments
 (0)