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
(() => {
function randomFrom(sequence) {
let seed = Number(BigInt.asUintN(32, sequence ^ (sequence >> 32n))) || 1;
return () => {
seed = Math.imul(seed ^ seed >>> 16, 0x21f0aaad);
seed = Math.imul(seed ^ seed >>> 15, 0x735a2d97);
seed ^= seed >>> 15;
return (seed >>> 0) / 4294967296;
};
}
function createCurrent() {
return { sequence: 0n, seed: 0n, entropy: 0n, width: 390, height: 900, shards: [], from: 0, to: 0, at: 0 };
}
function progress(current, now) {
return Math.max(0, Math.min(1, (now - current.at) / 5000));
}
function position(current, now) {
return current.from + (current.to - current.from) * progress(current, now);
}
function lightAt(shard, t) {
return shard.light + (shard.target - shard.light) * t;
}
function makeShards(sequence, width, height) {
const random = randomFrom(sequence);
const points = [];
const columns = Math.max(3, Math.min(24, Math.ceil(width / 120)));
const rows = Math.max(4, Math.min(16, Math.ceil(height / 115)));
for (let row = 0; row <= rows; row++) {
for (let col = 0; col <= columns; col++) {
points.push([
(col + (col && col < columns ? (random() - .5) * .45 : 0)) / columns,
(row + (row && row < rows ? (random() - .5) * .45 : 0)) / rows,
]);
}
}
const shards = [];
function shard(a, b, c) {
shards.push({ points: [points[a], points[b], points[c]], tint: random(), light: 0, target: 0 });
}
for (let row = 0; row < rows; row++) {
for (let col = 0; col < columns; col++) {
const a = row * (columns + 1) + col, b = a + 1, c = a + columns + 1, d = c + 1;
if (random() < .5) { shard(a, b, c); shard(b, d, c); }
else { shard(a, b, d); shard(a, d, c); }
}
}
return shards;
}
function resizeCurrent(current, width, height) {
if (current.width === width && current.height === height) return;
current.width = width; current.height = height;
if (current.sequence === 0n) return;
current.shards = makeShards(current.seed, width, height);
const random = randomFrom(current.entropy);
for (const shard of current.shards) {
shard.light = shard.target = current.entropy === 0n ? 0 : random() ** 4;
}
}
function receive(current, sequence, now, entropy = 0n) {
if (sequence <= 0n || sequence === current.sequence) return false;
const fresh = current.sequence === 0n || sequence < current.sequence;
const t = progress(current, now);
const delta = fresh ? 0 : Number(sequence - current.sequence > 5000n ? 5000n : sequence - current.sequence);
if (fresh) { current.seed = sequence; current.shards = makeShards(sequence, current.width, current.height); }
current.entropy = entropy;
const random = randomFrom(entropy);
for (const shard of current.shards) {
shard.light = lightAt(shard, t);
shard.target = entropy === 0n ? 0 : random() ** 4;
}
current.from = fresh ? 0 : position(current, now);
current.to = current.from + delta / 50000;
current.at = now;
current.sequence = sequence;
return true;
}
function absorb(seed, message) {
for (let i = 0; i < message.length; i++) seed = Math.imul(seed ^ message.charCodeAt(i), 16777619);
return seed >>> 0;
}
function grainFrom(seed, points = [[0,0],[384,0],[0,384]], tint = 0) {
const [a,b,c] = points;
const area = Math.abs((b[0]-a[0])*(c[1]-a[1])-(c[0]-a[0])*(b[1]-a[1]))/2;
const random = randomFrom(seed);
const angle = Math.atan2(b[1]-a[1],b[0]-a[0])*.12 + (tint-.5)*.8;
return Array.from({length:Math.ceil(area/90)},()=>{
let u=random(), v=random(); if(u+v>1){u=1-u;v=1-v;}
const x=a[0]+u*(b[0]-a[0])+v*(c[0]-a[0]);
const y=a[1]+u*(b[1]-a[1])+v*(c[1]-a[1]);
const theta=angle+Math.sin(x/190+y/260)*.35+(random()-.5)*.22;
const length=1.1+random()**3*5.4, breadth=.12+random()*.36;
return {x,y,dx:Math.cos(theta)*length,dy:Math.sin(theta)*length,
nx:-Math.sin(theta)*breadth,ny:Math.cos(theta)*breadth};
});
}
if (typeof module !== 'undefined') module.exports = { createCurrent, position, receive, absorb, resizeCurrent, grainFrom };
if (typeof document === 'undefined') return;
if (document.body.dataset.page === 'strata' && !document.getElementById('atmosphere')) {
const layer = document.createElement('div'); layer.id = 'atmosphere'; layer.setAttribute('aria-hidden','true');
const glass = document.createElement('canvas'); glass.id = 'depth'; glass.dataset.current = '';
layer.append(glass); document.body.prepend(layer);
}
const canvas = document.querySelector('canvas[data-current]');
if (!canvas) return;
const context = canvas.getContext('2d');
if (!context) return;
const current = createCurrent();
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
let width = 0, height = 0, frame = 0, last = 0, entropy = 0;
let socket, retry;
const material = new WeakMap();
const pigments = ['72,117,101','76,112,142','111,96,135','141,120,79','139,74,82'];
function texture(shard) {
if (material.has(shard)) return material.get(shard);
const points = shard.points.map(([x,y]) => [x*width,y*height]);
const [a,b,c] = points;
const faces = new Path2D(), edges = new Path2D(), shadows = new Path2D();
const flecks=grainFrom(current.seed+BigInt(Math.round(shard.tint*1e9)),points,shard.tint);
for (const {x,y,dx,dy,nx,ny} of flecks) {
faces.moveTo(x-dx*.5,y-dy*.5);faces.lineTo(x-dx*.12+nx,y-dy*.12+ny);faces.lineTo(x+dx*.5,y+dy*.5);faces.closePath();
shadows.moveTo(x-dx*.5,y-dy*.5);shadows.lineTo(x+dx*.18-nx*.6,y+dy*.18-ny*.6);shadows.lineTo(x+dx*.5,y+dy*.5);shadows.closePath();
edges.moveTo(x-dx*.5,y-dy*.5);edges.lineTo(x-dx*.12+nx*.35,y-dy*.12+ny*.35);
edges.moveTo(x+dx*.08,y+dy*.08);edges.lineTo(x+dx*.5,y+dy*.5);
}
const result={faces,edges,shadows,cx:(a[0]+b[0]+c[0])/3,cy:(a[1]+b[1]+c[1])/3};
material.set(shard,result);return result;
}
function connect() {
clearTimeout(retry);
if (document.hidden || socket) return;
const url = new URL('/subscribe?maxMessageSizeBytes=256', location.href);
url.protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
socket = new WebSocket(url);
socket.onmessage = event => {
if (typeof event.data !== 'string') return;
entropy = absorb(entropy || 2166136261, event.data);
dispatchEvent(new CustomEvent('stream-arrival',{detail:entropy}));
};
socket.onclose = () => { socket = null; if (!document.hidden) retry = setTimeout(connect, 15000); };
socket.onerror = () => socket.close();
}
function paint(now) {
const phase = reduced.matches ? current.to : position(current, now);
const t = reduced.matches ? 1 : progress(current, now);
context.fillStyle = '#040708';
context.fillRect(0,0,width,height);
for (const shard of current.shards) {
const surface=texture(shard);
const light=lightAt(shard,t);
const bend=Math.sin(surface.cx/340-phase*5)*65+Math.sin(surface.cx/150-phase*2)*18;
const stream=.5+.5*Math.sin((surface.cy-bend)/130+phase*3);
const margin=Math.min(1,Math.abs(surface.cx-width/2)/(width*.46));
const illumination=(.085+stream**5*(.10+light*.46))*(.65+.35*margin**1.4);
const pigment= pigments[shard.tint<.35?0:shard.tint<.70?1:shard.tint<.90?2:shard.tint<.97?3:4];
const [a,b,c]=shard.points.map(([x,y])=>[x*width,y*height]);
context.beginPath();context.moveTo(...a);context.lineTo(...b);context.lineTo(...c);context.closePath();
context.fillStyle=`rgba(${pigment},${illumination*.022})`;context.fill();
context.save();context.clip();
context.fillStyle=`rgba(${pigment},${illumination*.40})`;context.fill(surface.faces);
context.fillStyle=`rgba(${pigment},${illumination*.12})`;context.fill(surface.shadows);
context.strokeStyle=`rgba(${pigment},${illumination})`;
context.lineWidth=.26;context.stroke(surface.edges);
context.strokeStyle=`rgba(170,205,212,${illumination*light*.09})`;
context.lineWidth=.18;context.stroke(surface.edges);
context.restore();
context.beginPath();context.moveTo(...a);context.lineTo(...b);
context.strokeStyle=`rgba(${pigment},${illumination*.09})`;
context.lineWidth=.35;context.stroke();
}
}
function animate(now) {
frame = 0;
if (document.hidden) return;
if (now - last >= 50) { paint(now); last = now; }
if (!reduced.matches && now < current.at + 5000) frame = requestAnimationFrame(animate);
else paint(now);
}
function wake() {
if (!frame && !document.hidden) frame = requestAnimationFrame(animate);
}
function resize() {
const ratio = Math.min(devicePixelRatio || 1, 3);
width = canvas.clientWidth; height = canvas.clientHeight;
resizeCurrent(current, width, height);
canvas.width = Math.round(width * ratio); canvas.height = Math.round(height * ratio);
context.setTransform(ratio, 0, 0, ratio, 0, 0);
paint(performance.now());
}
addEventListener('stream-sequence', event => {
if (receive(current, BigInt(event.detail), performance.now(), BigInt(entropy))) wake();
});
addEventListener('resize', resize);
new ResizeObserver(resize).observe(canvas);
document.addEventListener('visibilitychange', () => {
if (document.hidden) { clearTimeout(retry); if (socket) socket.close(); }
else { connect(); wake(); }
});
reduced.addEventListener('change', wake);
resize(); connect();
if (document.body.dataset.page === 'strata') {
async function observe() {
if (document.hidden) return;
try {
const response = await fetch('/status', {headers:{Accept:'text/plain'},cache:'no-store'});
if (!response.ok) return;
const match = (await response.text()).match(/upstream seq\s+([\d,]+)/);
if (match) dispatchEvent(new CustomEvent('stream-sequence',{detail:match[1].replace(/,/g,'')}));
} catch {}
}
observe(); setInterval(observe,5000);
}
})();