import { cssNumber } from './pixel-grid'; import { between, ease } from '../lib/math'; import type { PageScope } from './page-scope'; import { createHighlightGlitches } from './highlight-glitches'; import { motionPaused } from './motion'; import { createDisplayNoise } from './display-noise'; import { createArtSurface } from './art-surface'; import { inViewport, type RenderSurface, type ArtEffects } from './render-surface'; const raster = { initial: [6000, 14000], interval: [8000, 22000], duration: [30, 65], gap: [30, 85], strength: [0.6, 1], count: [2, 5], seedRange: 1000, } as const; const ascii = { arrival: 240, arriveEase: 45, focusEase: 120, epsilon: 0.002 }; const charge = { interval: [12000, 22000], probability: 0.4 } as const; const maxFrameDelta = 100; export function mountEffects(scope: PageScope) { const surfaces: RenderSurface[] = []; const updateHighlights = createHighlightGlitches(scope); let frame = 0, lastTick = performance.now(); scope.defer(() => cancelAnimationFrame(frame)); let elapsed = 0; type RasterSlip = { start: number; duration: number; seed: number; strength: number }; let rasterBurst: RasterSlip[] = []; let nextBurstAt = between(...raster.initial); const arriving = document.documentElement.hasAttribute('data-astro-transition') && !motionPaused(); const effects: ArtEffects = { raster: 0, rasterSeed: 1, ascii: arriving ? 1 : 0 }; const graphs = [...document.querySelectorAll('.link-graph')]; let graphHovered = graphs.some((graph) => graph.matches(':hover')); let previewFocused = false, departingUntil = 0, nextChargeAt = 0; function interruptRaster() { let start = elapsed; rasterBurst = Array.from({ length: Math.floor(between(...raster.count)) }, () => { const slip = { start, duration: between(...raster.duration), seed: Math.random() * raster.seedRange, strength: between(...raster.strength), }; start += slip.duration + between(...raster.gap); return slip; }); nextBurstAt = start + between(...raster.interval); } function schedule() { if (scope.signal.aborted || frame || document.hidden) return; // Follow display refresh directly; a timer before rAF skips available frames. frame = requestAnimationFrame(tick); } function tick(now: number) { frame = 0; const moving = !motionPaused(); const delta = Math.min(maxFrameDelta, now - lastTick); if (moving) elapsed += delta; lastTick = now; updateHighlights(elapsed, moving); if (moving && elapsed >= nextBurstAt) interruptRaster(); const slip = moving ? rasterBurst.find((slip) => elapsed >= slip.start && elapsed < slip.start + slip.duration) : undefined; if (slip) effects.rasterSeed = slip.seed; effects.raster = slip ? slip.strength * (1 - (elapsed - slip.start) / slip.duration) : 0; const asciiTarget = graphHovered || previewFocused || (moving && elapsed < departingUntil) ? 1 : 0; effects.ascii = moving ? effects.ascii + (asciiTarget - effects.ascii) * ease(delta, arriving && elapsed < ascii.arrival ? ascii.arriveEase : ascii.focusEase) : asciiTarget; if (Math.abs(asciiTarget - effects.ascii) < ascii.epsilon) effects.ascii = asciiTarget; let animate = false; for (const surface of surfaces) { if (!surface.visible || !surface.ready) continue; if (surface.dirty || moving) { surface.draw(elapsed); surface.dirty = false; } animate ||= moving; } if (animate) schedule(); } const pending = new Set(document.querySelectorAll('.dither')); function startSurface(element: HTMLElement) { if (!pending.has(element)) return; const bounds = element.getBoundingClientRect(); if (!inViewport(bounds)) return; pending.delete(element); lazySurfaces.unobserve(element); const surface = createArtSurface( element, scope, { get time() { return elapsed; }, schedule, }, effects, ); if (surface) surfaces.push(surface); } const lazySurfaces = scope.intersect(pending, (entry) => { if (entry.intersectionRatio > 0) startSurface(entry.target as HTMLElement); }); pending.forEach(startSurface); const displayNoise = createDisplayNoise(schedule, scope); if (displayNoise) surfaces.push(displayNoise); function updateInteraction() { surfaces.forEach((surface) => (surface.dirty = true)); schedule(); } for (const graph of graphs) for (const type of ['pointerenter', 'pointerleave'] as const) scope.on(graph, type, (event) => { if (event.pointerType === 'touch') return; graphHovered = graphs.some((graph) => graph.matches(':hover')); updateInteraction(); }); scope.on(document, 'preview:change', (event) => { previewFocused = (event as CustomEvent).detail; updateInteraction(); }); scope.on(document, 'astro:before-preparation', (event) => { if (motionPaused()) return; departingUntil = elapsed + cssNumber('--transition-duration'); schedule(); scope.on( event.signal, 'abort', () => { departingUntil = 0; }, { once: true }, ); }); function chargeOnInteraction(event: Event) { if (motionPaused() || elapsed < nextChargeAt) return; const target = (event.target as Element).closest('.graph-node, button'); if (!target || (event instanceof PointerEvent && target.contains(event.relatedTarget as Node))) return; nextChargeAt = elapsed + between(...charge.interval); if (Math.random() > charge.probability) return; displayNoise?.charge(target, elapsed); } scope.on(document, 'pointerover', chargeOnInteraction); scope.on(document, 'focusin', chargeOnInteraction); scope.on(document, 'visibilitychange', () => { if (document.hidden) { cancelAnimationFrame(frame); frame = 0; } else { lastTick = performance.now(); schedule(); } }); scope.on(document, 'motion:change', () => { lastTick = performance.now(); rasterBurst = []; nextBurstAt = elapsed + between(...raster.interval); departingUntil = 0; displayNoise?.clearCharge(); surfaces.forEach((surface) => (surface.dirty = true)); schedule(); }); // Paint cached artwork during Astro's swap, before the transition snapshot. cancelAnimationFrame(frame); tick(performance.now()); }