-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathising.html
More file actions
245 lines (220 loc) · 6.31 KB
/
Copy pathising.html
File metadata and controls
245 lines (220 loc) · 6.31 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ising-style vector grid</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.js"></script>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background: #1a1a1a;
}
#controls {
position: fixed;
top: 12px;
left: 12px;
z-index: 10;
display: flex;
align-items: center;
gap: 8px;
font: 13px/1 system-ui, sans-serif;
color: #ccc;
background: rgba(0,0,0,0.55);
padding: 6px 12px;
border-radius: 6px;
user-select: none;
}
#controls input[type=range] {
width: 140px;
accent-color: #888;
}
#controls button {
background: rgba(255,255,255,0.12);
border: 1px solid rgba(255,255,255,0.25);
color: #ccc;
font: 13px/1 system-ui, sans-serif;
padding: 4px 10px;
border-radius: 4px;
cursor: pointer;
}
#controls button:hover {
background: rgba(255,255,255,0.22);
}
</style>
</head>
<body>
<div id="controls">
<label for="contrarianSlider">Contrarian</label>
<input id="contrarianSlider" type="range" min="0" max="100" value="2">
<span id="contrarianValue">2%</span>
<label for="noiseSlider">Noise</label>
<input id="noiseSlider" type="range" min="0" max="100" value="0">
<span id="noiseValue">0%</span>
<button id="resetBtn">Reset</button>
</div>
<script>
// Ising-style vector grid: each cell has a unit vector; each frame many random
// cells update their vector toward the average of their four immediate neighbors.
const CELL_SIZE = 14;
const BLEND_ALPHA = 0.3;
const VECTOR_SCALE = 0.675; // fraction of cell half-size for drawing the vector
const UPDATES_PER_FRAME_SCALE = 1; // updates per frame = scale * (cols * rows)
let cols, rows;
let cells = [];
let contrarianFraction = 0.02;
let noiseAmount = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100);
cols = max(1, floor(width / CELL_SIZE));
rows = max(1, floor(height / CELL_SIZE));
const slider = document.getElementById('contrarianSlider');
const label = document.getElementById('contrarianValue');
slider.addEventListener('input', () => {
contrarianFraction = parseInt(slider.value) / 100;
label.textContent = slider.value + '%';
initGrid();
});
const noiseSlider = document.getElementById('noiseSlider');
const noiseLabel = document.getElementById('noiseValue');
noiseSlider.addEventListener('input', () => {
noiseAmount = parseInt(noiseSlider.value) / 100;
noiseLabel.textContent = noiseSlider.value + '%';
});
document.getElementById('resetBtn').addEventListener('click', () => initGrid());
initGrid();
}
function initGrid() {
cells = [];
for (let i = 0; i < cols; i++) {
cells[i] = [];
for (let j = 0; j < rows; j++) {
const angle = random(TWO_PI);
cells[i][j] = {
vx: cos(angle),
vy: sin(angle),
contrarian: random() < contrarianFraction
};
}
}
}
function getNeighborAverage(i, j) {
const left = (i - 1 + cols) % cols;
const right = (i + 1) % cols;
const up = (j - 1 + rows) % rows;
const down = (j + 1) % rows;
const sumVx = cells[left][j].vx + cells[right][j].vx + cells[i][up].vx + cells[i][down].vx;
const sumVy = cells[left][j].vy + cells[right][j].vy + cells[i][up].vy + cells[i][down].vy;
const len = sqrt(sumVx * sumVx + sumVy * sumVy);
if (len < 1e-10) return null;
return { vx: sumVx / len, vy: sumVy / len };
}
function normalize(vx, vy) {
const len = sqrt(vx * vx + vy * vy);
if (len < 1e-10) return null;
return { vx: vx / len, vy: vy / len };
}
function updateOneCell() {
const i = floor(random(cols));
const j = floor(random(rows));
const avg = getNeighborAverage(i, j);
if (avg === null) return;
const c = cells[i][j];
const sign = c.contrarian ? -1 : 1;
const targetVx = avg.vx * sign;
const targetVy = avg.vy * sign;
let vxNew = c.vx + BLEND_ALPHA * (targetVx - c.vx);
let vyNew = c.vy + BLEND_ALPHA * (targetVy - c.vy);
if (noiseAmount > 0) {
const jitter = random(-PI, PI) * noiseAmount;
const cs = cos(jitter);
const sn = sin(jitter);
const rx = vxNew * cs - vyNew * sn;
const ry = vxNew * sn + vyNew * cs;
vxNew = rx;
vyNew = ry;
}
const n = normalize(vxNew, vyNew);
if (n !== null) {
c.vx = n.vx;
c.vy = n.vy;
}
}
function draw() {
background(0, 0, 12);
const half = CELL_SIZE * 0.5;
const len = half * VECTOR_SCALE;
noStroke();
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
const c = cells[i][j];
if (c.contrarian) {
fill(0, 0, 22);
rect(i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
const cx = (i + 0.5) * CELL_SIZE;
const cy = (j + 0.5) * CELL_SIZE;
const c = cells[i][j];
const angle = atan2(c.vy, c.vx);
const hue = map(angle, -PI, PI, 0, 360);
stroke(hue, 70, 95);
strokeWeight(1.5);
line(cx, cy, cx + c.vx * len, cy + c.vy * len);
}
}
const numUpdates = max(1, round(UPDATES_PER_FRAME_SCALE * cols * rows));
for (let k = 0; k < numUpdates; k++) {
updateOneCell();
}
}
let dragging = false;
let dragStartX, dragStartY;
let dragCellI, dragCellJ;
function getCellUnderMouse() {
const i = floor(mouseX / CELL_SIZE);
const j = floor(mouseY / CELL_SIZE);
if (i >= 0 && i < cols && j >= 0 && j < rows) return { i, j };
return null;
}
function mousePressed() {
if (mouseX < 0 || mouseX > width || mouseY < 0 || mouseY > height) return;
const cell = getCellUnderMouse();
if (!cell) return;
dragging = true;
dragStartX = mouseX;
dragStartY = mouseY;
dragCellI = cell.i;
dragCellJ = cell.j;
}
function mouseDragged() {
if (!dragging) return;
const dx = mouseX - dragStartX;
const dy = mouseY - dragStartY;
const len = sqrt(dx * dx + dy * dy);
if (len < 2) return;
const c = cells[dragCellI][dragCellJ];
c.vx = dx / len;
c.vy = dy / len;
}
function mouseReleased() {
if (!dragging) return;
const dx = mouseX - dragStartX;
const dy = mouseY - dragStartY;
const len = sqrt(dx * dx + dy * dy);
if (len >= 2) {
const c = cells[dragCellI][dragCellJ];
c.vx = dx / len;
c.vy = dy / len;
}
dragging = false;
}
</script>
</body>
</html>