diff --git a/deploy/site/ambient.js b/deploy/site/ambient.js index f8640e9..10f0c6c 100644 --- a/deploy/site/ambient.js +++ b/deploy/site/ambient.js @@ -1,213 +1,225 @@ (() => { - 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; - }; - } + // The background is a current flowing left to right past the sequence number. + // Everything it does comes from the sequence numbers the page already polls: + // it moves only as far as they say the firehose moved, and the rate against + // what is typical sets the regime, the way speed does for water past a stone. + // Slow is laminar, typical sheds a vortex street, busy goes turbulent. + // It opens no connection of its own. + const SETTLE_MS = 5000, REST_MS = 4000, MAX_DELTA = 5000n, TYPICAL_RATE = 450; + const RIVER_SHARE = 30; // one river packet stands for this many observed events + function createCurrent() { - return { sequence: 0n, seed: 0n, entropy: 0n, width: 390, height: 900, shards: [], from: 0, to: 0, at: 0 }; + return { sequence: 0n, from: 0, to: 0, at: 0, fromRegime: 1, toRegime: 1, rate: 0 }; } function progress(current, now) { - return Math.max(0, Math.min(1, (now - current.at) / 5000)); + return Math.max(0, Math.min(1, (now - current.at) / SETTLE_MS)); } + // seconds of flow at the typical rate: one poll's worth of events is played out over the next poll interval 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; - } + // observed rate over the typical rate, eased between polls. 1 is an ordinary hour. + function regime(current, now) { + return current.fromRegime + (current.toRegime - current.fromRegime) * progress(current, now); } - function receive(current, sequence, now, entropy = 0n) { + function receive(current, sequence, now, typical = TYPICAL_RATE) { 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; - } + const gap = sequence - current.sequence, seconds = (now - current.at) / 1000; + const delta = fresh ? 0 : Number(gap > MAX_DELTA ? MAX_DELTA : gap); + current.fromRegime = fresh ? 1 : regime(current, now); current.from = fresh ? 0 : position(current, now); - current.to = current.from + delta / 50000; + // a first sample, or one after a long gap, says nothing reliable about the rate + current.rate = fresh || seconds < 1 || seconds > 30 ? 0 : Number(gap) / seconds; + current.toRegime = current.rate > 0 && typical > 0 ? Math.max(.2, Math.min(2.5, current.rate / typical)) : current.fromRegime; + current.to = current.from + delta / TYPICAL_RATE; 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 }; + // 1 while the last observed advance is still being played out, easing to 0 once it has been + function energy(current, now) { + if (current.to === current.from) return 0; + return Math.max(0, Math.min(1, 1 - (now - current.at - SETTLE_MS) / REST_MS)); + } + function moving(current, now) { + return current.to !== current.from && now < current.at + SETTLE_MS + REST_MS; + } + // how many river packets the events observed up to `now` amount to, in all + function packets(current, now) { + return Math.floor(position(current, now) * TYPICAL_RATE / RIVER_SHARE); + } + function seedFor(index) { + let seed = Math.imul(index ^ 2166136261, 16777619); + seed = Math.imul(seed ^ seed >>> 15, 0x735a2d97); + return (seed ^ seed >>> 13) >>> 0; + } + // device pixels per css pixel for a surface of this css size, within a fixed pixel budget + function surfaceScale(width, height, ratio, budget = 3200000) { + return Math.max(.6, Math.min(ratio, 2, Math.sqrt(budget / Math.max(1, width * height)))); + } + if (typeof module !== 'undefined') module.exports = { createCurrent, position, regime, receive, energy, moving, packets, seedFor, surfaceScale }; 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 layer = document.createElement('div'); layer.id = 'atmosphere'; layer.setAttribute('aria-hidden', 'true'); + const surface = document.createElement('canvas'); surface.id = 'depth'; surface.dataset.current = ''; + layer.append(surface); 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})); + + const FRAGMENT = `#version 300 es +precision highp float; +uniform vec2 R; // surface size in device pixels +uniform float D; // device pixels per css pixel +uniform float T; // seconds of observed flow +uniform float E; // 1 while flowing, 0 at rest +uniform float Q; // observed rate over the typical rate +uniform vec2 C; // the sequence number's centre, in css pixels of this surface +out vec4 o; + +float h21(vec2 p){ p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); } +float vn(vec2 p){ vec2 i = floor(p), f = fract(p); f = f * f * (3. - 2. * f); + return mix(mix(h21(i), h21(i + vec2(1, 0)), f.x), mix(h21(i + vec2(0, 1)), h21(i + vec2(1, 1)), f.x), f.y); } +float ign(vec2 p){ return fract(52.9829189 * fract(dot(p, vec2(.06711056, .00583715)))); } +// decorative contrast is reduced through the text column instead of putting a panel behind the text +float column(float x, float w){ + float hw = min(w * .5, 330.), d = abs(x - w * .5), t = clamp((d - hw * .75) / (hw * .6), 0., 1.); + return mix(.5, 1., t * t * (3. - 2. * t)); +} +// small turbulence whose octaves fall off at Kolmogorov's rate, as a stream function +float psi(vec2 p){ + float t = T * .03, s = 0., a = 1.; vec2 q = p * .0032; + for (int i = 0; i < 3; i++) { s += a * vn(q + vec2(t * (1. + float(i)), -t * .7)); q = q * 2.03 + 17.3; a *= .397; } + return s; +} +vec2 curl(vec2 p){ float a = psi(p); return vec2(psi(p + vec2(0., 2.)) - a, a - psi(p + vec2(2., 0.))) / 2.; } +vec2 vort(vec2 p, vec2 c, float g, float core){ vec2 r = p - c; return g * vec2(-r.y, r.x) / (dot(r, r) + core * core); } +// a steady current past the sequence number. below about a third of the typical rate the wake +// stays attached and the strokes lie flat; above it the wake sheds alternating vortices, the two +// rows 0.281 of their spacing apart, the ratio von Karman showed is stable; well above typical the +// small turbulence takes over. T already runs at the observed rate, so the street sheds faster +// when the firehose does, as shedding frequency follows speed. +vec2 flow(vec2 p){ + float w = R.x / D, a = clamp(w * .1, 105., 175.), h = a * .281, core = a * .15, ph = fract(T * .02); + float g = core * 2.6 * smoothstep(.3, .9, Q) * (.7 + .3 * min(Q, 2.)); + vec2 v = vec2(1., 0.); + for (int i = -1; i < 11; i++) { + float f = float(i) + ph, x = C.x + 30. + f * a; + float s1 = smoothstep(-.6, 1.2, f) * (1. - .7 * smoothstep(7., 10.5, f)), s2 = smoothstep(-.1, 1.7, f) * (1. - .7 * smoothstep(6.5, 10., f)); + v += vort(p, vec2(x, C.y - h * .5), -g * s1, core); + v += vort(p, vec2(x + a * .5, C.y + h * .5), g * s2, core); + } + float q = min(Q, 2.); + return v + curl(p) * (6. + 16. * q * q); +} +// thin separate dabs with flat edges, laid along direction d: x is the stroke, y a rarer second family +vec2 strokes(vec2 p, vec2 d){ + vec2 drift = d * T * 5.; float a = 0., b = 0.; + for (int i = -7; i <= 7; i++) { + vec2 q = (p + d * float(i) * 2.1 - drift) / 2.05; + a += smoothstep(.73, .80, vn(q)); b += smoothstep(.76, .82, vn(q + 71.3)); + } + return vec2(smoothstep(.19, .25, a / 15.), smoothstep(.2, .26, b / 15.)); +} +void main(){ + vec2 px = vec2(gl_FragCoord.x, R.y - gl_FragCoord.y), p = px / D; + // matte: the page colour, strokes a shade above it, about one in seven in a muted pigment, no glow + vec3 base = vec3(4., 7., 8.) / 255., light = vec3(18., 27., 32.) / 255.; + vec2 v = flow(p); float speed = length(v); vec2 d = v / max(speed, 1e-6), st = strokes(p, d); + // strokes on one streamline share a pigment: the id comes from the coordinate across the flow + float lane = floor(dot(p, vec2(-d.y, d.x)) / 9.), pick = h21(vec2(lane, 3.1)), which = h21(vec2(lane, 9.7)); + vec3 pigment = which < .35 ? vec3(72., 117., 101.) : which < .70 ? vec3(76., 112., 142.) : which < .90 ? vec3(111., 96., 135.) : which < .97 ? vec3(141., 120., 79.) : vec3(139., 74., 82.); + vec3 tone = pick > .86 ? pigment / 255. * .42 : light; + float k = .62 * (.3 + .7 * E) * column(p.x, R.x / D) * (.55 + .45 * smoothstep(.3, 1.8, speed)); + vec3 col = mix(base, tone, st.x * k); + col = mix(col, light * .8, st.y * k * .5); + col *= .96 + .08 * h21(floor(gl_FragCoord.xy) + 3.7); + col += (ign(gl_FragCoord.xy) - .5) / 255.; + o = vec4(col, 1.); +}`; + + const gl = canvas.getContext('webgl2', { antialias: false, alpha: false, powerPreference: 'low-power' }); + const uniforms = {}; + function compile() { + if (!gl) return false; + const make = (type, source) => { + const shader = gl.createShader(type); gl.shaderSource(shader, source); gl.compileShader(shader); + return gl.getShaderParameter(shader, gl.COMPILE_STATUS) ? shader : null; }; - socket.onclose = () => { socket = null; if (!document.hidden) retry = setTimeout(connect, 15000); }; - socket.onerror = () => socket.close(); + const vertex = make(gl.VERTEX_SHADER, '#version 300 es\nvoid main(){ vec2 v = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2)); gl_Position = vec4(v * 2. - 1., 0., 1.); }'); + const fragment = make(gl.FRAGMENT_SHADER, FRAGMENT); + if (!vertex || !fragment) return false; + const program = gl.createProgram(); gl.attachShader(program, vertex); gl.attachShader(program, fragment); gl.linkProgram(program); + if (!gl.getProgramParameter(program, gl.LINK_STATUS)) return false; + gl.useProgram(program); gl.bindVertexArray(gl.createVertexArray()); + for (const name of ['R', 'D', 'T', 'E', 'Q', 'C']) uniforms[name] = gl.getUniformLocation(program, name); + return true; } + // without WebGL2 the page keeps its plain background, and the river still runs + const drawing = compile(); + + const current = createCurrent(); + const reduced = matchMedia('(prefers-reduced-motion: reduce)'); + const anchor = document.getElementById('seq'); + let scale = 1, frame = 0, last = 0, sent = 0; + 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(); - } + if (!drawing) return; + const still = reduced.matches, box = canvas.getBoundingClientRect(); + const at = anchor ? anchor.getBoundingClientRect() : { left: innerWidth * .5, top: innerHeight * .36, width: 0, height: 0 }; + gl.uniform2f(uniforms.R, canvas.width, canvas.height); + gl.uniform1f(uniforms.D, scale); + gl.uniform1f(uniforms.T, still ? current.to : position(current, now)); + gl.uniform1f(uniforms.E, still ? Math.min(1, current.to) : energy(current, now)); + gl.uniform1f(uniforms.Q, still ? current.toRegime : regime(current, now)); + gl.uniform2f(uniforms.C, at.left + at.width / 2 - box.left, at.top + at.height / 2 - box.top); + gl.drawArrays(gl.TRIANGLES, 0, 3); + } + // the river's packets are the same observed events, one for every RIVER_SHARE of them + function feedRiver(now) { + const due = packets(current, now); + if (due < sent || due - sent > 64) sent = due - Math.min(due, 8); + for (; sent < due; sent++) dispatchEvent(new CustomEvent('stream-arrival', { detail: seedFor(sent) })); } - 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); + if (now - last >= 33) { paint(now); if (!reduced.matches) feedRiver(now); last = now; } + if (!reduced.matches && moving(current, now)) frame = requestAnimationFrame(animate); } 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); + const width = canvas.clientWidth, height = canvas.clientHeight; + scale = surfaceScale(width, height, devicePixelRatio || 1); + canvas.dataset.scale = String(scale); + canvas.width = Math.round(width * scale); canvas.height = Math.round(height * scale); + if (drawing) gl.viewport(0, 0, canvas.width, canvas.height); paint(performance.now()); } addEventListener('stream-sequence', event => { - if (receive(current, BigInt(event.detail), performance.now(), BigInt(entropy))) wake(); + const typical = typeof window.__typicalRate === 'function' ? window.__typicalRate() : TYPICAL_RATE; + if (receive(current, BigInt(event.detail), performance.now(), typical)) wake(); }); - addEventListener('resize', resize); new ResizeObserver(resize).observe(canvas); - document.addEventListener('visibilitychange', () => { - if (document.hidden) { clearTimeout(retry); if (socket) socket.close(); } - else { connect(); wake(); } - }); + // the street is anchored to the sequence number, so a still surface follows it when the page scrolls + addEventListener('scroll', () => { if (!frame) paint(performance.now()); }, { passive: true }); + document.addEventListener('visibilitychange', () => { if (!document.hidden) wake(); }); reduced.addEventListener('change', wake); - resize(); connect(); + resize(); 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'}); + 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,'')})); + if (match) dispatchEvent(new CustomEvent('stream-sequence', { detail: match[1].replace(/,/g, '') })); } catch {} } - observe(); setInterval(observe,5000); + observe(); setInterval(observe, 5000); } })(); diff --git a/deploy/site/brand.css b/deploy/site/brand.css index 2e8cf0a..d128d6b 100644 --- a/deploy/site/brand.css +++ b/deploy/site/brand.css @@ -18,7 +18,6 @@ footer a[aria-current=page]{color:var(--hot);text-decoration-color:var(--water)} .sect{font-size:11px;letter-spacing:.2em;text-transform:uppercase;color:var(--dim);text-align:center} #atmosphere{position:fixed;inset:0;overflow:hidden;z-index:-1;pointer-events:none;background:var(--bg)} #depth{position:absolute;left:-12vw;top:-20lvh;width:124vw;height:140lvh} -body::after{content:none} .report{width:100%;max-width:66ch;min-width:0} .report section{padding:18px 0;border-bottom:1px solid var(--faint)} .report h2{font-size:12px;font-weight:500;color:var(--water);margin:0 0 12px} diff --git a/deploy/site/index.html b/deploy/site/index.html index 4bf9b1c..881c25a 100644 --- a/deploy/site/index.html +++ b/deploy/site/index.html @@ -67,7 +67,7 @@ footer .icon{width:11px;height:11px;vertical-align:-1px;margin-right:.6ch} - + + +