Something went wrong. Try again.
doll.systems community website
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180import { 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<HTMLElement>('.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<HTMLElement>('.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<boolean>).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());}