-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_2.html
More file actions
177 lines (150 loc) · 4.48 KB
/
grid_2.html
File metadata and controls
177 lines (150 loc) · 4.48 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html>
<head>
<title>Interactive Grid</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
#main-container {
display: flex;
position: relative; /* Make main-container relatively positioned */
}
#legend {
display: flex;
flex-direction: column;
width: 20px;
margin-right: 20px;
}
.legend-cell {
height: 1px;
}
#grid-container {
display: flex;
flex-direction: column;
align-items: center;
}
#grid {
display: grid;
grid-template-columns: repeat(var(--grid-size), 10px);
grid-template-rows: repeat(var(--grid-size), 10px);
border: 1px solid black;
width: calc(var(--grid-size) * 10px);
height: calc(var(--grid-size) * 10px);
justify-content: center;
align-content: center;
}
.cell {
width: 10px;
height: 10px;
}
#warning {
color: red;
visibility: hidden;
margin-bottom: 5px;
}
#slider-container { /* New styles for slider container */
position: absolute;
top: 10px;
left: 10px;
}
#slider {
width: 200px;
}
</style>
</head>
<body>
<div id="slider-container">
<input type="range" id="slider" min="0" max="100" value="0" oninput="updateGrid()">
<span id="slider-value">t = 0.00</span>
</div>
<div id="main-container">
<div id="legend"></div>
<div id="grid-container">
<div id="warning">⚠️ Invalid expression</div>
<input type="text" id="expression" placeholder="Enter expression (x, y, t)" onkeyup="handleInput()">
<div id="grid"></div>
</div>
</div>
<script>
const GRID_SIZE = 63;
document.documentElement.style.setProperty('--grid-size', GRID_SIZE);
let lastValidColors = {};
let warningTimeout;
let t = 0;
function updateGrid() {
const expression = document.getElementById("expression").value;
const slider = document.getElementById("slider");
const sliderValue = document.getElementById("slider-value");
t = slider.value / 100;
sliderValue.textContent = `t = ${t.toFixed(2)}`;
const grid = document.getElementById("grid");
const warning = document.getElementById("warning");
grid.innerHTML = '';
let allFailed = true;
for (let y = -Math.floor(GRID_SIZE / 2); y <= Math.floor(GRID_SIZE / 2); y++) {
for (let x = -Math.floor(GRID_SIZE / 2); x <= Math.floor(GRID_SIZE / 2); x++) {
const cell = document.createElement("div");
cell.classList.add("cell");
try {
const result = eval(expression); // Evaluate the expression from the textbox
const hue = (result % 256);
const color = `hsl(${hue}, 100%, 50%)`;
cell.style.backgroundColor = color;
lastValidColors[`${x},${y}`] = color;
allFailed = false;
} catch (error) {
cell.style.backgroundColor = lastValidColors[`${x},${y}`] || "gray";
}
grid.appendChild(cell);
}
}
if (allFailed) {
warningTimeout = setTimeout(() => {
warning.style.visibility = "visible";
}, 3000);
} else {
clearTimeout(warningTimeout);
warning.style.visibility = "hidden";
}
}
function handleInput() {
updateGrid();
clearTimeout(warningTimeout);
warning.style.visibility = "hidden";
const expression = document.getElementById("expression").value;
let allFailed = true;
// Check if the expression is still invalid
for (let y = -Math.floor(GRID_SIZE / 2); y <= Math.floor(GRID_SIZE / 2); y++) {
for (let x = -Math.floor(GRID_SIZE / 2); x <= Math.floor(GRID_SIZE / 2); x++) {
try {
eval(expression);
allFailed = false;
break; // No need to check further if one is valid
} catch (error) {
// Continue checking for errors in other cells
}
}
}
if (allFailed) {
warningTimeout = setTimeout(() => {
warning.style.visibility = "visible";
}, 3000);
}
}
// Create the legend
const legend = document.getElementById("legend");
for (let hue = 0; hue < 256; hue++) {
const cell = document.createElement("div");
cell.classList.add("legend-cell");
cell.style.backgroundColor = `hsl(${hue}, 100%, 50%)`;
legend.appendChild(cell);
}
// Initial grid update
updateGrid();
</script>
</body>
</html>