-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutlierJS.html
More file actions
145 lines (128 loc) · 4.55 KB
/
outlierJS.html
File metadata and controls
145 lines (128 loc) · 4.55 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cellular Automaton</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #000;
}
canvas {
border: 1px solid #333;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="frameInfo" style="position: absolute; top: 10px; left: 10px; font-family: monospace; background: rgba(0,0,0,0.8); color: white; padding: 5px; border: 1px solid white;">
Frame: <span id="frameCounter">0</span>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = 1024;
const height = 1024;
canvas.width = width;
canvas.height = height;
const rule = new Array(512);
let grid;
let cols, rows;
const cellSize = 1;
function hexStringToByteArray(hex) {
const bytes = [];
for (let i = 0; i < hex.length; i += 2) {
bytes.push(parseInt(hex.substr(i, 2), 16));
}
return bytes;
}
function loadRule() {
const hex = "111e111b144eebb41079234f151ad6ee111e2d171904dad0ef86b5e8e7ca4c40131d10881679e80a10682534a156861240692a952cada74bf8b086621315a0f2";
const bytes = hexStringToByteArray(hex);
let index = 0;
for (const b of bytes) {
for (let i = 7; i >= 0; i--) {
rule[index++] = (b >> i) & 1;
}
}
}
function initGrid() {
cols = Math.floor(width / cellSize);
rows = Math.floor(height / cellSize);
grid = Array(cols).fill().map(() => Array(rows).fill(0));
// Initialize center pattern
const sx = Math.floor(cols / 2);
const sy = Math.floor(rows / 2);
grid[sx][sy] = 1;
grid[sx - 1][sy + 1] = 1;
grid[sx][sy + 1] = 1;
grid[sx + 1][sy + 1] = 1;
grid[sx + 1][sy + 2] = 1;
}
function getNeighbors(x, y) {
const neighbors = new Array(9);
const directions = [
[-1, -1], [0, -1], [1, -1],
[1, 0],
[1, 1], [0, 1], [-1, 1],
[-1, 0],
[0, 0] // center cell
];
for (let i = 0; i < directions.length; i++) {
const dx = x + directions[i][0];
const dy = y + directions[i][1];
if (dx >= 0 && dx < cols && dy >= 0 && dy < rows) {
neighbors[i] = grid[dx][dy];
} else {
neighbors[i] = 0;
}
}
return neighbors;
}
function updateGrid() {
const nextGrid = Array(cols).fill().map(() => Array(rows).fill(0));
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
const neighbors = getNeighbors(x, y);
let index = 0;
for (let i = 0; i < neighbors.length; i++) {
index |= (neighbors[i] << i);
}
nextGrid[x][y] = rule[index];
}
}
grid = nextGrid;
}
let frameCount = 0;
const frameCounter = document.getElementById('frameCounter');
function draw() {
// Clear canvas with black background
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, width, height);
// Draw cells
ctx.fillStyle = 'white';
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (grid[i][j] === 1) {
ctx.fillRect(i * cellSize, j * cellSize, cellSize, cellSize);
}
}
}
frameCount++;
frameCounter.textContent = frameCount;
updateGrid();
requestAnimationFrame(draw);
}
// Initialize and start
loadRule();
initGrid();
draw();
</script>
</body>
</html>