-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
129 lines (117 loc) · 3.79 KB
/
index.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<title>0xB36S23</title>
<html>
<head>
<style>
body {
margin: 0;
padding: 0;
background: black;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
cursor: pointer;
}
#grid {
display: grid;
gap: 2px;
}
input[type="radio"] {
margin: 0;
appearance: none;
-webkit-appearance: none;
width: 12px;
height: 12px;
border-radius: 50%;
background: black;
border: 1px solid white;
cursor: pointer;
}
input[type="radio"]:checked {
background: white;
}
.inverted {
background: white !important;
}
.inverted input[type="radio"] {
border-color: black;
background: white;
}
.inverted input[type="radio"]:checked {
background: black;
}
</style>
</head>
<body onclick="toggleTheme()">
<div id="grid"></div>
<script>
let rows, cols;
let grid = [];
let isInverted = false;
function initGrid() {
rows = Math.floor(window.innerHeight / 14);
cols = Math.floor(window.innerWidth / 14);
const gridElement = document.getElementById('grid');
gridElement.style.gridTemplateColumns = `repeat(${cols}, 12px)`;
grid = Array(rows).fill().map(() => Array(cols).fill(false));
gridElement.innerHTML = '';
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const radio = document.createElement('input');
radio.type = 'radio';
radio.name = `cell-${i}-${j}`;
if (Math.random() < 0.3) {
radio.checked = true;
grid[i][j] = true;
}
gridElement.appendChild(radio);
}
}
}
function countNeighbors(r, c) {
let count = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue;
const newRow = (r + i + rows) % rows;
const newCol = (c + j + cols) % cols;
if (grid[newRow][newCol]) count++;
}
}
return count;
}
function update() {
const newGrid = Array(rows).fill().map(() => Array(cols).fill(false));
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const neighbors = countNeighbors(i, j);
if (grid[i][j]) {
newGrid[i][j] = neighbors === 2 || neighbors === 3;
} else {
newGrid[i][j] = neighbors === 3 || neighbors === 6;
}
}
}
grid = newGrid;
const radios = document.querySelectorAll('input[type="radio"]');
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
radios[i * cols + j].checked = grid[i][j];
}
}
}
function toggleTheme() {
isInverted = !isInverted;
document.body.classList.toggle('inverted');
initGrid();
}
window.onload = () => {
initGrid();
setInterval(update, 100);
};
window.onresize = initGrid;
</script>
</body>
</html>