@sybilion/uilib 1.4.37 → 1.4.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-glossary/workspace.generated.ts +1 -0
- package/dist/agent-glossary/workspace.index.md +1 -0
- package/dist/agent-glossary/workspace.md +1 -0
- package/dist/esm/src/components/ui/Banner/Banner.styl.js +1 -1
- package/dist/esm/src/components/ui/Chart/Chart.js +1 -2
- package/dist/esm/src/components/ui/Dialog/Dialog.styl.js +1 -1
- package/dist/esm/src/components/ui/Drawer/Drawer.styl.js +1 -1
- package/dist/esm/src/components/ui/DropdownMenu/DropdownMenu.styl.js +1 -1
- package/dist/esm/src/components/ui/Foldable/Foldable.js +2 -2
- package/dist/esm/src/components/ui/Foldable/Foldable.styl.js +1 -1
- package/dist/esm/src/components/ui/MobileAdaptiveSelector/MobileAdaptiveSelector.styl.js +1 -1
- package/dist/esm/src/components/ui/Page/PageHeader/PageHeader.styl.js +1 -1
- package/dist/esm/src/components/ui/Select/Select.js +313 -5
- package/dist/esm/src/components/ui/Select/Select.styl.js +2 -2
- package/dist/esm/src/components/ui/Sheet/Sheet.styl.js +1 -1
- package/dist/esm/src/components/ui/SupplyChainMap/SupplyChainMap.js +384 -0
- package/dist/esm/src/components/ui/SupplyChainMap/SupplyChainMap.styl.js +7 -0
- package/dist/esm/src/components/ui/SupplyChainMap/map-colors.js +44 -0
- package/dist/esm/src/components/ui/SupplyChainMap/map-disclosure.js +142 -0
- package/dist/esm/src/components/ui/SupplyChainMap/map-labels.js +55 -0
- package/dist/esm/src/components/ui/SupplyChainMap/map-projection.js +186 -0
- package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-clusters.js +96 -0
- package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-draw.js +66 -0
- package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-scene.js +59 -0
- package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-types.js +10 -0
- package/dist/esm/src/components/ui/SupplyChainMap/use-map-base-renderer.js +122 -0
- package/dist/esm/src/components/ui/Table/Table.styl.js +1 -1
- package/dist/esm/src/components/ui/Tabs/Tabs.styl.js +1 -1
- package/dist/esm/src/components/ui/Tooltip/Tooltip.styl.js +1 -1
- package/dist/esm/src/components/widgets/DriverCard/DriverPerformanceChart.js +1 -2
- package/dist/esm/src/index.js +2 -0
- package/dist/esm/types/src/components/ui/Select/Select.d.ts +3 -3
- package/dist/esm/types/src/components/ui/Select/Select.types.d.ts +3 -1
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.contract.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.d.ts +2 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.example.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.render.worker.d.ts +15 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.types.d.ts +10 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/index.d.ts +3 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-colors.d.ts +43 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-disclosure.d.ts +35 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-disclosure.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-labels.d.ts +14 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-labels.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-projection.d.ts +83 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-projection.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-clusters.d.ts +25 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-clusters.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-draw.d.ts +30 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-scene.d.ts +28 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-scene.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-types.d.ts +31 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/use-map-base-renderer.d.ts +14 -0
- package/dist/esm/types/src/docs/pages/BannerPage.d.ts +1 -0
- package/dist/esm/types/src/docs/pages/SupplyChainMapPage.d.ts +1 -0
- package/dist/esm/types/src/index.d.ts +1 -0
- package/dist/esm/types/src/lib/logger.d.ts +1 -0
- package/package.json +7 -1
- package/src/components/ui/Banner/Banner.styl +1 -1
- package/src/components/ui/Dialog/Dialog.styl +2 -2
- package/src/components/ui/Drawer/Drawer.styl +2 -2
- package/src/components/ui/DropdownMenu/DropdownMenu.styl +2 -2
- package/src/components/ui/Foldable/Foldable.styl +1 -1
- package/src/components/ui/Foldable/Foldable.tsx +2 -2
- package/src/components/ui/MobileAdaptiveSelector/MobileAdaptiveSelector.styl +1 -1
- package/src/components/ui/Page/PageHeader/PageHeader.styl +1 -1
- package/src/components/ui/Select/AGENT.md +3 -1
- package/src/components/ui/Select/Select.styl +31 -0
- package/src/components/ui/Select/Select.styl.d.ts +3 -0
- package/src/components/ui/Select/Select.tsx +506 -4
- package/src/components/ui/Select/Select.types.ts +3 -1
- package/src/components/ui/Sheet/Sheet.styl +2 -2
- package/src/components/ui/SupplyChainMap/AGENT.md +45 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.contract.test.ts +45 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.example.tsx +176 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.render.worker.ts +63 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.styl +221 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.styl.d.ts +25 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.tsx +720 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.types.ts +15 -0
- package/src/components/ui/SupplyChainMap/index.ts +11 -0
- package/src/components/ui/SupplyChainMap/map-colors.ts +66 -0
- package/src/components/ui/SupplyChainMap/map-disclosure.test.ts +237 -0
- package/src/components/ui/SupplyChainMap/map-disclosure.ts +226 -0
- package/src/components/ui/SupplyChainMap/map-labels.test.ts +102 -0
- package/src/components/ui/SupplyChainMap/map-labels.ts +85 -0
- package/src/components/ui/SupplyChainMap/map-projection.test.ts +172 -0
- package/src/components/ui/SupplyChainMap/map-projection.ts +281 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-clusters.test.ts +182 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-clusters.ts +127 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-draw.ts +114 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-scene.test.ts +196 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-scene.ts +118 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-types.ts +45 -0
- package/src/components/ui/SupplyChainMap/use-map-base-renderer.ts +145 -0
- package/src/components/ui/Table/Table.styl +3 -3
- package/src/components/ui/Tabs/Tabs.styl +1 -1
- package/src/components/ui/Tooltip/Tooltip.styl +7 -2
- package/src/declarations.d.ts +4 -0
- package/src/docs/pages/BannerPage.tsx +83 -0
- package/src/docs/pages/FeedbackOverviewPage.tsx +7 -1
- package/src/docs/pages/SelectPage.tsx +30 -1
- package/src/docs/pages/SupplyChainMapPage.tsx +21 -0
- package/src/docs/registry.ts +12 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export type MapLabel = {
|
|
2
|
+
id: string;
|
|
3
|
+
lat: number;
|
|
4
|
+
lng: number;
|
|
5
|
+
text: string;
|
|
6
|
+
primary: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type PlacedLabel = MapLabel & { x: number; y: number };
|
|
10
|
+
|
|
11
|
+
type LabelBox = {
|
|
12
|
+
id: string;
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
w: number;
|
|
16
|
+
h: number;
|
|
17
|
+
priority: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const CHAR_PX = 6.2;
|
|
21
|
+
const PAD_PX = 14;
|
|
22
|
+
const LABEL_H = 18;
|
|
23
|
+
|
|
24
|
+
export const LABEL_OFFSET_X = 12;
|
|
25
|
+
export const LABEL_OFFSET_Y = 0;
|
|
26
|
+
|
|
27
|
+
const OFFSCREEN_MARGIN = 60;
|
|
28
|
+
|
|
29
|
+
function overlaps(a: LabelBox, b: LabelBox, padding: number): boolean {
|
|
30
|
+
return (
|
|
31
|
+
a.x < b.x + b.w + padding &&
|
|
32
|
+
a.x + a.w + padding > b.x &&
|
|
33
|
+
a.y < b.y + b.h + padding &&
|
|
34
|
+
a.y + a.h + padding > b.y
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function declutter(boxes: readonly LabelBox[], padding = 2): Set<string> {
|
|
39
|
+
const ordered = [...boxes].sort(
|
|
40
|
+
(a, b) => b.priority - a.priority || (a.id < b.id ? -1 : 1),
|
|
41
|
+
);
|
|
42
|
+
const placed: LabelBox[] = [];
|
|
43
|
+
const kept = new Set<string>();
|
|
44
|
+
for (const box of ordered) {
|
|
45
|
+
if (box.w <= 0 || box.h <= 0) continue;
|
|
46
|
+
if (placed.some(p => overlaps(p, box, padding))) continue;
|
|
47
|
+
placed.push(box);
|
|
48
|
+
kept.add(box.id);
|
|
49
|
+
}
|
|
50
|
+
return kept;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function placeMapLabels(
|
|
54
|
+
labels: readonly MapLabel[],
|
|
55
|
+
project: (lat: number, lng: number) => [number, number] | null,
|
|
56
|
+
width: number,
|
|
57
|
+
height: number,
|
|
58
|
+
): PlacedLabel[] {
|
|
59
|
+
const placed: PlacedLabel[] = [];
|
|
60
|
+
const boxes: LabelBox[] = [];
|
|
61
|
+
for (const [i, label] of labels.entries()) {
|
|
62
|
+
const p = project(label.lat, label.lng);
|
|
63
|
+
if (!p) continue;
|
|
64
|
+
const [x, y] = p;
|
|
65
|
+
if (
|
|
66
|
+
x < -OFFSCREEN_MARGIN ||
|
|
67
|
+
x > width + OFFSCREEN_MARGIN ||
|
|
68
|
+
y < -OFFSCREEN_MARGIN ||
|
|
69
|
+
y > height + OFFSCREEN_MARGIN
|
|
70
|
+
) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
placed.push({ ...label, x, y });
|
|
74
|
+
boxes.push({
|
|
75
|
+
id: label.id,
|
|
76
|
+
x: x + LABEL_OFFSET_X,
|
|
77
|
+
y: y + LABEL_OFFSET_Y - LABEL_H / 2,
|
|
78
|
+
w: PAD_PX + label.text.length * CHAR_PX,
|
|
79
|
+
h: LABEL_H,
|
|
80
|
+
priority: labels.length - i,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const kept = declutter(boxes);
|
|
84
|
+
return placed.filter(l => kept.has(l.id));
|
|
85
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
HOME_CAMERA,
|
|
5
|
+
MAX_ZOOM,
|
|
6
|
+
MIN_ZOOM,
|
|
7
|
+
cameraCenteredOn,
|
|
8
|
+
cameraFitPoints,
|
|
9
|
+
clampCamera,
|
|
10
|
+
mapProjection,
|
|
11
|
+
panCamera,
|
|
12
|
+
projectPoint,
|
|
13
|
+
syntheticAltitude,
|
|
14
|
+
unprojectPoint,
|
|
15
|
+
zoomCamera,
|
|
16
|
+
} from './map-projection';
|
|
17
|
+
|
|
18
|
+
const W = 1200;
|
|
19
|
+
const H = 700;
|
|
20
|
+
|
|
21
|
+
function labelTier(altitude: number): 'far' | 'mid' | 'near' {
|
|
22
|
+
if (altitude > 1.6) return 'far';
|
|
23
|
+
if (altitude > 0.9) return 'mid';
|
|
24
|
+
return 'near';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function testAKnownPlaceProjectsInsideTheViewport() {
|
|
28
|
+
const proj = mapProjection(W, H, HOME_CAMERA);
|
|
29
|
+
for (const [lat, lng] of [
|
|
30
|
+
[51.9, 4.5],
|
|
31
|
+
[31.2, 121.5],
|
|
32
|
+
[-33.9, 18.4],
|
|
33
|
+
[40.7, -74.0],
|
|
34
|
+
]) {
|
|
35
|
+
const p = projectPoint(proj, lat, lng);
|
|
36
|
+
assert.ok(p, `(${lat}, ${lng}) projected to nothing`);
|
|
37
|
+
assert.ok(p[0] > 0 && p[0] < W, `x ${p[0]} off canvas`);
|
|
38
|
+
assert.ok(p[1] > 0 && p[1] < H, `y ${p[1]} off canvas`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function testProjectAndUnprojectRoundTrip() {
|
|
43
|
+
const proj = mapProjection(W, H, { k: 3, x: -420, y: -180 });
|
|
44
|
+
const p = projectPoint(proj, 48.1, 11.6)!;
|
|
45
|
+
const back = unprojectPoint(proj, p[0], p[1])!;
|
|
46
|
+
assert.ok(Math.abs(back.lat - 48.1) < 1e-6, `lat came back as ${back.lat}`);
|
|
47
|
+
assert.ok(Math.abs(back.lng - 11.6) < 1e-6, `lng came back as ${back.lng}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function testWestIsLeftAndNorthIsUp() {
|
|
51
|
+
const proj = mapProjection(W, H, HOME_CAMERA);
|
|
52
|
+
const ny = projectPoint(proj, 40.7, -74.0)!;
|
|
53
|
+
const shanghai = projectPoint(proj, 31.2, 121.5)!;
|
|
54
|
+
const capeTown = projectPoint(proj, -33.9, 18.4)!;
|
|
55
|
+
const oslo = projectPoint(proj, 59.9, 10.7)!;
|
|
56
|
+
assert.ok(ny[0] < shanghai[0], 'New York should draw left of Shanghai');
|
|
57
|
+
assert.ok(oslo[1] < capeTown[1], 'Oslo should draw above Cape Town');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function testZoomKeepsTheAnchorPointStill() {
|
|
61
|
+
const before = { k: 2, x: -300, y: -100 };
|
|
62
|
+
const proj = mapProjection(W, H, before);
|
|
63
|
+
const anchor = { x: 500, y: 300 };
|
|
64
|
+
const place = unprojectPoint(proj, anchor.x, anchor.y)!;
|
|
65
|
+
const after = zoomCamera(before, 1.7, anchor, W, H);
|
|
66
|
+
const p = projectPoint(mapProjection(W, H, after), place.lat, place.lng)!;
|
|
67
|
+
assert.ok(Math.abs(p[0] - anchor.x) < 1e-6, `anchor drifted to x ${p[0]}`);
|
|
68
|
+
assert.ok(Math.abs(p[1] - anchor.y) < 1e-6, `anchor drifted to y ${p[1]}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function testZoomIsClampedToTheReachableRange() {
|
|
72
|
+
const at = { x: W / 2, y: H / 2 };
|
|
73
|
+
assert.equal(zoomCamera(HOME_CAMERA, 0.01, at, W, H).k, MIN_ZOOM);
|
|
74
|
+
assert.equal(zoomCamera({ k: 20, x: 0, y: 0 }, 100, at, W, H).k, MAX_ZOOM);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function testPanCannotThrowTheWorldOffTheCanvas() {
|
|
78
|
+
let cam = clampCamera({ k: 4, x: 0, y: 0 }, W, H);
|
|
79
|
+
cam = panCamera(cam, 1e6, 1e6, W, H);
|
|
80
|
+
const proj = mapProjection(W, H, cam);
|
|
81
|
+
const corners = [
|
|
82
|
+
projectPoint(proj, 0, 0),
|
|
83
|
+
projectPoint(proj, 80, -170),
|
|
84
|
+
projectPoint(proj, -80, 170),
|
|
85
|
+
].filter(Boolean) as [number, number][];
|
|
86
|
+
assert.ok(
|
|
87
|
+
corners.some(p => p[0] > -W && p[0] < 2 * W && p[1] > -H && p[1] < 2 * H),
|
|
88
|
+
'the whole world was flung away',
|
|
89
|
+
);
|
|
90
|
+
const again = clampCamera(cam, W, H);
|
|
91
|
+
assert.ok(
|
|
92
|
+
Math.abs(again.x - cam.x) < 1e-9 && Math.abs(again.y - cam.y) < 1e-9,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function testCenteringPutsThePlaceMidViewport() {
|
|
97
|
+
const cam = cameraCenteredOn(48.1, 11.6, 5, W, H);
|
|
98
|
+
const p = projectPoint(mapProjection(W, H, cam), 48.1, 11.6)!;
|
|
99
|
+
assert.ok(Math.abs(p[0] - W / 2) < 1, `x ${p[0]} not centred`);
|
|
100
|
+
assert.ok(Math.abs(p[1] - H / 2) < 1, `y ${p[1]} not centred`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function testFitPointsFramesEveryInput() {
|
|
104
|
+
const points = [
|
|
105
|
+
{ lat: 51.9, lng: 4.5 },
|
|
106
|
+
{ lat: 31.2, lng: 121.5 },
|
|
107
|
+
{ lat: 1.35, lng: 103.8 },
|
|
108
|
+
];
|
|
109
|
+
const cam = cameraFitPoints(points, W, H);
|
|
110
|
+
const proj = mapProjection(W, H, cam);
|
|
111
|
+
for (const pt of points) {
|
|
112
|
+
const p = projectPoint(proj, pt.lat, pt.lng)!;
|
|
113
|
+
assert.ok(p[0] > 0 && p[0] < W, `${pt.lng} fitted off canvas at x ${p[0]}`);
|
|
114
|
+
assert.ok(p[1] > 0 && p[1] < H, `${pt.lat} fitted off canvas at y ${p[1]}`);
|
|
115
|
+
}
|
|
116
|
+
const near = cameraFitPoints(
|
|
117
|
+
[
|
|
118
|
+
{ lat: 51.9, lng: 4.5 },
|
|
119
|
+
{ lat: 51.2, lng: 4.4 },
|
|
120
|
+
],
|
|
121
|
+
W,
|
|
122
|
+
H,
|
|
123
|
+
);
|
|
124
|
+
assert.ok(near.k > 2, `nearby pair fitted at k ${near.k}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function testFitWithNothingIsTheHomeView() {
|
|
128
|
+
const cam = cameraFitPoints([], W, H);
|
|
129
|
+
assert.equal(cam.k, 1);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function testSyntheticAltitudeSpansTheGlobesTiers() {
|
|
133
|
+
assert.equal(labelTier(syntheticAltitude(1)), 'far');
|
|
134
|
+
assert.equal(labelTier(syntheticAltitude(2)), 'mid');
|
|
135
|
+
assert.equal(labelTier(syntheticAltitude(4)), 'near');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function testSyntheticAltitudeIsMonotonicAndClamped() {
|
|
139
|
+
assert.ok(syntheticAltitude(1) > syntheticAltitude(2));
|
|
140
|
+
assert.ok(syntheticAltitude(2) > syntheticAltitude(8));
|
|
141
|
+
assert.equal(syntheticAltitude(1000), 0.12);
|
|
142
|
+
assert.ok(syntheticAltitude(1) <= 2.6);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
describe('map-projection', () => {
|
|
146
|
+
it(
|
|
147
|
+
'projects known places inside the viewport',
|
|
148
|
+
testAKnownPlaceProjectsInsideTheViewport,
|
|
149
|
+
);
|
|
150
|
+
it('round-trips project and unproject', testProjectAndUnprojectRoundTrip);
|
|
151
|
+
it('draws west left and north up', testWestIsLeftAndNorthIsUp);
|
|
152
|
+
it('keeps the zoom anchor still', testZoomKeepsTheAnchorPointStill);
|
|
153
|
+
it(
|
|
154
|
+
'clamps zoom to the reachable range',
|
|
155
|
+
testZoomIsClampedToTheReachableRange,
|
|
156
|
+
);
|
|
157
|
+
it(
|
|
158
|
+
'cannot pan the world off the canvas',
|
|
159
|
+
testPanCannotThrowTheWorldOffTheCanvas,
|
|
160
|
+
);
|
|
161
|
+
it('centers a place mid-viewport', testCenteringPutsThePlaceMidViewport);
|
|
162
|
+
it('fits every input point', testFitPointsFramesEveryInput);
|
|
163
|
+
it('fits nothing as the home view', testFitWithNothingIsTheHomeView);
|
|
164
|
+
it(
|
|
165
|
+
'maps synthetic altitude to globe tiers',
|
|
166
|
+
testSyntheticAltitudeSpansTheGlobesTiers,
|
|
167
|
+
);
|
|
168
|
+
it(
|
|
169
|
+
'keeps synthetic altitude monotonic and clamped',
|
|
170
|
+
testSyntheticAltitudeIsMonotonicAndClamped,
|
|
171
|
+
);
|
|
172
|
+
});
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// The flat map's projection and camera (components/supply-chain-map.tsx): where
|
|
2
|
+
// a lat/lng lands on the canvas, how the reader pans and zooms, and how a zoom
|
|
3
|
+
// level translates into the altitude vocabulary the globe's scene code already
|
|
4
|
+
// speaks (label tiers, lane-width rungs, admin-1 reveal). No canvas, no React —
|
|
5
|
+
// table-tested in lib/map-projection.test.ts.
|
|
6
|
+
//
|
|
7
|
+
// Natural Earth (geoNaturalEarth1) rather than Mercator or equirectangular: it
|
|
8
|
+
// is the projection tuned for exactly this reading — the whole world at once
|
|
9
|
+
// with landmasses at honest relative sizes — and it matches the globe's crafted
|
|
10
|
+
// look better than a web-tile grid would. d3-geo also resamples along great
|
|
11
|
+
// circles, so a two-point LineString drawn through it curves the way the same
|
|
12
|
+
// lane bows on the sphere.
|
|
13
|
+
import { type GeoProjection, geoNaturalEarth1, geoPath } from 'd3-geo';
|
|
14
|
+
|
|
15
|
+
/** Where the flat map's camera is: a zoom factor over the fitted world, and a
|
|
16
|
+
* screen-pixel translation applied AFTER that zoom. k = 1, x = 0, y = 0 is the
|
|
17
|
+
* whole world fitted to the viewport. */
|
|
18
|
+
export type MapCamera = { k: number; x: number; y: number };
|
|
19
|
+
|
|
20
|
+
export const HOME_CAMERA: MapCamera = { k: 1, x: 0, y: 0 };
|
|
21
|
+
|
|
22
|
+
/** Zoomed all the way out the map IS the fitted world — there is nothing
|
|
23
|
+
* beyond it to show, so 1 is the floor rather than a preference. */
|
|
24
|
+
export const MIN_ZOOM = 1;
|
|
25
|
+
|
|
26
|
+
/** Deep enough to separate two plants in one city (at a typical viewport this
|
|
27
|
+
* is a few tens of kilometres across), shallow enough that the 110m coastline
|
|
28
|
+
* under it has not visibly fallen apart. */
|
|
29
|
+
export const MAX_ZOOM = 24;
|
|
30
|
+
|
|
31
|
+
/** One press of the +/- buttons. */
|
|
32
|
+
export const ZOOM_STEP = 1.6;
|
|
33
|
+
|
|
34
|
+
/** Breathing room between the fitted world and the viewport edge, so the
|
|
35
|
+
* Antarctic coast is not kissing the canvas border. */
|
|
36
|
+
const FIT_PADDING_PX = 12;
|
|
37
|
+
|
|
38
|
+
/** How much of the viewport the world must always cover while panning — the
|
|
39
|
+
* clamp that stops a drag from throwing the planet off the canvas entirely. */
|
|
40
|
+
const MIN_WORLD_VISIBLE_PX = 120;
|
|
41
|
+
|
|
42
|
+
function clamp(v: number, lo: number, hi: number): number {
|
|
43
|
+
return Math.min(hi, Math.max(lo, v));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The world fitted to this viewport, before any camera. Recomputed rather than
|
|
47
|
+
* cached: geoNaturalEarth1() is cheap arithmetic and a cache keyed on size
|
|
48
|
+
* would be one more thing that can disagree with the canvas. */
|
|
49
|
+
function fittedProjection(width: number, height: number): GeoProjection {
|
|
50
|
+
return geoNaturalEarth1().fitExtent(
|
|
51
|
+
[
|
|
52
|
+
[FIT_PADDING_PX, FIT_PADDING_PX],
|
|
53
|
+
[
|
|
54
|
+
Math.max(FIT_PADDING_PX + 1, width - FIT_PADDING_PX),
|
|
55
|
+
Math.max(FIT_PADDING_PX + 1, height - FIT_PADDING_PX),
|
|
56
|
+
],
|
|
57
|
+
],
|
|
58
|
+
{ type: 'Sphere' },
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The projection the camera sees. The camera transform (scale k, then translate
|
|
64
|
+
* x/y) composes with the base fit linearly, so it folds into the projection's
|
|
65
|
+
* own scale and translate — which is what lets d3's geoPath render polygons,
|
|
66
|
+
* graticule and resampled lines under the camera with no second transform pass
|
|
67
|
+
* (and no stroke widths scaling with the zoom).
|
|
68
|
+
*/
|
|
69
|
+
export function mapProjection(
|
|
70
|
+
width: number,
|
|
71
|
+
height: number,
|
|
72
|
+
camera: MapCamera,
|
|
73
|
+
): GeoProjection {
|
|
74
|
+
const base = fittedProjection(width, height);
|
|
75
|
+
const [tx, ty] = base.translate();
|
|
76
|
+
return geoNaturalEarth1()
|
|
77
|
+
.scale(base.scale() * camera.k)
|
|
78
|
+
.translate([tx * camera.k + camera.x, ty * camera.k + camera.y]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Screen position of a place, or null for a point the projection rejects. */
|
|
82
|
+
export function projectPoint(
|
|
83
|
+
proj: GeoProjection,
|
|
84
|
+
lat: number,
|
|
85
|
+
lng: number,
|
|
86
|
+
): [number, number] | null {
|
|
87
|
+
const p = proj([lng, lat]);
|
|
88
|
+
return p ? [p[0], p[1]] : null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The place under a screen position, or null off the projected world. */
|
|
92
|
+
export function unprojectPoint(
|
|
93
|
+
proj: GeoProjection,
|
|
94
|
+
x: number,
|
|
95
|
+
y: number,
|
|
96
|
+
): { lat: number; lng: number } | null {
|
|
97
|
+
const p = proj.invert?.([x, y]);
|
|
98
|
+
if (!p || !Number.isFinite(p[0]) || !Number.isFinite(p[1])) return null;
|
|
99
|
+
return { lat: p[1], lng: p[0] };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The projected world's bounding box under this camera, in screen pixels.
|
|
103
|
+
* Measured off the base fit (fitExtent letterboxes — Natural Earth is wider
|
|
104
|
+
* than it is tall, so the sphere never fills a squarer viewport vertically)
|
|
105
|
+
* and then carried through the camera, which is linear. */
|
|
106
|
+
function worldBounds(
|
|
107
|
+
width: number,
|
|
108
|
+
height: number,
|
|
109
|
+
camera: MapCamera,
|
|
110
|
+
): { x0: number; y0: number; x1: number; y1: number } {
|
|
111
|
+
const base = fittedProjection(width, height);
|
|
112
|
+
const [[bx0, by0], [bx1, by1]] = geoPath(base).bounds({ type: 'Sphere' });
|
|
113
|
+
return {
|
|
114
|
+
x0: bx0 * camera.k + camera.x,
|
|
115
|
+
y0: by0 * camera.k + camera.y,
|
|
116
|
+
x1: bx1 * camera.k + camera.x,
|
|
117
|
+
y1: by1 * camera.k + camera.y,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Keep the world on the canvas. Zoom is clamped to the reachable range, and the
|
|
123
|
+
* translation so that at least MIN_WORLD_VISIBLE_PX of the world's box remains
|
|
124
|
+
* in view on each axis — a drag can park the map half off-screen, never lose it.
|
|
125
|
+
*/
|
|
126
|
+
export function clampCamera(
|
|
127
|
+
camera: MapCamera,
|
|
128
|
+
width: number,
|
|
129
|
+
height: number,
|
|
130
|
+
): MapCamera {
|
|
131
|
+
const k = clamp(camera.k, MIN_ZOOM, MAX_ZOOM);
|
|
132
|
+
const probe = { k, x: camera.x, y: camera.y };
|
|
133
|
+
const box = worldBounds(width, height, probe);
|
|
134
|
+
const w = box.x1 - box.x0;
|
|
135
|
+
const h = box.y1 - box.y0;
|
|
136
|
+
const visible = Math.min(MIN_WORLD_VISIBLE_PX, w / 2, h / 2);
|
|
137
|
+
// The box must keep `visible` px inside [0,width] x [0,height].
|
|
138
|
+
const minX = visible - (box.x1 - probe.x); // world right edge >= visible
|
|
139
|
+
const maxX = width - visible - (box.x0 - probe.x); // world left edge <= width - visible
|
|
140
|
+
const minY = visible - (box.y1 - probe.y);
|
|
141
|
+
const maxY = height - visible - (box.y0 - probe.y);
|
|
142
|
+
return {
|
|
143
|
+
k,
|
|
144
|
+
x: clamp(probe.x, minX, maxX),
|
|
145
|
+
y: clamp(probe.y, minY, maxY),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Slide the camera by a screen-pixel delta (the drag gesture). */
|
|
150
|
+
export function panCamera(
|
|
151
|
+
camera: MapCamera,
|
|
152
|
+
dx: number,
|
|
153
|
+
dy: number,
|
|
154
|
+
width: number,
|
|
155
|
+
height: number,
|
|
156
|
+
): MapCamera {
|
|
157
|
+
return clampCamera(
|
|
158
|
+
{ k: camera.k, x: camera.x + dx, y: camera.y + dy },
|
|
159
|
+
width,
|
|
160
|
+
height,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Zoom by `factor` keeping the screen point `at` over the same place — the
|
|
166
|
+
* wheel gesture's contract. Derivation: a base-projected point v lands at
|
|
167
|
+
* screen s = k·v + t, so holding s fixed across k -> k' means
|
|
168
|
+
* t' = s - (k'/k)(s - t).
|
|
169
|
+
*/
|
|
170
|
+
export function zoomCamera(
|
|
171
|
+
camera: MapCamera,
|
|
172
|
+
factor: number,
|
|
173
|
+
at: { x: number; y: number },
|
|
174
|
+
width: number,
|
|
175
|
+
height: number,
|
|
176
|
+
): MapCamera {
|
|
177
|
+
const k = clamp(camera.k * factor, MIN_ZOOM, MAX_ZOOM);
|
|
178
|
+
const ratio = k / camera.k;
|
|
179
|
+
return clampCamera(
|
|
180
|
+
{
|
|
181
|
+
k,
|
|
182
|
+
x: at.x - ratio * (at.x - camera.x),
|
|
183
|
+
y: at.y - ratio * (at.y - camera.y),
|
|
184
|
+
},
|
|
185
|
+
width,
|
|
186
|
+
height,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** A camera holding this place at the viewport centre, at zoom k. */
|
|
191
|
+
export function cameraCenteredOn(
|
|
192
|
+
lat: number,
|
|
193
|
+
lng: number,
|
|
194
|
+
k: number,
|
|
195
|
+
width: number,
|
|
196
|
+
height: number,
|
|
197
|
+
): MapCamera {
|
|
198
|
+
const base = fittedProjection(width, height);
|
|
199
|
+
const p = base([lng, lat]);
|
|
200
|
+
if (!p) return clampCamera({ k, x: 0, y: 0 }, width, height);
|
|
201
|
+
return clampCamera(
|
|
202
|
+
{ k, x: width / 2 - k * p[0], y: height / 2 - k * p[1] },
|
|
203
|
+
width,
|
|
204
|
+
height,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Breathing room around a fitted set of points — same intent as the globe's
|
|
209
|
+
* FIT_MARGIN: endpoints land inside the frame, not on its rim. */
|
|
210
|
+
const FIT_POINTS_MARGIN = 1.25;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A camera that frames every one of these points at once — the flat analogue of
|
|
214
|
+
* the globe's povFitPoints. Framing is done in base-projected space, where there
|
|
215
|
+
* is no antimeridian to straddle: two points either side of the dateline project
|
|
216
|
+
* to the two edges of the map, and the honest answer there is a wide view, not a
|
|
217
|
+
* wrapped one.
|
|
218
|
+
*/
|
|
219
|
+
export function cameraFitPoints(
|
|
220
|
+
points: readonly { lat: number; lng: number }[],
|
|
221
|
+
width: number,
|
|
222
|
+
height: number,
|
|
223
|
+
): MapCamera {
|
|
224
|
+
if (points.length === 0) return clampCamera(HOME_CAMERA, width, height);
|
|
225
|
+
const base = fittedProjection(width, height);
|
|
226
|
+
let x0 = Infinity;
|
|
227
|
+
let y0 = Infinity;
|
|
228
|
+
let x1 = -Infinity;
|
|
229
|
+
let y1 = -Infinity;
|
|
230
|
+
for (const pt of points) {
|
|
231
|
+
const p = base([pt.lng, pt.lat]);
|
|
232
|
+
if (!p) continue;
|
|
233
|
+
x0 = Math.min(x0, p[0]);
|
|
234
|
+
y0 = Math.min(y0, p[1]);
|
|
235
|
+
x1 = Math.max(x1, p[0]);
|
|
236
|
+
y1 = Math.max(y1, p[1]);
|
|
237
|
+
}
|
|
238
|
+
if (!Number.isFinite(x0)) return clampCamera(HOME_CAMERA, width, height);
|
|
239
|
+
if (points.length === 1) {
|
|
240
|
+
return cameraCenteredOn(
|
|
241
|
+
points[0].lat,
|
|
242
|
+
points[0].lng,
|
|
243
|
+
FOCUS_ZOOM,
|
|
244
|
+
width,
|
|
245
|
+
height,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
const spanW = Math.max(1, (x1 - x0) * FIT_POINTS_MARGIN);
|
|
249
|
+
const spanH = Math.max(1, (y1 - y0) * FIT_POINTS_MARGIN);
|
|
250
|
+
const k = clamp(Math.min(width / spanW, height / spanH), MIN_ZOOM, MAX_ZOOM);
|
|
251
|
+
const cx = (x0 + x1) / 2;
|
|
252
|
+
const cy = (y0 + y1) / 2;
|
|
253
|
+
return clampCamera(
|
|
254
|
+
{ k, x: width / 2 - k * cx, y: height / 2 - k * cy },
|
|
255
|
+
width,
|
|
256
|
+
height,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** The zoom a single-target fly-to lands at: close enough that the node's city
|
|
261
|
+
* and neighbours read, far enough that there is context around it. */
|
|
262
|
+
export const FOCUS_ZOOM = 6;
|
|
263
|
+
|
|
264
|
+
/** The zoom the map opens at over the org's own site — the flat counterpart of
|
|
265
|
+
* the globe's establishing altitude of 1.8 (see syntheticAltitude: 2.2/1.8). */
|
|
266
|
+
export const OPENING_ZOOM = 2.2 / 1.8;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* The globe-altitude this zoom level corresponds to.
|
|
270
|
+
*
|
|
271
|
+
* The scene code speaks altitude everywhere — labelTier, laneZoomT, the admin-1
|
|
272
|
+
* reveal — and re-deriving three parallel zoom ladders for the flat map would
|
|
273
|
+
* guarantee the two views disagree about when detail arrives. So the map speaks
|
|
274
|
+
* altitude too, through this one mapping: at k = 1 the whole world is in frame,
|
|
275
|
+
* which on the globe is the far end of the wheel (2.2 is the intro's own pull-
|
|
276
|
+
* back altitude); every doubling of zoom halves the "height". Clamped to the
|
|
277
|
+
* globe's reachable range so every consumer sees numbers it was tuned for.
|
|
278
|
+
*/
|
|
279
|
+
export function syntheticAltitude(k: number): number {
|
|
280
|
+
return clamp(2.2 / Math.max(MIN_ZOOM, k), 0.12, 2.6);
|
|
281
|
+
}
|