-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (94 loc) · 2.79 KB
/
index.js
File metadata and controls
112 lines (94 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const canvas = document.querySelector(".gameCanvas");
const ctx = canvas.getContext("2d");
let cellSize = 15;
let rows = Math.floor(canvas.width / cellSize);
let cols = Math.floor(canvas.height / cellSize);
const game = new Game(rows, cols);
window.onload = () => {
const drawGrid = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
game.grid.forEach((row, i) => {
row.forEach((cell, j) => {
if (cell.alive) {
ctx.fillStyle = "rgba(255, 255, 255, 1)";
ctx.fillRect(i * cellSize, j * cellSize, cellSize, cellSize);
} else {
ctx.fillStyle = "rgba(255, 255, 255, 0)";
ctx.fillRect(i * cellSize, j * cellSize, cellSize, cellSize);
}
});
});
};
let running = false;
let drawing = false;
const isInsideBounds = (event) => {
const rect = canvas.getBoundingClientRect();
return (
event.clientX >= rect.left &&
event.clientX <= rect.right &&
event.clientY >= rect.top &&
event.clientY <= rect.bottom
);
};
const handleMouseUp = () => {
drawing = false;
};
const handleMouseDown = (event) => {
if (isInsideBounds(event)) {
drawing = true;
handleMouseMove(event);
} else {
drawing = false;
}
};
const handleMouseMove = (event) => {
if (!drawing) return;
const rect = canvas.getBoundingClientRect();
const scaleX = canvas.width / rect.width;
const scaleY = canvas.height / rect.height;
if (isInsideBounds(event)) {
const x = Math.floor((event.clientX - rect.left) * scaleX / cellSize);
const y = Math.floor((event.clientY - rect.top) * scaleY / cellSize);
game.grid[x][y].alive = true;
} else {
drawing = false;
}
};
const handleRandomize = () => {
running = false;
game.randomize();
};
const handleClear = () => {
running = false;
game.clear();
};
const handleStop = () => {
running = false;
};
const handleStart = () => {
running = true;
};
const gameLoop = () => {
if (running) {
game.nextGeneration();
if (game.allCellsDead()) {
running = false;
}
}
};
const drawLoop = () => {
drawGrid();
requestAnimationFrame(drawLoop);
};
document
.querySelector(".randomizeButton")
.addEventListener("click", handleRandomize);
document.querySelector(".clearButton").addEventListener("click", handleClear);
document.querySelector(".stopButton").addEventListener("click", handleStop);
document.querySelector(".startButton").addEventListener("click", handleStart);
document.addEventListener("mousedown", handleMouseDown);
document.addEventListener("mouseup", handleMouseUp);
document.addEventListener("mousemove", handleMouseMove);
setInterval(gameLoop, 500);
window.requestAnimationFrame(drawLoop);
};