Elffuss BraidHub

kikocis/osiris public

master · 2.0 KiBRawPermalink
1'use client';
2
3import { useMemo } from 'react';
4
5/* ═══════════════════════════════════════════════════════════════
6 OSIRIS — Scale Bar
7 Dynamic map scale indicator
8 ═══════════════════════════════════════════════════════════════ */
9
10interface ScaleBarProps {
11 zoom: number;
12 latitude: number;
13}
14
15const SCALE_STEPS = [5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1];
16
17export default function ScaleBar({ zoom, latitude }: ScaleBarProps) {
18 const scaleInfo = useMemo(() => {
19 // Meters per pixel at given zoom and latitude
20 const metersPerPx = 156543.03392 * Math.cos(latitude * Math.PI / 180) / Math.pow(2, zoom);
21 const maxWidth = 120; // Max bar width in pixels
22 const maxMeters = metersPerPx * maxWidth;
23 const maxKm = maxMeters / 1000;
24
25 // Find the best scale step
26 let bestStep = SCALE_STEPS[0];
27 for (const step of SCALE_STEPS) {
28 if (step <= maxKm) { bestStep = step; break; }
29 }
30
31 const barWidth = Math.round((bestStep * 1000) / metersPerPx);
32 const label = bestStep >= 1 ? `${bestStep} km` : `${bestStep * 1000} m`;
33
34 return { barWidth, label };
35 }, [zoom, latitude]);
36
37 return (
38 <div className="flex items-end gap-2 pointer-events-none">
39 <div className="flex flex-col items-start">
40 <span className="text-[7px] font-mono text-[var(--text-muted)] tracking-wider mb-0.5">
41 {scaleInfo.label}
42 </span>
43 <div
44 className="h-[2px] bg-[var(--gold-primary)] rounded-full"
45 style={{ width: scaleInfo.barWidth }}
46 />
47 <div className="flex justify-between w-full">
48 <div className="w-[1px] h-1 bg-[var(--gold-primary)]" />
49 <div className="w-[1px] h-1 bg-[var(--gold-primary)]" />
50 </div>
51 </div>
52 </div>
53 );
54}