smithkit 0.2.0 → 0.3.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 (38) hide show
  1. package/README.md +176 -6
  2. package/dist/lib/Smith.d.ts +13 -37
  3. package/dist/lib/Smith.js +82 -282
  4. package/dist/lib/appearance/SvgTheme.d.ts +6 -0
  5. package/dist/lib/appearance/SvgTheme.js +21 -0
  6. package/dist/lib/appearance/Theme.d.ts +8 -0
  7. package/dist/lib/appearance/Theme.js +120 -0
  8. package/dist/lib/appearance/types.d.ts +55 -0
  9. package/dist/lib/appearance/types.js +1 -0
  10. package/dist/lib/grid/ConstCircles.d.ts +0 -3
  11. package/dist/lib/grid/ConstCircles.js +6 -20
  12. package/dist/lib/grid/ConstQCircles.js +1 -1
  13. package/dist/lib/grid/ConstSwrCircles.js +1 -1
  14. package/dist/lib/grid/GridDefinitions.d.ts +7 -1
  15. package/dist/lib/grid/GridDefinitions.js +29 -0
  16. package/dist/lib/grid/GridLayerControl.d.ts +5 -4
  17. package/dist/lib/grid/GridLayerControl.js +11 -2
  18. package/dist/lib/grid/SmithGridLayer.d.ts +7 -1
  19. package/dist/lib/grid/SmithGridLayer.js +35 -5
  20. package/dist/lib/index.d.ts +3 -1
  21. package/dist/lib/interaction/SmithCursor.d.ts +0 -17
  22. package/dist/lib/interaction/SmithCursor.js +6 -14
  23. package/dist/lib/layers.d.ts +5 -1
  24. package/dist/lib/rendering/SvgChartRenderer.d.ts +66 -0
  25. package/dist/lib/rendering/SvgChartRenderer.js +342 -0
  26. package/dist/lib/scales/SmithPeripheralScales.js +11 -11
  27. package/dist/lib/scales/SmithScales.d.ts +10 -3
  28. package/dist/lib/scales/SmithScales.js +73 -39
  29. package/dist/lib/svg/ImageExporter.d.ts +18 -0
  30. package/dist/lib/svg/ImageExporter.js +211 -0
  31. package/dist/lib/svg/MarkerLegend.d.ts +12 -0
  32. package/dist/lib/svg/MarkerLegend.js +63 -0
  33. package/dist/lib/svg/SvgExporter.d.ts +6 -1
  34. package/dist/lib/svg/SvgExporter.js +50 -5
  35. package/dist/lib/svg/export.d.ts +36 -0
  36. package/dist/lib/svg/export.js +1 -0
  37. package/dist/lib/traces/SmithMarker.js +8 -5
  38. package/package.json +1 -1
package/README.md CHANGED
@@ -5,6 +5,10 @@ interactive SVG Smith charts, RF calculations, and S11 measurement exploration.
5
5
 
