@opendata-ai/openchart-vanilla 8.0.0-rc.8 → 8.0.0

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.
Files changed (147) hide show
  1. package/dist/{chunk-W34KN7FD.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/{chunk-UZ7J3NOF.js → chunk-6BUCYCLD.js} +7314 -5605
  3. package/dist/chunk-6BUCYCLD.js.map +1 -0
  4. package/dist/chunk-DCODCLWI.js +234 -0
  5. package/dist/chunk-DCODCLWI.js.map +1 -0
  6. package/dist/{chunk-5EOV5JQZ.js → chunk-EKAUQAAX.js} +50 -46
  7. package/dist/chunk-EKAUQAAX.js.map +1 -0
  8. package/dist/chunk-RM5XS6NR.js +113 -0
  9. package/dist/chunk-RM5XS6NR.js.map +1 -0
  10. package/dist/chunk-T32UHMZA.js +282 -0
  11. package/dist/chunk-T32UHMZA.js.map +1 -0
  12. package/dist/export-gif.d.ts +113 -0
  13. package/dist/export-gif.js +21 -0
  14. package/dist/export-gif.js.map +1 -0
  15. package/dist/index.d.ts +309 -27
  16. package/dist/index.js +2020 -836
  17. package/dist/index.js.map +1 -1
  18. package/dist/{mount-CUKQLeqD.d.ts → mount-CL-nA0f-.d.ts} +85 -2
  19. package/dist/simulation-worker.js +83 -2
  20. package/dist/static.d.ts +2 -0
  21. package/dist/static.js +8 -69
  22. package/dist/static.js.map +1 -1
  23. package/dist/story/index.d.ts +25 -9
  24. package/dist/story/index.js +9 -7
  25. package/dist/story/index.js.map +1 -1
  26. package/dist/styles.css +1 -1
  27. package/dist/styles.css.map +1 -1
  28. package/package.json +14 -6
  29. package/dist/chunk-5EOV5JQZ.js.map +0 -1
  30. package/dist/chunk-UZ7J3NOF.js.map +0 -1
  31. package/src/__test-fixtures__/dom.ts +0 -70
  32. package/src/__test-fixtures__/specs.ts +0 -214
  33. package/src/__tests__/animation.test.ts +0 -358
  34. package/src/__tests__/annotations-render.test.ts +0 -201
  35. package/src/__tests__/compound-labels.test.ts +0 -122
  36. package/src/__tests__/continuous-legend.test.ts +0 -91
  37. package/src/__tests__/crosshair.test.ts +0 -130
  38. package/src/__tests__/edit-events.test.ts +0 -950
  39. package/src/__tests__/events.test.ts +0 -387
  40. package/src/__tests__/export.test.ts +0 -241
  41. package/src/__tests__/font-race.test.ts +0 -199
  42. package/src/__tests__/gradient-ids.test.ts +0 -159
  43. package/src/__tests__/gradient-utils.test.ts +0 -125
  44. package/src/__tests__/known-bugs-render.test.ts +0 -183
  45. package/src/__tests__/map.test.ts +0 -636
  46. package/src/__tests__/mount.test.ts +0 -357
  47. package/src/__tests__/pattern-fills.test.ts +0 -105
  48. package/src/__tests__/resize-timing.test.ts +0 -218
  49. package/src/__tests__/sankey.test.ts +0 -133
  50. package/src/__tests__/selection-events.test.ts +0 -869
  51. package/src/__tests__/series-search.test.ts +0 -311
  52. package/src/__tests__/static.test.ts +0 -141
  53. package/src/__tests__/svg-renderer.test.ts +0 -895
  54. package/src/__tests__/table-keyboard.test.ts +0 -361
  55. package/src/__tests__/table-mount.test.ts +0 -639
  56. package/src/__tests__/tilemap.test.ts +0 -158
  57. package/src/__tests__/tooltip.test.ts +0 -337
  58. package/src/__tests__/transition.test.ts +0 -1792
  59. package/src/__tests__/update-scrolly-matrix.test.ts +0 -563
  60. package/src/__tests__/you-draw-it.test.ts +0 -273
  61. package/src/animation-vars.ts +0 -42
  62. package/src/animation.ts +0 -76
  63. package/src/barlist-mount.ts +0 -383
  64. package/src/barlist-renderer.ts +0 -267
  65. package/src/dom-helpers.ts +0 -17
  66. package/src/export.ts +0 -482
  67. package/src/gradient-utils.ts +0 -138
  68. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  69. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  70. package/src/graph/__tests__/interaction.test.ts +0 -205
  71. package/src/graph/__tests__/keyboard.test.ts +0 -653
  72. package/src/graph/__tests__/search.test.ts +0 -88
  73. package/src/graph/__tests__/simulation.test.ts +0 -233
  74. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  75. package/src/graph/__tests__/zoom.test.ts +0 -203
  76. package/src/graph/canvas-renderer.ts +0 -747
  77. package/src/graph/interaction.ts +0 -367
  78. package/src/graph/keyboard.ts +0 -208
  79. package/src/graph/search.ts +0 -50
  80. package/src/graph/simulation-worker-url.ts +0 -12
  81. package/src/graph/simulation-worker.ts +0 -278
  82. package/src/graph/simulation.ts +0 -422
  83. package/src/graph/spatial-index.ts +0 -121
  84. package/src/graph/types.ts +0 -47
  85. package/src/graph/worker-protocol.ts +0 -70
  86. package/src/graph/zoom.ts +0 -134
  87. package/src/graph-mount.ts +0 -888
  88. package/src/index.ts +0 -76
  89. package/src/interactions/chart-events.ts +0 -139
  90. package/src/interactions/crosshair.ts +0 -233
  91. package/src/interactions/drag-handler.ts +0 -175
  92. package/src/interactions/editing-drags.ts +0 -515
  93. package/src/interactions/index.ts +0 -25
  94. package/src/interactions/keyboard-nav.ts +0 -111
  95. package/src/interactions/legend-interaction.ts +0 -46
  96. package/src/interactions/selection.ts +0 -280
  97. package/src/interactions/tooltip-events.ts +0 -72
  98. package/src/map-camera.ts +0 -111
  99. package/src/map-mount.ts +0 -792
  100. package/src/map-renderer.ts +0 -460
  101. package/src/map-transition.ts +0 -99
  102. package/src/measure-text.ts +0 -134
  103. package/src/mount.ts +0 -1382
  104. package/src/pattern-utils.ts +0 -110
  105. package/src/renderers/annotations.ts +0 -388
  106. package/src/renderers/axes.ts +0 -351
  107. package/src/renderers/brand.ts +0 -87
  108. package/src/renderers/chrome.ts +0 -196
  109. package/src/renderers/endpoint-labels.ts +0 -150
  110. package/src/renderers/legend.ts +0 -311
  111. package/src/renderers/marks.ts +0 -561
  112. package/src/renderers/metrics.ts +0 -63
  113. package/src/renderers/svg-dom.ts +0 -40
  114. package/src/renderers/table-cells.ts +0 -446
  115. package/src/resize-observer.ts +0 -46
  116. package/src/sankey-mount.ts +0 -622
  117. package/src/sankey-renderer.ts +0 -614
  118. package/src/series-search.ts +0 -392
  119. package/src/static.ts +0 -251
  120. package/src/story/__tests__/camera-math.test.ts +0 -145
  121. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  122. package/src/story/__tests__/progress-math.test.ts +0 -95
  123. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  124. package/src/story/__tests__/tween.test.ts +0 -159
  125. package/src/story/camera-math.ts +0 -125
  126. package/src/story/create-chart-story.ts +0 -353
  127. package/src/story/crossfade.ts +0 -70
  128. package/src/story/index.ts +0 -39
  129. package/src/story/progress-math.ts +0 -85
  130. package/src/story/resolve-camera-target.ts +0 -119
  131. package/src/story/scroll-driver.ts +0 -206
  132. package/src/story/story-camera.ts +0 -49
  133. package/src/story/tween.ts +0 -112
  134. package/src/story/types.ts +0 -76
  135. package/src/svg-ids.ts +0 -23
  136. package/src/svg-renderer.ts +0 -383
  137. package/src/table-keyboard.ts +0 -266
  138. package/src/table-mount.ts +0 -567
  139. package/src/table-renderer.ts +0 -414
  140. package/src/text-edit-overlay.ts +0 -255
  141. package/src/theme-tokens.ts +0 -51
  142. package/src/tilemap-mount.ts +0 -511
  143. package/src/tilemap-renderer.ts +0 -396
  144. package/src/tooltip.ts +0 -177
  145. package/src/transition.ts +0 -2816
  146. package/src/you-draw-it.ts +0 -480
  147. /package/dist/{chunk-W34KN7FD.js.map → chunk-2NAQRP66.js.map} +0 -0
@@ -1,280 +0,0 @@
1
- import type {
2
- Annotation,
3
- ChartLayout,
4
- ChartSpec,
5
- ChromeKey,
6
- ElementRef,
7
- GraphSpec,
8
- LayerSpec,
9
- TextAnnotation,
10
- } from '@opendata-ai/openchart-core';
11
- import { elementRef } from '@opendata-ai/openchart-core';
12
-
13
- /**
14
- * Find a DOM element inside the SVG that matches the given ElementRef.
15
- */
16
- export function findElementByRef(svg: SVGElement, ref: ElementRef): SVGElement | null {
17
- switch (ref.type) {
18
- case 'annotation': {
19
- if (ref.id) {
20
- const byId = svg.querySelector(`[data-annotation-id="${ref.id}"]`);
21
- if (byId) return byId as SVGElement;
22
- }
23
- return svg.querySelector(`[data-annotation-index="${ref.index}"]`) as SVGElement | null;
24
- }
25
- case 'chrome':
26
- return svg.querySelector(`[data-chrome-key="${ref.key}"]`) as SVGElement | null;
27
- case 'series-label':
28
- return svg.querySelector(`.oc-mark-label[data-series="${ref.series}"]`) as SVGElement | null;
29
- // The editable legend is the color legend. Excluding the size legend
30
- // explicitly rather than relying on it rendering second: `querySelector`
31
- // takes the first match, so a bare `.oc-legend` would silently retarget
32
- // (and drag) the wrong legend if render order ever changed.
33
- case 'legend':
34
- return svg.querySelector('.oc-legend:not(.oc-legend--size)') as SVGElement | null;
35
- case 'legend-entry':
36
- return svg.querySelector(
37
- `.oc-legend:not(.oc-legend--size) [data-legend-index="${ref.index}"]`,
38
- ) as SVGElement | null;
39
- }
40
- }
41
-
42
- /**
43
- * Build an ElementRef from a DOM element's data attributes.
44
- */
45
- export function buildElementRef(
46
- element: Element,
47
- _specAnnotations: Annotation[],
48
- ): ElementRef | null {
49
- const annotationEl = element.closest('[data-annotation-index]');
50
- if (annotationEl) {
51
- const index = Number(annotationEl.getAttribute('data-annotation-index'));
52
- const id = annotationEl.getAttribute('data-annotation-id') ?? undefined;
53
- return elementRef.annotation(index, id);
54
- }
55
-
56
- const chromeEl = element.closest('[data-chrome-key]');
57
- if (chromeEl) {
58
- const key = chromeEl.getAttribute('data-chrome-key') as ChromeKey;
59
- if (key) return elementRef.chrome(key);
60
- }
61
-
62
- const seriesLabelEl = element.closest('.oc-mark-label[data-series]');
63
- if (seriesLabelEl) {
64
- const series = seriesLabelEl.getAttribute('data-series');
65
- if (series) return elementRef.seriesLabel(series);
66
- }
67
-
68
- const legendEntryEl = element.closest('[data-legend-index]');
69
- if (legendEntryEl) {
70
- const index = Number(legendEntryEl.getAttribute('data-legend-index'));
71
- const series = legendEntryEl.getAttribute('data-legend-label') ?? '';
72
- return elementRef.legendEntry(series, index);
73
- }
74
-
75
- // Only the color legend is selectable/editable. A click inside the size legend
76
- // resolves to no ref rather than reporting the *color* legend's ref, which is
77
- // what a bare `.oc-legend` match would do.
78
- const legendEl = element.closest('.oc-legend');
79
- if (legendEl && !legendEl.classList.contains('oc-legend--size')) return elementRef.legend();
80
-
81
- return null;
82
- }
83
-
84
- /**
85
- * Get an ordered list of all editable ElementRefs from the current spec and layout.
86
- */
87
- export function getEditableElements(
88
- spec: ChartSpec | LayerSpec | GraphSpec,
89
- layout: ChartLayout,
90
- ): ElementRef[] {
91
- const refs: ElementRef[] = [];
92
-
93
- const chromeKeys: ChromeKey[] = ['title', 'subtitle', 'source', 'byline', 'footer'];
94
- for (const key of chromeKeys) {
95
- if (layout.chrome[key]) {
96
- refs.push(elementRef.chrome(key));
97
- }
98
- }
99
-
100
- const annotations: Annotation[] =
101
- 'annotations' in spec && Array.isArray(spec.annotations) ? spec.annotations : [];
102
- for (let i = 0; i < annotations.length; i++) {
103
- refs.push(elementRef.annotation(i, annotations[i].id));
104
- }
105
-
106
- const seriesLabels: string[] = [];
107
- for (const mark of layout.marks) {
108
- if (mark.type === 'line' && mark.label?.visible && mark.seriesKey) {
109
- seriesLabels.push(mark.seriesKey);
110
- }
111
- }
112
- seriesLabels.sort();
113
- for (const series of seriesLabels) {
114
- refs.push(elementRef.seriesLabel(series));
115
- }
116
-
117
- if ('entries' in layout.legend && layout.legend.entries.length > 0) {
118
- refs.push(elementRef.legend());
119
- }
120
-
121
- return refs;
122
- }
123
-
124
- /**
125
- * Check if an ElementRef points to a text-editable element.
126
- */
127
- export function isTextEditable(ref: ElementRef, specAnnotations: Annotation[]): boolean {
128
- if (ref.type === 'chrome') return true;
129
- if (ref.type === 'annotation') {
130
- const annotation = specAnnotations[ref.index];
131
- return annotation?.type === 'text';
132
- }
133
- return false;
134
- }
135
-
136
- /**
137
- * Get the current text content for an element ref.
138
- */
139
- export function getElementText(
140
- ref: ElementRef,
141
- spec: ChartSpec | LayerSpec | GraphSpec,
142
- ): string | null {
143
- if (ref.type === 'chrome') {
144
- const chromeConfig = 'chrome' in spec ? spec.chrome : undefined;
145
- if (!chromeConfig) return null;
146
- const entry = chromeConfig[ref.key];
147
- if (typeof entry === 'string') return entry;
148
- if (typeof entry === 'object' && entry !== null && 'text' in entry) {
149
- return (entry as { text: string }).text;
150
- }
151
- return null;
152
- }
153
- if (ref.type === 'annotation') {
154
- const annotations: Annotation[] =
155
- 'annotations' in spec && Array.isArray(spec.annotations) ? spec.annotations : [];
156
- const annotation = annotations[ref.index];
157
- if (annotation?.type === 'text') return (annotation as TextAnnotation).text ?? null;
158
- if (annotation?.label) return annotation.label;
159
- return null;
160
- }
161
- return null;
162
- }
163
-
164
- /**
165
- * Compare two ElementRefs for equality.
166
- */
167
- export function refsEqual(a: ElementRef | null, b: ElementRef | null): boolean {
168
- if (a === null || b === null) return a === b;
169
- if (a.type !== b.type) return false;
170
- switch (a.type) {
171
- case 'annotation': {
172
- const bAnno = b as typeof a;
173
- if (a.id && bAnno.id) return a.id === bAnno.id;
174
- return a.index === bAnno.index;
175
- }
176
- case 'chrome':
177
- return a.key === (b as typeof a).key;
178
- case 'series-label':
179
- return a.series === (b as typeof a).series;
180
- case 'legend':
181
- return true;
182
- case 'legend-entry': {
183
- const bEntry = b as typeof a;
184
- return a.index === bEntry.index && a.series === bEntry.series;
185
- }
186
- }
187
- }
188
-
189
- /**
190
- * Render a selection overlay rectangle around a target element.
191
- */
192
- export function renderSelectionOverlay(
193
- svg: SVGElement,
194
- ref: ElementRef,
195
- layout: ChartLayout,
196
- ): SVGGElement | null {
197
- const target = findElementByRef(svg, ref);
198
- if (!target) return null;
199
-
200
- const bbox = (target as SVGGraphicsElement).getBBox();
201
- const padding = 4;
202
-
203
- const accentColor = layout.theme.colors.categorical?.[0] ?? '#4f46e5';
204
-
205
- const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
206
- g.setAttribute('class', 'oc-selection-overlay');
207
-
208
- const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
209
- rect.setAttribute('x', String(bbox.x - padding));
210
- rect.setAttribute('y', String(bbox.y - padding));
211
- rect.setAttribute('width', String(bbox.width + padding * 2));
212
- rect.setAttribute('height', String(bbox.height + padding * 2));
213
- rect.setAttribute('rx', '3');
214
- rect.setAttribute('fill', 'transparent');
215
- rect.setAttribute('stroke', accentColor);
216
- rect.setAttribute('stroke-width', '1.5');
217
- rect.setAttribute('pointer-events', 'none');
218
-
219
- g.appendChild(rect);
220
- svg.appendChild(g);
221
-
222
- return g;
223
- }
224
-
225
- /**
226
- * Create a visually-hidden data table from the chart's a11y fallback data.
227
- */
228
- export function createScreenReaderTable(
229
- layout: ChartLayout,
230
- container: HTMLElement,
231
- ): HTMLTableElement | null {
232
- const data = layout.a11y.dataTableFallback;
233
- if (!data || data.length === 0) return null;
234
-
235
- const table = document.createElement('table');
236
- table.className = 'oc-sr-only';
237
- table.style.position = 'absolute';
238
- table.style.width = '1px';
239
- table.style.height = '1px';
240
- table.style.padding = '0';
241
- table.style.margin = '-1px';
242
- table.style.overflow = 'hidden';
243
- table.style.clipPath = 'inset(50%)';
244
- table.style.whiteSpace = 'nowrap';
245
- table.style.borderWidth = '0';
246
- table.setAttribute('role', 'table');
247
- table.setAttribute('aria-label', `Data table: ${layout.a11y.altText}`);
248
-
249
- if (data.length > 0) {
250
- const thead = document.createElement('thead');
251
- const headerRow = document.createElement('tr');
252
- const headers = data[0] as unknown[];
253
- for (const header of headers) {
254
- const th = document.createElement('th');
255
- th.textContent = String(header ?? '');
256
- th.setAttribute('scope', 'col');
257
- headerRow.appendChild(th);
258
- }
259
- thead.appendChild(headerRow);
260
- table.appendChild(thead);
261
- }
262
-
263
- if (data.length > 1) {
264
- const tbody = document.createElement('tbody');
265
- for (let i = 1; i < data.length; i++) {
266
- const tr = document.createElement('tr');
267
- const cells = data[i] as unknown[];
268
- for (const cell of cells) {
269
- const td = document.createElement('td');
270
- td.textContent = String(cell ?? '');
271
- tr.appendChild(td);
272
- }
273
- tbody.appendChild(tr);
274
- }
275
- table.appendChild(tbody);
276
- }
277
-
278
- container.appendChild(table);
279
- return table;
280
- }
@@ -1,72 +0,0 @@
1
- import type { TooltipContent } from '@opendata-ai/openchart-core';
2
- import type { TooltipManager } from '../tooltip';
3
-
4
- /**
5
- * Wire tooltip events on mark elements inside an SVG.
6
- * Returns a cleanup function to remove all listeners.
7
- */
8
- export function wireTooltipEvents(
9
- svg: SVGElement,
10
- tooltipDescriptors: Map<string, TooltipContent>,
11
- tooltipManager: TooltipManager,
12
- ): () => void {
13
- const markElements = svg.querySelectorAll('[data-mark-id]');
14
- const cleanups: Array<() => void> = [];
15
-
16
- for (const el of markElements) {
17
- const markId = el.getAttribute('data-mark-id');
18
- if (!markId) continue;
19
-
20
- const content = tooltipDescriptors.get(markId);
21
- if (!content) continue;
22
-
23
- const handleMouseEnter = (e: Event) => {
24
- const mouseEvent = e as MouseEvent;
25
- const svgRect = svg.getBoundingClientRect();
26
- const x = mouseEvent.clientX - svgRect.left;
27
- const y = mouseEvent.clientY - svgRect.top;
28
- tooltipManager.show(content, x, y);
29
- };
30
-
31
- const handleMouseMove = (e: Event) => {
32
- const mouseEvent = e as MouseEvent;
33
- const svgRect = svg.getBoundingClientRect();
34
- const x = mouseEvent.clientX - svgRect.left;
35
- const y = mouseEvent.clientY - svgRect.top;
36
- tooltipManager.show(content, x, y);
37
- };
38
-
39
- const handleMouseLeave = () => {
40
- tooltipManager.hide();
41
- };
42
-
43
- const handleTouchStart = (e: Event) => {
44
- const touchEvent = e as TouchEvent;
45
- if (touchEvent.touches.length > 0) {
46
- const touch = touchEvent.touches[0];
47
- const svgRect = svg.getBoundingClientRect();
48
- const x = touch.clientX - svgRect.left;
49
- const y = touch.clientY - svgRect.top;
50
- tooltipManager.show(content, x, y);
51
- }
52
- };
53
-
54
- el.addEventListener('mouseenter', handleMouseEnter);
55
- el.addEventListener('mousemove', handleMouseMove);
56
- el.addEventListener('mouseleave', handleMouseLeave);
57
- el.addEventListener('touchstart', handleTouchStart);
58
-
59
- cleanups.push(() => {
60
- el.removeEventListener('mouseenter', handleMouseEnter);
61
- el.removeEventListener('mousemove', handleMouseMove);
62
- el.removeEventListener('mouseleave', handleMouseLeave);
63
- el.removeEventListener('touchstart', handleTouchStart);
64
- });
65
- }
66
-
67
- return () => {
68
- for (const cleanup of cleanups) {
69
- cleanup();
70
- }
71
- };
72
- }
package/src/map-camera.ts DELETED
@@ -1,111 +0,0 @@
1
- /**
2
- * Map camera utilities: zoom, pan, and focus for map visualizations.
3
- *
4
- * Reuses the existing camera-math infrastructure from the scrollytelling
5
- * system. The camera transform is applied as an SVG `transform` attribute
6
- * on the `[data-oc-map-camera]` group, never as a CSS transform.
7
- */
8
-
9
- import type { MapLayout } from '@opendata-ai/openchart-core';
10
- import type { Camera, CameraTarget } from './story/camera-math';
11
- import { cameraTransform, FULL_VIEW, fitTarget } from './story/camera-math';
12
-
13
- export type { Camera } from './story/camera-math';
14
-
15
- /** Resting opacity for features outside the current focus set. */
16
- export const FOCUS_DIM_OPACITY = 0.25;
17
-
18
- export interface MapCameraOptions {
19
- /** Transition duration in ms. 0 snaps instantly. Default 600. */
20
- duration?: number;
21
- /** Padding in map-local units around the target. Default 16. */
22
- padding?: number;
23
- }
24
-
25
- /**
26
- * Compute a CameraTarget from the union of feature bounds.
27
- * Returns null if no matching features found.
28
- */
29
- export function focusTargetForFeatures(
30
- layout: MapLayout,
31
- ids: Array<string | number>,
32
- padding = 16,
33
- ): CameraTarget | null {
34
- const idSet = new Set(ids.map(String));
35
- let minX = Infinity;
36
- let minY = Infinity;
37
- let maxX = -Infinity;
38
- let maxY = -Infinity;
39
- let found = false;
40
-
41
- for (const f of layout.features) {
42
- if (idSet.has(String(f.id))) {
43
- found = true;
44
- minX = Math.min(minX, f.bounds.x);
45
- minY = Math.min(minY, f.bounds.y);
46
- maxX = Math.max(maxX, f.bounds.x + f.bounds.width);
47
- maxY = Math.max(maxY, f.bounds.y + f.bounds.height);
48
- }
49
- }
50
-
51
- if (!found) return null;
52
- return { x: minX, y: minY, width: maxX - minX, height: maxY - minY, padding };
53
- }
54
-
55
- /**
56
- * Compute the Camera for a target, fitting against mapSize.
57
- * Returns the full-view camera if no target provided.
58
- */
59
- export function cameraForTarget(layout: MapLayout, target?: CameraTarget | null): Camera {
60
- const vb = layout.mapSize;
61
- if (!target) return fitTarget(FULL_VIEW(vb), vb);
62
- const cam = fitTarget(target, vb);
63
- return { ...cam, k: Math.max(1, Math.min(40, cam.k)) };
64
- }
65
-
66
- /**
67
- * Apply a camera transform to the map's camera group.
68
- * Toggles vector-effect="non-scaling-stroke" on features/borders while zoomed.
69
- */
70
- export function applyMapCamera(svg: SVGElement, camera: Camera, layout: MapLayout): void {
71
- const cameraGroup = svg.querySelector('[data-oc-map-camera]');
72
- if (!cameraGroup) return;
73
-
74
- const vb = layout.mapSize;
75
- const isZoomed = Math.abs(camera.k - 1) > 1e-3;
76
-
77
- if (isZoomed) {
78
- cameraGroup.setAttribute('transform', cameraTransform(camera, vb));
79
- // Toggle non-scaling-stroke so strokes don't fatten when zoomed
80
- const paths = svg.querySelectorAll('.oc-map-feature, .oc-map-borders path');
81
- for (const p of paths) {
82
- p.setAttribute('vector-effect', 'non-scaling-stroke');
83
- }
84
- // Counter-scale point radii so dots keep constant screen size through zoom.
85
- // Only use attribute counter-scaling (r/k, stroke-width/k) without
86
- // vector-effect: the group transform scales both back to constant screen
87
- // size. Adding vector-effect on top would double-compensate stroke.
88
- const points = svg.querySelectorAll('.oc-map-point');
89
- for (const p of points) {
90
- const baseR = Number(p.getAttribute('data-base-r') ?? 5);
91
- const baseSW = Number(p.getAttribute('data-base-stroke-width') ?? 1);
92
- p.setAttribute('r', String(baseR / camera.k));
93
- p.setAttribute('stroke-width', String(baseSW / camera.k));
94
- }
95
- } else {
96
- cameraGroup.removeAttribute('transform');
97
- const paths = svg.querySelectorAll('.oc-map-feature, .oc-map-borders path');
98
- for (const p of paths) {
99
- p.removeAttribute('vector-effect');
100
- }
101
- // Reset point radii when not zoomed
102
- const points = svg.querySelectorAll('.oc-map-point');
103
- for (const p of points) {
104
- const baseR = Number(p.getAttribute('data-base-r') ?? 5);
105
- const baseSW = Number(p.getAttribute('data-base-stroke-width') ?? 1);
106
- p.setAttribute('r', String(baseR));
107
- p.setAttribute('stroke-width', String(baseSW));
108
- p.removeAttribute('vector-effect');
109
- }
110
- }
111
- }