import { webgl } from './webgl'; import type { PageScope } from './page-scope'; import { pixelSize as grid } from './pixel-grid'; import vertex from './dust.vert.glsl?raw'; import fragment from './dust.frag.glsl?raw'; const capacity = 8192, stride = 12, maskScale = 2, sparkOpacity = 10, sparkKind = 11; const dust = { minimum: 700, areaPerParticle: 350 }; const charge = { length: 9, duration: 180, growth: 35 }; const random = (seed: number) => { const value = Math.sin(seed * 127.1 + 311.7) * 43758.5453; return value - Math.floor(value); }; type Spark = { start: number; points: { x: number; y: number }[] }; type Rect = { left: number; top: number; width: number; height: number }; type Region = { element: HTMLElement; lines?: Rect[] }; export function createDisplayNoise(schedule: () => void, scope: PageScope) { const element = document.querySelector('.display-noise'); if (!element) return; const canvas = element; const gpu = webgl(canvas, scope, vertex, fragment); if (!gpu) return; const { gl, set } = gpu; gl.bufferData( gl.ARRAY_BUFFER, (capacity + charge.length) * stride * Float32Array.BYTES_PER_ELEMENT, gl.DYNAMIC_DRAW, ); ['origin', 'motion', 'variation'].forEach((name, index) => gpu.attribute(name, 4, stride, index * 4), ); gl.enable(gl.BLEND); gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); set({ grid }); const mask = document.createElement('canvas'), maskContext = mask.getContext('2d')!; const maskTexture = gpu.texture('readingMask'); const regions: Region[] = [ ...document.querySelectorAll( '[data-reading-content], .site-header, .graph-panel, .hover-preview', ), ].map((element) => ({ element })); const artworks = [ ...document.querySelectorAll('.layout-art .dither, .art-echo .dither'), ]; const artBounds = new Float32Array(8); // Main artwork and the narrow-screen echo. let needsLayout = true, needsMask = true, count = dust.minimum, spark: Spark | undefined; const sparkVertices = new Float32Array(charge.length * stride); function seedDust(seed: number) { const vertices = new Float32Array(capacity * stride); for (let i = 0; i < capacity; i++) { const id = seed + i * 13, offset = i * stride; vertices[offset] = random(id); vertices[offset + 1] = random(id + 1); vertices[offset + 2] = 1.6 + random(id + 2) * 5.6; vertices[offset + 3] = (random(id + 3) - 0.5) * 0.96; vertices[offset + 4] = 8 + random(id + 4) * 20; vertices[offset + 5] = 8 + random(id + 5) * 16; vertices[offset + 6] = random(id + 6) * 100; vertices[offset + 7] = 0.7 + random(id + 7) * 5.6; vertices[offset + 8] = 0.12 + random(id + 8) * 0.26; vertices[offset + 9] = id; } gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices); } function collectLines() { for (const region of regions) { if (!region.element.hasAttribute('data-reading-content')) continue; const bounds = region.element.getBoundingClientRect(); const walker = document.createTreeWalker(region.element, NodeFilter.SHOW_TEXT); const range = document.createRange(); region.lines = []; while (walker.nextNode()) { const node = walker.currentNode; if (!node.textContent?.trim() || node.parentElement?.closest('script, style')) continue; range.selectNodeContents(node); for (const rect of range.getClientRects()) region.lines.push({ left: rect.left - bounds.left - 1, top: rect.top - bounds.top - 1, width: rect.width + 2, height: rect.height + 2, }); } } needsLayout = false; } function updateMask() { if (needsLayout) collectLines(); maskContext.fillStyle = '#fff'; maskContext.fillRect(0, 0, mask.width, mask.height); maskContext.fillStyle = '#242424'; for (const { element, lines } of regions) { if (getComputedStyle(element).visibility === 'hidden') continue; const bounds = element.getBoundingClientRect(); for (const rect of lines || [ { left: 0, top: 0, width: bounds.width, height: bounds.height }, ]) { const top = bounds.top + rect.top; if (top > innerHeight || top + rect.height < 0) continue; maskContext.fillRect( (bounds.left + rect.left) / maskScale, top / maskScale, rect.width / maskScale, rect.height / maskScale, ); } } maskTexture(mask, { update: true }); needsMask = false; } function updateArtBounds() { let count = 0; for (const artwork of artworks) { const bounds = artwork.getBoundingClientRect(); if (!bounds.width || !bounds.height || count === 2) continue; artBounds.set([bounds.left, bounds.top, bounds.right, bounds.bottom], count++ * 4); } set({ artCount: count, 'artBounds[0]': artBounds }); } const surface = { dirty: true, visible: true, ready: true, charge(target: Element, time: number) { const core = target.querySelector('.node-hit-area'), box = (core || target).getBoundingClientRect(); const direction = Math.random() < 0.5 ? -1 : 1; let x = (core ? box.left + box.width / 2 : box.left + box.width * 0.7) / grid; let y = (core ? box.top + box.height / 2 : box.top - grid) / grid; const seed = Math.random() * 10000; spark = { start: time, points: Array.from({ length: charge.length }, (_, step) => { x += direction * 2; y += random(seed + step) > 0.45 ? -1 : 0; return { x, y }; }), }; surface.dirty = true; schedule(); }, clearCharge() { spark = undefined; surface.dirty = true; }, draw(time: number) { if (needsMask) { updateArtBounds(); updateMask(); } gl.clear(gl.COLOR_BUFFER_BIT); set({ time: time / 1000 }); gl.drawArrays(gl.POINTS, 0, count); if (spark) { const age = (time - spark.start) / charge.duration; if (age >= 1) spark = undefined; else { const visible = Math.min(charge.length, Math.ceil(age * charge.growth) + 1); for (let i = 0; i < visible; i++) { const offset = i * stride; sparkVertices.set([spark.points[i].x, spark.points[i].y], offset); sparkVertices[offset + sparkOpacity] = (1 - age) * (i === visible - 1 ? 0.95 : 0.5); sparkVertices[offset + sparkKind] = i === 7 ? 2 : 1; } gl.bufferSubData( gl.ARRAY_BUFFER, capacity * stride * Float32Array.BYTES_PER_ELEMENT, sparkVertices, ); gl.drawArrays(gl.POINTS, capacity, visible); } } }, }; function measure(layout = false) { needsLayout ||= layout; needsMask = true; surface.dirty = true; schedule(); } function resize() { canvas.width = Math.ceil(innerWidth / grid); canvas.height = Math.ceil(innerHeight / grid); canvas.style.width = `${canvas.width * grid}px`; canvas.style.height = `${canvas.height * grid}px`; mask.width = Math.ceil(innerWidth / maskScale); mask.height = Math.ceil(innerHeight / maskScale); // Allocate on resize; scrolling updates existing storage. maskTexture({ width: mask.width, height: mask.height, pixels: null }); count = Math.min( capacity, Math.max(dust.minimum, Math.floor((innerWidth * innerHeight) / dust.areaPerParticle)), ); canvas.dataset.particles = String(count); gl.viewport(0, 0, canvas.width, canvas.height); set({ resolution: [canvas.width, canvas.height] }); measure(true); } const observer = scope.observe(new ResizeObserver(() => measure(true))); regions.forEach(({ element }) => observer.observe(element)); artworks.forEach((element) => observer.observe(element)); document.fonts.ready.then(() => { if (!scope.signal.aborted) measure(true); }); scope.on(document, 'preview:change', () => measure()); scope.on(document, 'reading:change', () => measure(true)); scope.on(window, 'resize', resize, { passive: true }); scope.on(window, 'scroll', () => measure(), { passive: true, capture: true }); scope.on(canvas, 'webglcontextlost', (event) => { event.preventDefault(); surface.ready = false; }); seedDust(1); resize(); return surface; }