6
6
  [Try the interactive demo](https://rosek86.github.io/smithkit/).
7
7
 
8
+ Upgrading from 0.2.x? Version 0.3.0 replaces `GridLayer.setMinorVisible()` with
9
+ `setDetail('basic' | 'standard' | 'detailed')`. Replace `false` with `'standard'`
10
+ and `true` with `'detailed'`; `'basic'` shows only the principal circles.
11
+
8
12
  Upgrading from 0.1.x? Version 0.2.0 groups calculation, parsing, formatting, and
9
13
  comparison functions into classes. See the [migration guide](https://github.com/rosek86/smithkit/blob/v0.2.0/docs/migration-0.2.md)
10
14
  for the changed imports.
@@ -20,7 +24,7 @@ for the changed imports.
20
24
  - Twelve parameter scales mounted independently of chart zoom.
21
25
  - Four peripheral rulers for phase and electrical length.
22
26
  - One-port Touchstone 1.x import with RI, MA, and DB representations.
23
- - Standalone SVG export of the current chart and radial scales.
27
+ - Standalone SVG and configurable PNG export of the chart and radial scales.
24
28
  - ESM modules and TypeScript declarations.
25
29
 
26
30
  ## Installation
@@ -344,6 +348,69 @@ Zero frequency, zero reactance, and unrepresentable results return `undefined`.
344
348
  Non-finite reactance or negative/non-finite frequency throws `RangeError`.
345
349
  This is a single-frequency equivalence, not a fitted circuit model.
346
350
 
351
+ ## Appearance and themes
352
+
353
+ `Smith` and `SmithScales` accept the same appearance configuration. The default
354
+ is the light preset with a transparent background. The dark preset includes a
355
+ solid dark background so its labels remain readable when embedded or exported.
356
+
357
+ ```ts
358
+ import { Smith, SmithScales } from 'smithkit';
359
+ import type { SmithAppearance } from 'smithkit';
360
+
361
+ const appearance: SmithAppearance = {
362
+ theme: 'dark',
363
+ overrides: {
364
+ fontFamily: 'Arial, sans-serif',
365
+ grid: { majorWidth: 1.2 },
366
+ cursor: { impedanceColor: '#fbbf24' },
367
+ marker: { focusColor: '#38bdf8' },
368
+ traceColors: ['#38bdf8', '#fb923c', '#a78bfa'],
369
+ },
370
+ };
371
+ const chart = new Smith(50, appearance);
372
+ const scales = new SmithScales(appearance);
373
+ chart.draw('#smith');
374
+ scales.draw('#scales');
375
+
376
+ // Update both independent components using the same configuration.
377
+ chart.setAppearance({ theme: 'light' });
378
+ scales.setAppearance({ theme: 'light' });
379
+ ```
380
+
381
+ Each `setAppearance` call replaces the previous preset and overrides; omitted
382
+ settings return to the selected preset, or light when `theme` is omitted.
383
+ Nested overrides are partial. Appearance is scoped to each component and does not
384
+ change the surrounding application's CSS or other chart instances.
385
+
386
+ | Override | Settings |
387
+ | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
388
+ | `background`, `fontFamily` | Component background and shared font family. |
389
+ | `grid` | `stroke`, `textColor`, `majorWidth`, `minorWidth`, `fontSize`. |
390
+ | `boundary` | `stroke`, `width` for the Smith chart boundary. |
391
+ | `circles` | `q`, `vswr` colors and shared `width`. |
392
+ | `scales` | `stroke`, `boundaryColor`, `textColor`, `valueColor`, `indicatorColor`, `indicatorOutline`; `fontSize` for radial ticks, `titleFontSize`, `valueFontSize`, and `peripheralFontSize`. |
393
+ | `cursor` | `pointColor`, `impedanceColor`, `admittanceColor`, `pointSize` (dot diameter), `lineWidth`. |
394
+ | `marker` | `outlineColor`, `textColor`, `focusColor`, `focusHaloColor`. Marker fill follows its trace color. |
395
+ | `traceColors` | Non-empty palette for automatically colored traces. |
396
+
397
+ Colors must be solid CSS colors accepted by the library's color parser, including
398
+ hex, RGB, HSL, named colors, and `transparent`; CSS variable references are not
399
+ accepted in the configuration. Numeric values must be positive and finite.
400
+ Line widths and cursor point size are CSS pixels. Font sizes are SVG chart/scale
401
+ units and follow the existing layout; grid labels retain their adaptive minimum
402
+ size. Marker geometry and the 44 px touch target stay unchanged.
403
+
404
+ Appearance updates retain zoom, samples, marker selection/focus, layer visibility,
405
+ and scale readings. They recolor existing and future automatically colored traces.
406
+ Explicit colors passed through trace options and explicit layer styles take
407
+ precedence and survive theme changes. Applying a new appearance does not reset
408
+ those individual overrides. Invalid appearance settings throw before making changes.
409
+
410
+ SVG exports resolve the appearance without requiring theme CSS. A configured
411
+ background is included in the exported image; `transparent` preserves transparency.
412
+ The **Theme** selector in the demo's **Chart settings** dialog updates both library components and the demo UI.
413
+
347
414
  ## Grid layers
348
415
 
349
416
  Resistance and reactance are initially visible. Conductance, susceptance, Q,
@@ -354,13 +421,15 @@ chart.layers.resistance.setVisible(false);
354
421
  chart.layers.reactance.setVisible(false);
355
422
  chart.layers.conductance.setVisible(true);
356
423
  chart.layers.susceptance.setVisible(true);
357
- chart.layers.conductance.setMinorVisible(false);
424
+ chart.layers.conductance.setDetail('standard');
425
+ chart.layers.conductance.setLabelsVisible(false);
426
+ chart.layers.susceptance.setLabelsVisible(false);
358
427
  chart.layers.susceptance.setStyle({ stroke: '#64748b', textColor: '#2a7f62' });
359
428
  chart.layers.vswr.addValue(4);
360
429
  chart.layers.vswr.setVisible(true);
361
430
  ```
362
431
 
363
- The four grid layers support `setVisible`, `setMinorVisible`, and
432
+ The four grid layers support `setVisible`, `setLabelsVisible`, `setDetail`, and
364
433
  `setStyle(Partial<GridStyle>)`. Style fields: `stroke`, `majorWidth`, `minorWidth`,
365
434
  `textColor`, `textFontFamily`, `textFontSize`. Widths are positive finite numbers
366
435
  in CSS pixels. Font size is a positive finite base size in chart units; adaptive
@@ -368,6 +437,23 @@ layout raises it when needed to keep the font size at least 9 CSS pixels in the
368
437
  default view. Partial updates retain other settings. Invalid numeric styles throw
369
438
  before any property is changed.
370
439
 
440
+ Choose `setDetail('basic' | 'standard' | 'detailed')` independently for each grid layer:
441
+
442
+ - **Basic:** complete circles and arcs for normalized values 0.2, 0.5, 1, 2, and 5,
443
+ with matching axis/rim labels and the zero label. Intended for small or static charts.
444
+ - **Standard:** the full major grid and its labels, without minor lines.
445
+ - **Detailed** (default): the full major and minor grid.
446
+
447
+ Changing detail preserves layer visibility, styles, and the current view. Label
448
+ collision handling still applies at every level. Peripheral scales and Q/VSWR
449
+ circles are independent. The demo's **Chart settings → Grid detail** selector updates all four grid layers.
450
+
451
+ Use `setLabelsVisible(false)` to hide a grid layer's labels while keeping its lines.
452
+ Labels default to visible. The setting persists through detail, style, theme, and
453
+ whole-layer visibility changes and is included in SVG exports. Calling
454
+ `setLabelsVisible(true)` restores labels allowed by the current detail and collision
455
+ layout; it does not show a hidden layer. The **Grid labels** checkbox in the demo’s **Chart settings** dialog controls all four grid layers together. Peripheral scale labels are unaffected.
456
+
371
457
  `layers.q` and `layers.vswr` support `setVisible`,
372
458
  `setStyle({ stroke?, strokeWidth? })`, `addValue`, and `removeValue`.
373
459
  Circle stroke widths are positive finite numbers in CSS pixels.
@@ -491,9 +577,92 @@ use `RfCalculations.renormalizeSamples` when the file reference differs from
491
577
  `chart.referenceImpedanceOhms`. The parser itself does not renormalize data.
492
578
  Multiport data and Touchstone 2.x are not supported.
493
579
 
580
+ ## PNG and report export
581
+
582
+ `chart.toPng(options?)` and `scales.toPng(options?)` return `Promise<Blob>` with MIME
583
+ type `image/png`. Call them in a browser after mounting the components at non-zero
584
+ sizes, as with SVG export. The current view, labels, colors, markers, and scale
585
+ readings are captured without modifying the live components.
586
+
587
+ ```ts
588
+ import { SmithScales } from 'smithkit';
589
+
590
+ const scales = new SmithScales();
591
+ scales.draw('#scales');
592
+ const png = await chart.toPng({ width: 3200, background: 'white', legend: true });
593
+ // Use the Blob with your application's download, preview, or upload flow.
594
+ const report = await chart.toPng({ width: 2400, scales, legend: true });
595
+ const scalePng = await scales.toPng({ width: 1200, background: 'transparent' });
596
+ ```
597
+
598
+ - `width` / `height`: output pixels. Omit both to use the snapshot's natural size;
599
+ specify one to preserve proportions. With both set, the complete image is
600
+ centered and fitted without distortion or cropping. Padding uses the chosen
601
+ background, or transparency when no background is specified.
602
+ - `background`: omit to keep component backgrounds, or pass a concrete CSS color
603
+ (for example `'#fff'`) or `'transparent'` to replace them. Chart colors stay as
604
+ configured; choose a suitable chart theme for your report background.
605
+ - `legend` (chart only): append the names and colors of visible traces. Long names
606
+ wrap; hidden traces are omitted. Default: `false`.
607
+ - `markerLegend` (chart only): independently append marker descriptions below the chart/scales.
608
+ Default: `false`. `true` includes every marker of visible traces, with trace name,
609
+ marker number, frequency, and impedance. Use an options object to select `markerIds`
610
+ and `fields` (`frequency`, `impedance`, `admittance`, `reflectionCoefficient`, `vswr`,
611
+ `returnLoss`). Fields use Hz with SI prefixes, Ω, mS, dimensionless Γ, VSWR ratios,
612
+ and dB respectively. Empty `markerIds` selects none; empty `fields` keeps only names
613
+ and marker numbers. Hidden traces are always omitted, duplicate selections appear
614
+ once, and unknown IDs/fields throw. Long descriptions wrap. Readings are captured at
615
+ export invocation, without moving markers or changing the live view.
616
+ - `scales` (chart only): a mounted `SmithScales` instance to place centered below the chart.
617
+ Its current responsive layout and readings are preserved. Use `scaleReadout` to
618
+ export a specific reading independently of the live view.
619
+
620
+ ```ts
621
+ // Works with toSvg() as well. Trace and marker legends are independent.
622
+ const png = await chart.toPng({
623
+ legend: false,
624
+ markerLegend: {
625
+ fields: ['frequency', 'impedance', 'vswr'],
626
+ // Omit markerIds to include all markers of visible traces.
627
+ },
628
+ });
629
+ ```
630
+
631
+ Dimensions must be positive integers, at most 8192 px per side and 32 megapixels
632
+ in total. Invalid options and unavailable/destroyed components reject the promise.
633
+ PNG export uses browser SVG decoding and Canvas 2D and releases temporary image URLs.
634
+ Fonts must be available to the browser; external font files are not embedded.
635
+ Export size increases resolution; it does not change the current label density.
636
+
637
+ The demo's **Export** dialog supports chart, scales, or a combined report,
638
+ custom dimensions/background, and independent trace and marker legends. **Include marker legend** reveals field and marker checkboxes; it is disabled for scales-only exports. The default output width is 3200 px. Choose PNG or SVG in the same dialog; both formats support these report options. The demo starts in dark mode. For exports containing parameter scales, **Scale readings** explicitly selects a marker or **None**, independently of the Cursor/Marker tab. Marker exports include the trace name, marker number, and frequency; cursor readings are not exported.
639
+
640
+ To export a specific scale reading without changing the live view, pass `readout` to
641
+ `scales.toSvg()` / `scales.toPng()`, or `scaleReadout` to a combined chart export:
642
+
643
+ ```ts
644
+ import { Complex, SmithScales } from 'smithkit';
645
+
646
+ const scales = new SmithScales();
647
+ scales.draw('#scales');
648
+ const report = await chart.toPng({
649
+ scales,
650
+ scaleReadout: {
651
+ reflectionCoefficient: Complex.from(0.2, 0.3),
652
+ label: 'Antenna · Marker 1 · Frequency: 1 GHz',
653
+ },
654
+ });
655
+ ```
656
+
657
+ Use `{ reflectionCoefficient: null }` for scales without readings. Omit the override to
658
+ preserve the current readings. Labels wrap to fit the exported scales.
659
+
494
660
  ## SVG export
495
661
 
496
- `chart.toSvg()` and `scales.toSvg()` return SVG strings. Call them after `draw()`
662
+ `chart.toSvg(options?)` and `scales.toSvg(options?)` return SVG strings synchronously.
663
+ They accept the same `SmithImageExportOptions` / `ImageExportOptions` as PNG export,
664
+ including size, background, legend, and scales. Omitting options preserves the
665
+ original standalone SVG snapshot. Call them after `draw()`
497
666
  while the component is mounted and has a non-zero rendered size; exporting before
498
667
  mounting, from a `display: none` container, or after destruction throws an error.
499
668
 
@@ -517,12 +686,13 @@ The scale export combines all twelve axes into one SVG using their current layou
517
686
  and displayed readings. To export a marker reading, call `scales.update()` with
518
687
  that marker's reflection coefficient before `scales.toSvg()`.
519
688
 
520
- Files have explicit pixel dimensions and a transparent background. Presentation
689
+ Files have explicit pixel dimensions and preserve the configured component background
690
+ (transparent by default). Presentation
521
691
  styles are inlined so the application's stylesheet is not required. Text remains
522
692
  editable, and curved captions retain their internal path references. Font files
523
693
  and surrounding HTML controls/backgrounds are not embedded. Downloading is the
524
694
  consumer's responsibility; the library does not create files or modify live nodes.
525
- The demo provides separate **Export chart SVG** and **Export scales SVG** buttons.
695
+ In the demo, choose **Export → Format → SVG**, then select chart, scales, or the combined report.
526
696
 
527
697
  ## Development and license
528
698
 
@@ -1,4 +1,6 @@
1
- import { TraceSamples } from './samples.js';
1
+ import type { SmithImageExportOptions } from './svg/export.js';
2
+ import type { SmithAppearance } from './appearance/types.js';
3
+ import type { TraceSamples } from './samples.js';
2
4
  import type { ChartLayers, PeripheralScales } from './layers.js';
3
5
  import type { SmithReading } from './rf/RfCalculations.js';
4
6
  import type { TraceOptions, TraceUpdateOptions, TraceInfo, MarkerSnapshot, MarkerComparison } from './measurements.js';
@@ -18,63 +20,37 @@ export type SmithEvent = {
18
20
  };
19
21
  export declare class Smith {
20
22
  private referenceOhms;
21
- private calcs;
22
- private scalers;
23
- private readonly defaultTransform;
24
- private transform;
25
- private zoomEnabled;
26
- private applyingView;
27
- private zoomBehavior;
28
- private svg;
29
- private resizeObserver;
30
- private readonly labelLayout;
31
- private container;
32
- private dataContainer;
33
- private markerContainer;
34
- private reactanceAxis;
35
- private constResistance;
36
- private constReactance;
37
- private constConductance;
38
- private constSusceptance;
39
- private constSwrCircles;
40
- private constQCircles;
41
- private cursor;
42
- private peripheralScaleRenderer;
23
+ private theme;
24
+ private readonly renderer;
43
25
  private data;
44
26
  private destroyed;
45
- private mouseGesture;
46
27
  private nextDatasetColor;
47
28
  private nextTraceId;
48
29
  private nextMarkerId;
49
30
  private traceMetadata;
50
31
  private markerIds;
51
32
  private draggedMarkers;
52
- private cursorBeforeMarkerDrag;
53
33
  private listeners;
54
34
  readonly layers: ChartLayers;
55
35
  readonly peripheralScales: PeripheralScales;
56
- constructor(referenceOhms?: number);
36
+ constructor(referenceOhms?: number, appearance?: SmithAppearance);
37
+ /** Replace the appearance while retaining explicit layer styles and trace colors. */
38
+ setAppearance(appearance: SmithAppearance): void;
57
39
  draw(target: string | HTMLElement): void;
58
- /** Export the current mounted view as standalone SVG with a transparent background. */
59
- toSvg(): string;
40
+ /** Export the current mounted view as standalone SVG with its configured background. */
41
+ toSvg(options?: SmithImageExportOptions): string;
42
+ private exportLegend;
43
+ /** Export the mounted view as PNG, optionally with radial scales and a trace legend. */
44
+ toPng(options?: SmithImageExportOptions): Promise<Blob>;
60
45
  /** Remove this chart and release its event handlers. Safe to call more than once. */
61
46
  destroy(): void;
62
- private updateViewportScale;
63
47
  private assertAlive;
64
- private createScalers;
65
- private cursorMove;
66
48
  private markerDragChanged;
67
- private initCursor;
68
49
  /** Last cursor position. Use cursor events to detect pointer leave. */
69
50
  get cursorReading(): SmithReading;
70
- private initializeZoom;
71
51
  /** Enable or disable wheel, double-click, and mouse/touch zoom/pan. Keeps the current view. */
72
52
  setZoomEnabled(enabled: boolean): void;
73
- private applyView;
74
53
  resetView(): void;
75
- private onZoom;
76
- private cursorContainer;
77
- private drawReactanceAxis;
78
54
  /** Add a named trace with one initial marker. Returns a chart-local, stable ID. */
79
55
  addTrace(values: TraceSamples, options?: TraceOptions): string;
80
56
  /** Detached metadata snapshots; IDs and marker display numbers survive removals. */