staging-org / ocean-landing-serve / src/ lib/ AsciiOcean.svelte

<script lang="ts">
	import {
		beacon,
		birds,
		bubbles,
		clouds,
		coastGrid,
		layout,
		moon,
		rippleLife,
		sailboat,
		seaGrid,
		stars,
		sun,
		waveLine,
		type Glyph,
		type Ripple
	} from './ocean-scene';

	type Props = { contentPx?: number; class?: string };

	let { contentPx = 0, class: className = '' }: Props = $props();

	const cellW = 8;
	const cellH = 15;
	const uid = $props.id();

	let width = $state(0);
	let height = $state(0);
	let t = $state(3);
	let ripples = $state<Ripple[]>([]);
	let hour = $state<number | null>(null);
	let still = $state(true);
	let lastWake = 0;

	const l = $derived.by(() => {
		const cols = Math.ceil(width / cellW) + 1;
		const rows = Math.ceil(height / cellH) + 1;
		const skyTop = Math.ceil(contentPx / cellH) + 1;
		const surface = Math.min(rows - 8, Math.max(Math.round(rows * 0.45), skyTop + 7));
		return layout(cols, rows, surface, skyTop);
	});

	const coast = $derived(coastGrid(l));
	const sea = $derived(seaGrid(t, l, ripples));
	const wave = $derived(waveLine(t, l, ripples));
	const heavens = $derived([...stars(t, l, hour), ...sun(l, hour), ...moon(l, hour)]);
	const cloudCover = $derived(clouds(t, l));
	const flock = $derived([...birds(t, l), ...sailboat(t, l)]);
	const air = $derived(bubbles(t, l));
	const lamp = $derived(beacon(t, l));

	// The local hour is read after mount so the server render and the first client render agree.
	$effect(() => {
		const read = () => {
			const now = new Date();
			hour = now.getHours() + now.getMinutes() / 60;
		};
		read();
		const timer = setInterval(read, 60_000);
		return () => clearInterval(timer);
	});

	// A pointer over the water leaves a wake and a press sends out a ring; both stay off under reduced motion.
	const disturb = (e: PointerEvent, strength: number) => {
		if (still) return;
		const box = (e.currentTarget as HTMLElement).getBoundingClientRect();
		const x = (e.clientX - box.left) / cellW;
		const y = (e.clientY - box.top) / cellH;
		if (y < l.surface || x > l.coast) return;
		ripples = [...ripples.filter((r) => t - r.at < rippleLife), { x, y, at: t, strength }];
	};

	$effect(() => {
		if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
		still = false;
		let last = performance.now();
		let id = requestAnimationFrame(function tick(now) {
			t += Math.min(now - last, 100) / 1000;
			last = now;
			id = requestAnimationFrame(tick);
		});
		return () => cancelAnimationFrame(id);
	});
</script>

<div
	bind:clientWidth={width}
	bind:clientHeight={height}
	class={['overflow-hidden', className]}
	aria-hidden="true"
	onpointermove={(e) => {
		if (t - lastWake < 0.12) return;
		lastWake = t;
		disturb(e, 0.45);
	}}
	onpointerdown={(e) => disturb(e, 1.3)}
>
	{#if width > 0 && height > 0}
		<svg {width} {height} viewBox="0 0 {width} {height}" class="type-ascii block">
			<defs>
				<linearGradient
					id="{uid}-depth"
					gradientUnits="userSpaceOnUse"
					x1="0"
					y1={(l.surface + 1) * cellH}
					x2="0"
					y2={l.rows * cellH}
				>
					<stop offset="0%" stop-color="#7dd3fc" />
					<stop offset="45%" stop-color="#3b82f6" />
					<stop offset="100%" stop-color="#1e3a8a" />
				</linearGradient>
				<clipPath id="{uid}-surf"><rect width={l.coast * cellW} height={height} /></clipPath>
			</defs>

			{#snippet glyphs(items: Glyph[], cls: string)}
				<g class={cls}>
					{#each items as g, i (i)}
						<text
							x={g.x * cellW}
							y={(g.y + 1) * cellH}
							textLength={g.ch.length > 1 ? g.ch.length * cellW : undefined}
							lengthAdjust="spacing"
							xml:space="preserve">{g.ch}</text
						>
					{/each}
				</g>
			{/snippet}

			{@render glyphs(heavens, 'fill-sky-300')}
			<!-- A background-colored outline lets clouds pass in front of the sun and moon instead of mixing glyphs with them. -->
			{@render glyphs(cloudCover, 'fill-sky-300 stroke-background [paint-order:stroke] [stroke-width:6px] [stroke-linejoin:round]')}
			{@render glyphs(flock, 'fill-blue-400')}

			<g fill="url(#{uid}-depth)">
				{#each sea as line, i (i)}
					<text
						x="0"
						y={(l.surface + 2 + i) * cellH}
						textLength={l.cols * cellW}
						lengthAdjust="spacing"
						xml:space="preserve">{line}</text
					>
				{/each}
			</g>

			{@render glyphs(air, 'fill-sky-400')}

			<g clip-path="url(#{uid}-surf)" class="fill-blue-500">
				<text
					x="0"
					y={(l.surface + 1) * cellH}
					textLength={wave.line.length * cellW}
					lengthAdjust="spacing"
					xml:space="preserve"
					transform="translate({wave.offset * cellW} 0)">{wave.line}</text
				>
			</g>

			<g class="fill-blue-800">
				{#each coast as line, y (y)}
					{#if line.trim()}
						<text x="0" y={(y + 1) * cellH} textLength={l.cols * cellW} lengthAdjust="spacing" xml:space="preserve"
							>{line}</text
						>
					{/if}
				{/each}
			</g>
			{@render glyphs(lamp, 'fill-sky-500')}
		</svg>
	{/if}
</div>