Repository navigation
Expand file tree
/
Copy pathtiles.html
More file actions
423 lines (376 loc) · 21.2 KB
/
Copy pathtiles.html
File metadata and controls
423 lines (376 loc) · 21.2 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>bradi.sh | Regime Shift</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Homepage for bradi.sh - Viral Regime Edition">
<script src="https://d3js.org/d3.v6.min.js"></script>
<style>
body, html { margin: 0; padding: 0; overflow: hidden; width: 100%; height: 100%; background: #17191D; }
#svg { width: 100vw; height: 100vh; touch-action: none; cursor: grab; }
</style>
</head>
<body>
<div id="svg"></div>
<script type="application/javascript">
const margin = {top: 0, right: 0, bottom: 0, left: 0};
let width = window.innerWidth - margin.left - margin.right; // updated on resize
let height = window.innerHeight - margin.top - margin.bottom;
const TILE_SIZE = 80;
let cameraX = 0;
let cameraY = 0;
let targetCameraX = 0;
let targetCameraY = 0;
// --- AUTO SCROLL & BROWNIAN STATE ---
let lastUserActivity = 0;
const SUSPEND_DURATION = 5000;
let vx = 0;
let vy = 0;
const maxVelocity = 1.5;
const friction = 0.97;
const impulse = 0.1;
// --- REGIME STATE ---
let currentGlobalRegime = 0;
const regimePalettes = {
0: ["#FFF4E0", "#F8B501", "#06ACB5", "#17191D", "#FC3D3C"]
};
const PAINT_LOCATIONS = {
TOP_LEFT_CORNER: 0, TOP_RIGHT_CORNER: 1, BOTTOM_RIGHT_CORNER: 2,
BOTTOM_LEFT_CORNER: 3, CENTER: 4, BASE: 5, TEXT: 6
};
const N_PAINT_LOCATIONS = Object.keys(PAINT_LOCATIONS).length;
const CORNERS = [0, 1, 2, 3];
const CHANCE = { ofHavingACenter: 0.75, ofOffCorners: 0.1 };
// Helper to generate new palettes
const generatePalette = () => {
const rotation = (Math.random() * 360 | 0)
const rotateHue = (hex, degree) => {
// 1. Convert hex to RGB
let r = parseInt(hex.slice(1, 3), 16) / 255;
let g = parseInt(hex.slice(3, 5), 16) / 255;
let b = parseInt(hex.slice(5, 7), 16) / 255;
// 2. Find HSL values
let max = Math.max(r, g, b), min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max === min) {
h = s = 0; // achromatic
} else {
let d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
// 3. Rotate Hue
h = (h * 360 + degree) % 360;
if (h < 0) h += 360; // Handle negative rotations
h /= 360;
// 4. Convert HSL back to RGB
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
};
let q = l < 0.5 ? l * (1 + s) : l + s - l * s;
let p = 2 * l - q;
r = Math.round(hue2rgb(p, q, h + 1/3) * 255);
g = Math.round(hue2rgb(p, q, h) * 255);
b = Math.round(hue2rgb(p, q, h - 1/3) * 255);
// 5. Back to Hex
const toHex = val => val.toString(16).padStart(2, '0');
return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase();
};
return regimePalettes[0].map(c=>rotateHue(c,rotation))
};
class Tile {
constructor(ix, iy, regime = 0) {
this._regime = regime;
this._rotation = 0; // Initialize rotation explicitly
this._sel = null; // D3 selection of this tile's own <g>, set by mountTile
this.init(ix, iy);
}
// The D3 selection for this specific tile's group
get selection() {
return this._sel;
}
init(ix, iy) {
this._ix = ix;
this._iy = iy;
this._cx = this._cy = TILE_SIZE / 2;
this._index = tileKey(ix, iy);
// Initial state without transitions
this._rotation = [-180, -90, 0, 90, 180][Math.random() * 5 | 0];
this.recircle(true, false);
this.recolour(this._regime, false);
}
recircle(noOffCorners = false, animate = true) {
this._centerCircleRadius = Math.random() < CHANCE.ofHavingACenter ? TILE_SIZE / 2 * Math.SQRT2 * (Math.random() * 0.1 + 0.15) : 0;
this._cornerCircleRadius = TILE_SIZE / 2;
this._corners = Math.random() > 0.5 ? [1, 0, 1, 0] : [0, 1, 0, 1];
if (!noOffCorners) this._corners = this._corners.map(corner => Math.random() < CHANCE.ofOffCorners ? 1 - corner : corner);
if (animate) {
const s = this.selection;
s.select("circle").transition().duration(800).attr("r", this._centerCircleRadius);
s.selectAll("path").filter((d, i) => i < 4)
.transition().duration(800)
.attr("d", (d, i) => this.corners[i]);
}
}
recolour(regime = 0, animate = true) {
this._regime = regime;
const palette = regimePalettes[regime] || regimePalettes[0];
this._colours = Array(N_PAINT_LOCATIONS).fill(0).map(_ => palette[Math.random() * palette.length | 0]);
if (animate) {
const s = this.selection;
const dur = 800;
s.select("rect").transition().duration(dur).attr("fill", this._colours[PAINT_LOCATIONS.BASE]);
s.select("circle").transition().duration(dur).attr("fill", this._colours[PAINT_LOCATIONS.CENTER]);
s.selectAll("path").filter((d, i) => i < 4)
.transition().duration(dur)
.attr("fill", (d, i) => this._colours[i]);
}
}
rerotate(animate = true) {
this._rotation = [-180, -90, 0, 90, 180][Math.random() * 5 | 0]
if (animate) {
// Target the nested rotation group specifically
this.selection.select(".tileRotation")
.transition()
.duration(500)
.attr("transform", `rotate(${this._rotation})`);
}
}
get ix() { return this._ix; }
get iy() { return this._iy; }
get x() { return this._ix * TILE_SIZE - cameraX; }
get y() { return this._iy * TILE_SIZE - cameraY; }
get cx() { return this._cx; }
get cy() { return this._cy; }
get index() { return this._index; }
get cornerCircleRadius() { return this._cornerCircleRadius; }
get centerCircleRadius() { return this._centerCircleRadius; }
get colours() { return this._colours; }
get rotation() { return this._rotation; }
get corners() {
return [
(r => `M${r},0A${r},${r},0,0,1,0,${r}L0,0Z`)(this._corners[0] * this.cornerCircleRadius),
(r => `M${TILE_SIZE},${r}A${r},${r},0,0,1,${TILE_SIZE - r},0L${TILE_SIZE},0Z`)(this._corners[1] * this.cornerCircleRadius),
(r => `M${TILE_SIZE - r},${TILE_SIZE}A${r},${r},0,0,1,${TILE_SIZE},${TILE_SIZE - r}L${TILE_SIZE},${TILE_SIZE}Z`)(this._corners[2] * this.cornerCircleRadius),
(r => `M0,${TILE_SIZE - r}A${r},${r},0,0,1,${r},${TILE_SIZE}L0,${TILE_SIZE}Z`)(this._corners[3] * this.cornerCircleRadius)
];
}
get arcs() {
return [
(r => `M${r},0A${r},${r},0,0,1,0,${r}`)(this._corners[0] * this.cornerCircleRadius),
(r => `M${TILE_SIZE},${r}A${r},${r},0,0,1,${TILE_SIZE - r},0`)(this._corners[1] * this.cornerCircleRadius),
(r => `M${TILE_SIZE - r},${TILE_SIZE}A${r},${r},0,0,1,${TILE_SIZE},${TILE_SIZE - r}`)(this._corners[2] * this.cornerCircleRadius),
(r => `M0,${TILE_SIZE - r}A${r},${r},0,0,1,${r},${TILE_SIZE}`)(this._corners[3] * this.cornerCircleRadius)
];
}
}
// --- TILE FIELD ---
// Tiles sit at fixed world positions inside one group; scrolling moves only that group.
// Tiles are made as they come within one tile of the screen and removed once they leave it,
// so the field follows the window size as well as the camera.
const tiles = new Map(); // "ix,iy" -> Tile
function tileKey(ix, iy) { return ix + "," + iy; }
const NEIGHBOURS = [[1, 0], [-1, 0], [0, 1], [0, -1]];
let lastRegime = 0; // regime of the tile most recently removed
const svg = d3.select("#svg").append("svg")
.attr("viewBox", [0, 0, width, height])
.style("width", "100%").style("height", "100%").style("display", "block");
const theTiles = svg.append("g").attr("id", "theTiles");
const mountTile = (tile) => {
const g = theTiles.append("g")
.attr("class", "tileTranslations")
.attr("transform", `translate(${tile.ix * TILE_SIZE},${tile.iy * TILE_SIZE})`);
const group = g
.append("g").attr("transform", `translate(${TILE_SIZE / 2},${TILE_SIZE / 2})`).attr("class", "tileTranslation")
.append("g").attr("transform", `rotate(${tile.rotation})`).attr("class", "tileRotation")
.append("g").attr("transform", `translate(${TILE_SIZE / -2},${TILE_SIZE / -2})`).attr("class", "tileGroup");
group.append("rect").attr("width", TILE_SIZE).attr("height", TILE_SIZE).attr("fill", tile.colours[PAINT_LOCATIONS.BASE]);
CORNERS.forEach(c => group.append("path").attr("d", tile.corners[c]).attr("fill", tile.colours[c]));
CORNERS.forEach(a => group.append("path").attr("d", tile.arcs[a]).attr("fill", "none").attr("stroke", "rgba(255,255,255,0.3)"));
group.append("circle").attr("r", tile.centerCircleRadius).attr("cx", tile.cx).attr("cy", tile.cy).attr("fill", tile.colours[PAINT_LOCATIONS.CENTER]);
tile._sel = g;
};
// A new tile joins the regime around it, so a regime carries on past the edge of the screen.
const regimeAround = (ix, iy) => {
let regime = null;
for (const [dx, dy] of NEIGHBOURS) {
const n = tiles.get(tileKey(ix + dx, iy + dy));
if (n && (regime === null || n._regime > regime)) regime = n._regime;
}
return regime === null ? lastRegime : regime;
};
let range = null; // tile indices currently covered
const layout = () => {
const x0 = Math.floor(cameraX / TILE_SIZE) - 1, x1 = Math.floor((cameraX + width) / TILE_SIZE) + 1;
const y0 = Math.floor(cameraY / TILE_SIZE) - 1, y1 = Math.floor((cameraY + height) / TILE_SIZE) + 1;
if (range && range.x0 === x0 && range.x1 === x1 && range.y0 === y0 && range.y1 === y1) return;
range = { x0, x1, y0, y1 };
for (const [key, tile] of tiles) {
if (tile.ix < x0 || tile.ix > x1 || tile.iy < y0 || tile.iy > y1) {
lastRegime = tile._regime;
tile.selection.remove();
tiles.delete(key);
}
}
for (let iy = y0; iy <= y1; iy++) {
for (let ix = x0; ix <= x1; ix++) {
if (tiles.has(tileKey(ix, iy))) continue;
const tile = new Tile(ix, iy, regimeAround(ix, iy));
tiles.set(tile.index, tile);
mountTile(tile);
}
}
};
// --- INTERACTION ---
const markActivity = () => { lastUserActivity = Date.now(); };
window.addEventListener('wheel', e => {
e.preventDefault(); markActivity();
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? height : 1; // lines / pages -> px
targetCameraX += e.deltaX * unit; targetCameraY += e.deltaY * unit;
}, { passive: false });
// Drag with mouse, finger or pen; letting go while moving carries on as a flick.
const surface = document.getElementById("svg");
let drag = null;
let flingX = 0, flingY = 0; // px per ms
surface.addEventListener('pointerdown', e => {
if (drag) return;
markActivity();
surface.setPointerCapture(e.pointerId);
surface.style.cursor = "grabbing";
drag = { id: e.pointerId, x: e.clientX, y: e.clientY, t: performance.now() };
flingX = flingY = 0;
});
surface.addEventListener('pointermove', e => {
if (!drag || e.pointerId !== drag.id) return;
markActivity();
const now = performance.now(), dt = Math.max(1, now - drag.t);
const dx = e.clientX - drag.x, dy = e.clientY - drag.y;
targetCameraX -= dx; targetCameraY -= dy;
flingX = 0.7 * flingX + 0.3 * (-dx / dt); flingY = 0.7 * flingY + 0.3 * (-dy / dt);
drag.x = e.clientX; drag.y = e.clientY; drag.t = now;
});
const endDrag = e => {
if (!drag || e.pointerId !== drag.id) return;
if (e.type === 'pointercancel' || performance.now() - drag.t > 80) flingX = flingY = 0; // held still before letting go
drag = null;
surface.style.cursor = "grab";
};
surface.addEventListener('pointerup', endDrag);
surface.addEventListener('pointercancel', endDrag);
window.addEventListener('keydown', e => {
const step = TILE_SIZE * (e.shiftKey ? 6 : 2);
const move = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, -step], ArrowDown: [0, step],
PageUp: [0, -height * 0.85], PageDown: [0, height * 0.85] }[e.key];
if (!move) return;
e.preventDefault(); markActivity();
flingX = flingY = 0;
targetCameraX += move[0]; targetCameraY += move[1];
});
// Big Overlay Text
const textLabels = ["bradi.sh", "bradi.sh"];
textLabels.forEach((t, i) => {
svg.append("text")
.attr("x", width / 2).attr("y", height / 2)
.attr("text-anchor", "middle").attr("dominant-baseline", "central")
.attr("font-family", "sans-serif").attr("font-weight", "900").attr("font-size", "20vw")
.style("pointer-events", "none").text(t)
.style("fill", i === 0 ? "none" : "#fff")
.style("stroke", i === 0 ? "black" : "none")
.style("filter", i === 0 ? "blur(5px)" : "none")
.style("mix-blend-mode", i === 0 ? "hard-light" : "difference");
});
window.addEventListener('resize', () => {
width = window.innerWidth - margin.left - margin.right;
height = window.innerHeight - margin.top - margin.bottom;
svg.attr("viewBox", [0, 0, width, height]);
svg.selectAll("text").attr("x", width / 2).attr("y", height / 2);
range = null; // re-cover the new window on the next frame
});
const updateVisuals = (tile) => {
const group = tile.selection;
group.select("rect").transition().duration(800).attr("fill", tile.colours[PAINT_LOCATIONS.BASE]);
group.select("circle").transition().duration(800).attr("fill", tile.colours[PAINT_LOCATIONS.CENTER]);
group.selectAll("path").filter((d, i) => i < 4).transition().duration(800).attr("fill", (d, i) => tile.colours[i]);
};
let drawnX = null, drawnY = null;
const redraw = () => {
const now = Date.now();
if (!drag && (flingX || flingY)) {
markActivity();
targetCameraX += flingX * 16; targetCameraY += flingY * 16;
flingX *= 0.95; flingY *= 0.95;
if (Math.hypot(flingX, flingY) < 0.01) flingX = flingY = 0;
}
if (now - lastUserActivity > SUSPEND_DURATION) {
vx += (Math.random() - 0.5) * impulse;
vy += (Math.random() - 0.5) * impulse;
vx *= friction; vy *= friction;
vx = Math.max(-maxVelocity, Math.min(maxVelocity, vx));
vy = Math.max(-maxVelocity, Math.min(maxVelocity, vy));
targetCameraX += vx; targetCameraY += vy;
} else {
vx = 0; vy = 0;
}
cameraX += (targetCameraX - cameraX) * 0.1;
cameraY += (targetCameraY - cameraY) * 0.1;
layout();
// one write moves every tile; skipped while the camera is at rest
if (drawnX === null || Math.abs(cameraX - drawnX) > 0.01 || Math.abs(cameraY - drawnY) > 0.01) {
theTiles.attr("transform", `translate(${-cameraX},${-cameraY})`);
drawnX = cameraX; drawnY = cameraY;
}
};
const animate = () => { redraw(); requestAnimationFrame(animate); };
animate();
// --- FEATURE 1 & 2: REGIME SPONTANEITY (10s) ---
setInterval(() => {
currentGlobalRegime++;
regimePalettes[currentGlobalRegime] = generatePalette();
// Pick a visible-ish tile to be "Patient Zero"
const all = [...tiles.values()];
const visibleTiles = all.filter(t => t.x > 0 && t.x < width && t.y > 0 && t.y < height);
const target = visibleTiles.length > 0 ? visibleTiles[Math.floor(Math.random()*visibleTiles.length)] : all[0];
target.recolour(currentGlobalRegime);
updateVisuals(target);
}, 10000);
// --- FEATURE 4: NEIGHBOR CHECK (0.5s) ---
setInterval(() => {
const updates = [];
for (const tile of tiles.values()) {
// Find cardinal neighbors
let highestNeighborRegime = -Infinity;
for (const [dx, dy] of NEIGHBOURS) {
const n = tiles.get(tileKey(tile.ix + dx, tile.iy + dy));
if (n && n._regime > highestNeighborRegime) highestNeighborRegime = n._regime;
}
if (highestNeighborRegime > tile._regime && Math.random() > 0.5) {
updates.push({tile, regime: highestNeighborRegime});
}
}
// Apply updates after check to prevent synchronous chain reactions in one tick
updates.forEach(u => {
u.tile.recolour(u.regime);
u.tile.rerotate();
updateVisuals(u.tile);
});
}, 500);
// Random Tile Pulse (Rotation only now to separate from regime change)
setInterval(() => {
const all = [...tiles.values()];
const tile = all[Math.floor(Math.random() * all.length)];
tile.rerotate()
}, 500);
</script>
</body>
</html>