Straight-line distance between nursery coordinates.
+
+ Shortest water route around Moorea between nurseries, alongside direct straight-line
+ distance. Routes are an estimate and not the actual path swum.
+
Most-caught sharks
diff --git a/src/routes/sharks/[id]/+page.server.ts b/src/routes/sharks/[id]/+page.server.ts
index 85d8bec..0a2fac4 100644
--- a/src/routes/sharks/[id]/+page.server.ts
+++ b/src/routes/sharks/[id]/+page.server.ts
@@ -1,5 +1,5 @@
import { error } from '@sveltejs/kit';
-import { dataset } from '#lib/server/data';
+import { dataset, nurseryRoute } from '#lib/server/data';
import { fulton, round, siteName } from '#lib/server/analytics/common';
import type { PageServerLoad } from './$types';
@@ -43,13 +43,15 @@ export const load: PageServerLoad = ({ params }) => {
relMass: l.relMassKg,
notes: l.notes
})),
- // Consecutive captures at different nurseries, in order (#173).
+ // Consecutive captures at different nurseries, in order (#173, #183).
moves: caps.flatMap((c, i) => {
const prev = caps[i - 1];
if (!prev || !prev.siteId || !c.siteId || prev.siteId === c.siteId) return [];
const a = d.siteById.get(prev.siteId);
const b = d.siteById.get(c.siteId);
if (a?.lat == null || a.lon == null || b?.lat == null || b.lon == null) return [];
+ const route = nurseryRoute(d, prev.siteId, c.siteId);
+ const km = route?.km;
return [
{
id: `move-${i}`,
@@ -57,7 +59,9 @@ export const load: PageServerLoad = ({ params }) => {
label: `${a.name} → ${b.name}`,
from: { lat: a.lat, lon: a.lon },
to: { lat: b.lat, lon: b.lon },
- sub: `Capture ${i} to ${i + 1}: ${prev.date ?? '?'} → ${c.date ?? '?'}`
+ path: route?.coordinates,
+ km: km != null ? round(km, 1) : null,
+ sub: `Capture ${i} to ${i + 1}: ${prev.date ?? '?'} → ${c.date ?? '?'}${km != null ? ` · ≥ ${round(km, 1)} km by water` : ''}`
}
];
}),
diff --git a/src/routes/sharks/[id]/+page.svelte b/src/routes/sharks/[id]/+page.svelte
index aac08d8..48b2698 100644
--- a/src/routes/sharks/[id]/+page.svelte
+++ b/src/routes/sharks/[id]/+page.svelte
@@ -3,6 +3,7 @@
import DataTable from '#lib/components/DataTable.svelte';
import InteractiveMap from '#lib/components/InteractiveMap.svelte';
import MooreaMap from '#lib/components/MooreaMap.svelte';
+ import { moveDuration } from '#lib/components/lineGeometry';
import LineChart from '#lib/components/charts/LineChart.svelte';
import { toDay } from '#lib/components/charts/scale';
import { clock, date, num } from '#lib/format';
@@ -13,35 +14,100 @@
const ind = $derived(data.ind);
const growth = $derived(data.captures.filter((c) => c.date && c.tl != null));
- // Play this shark's captures in order (#173): lines appear as it moves, a dot glides along.
+ // Play this shark's captures in order (#173, #183): continuous slider scrubbing and animated water routes.
const stops = $derived(data.track.length);
- let step = $state(Number.MAX_SAFE_INTEGER);
- const at = $derived(Math.min(step, stops));
- const current = $derived(at < stops ? data.track[at] : null);
- // An unlocated capture breaks the path: do not animate across that unknown stop.
- const previous = $derived(
- current && at > 0 && data.track[at - 1].n === current.n - 1 ? data.track[at - 1] : null
- );
+ const totalMoves = $derived(Math.max(1, stops - 1));
+ let progress = $state(0);
+ let playing = $state(false);
+ let showAll = $state(true);
+
+ // Which leg of the track is active (0 to totalMoves - 1):
+ const activeLeg = $derived(Math.min(totalMoves - 1, Math.floor(progress)));
+ const legFraction = $derived(progress >= totalMoves ? 1 : Math.max(0, progress - activeLeg));
+ const fromStop = $derived(data.track[activeLeg] ?? data.track[0]);
+ const toStop = $derived(data.track[activeLeg + 1] ?? data.track[stops - 1]);
+ // The move between consecutive captures (m.at is the destination capture index in +page.server.ts):
+ const activeMove = $derived(data.moves.find((m) => m.at === activeLeg + 1));
const color = $derived(speciesColor(ind.species));
+
const shownMoves = $derived(
- current ? data.moves.filter((m) => m.at + 1 <= current.n) : data.moves
+ showAll ? data.moves : data.moves.filter((m) => m.at <= activeLeg + 1)
);
- let timer: ReturnType | undefined;
- let playing = $state(false);
+ const moveLayers = $derived([{ name: 'Moves', color, lines: shownMoves }]);
+
function stop() {
- clearInterval(timer);
playing = false;
}
+
function play() {
if (playing) return stop();
- if (at >= stops - 1) step = 0;
+ if (showAll || progress >= totalMoves - 0.001) {
+ progress = 0;
+ }
+ showAll = false;
playing = true;
- timer = setInterval(() => {
- if (step >= stops - 1) stop();
- else step = Math.min(step, stops) + 1;
- }, 1400);
}
- $effect(() => stop);
+
+ function jumpTo(idx: number) {
+ stop();
+ showAll = false;
+ progress = idx;
+ }
+
+ function toggleAll() {
+ stop();
+ showAll = true;
+ progress = totalMoves;
+ }
+
+ $effect(() => {
+ // Default to All moves overview when opening or switching sharks (#183).
+ const _id = data.ind.id;
+ stop();
+ showAll = Boolean(data.moves.length);
+ progress = data.moves.length ? totalMoves : 0;
+ });
+
+ $effect(() => {
+ if (!playing) return;
+ let lastTime: number | null = null;
+ let raf: number;
+ const tick = (now: number) => {
+ if (!playing) return;
+ if (lastTime == null) {
+ lastTime = now;
+ raf = requestAnimationFrame(tick);
+ return;
+ }
+ const dt = now - lastTime;
+ lastTime = now;
+
+ const leg = Math.min(totalMoves - 1, Math.floor(progress));
+ const move = data.moves.find((m) => m.at === leg + 1);
+ const duration = moveDuration(move?.km);
+ const next = progress + dt / Math.max(800, duration);
+
+ if (next >= totalMoves) {
+ progress = totalMoves;
+ playing = false;
+ return;
+ }
+ progress = next;
+ raf = requestAnimationFrame(tick);
+ };
+ raf = requestAnimationFrame(tick);
+ return () => cancelAnimationFrame(raf);
+ });
+
+ const sliderAriaText = $derived(
+ showAll
+ ? 'All captures'
+ : activeMove && legFraction > 0.01 && legFraction < 0.99
+ ? `Swimming from ${fromStop.site} to ${toStop.site}, ${Math.round(legFraction * 100)}%`
+ : progress >= 0.99
+ ? `Capture ${toStop.n}: ${toStop.site}, ${date(toStop.date)}`
+ : `Capture 1: ${data.track[0].site}, ${date(data.track[0].date)}`
+ );