import {useEffect, useMemo, useRef} from 'react'; import type {PointerEvent, MouseEvent} from 'react'; import type {SegmentView, Span} from './contract.ts'; import type {ReviewClock} from './clock.ts'; import {actorStyle, KIND_LABELS, shapePath} from './visual.ts'; import './motion-timelines.css'; type MotionClass = 'moving' | 'slow' | 'still'; type MotionRun = {start: number; end: number; cls: MotionClass}; export function motionRuns(t_s: number[], speed: number[], moving = 0.35, still = 0.01): MotionRun[] { const runs: MotionRun[] = []; for (let i = 0; i < t_s.length - 1 && i < speed.length; i++) { const cls = speed[i] >= moving ? 'moving' : speed[i] <= still ? 'still' : 'slow'; const previous = runs[runs.length - 1]; if (previous?.cls === cls) previous.end = t_s[i + 1]; else runs.push({start: t_s[i], end: t_s[i + 1], cls}); } return runs; } export function speedPoints(t_s: number[], speed: number[]): [number, number][] { return t_s.map((t, i) => [t, speed[i]]); } export function MotionTimelines({view, clock, visible, range, setRange, onRange, highlight}: { view: SegmentView; clock: ReviewClock; visible: Set; range: Span | null; setRange: (range: Span) => void; onRange: (range: Span) => void; highlight?: Span | null; }) { const root = useRef(null), span = clock.end - clock.start; const selecting = useRef<{x: number; rect: DOMRect; start: number} | null>(null), suppressClick = useRef(false); const tracks = useMemo(() => view.actors.filter(a => visible.has(a.id)).map(actor => { const track = view.tracks[actor.id]; return {actor, style: actorStyle(actor), runs: motionRuns(track.t_s, track.speed_mps), points: speedPoints(track.t_s, track.speed_mps)}; }), [view, visible]); let maximum = 1; for (const track of tracks) for (const [, speed] of track.points) maximum = Math.max(maximum, speed); const ymax = Math.ceil(maximum / 0.5) * 0.5; const x = (time: number) => (time - clock.start) / span * 100; const y = (speed: number) => 160 * (1 - speed / ymax); useEffect(() => clock.subscribe(({time}) => { root.current?.querySelectorAll('[data-motion-cursor]').forEach(node => {node.style.left = `${(time - clock.start) / span * 100}%`;}); }), [clock, span]); const seek = (event: MouseEvent) => { if (suppressClick.current) {suppressClick.current = false; return;} const rect = event.currentTarget.getBoundingClientRect(); if (!Number.isFinite(rect.width) || rect.width <= 0) return; const time = clock.start + (event.clientX - rect.left) / rect.width * span; if (!Number.isFinite(time)) return; clock.setPlaying(false); clock.seek(time); }; const endTime = (event: PointerEvent, rect: DOMRect) => clock.start + Math.max(0, Math.min(1, (event.clientX - rect.left) / rect.width)) * span; const interaction = { onClick: seek, onPointerDown: (e: PointerEvent) => {const rect = e.currentTarget.getBoundingClientRect(); if (rect.width <= 0) return; clock.setPlaying(false); selecting.current = {x: e.clientX, rect, start: endTime(e, rect)}; e.currentTarget.setPointerCapture(e.pointerId);}, onPointerMove: (e: PointerEvent) => {const start = selecting.current; if (!start || Math.abs(e.clientX - start.x) < 3) return; const end = endTime(e, start.rect); setRange([Math.min(start.start, end), Math.max(start.start, end)]);}, onPointerUp: (e: PointerEvent) => {const start = selecting.current; selecting.current = null; if (!start || Math.abs(e.clientX - start.x) < 3) return; const end = endTime(e, start.rect); const selected: Span = [Math.min(start.start, end), Math.max(start.start, end)]; suppressClick.current = true; setRange(selected); onRange(selected);}, onPointerCancel: () => {selecting.current = null;}, onKeyDown: (e: React.KeyboardEvent) => {if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {e.preventDefault(); clock.setPlaying(false); clock.step(e.key === 'ArrowRight' ? 1 : -1);}}, }; const overlays = () => <>
{highlight &&
} {range &&
}
{Array.from({length: 7}, (_, i) => {(clock.start + i / 6 * span).toFixed(0)}s)}
; return

运动与速度

运动 / 停止

实色 运动(≥0.35 m/s)· 半透明 慢移 · 淡色 停止(≤0.01 m/s)

{tracks.map(({actor, style}) =>
{actor.id} {KIND_LABELS[actor.kind] || actor.kind}
)}
{tracks.map(({actor, style, runs}) =>
{runs.map((run, i) =>
)}
)} {overlays()}

速度 m/s

{Array.from({length: ymax / 0.5 + 1}, (_, i) => i * 0.5).map(value => {value.toFixed(1)})}
{Array.from({length: ymax / 0.5 + 1}, (_, i) => i * 0.5).map(value => )} {tracks.map(({actor, style, points}) => `${x(time) * 10},${y(speed)}`).join(' ')} fill="none" stroke={style.color} strokeWidth="1.5" strokeDasharray={actor.kind.startsWith('observer') ? '4 3' : undefined} vectorEffect="non-scaling-stroke">{`${actor.id} · ${KIND_LABELS[actor.kind] || actor.kind}`})} 运动门槛 0.35 {overlays()}
; }