layerchart 2.0.0-next.15 → 2.0.0-next.17

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 (33) hide show
  1. package/dist/components/Arc.svelte +2 -2
  2. package/dist/components/Blur.svelte +5 -3
  3. package/dist/components/Blur.svelte.d.ts +2 -5
  4. package/dist/components/Connector.svelte +2 -2
  5. package/dist/components/Connector.svelte.d.ts +1 -1
  6. package/dist/components/Ellipse.svelte +187 -0
  7. package/dist/components/Ellipse.svelte.d.ts +64 -0
  8. package/dist/components/ForceSimulation.svelte +122 -38
  9. package/dist/components/ForceSimulation.svelte.d.ts +58 -23
  10. package/dist/components/GeoPath.svelte +5 -4
  11. package/dist/components/GeoPoint.svelte +1 -2
  12. package/dist/components/GeoSpline.svelte +4 -4
  13. package/dist/components/GeoSpline.svelte.d.ts +1 -1
  14. package/dist/components/MonthPath.svelte +11 -8
  15. package/dist/components/MonthPath.svelte.d.ts +4 -3
  16. package/dist/components/Polygon.svelte +285 -0
  17. package/dist/components/Polygon.svelte.d.ts +115 -0
  18. package/dist/components/Spline.svelte +29 -17
  19. package/dist/components/Spline.svelte.d.ts +12 -4
  20. package/dist/components/Treemap.svelte +63 -26
  21. package/dist/components/Treemap.svelte.d.ts +11 -11
  22. package/dist/components/index.d.ts +4 -0
  23. package/dist/components/index.js +4 -0
  24. package/dist/components/layout/Layer.svelte +6 -4
  25. package/dist/components/layout/Layer.svelte.d.ts +6 -4
  26. package/dist/components/tooltip/TooltipList.svelte +1 -1
  27. package/dist/utils/canvas.js +15 -0
  28. package/dist/utils/path.d.ts +10 -0
  29. package/dist/utils/path.js +30 -0
  30. package/dist/utils/shape.d.ts +43 -0
  31. package/dist/utils/shape.js +59 -0
  32. package/dist/utils/treemap.d.ts +1 -1
  33. package/package.json +1 -1
@@ -87,23 +87,23 @@
87
87
  /**
88
88
  * Add additional content at the start of the line.
89
89
  *
90
- * Receives `{ point: DOMPoint }` as a snippet prop.
90
+ * Receives `{ point: DOMPoint; value: { x: number; y: number } }` as a snippet prop.
91
91
  */
92
- startContent?: Snippet<[{ point: DOMPoint }]>;
92
+ startContent?: Snippet<[{ point: DOMPoint; value: { x: number; y: number } }]>;
93
93
 
94
94
  /**
95
95
  * Add additional content at the end of the line.
96
96
  *
97
- * Receives `{ point: DOMPoint }` as a snippet prop.
97
+ * Receives `{ point: DOMPoint; value: { x: number; y: number } }` as a snippet prop.
98
98
  */
99
- endContent?: Snippet<[{ point: DOMPoint }]>;
99
+ endContent?: Snippet<[{ point: DOMPoint; value: { x: number; y: number } }]>;
100
100
 
101
101
  /**
102
102
  * A reference to the `<path>` element.
103
103
  *
104
104
  * @bindable
105
105
  */
106
- splineRef?: SVGPathElement;
106
+ pathRef?: SVGPathElement;
107
107
 
108
108
  motion?: MotionProp;
109
109
  } & CommonStyleProps;
@@ -159,14 +159,14 @@
159
159
  startContent,
160
160
  endContent,
161
161
  opacity,
162
- splineRef: splineRefProp = $bindable(),
162
+ pathRef: pathRefProp = $bindable(),
163
163
  ...restProps
164
164
  }: SplineProps = $props();
165
165
 
166
- let splineRef = $state<SVGPathElement>();
166
+ let pathRef = $state<SVGPathElement>();
167
167
 
168
168
  $effect.pre(() => {
169
- splineRefProp = splineRef;
169
+ pathRefProp = pathRef;
170
170
  });
171
171
 
172
172
  const markerStart = $derived(markerStartProp ?? marker);
@@ -331,8 +331,8 @@
331
331
  easing: typeof draw === 'object' && draw.easing ? draw.easing : cubicInOut,
332
332
  interpolate() {
333
333
  return (t: number) => {
334
- const totalLength = splineRef?.getTotalLength() ?? 0;
335
- const point = splineRef?.getPointAtLength(totalLength * t);
334
+ const totalLength = pathRef?.getTotalLength() ?? 0;
335
+ const point = pathRef?.getPointAtLength(totalLength * t);
336
336
  return point;
337
337
  };
338
338
  },
@@ -343,10 +343,10 @@
343
343
  $effect(() => {
344
344
  if (!startContent && !endContent) return;
345
345
  d;
346
- if (!splineRef || !splineRef.getTotalLength()) return;
347
- startPoint = splineRef.getPointAtLength(0);
348
- const totalLength = splineRef.getTotalLength();
349
- endPoint.target = splineRef.getPointAtLength(totalLength);
346
+ if (!pathRef || !pathRef.getTotalLength()) return;
347
+ startPoint = pathRef.getPointAtLength(0);
348
+ const totalLength = pathRef.getTotalLength();
349
+ endPoint.target = pathRef.getPointAtLength(totalLength);
350
350
  });
351
351
 
352
352
  $effect(() => {
@@ -377,7 +377,7 @@
377
377
  marker-mid={markerMidId ? `url(#${markerMidId})` : undefined}
378
378
  marker-end={markerEndId ? `url(#${markerEndId})` : undefined}
379
379
  in:drawTransition|global={typeof draw === 'object' ? draw : undefined}
380
- bind:this={splineRef}
380
+ bind:this={pathRef}
381
381
  />
382
382
  <MarkerWrapper id={markerStartId} marker={markerStart} />
383
383
  <MarkerWrapper id={markerMidId} marker={markerMid} />
@@ -385,13 +385,25 @@
385
385
 
386
386
  {#if startContent && startPoint}
387
387
  <Group x={startPoint.x} y={startPoint.y} class={layerClass('spline-g-start')}>
388
- {@render startContent({ point: startPoint })}
388
+ {@render startContent({
389
+ point: startPoint,
390
+ value: {
391
+ x: ctx.xScale?.invert?.(startPoint.x),
392
+ y: ctx.yScale?.invert?.(startPoint.y),
393
+ },
394
+ })}
389
395
  </Group>
390
396
  {/if}
391
397
 
392
398
  {#if endContent && endPoint.current}
393
399
  <Group x={endPoint.current.x} y={endPoint.current.y} class={layerClass('spline-g-end')}>
394
- {@render endContent({ point: endPoint.current })}
400
+ {@render endContent({
401
+ point: endPoint.current,
402
+ value: {
403
+ x: ctx.xScale?.invert?.(endPoint.current.x),
404
+ y: ctx.yScale?.invert?.(endPoint.current.y),
405
+ },
406
+ })}
395
407
  </Group>
396
408
  {/if}
397
409
  {/key}
@@ -67,29 +67,37 @@ export type SplinePropsWithoutHTML = {
67
67
  /**
68
68
  * Add additional content at the start of the line.
69
69
  *
70
- * Receives `{ point: DOMPoint }` as a snippet prop.
70
+ * Receives `{ point: DOMPoint; value: { x: number; y: number } }` as a snippet prop.
71
71
  */
72
72
  startContent?: Snippet<[{
73
73
  point: DOMPoint;
74
+ value: {
75
+ x: number;
76
+ y: number;
77
+ };
74
78
  }]>;
75
79
  /**
76
80
  * Add additional content at the end of the line.
77
81
  *
78
- * Receives `{ point: DOMPoint }` as a snippet prop.
82
+ * Receives `{ point: DOMPoint; value: { x: number; y: number } }` as a snippet prop.
79
83
  */
80
84
  endContent?: Snippet<[{
81
85
  point: DOMPoint;
86
+ value: {
87
+ x: number;
88
+ y: number;
89
+ };
82
90
  }]>;
83
91
  /**
84
92
  * A reference to the `<path>` element.
85
93
  *
86
94
  * @bindable
87
95
  */
88
- splineRef?: SVGPathElement;
96
+ pathRef?: SVGPathElement;
89
97
  motion?: MotionProp;
90
98
  } & CommonStyleProps;
91
99
  export type SplineProps = SplinePropsWithoutHTML & Without<SVGAttributes<SVGPathElement>, SplinePropsWithoutHTML>;
92
100
  import { draw as _drawTransition } from 'svelte/transition';
93
- declare const Spline: import("svelte").Component<SplineProps, {}, "splineRef">;
101
+ declare const Spline: import("svelte").Component<SplineProps, {}, "pathRef">;
94
102
  type Spline = ReturnType<typeof Spline>;
95
103
  export default Spline;
@@ -18,53 +18,53 @@
18
18
  *
19
19
  * @default 0
20
20
  */
21
- padding?: number;
21
+ padding?: number | ((node: HierarchyRectangularNode<T>) => number);
22
22
 
23
23
  /**
24
24
  * The inner padding between nodes.
25
25
  *
26
26
  * @default 0
27
27
  */
28
- paddingInner?: number;
28
+ paddingInner?: number | ((node: HierarchyRectangularNode<T>) => number);
29
29
 
30
30
  /**
31
31
  * The outer padding between nodes.
32
32
  *
33
33
  * @default 0
34
34
  */
35
- paddingOuter?: number;
35
+ paddingOuter?: number | ((node: HierarchyRectangularNode<T>) => number);
36
36
 
37
37
  /**
38
38
  * The top padding between nodes.
39
39
  *
40
40
  * @default 0
41
41
  */
42
- paddingTop?: number;
42
+ paddingTop?: number | ((node: HierarchyRectangularNode<T>) => number);
43
43
 
44
44
  /**
45
45
  * The bottom padding between nodes.
46
46
  *
47
47
  * @default 0
48
48
  */
49
- paddingBottom?: number;
49
+ paddingBottom?: number | ((node: HierarchyRectangularNode<T>) => number);
50
50
  /**
51
51
  * The left padding between nodes.
52
52
  *
53
53
  */
54
- paddingLeft?: number;
54
+ paddingLeft?: number | ((node: HierarchyRectangularNode<T>) => number);
55
55
 
56
56
  /**
57
57
  * The right padding between nodes.
58
58
  *
59
59
  */
60
- paddingRight?: number;
60
+ paddingRight?: number | ((node: HierarchyRectangularNode<T>) => number);
61
61
 
62
62
  /**
63
- * The selected node.
63
+ * Modify tiling function for approapriate aspect ratio when treemap is zoomed in
64
64
  *
65
- * @default null
65
+ * @default false
66
66
  */
67
- selected?: HierarchyRectangularNode<T> | null;
67
+ maintainAspectRatio?: boolean;
68
68
 
69
69
  hierarchy?: HierarchyNode<T>;
70
70
 
@@ -99,7 +99,7 @@
99
99
  paddingBottom = 0,
100
100
  paddingLeft,
101
101
  paddingRight,
102
- selected = $bindable(null),
102
+ maintainAspectRatio = false,
103
103
  children,
104
104
  }: TreemapProps<T> = $props();
105
105
 
@@ -121,45 +121,82 @@
121
121
  : tile
122
122
  );
123
123
 
124
- const treemap = $derived.by(() => {
124
+ const treemapData = $derived.by(() => {
125
125
  const _treemap = d3treemap<T>()
126
126
  .size([ctx.width, ctx.height])
127
- .tile(aspectTile(tileFunc, ctx.width, ctx.height));
127
+ .tile(maintainAspectRatio ? aspectTile(tileFunc, ctx.width, ctx.height) : tileFunc);
128
128
 
129
129
  if (padding) {
130
- _treemap.padding(padding);
130
+ // Make Typescript happy to pick the correct overload
131
+ // TODO: Better way to do this?
132
+ if (typeof padding === 'number') {
133
+ _treemap.padding(padding);
134
+ } else {
135
+ _treemap.padding(padding);
136
+ }
131
137
  }
132
138
 
133
139
  if (paddingInner) {
134
- _treemap.paddingInner(paddingInner);
140
+ if (typeof paddingInner === 'number') {
141
+ _treemap.paddingInner(typeof paddingInner === 'number' ? paddingInner : paddingInner);
142
+ } else {
143
+ _treemap.paddingInner(paddingInner);
144
+ }
135
145
  }
136
146
 
137
147
  if (paddingOuter) {
138
- _treemap.paddingOuter(paddingOuter);
148
+ if (typeof paddingOuter === 'number') {
149
+ _treemap.paddingOuter(paddingOuter);
150
+ } else {
151
+ _treemap.paddingOuter(paddingOuter);
152
+ }
139
153
  }
140
154
 
141
155
  if (paddingTop) {
142
- _treemap.paddingTop(paddingTop);
156
+ if (typeof paddingTop === 'number') {
157
+ _treemap.paddingTop(paddingTop);
158
+ } else {
159
+ _treemap.paddingTop(paddingTop);
160
+ }
143
161
  }
144
162
 
145
163
  if (paddingBottom) {
146
- _treemap.paddingBottom(paddingBottom);
164
+ if (typeof paddingBottom === 'number') {
165
+ _treemap.paddingBottom(paddingBottom);
166
+ } else {
167
+ _treemap.paddingBottom(paddingBottom);
168
+ }
147
169
  }
148
170
 
149
171
  if (paddingLeft) {
150
- _treemap.paddingLeft(paddingLeft);
172
+ if (typeof paddingLeft === 'number') {
173
+ _treemap.paddingLeft(paddingLeft);
174
+ } else {
175
+ _treemap.paddingLeft(paddingLeft);
176
+ }
151
177
  }
152
178
  if (paddingRight) {
153
- _treemap.paddingRight(paddingRight);
179
+ if (typeof paddingRight === 'number') {
180
+ _treemap.paddingRight(paddingRight);
181
+ } else {
182
+ _treemap.paddingRight(paddingRight);
183
+ }
154
184
  }
155
- return _treemap;
156
- });
157
185
 
158
- const treemapData = $derived(hierarchy ? treemap(hierarchy) : null);
186
+ if (hierarchy) {
187
+ const h = hierarchy.copy();
188
+ const treemapData = _treemap(h);
189
+ return {
190
+ links: treemapData.links(),
191
+ nodes: treemapData.descendants(),
192
+ };
193
+ }
159
194
 
160
- $effect.pre(() => {
161
- selected = treemapData;
195
+ return {
196
+ links: [],
197
+ nodes: [],
198
+ };
162
199
  });
163
200
  </script>
164
201
 
165
- {@render children?.({ nodes: treemapData ? treemapData.descendants() : [] })}
202
+ {@render children?.({ nodes: treemapData.nodes })}
@@ -10,47 +10,47 @@ export type TreemapProps<T> = {
10
10
  *
11
11
  * @default 0
12
12
  */
13
- padding?: number;
13
+ padding?: number | ((node: HierarchyRectangularNode<T>) => number);
14
14
  /**
15
15
  * The inner padding between nodes.
16
16
  *
17
17
  * @default 0
18
18
  */
19
- paddingInner?: number;
19
+ paddingInner?: number | ((node: HierarchyRectangularNode<T>) => number);
20
20
  /**
21
21
  * The outer padding between nodes.
22
22
  *
23
23
  * @default 0
24
24
  */
25
- paddingOuter?: number;
25
+ paddingOuter?: number | ((node: HierarchyRectangularNode<T>) => number);
26
26
  /**
27
27
  * The top padding between nodes.
28
28
  *
29
29
  * @default 0
30
30
  */
31
- paddingTop?: number;
31
+ paddingTop?: number | ((node: HierarchyRectangularNode<T>) => number);
32
32
  /**
33
33
  * The bottom padding between nodes.
34
34
  *
35
35
  * @default 0
36
36
  */
37
- paddingBottom?: number;
37
+ paddingBottom?: number | ((node: HierarchyRectangularNode<T>) => number);
38
38
  /**
39
39
  * The left padding between nodes.
40
40
  *
41
41
  */
42
- paddingLeft?: number;
42
+ paddingLeft?: number | ((node: HierarchyRectangularNode<T>) => number);
43
43
  /**
44
44
  * The right padding between nodes.
45
45
  *
46
46
  */
47
- paddingRight?: number;
47
+ paddingRight?: number | ((node: HierarchyRectangularNode<T>) => number);
48
48
  /**
49
- * The selected node.
49
+ * Modify tiling function for approapriate aspect ratio when treemap is zoomed in
50
50
  *
51
- * @default null
51
+ * @default false
52
52
  */
53
- selected?: HierarchyRectangularNode<T> | null;
53
+ maintainAspectRatio?: boolean;
54
54
  hierarchy?: HierarchyNode<T>;
55
55
  children?: Snippet<[{
56
56
  nodes: HierarchyRectangularNode<T>[];
@@ -62,7 +62,7 @@ declare class __sveltets_Render<T> {
62
62
  props(): TreemapProps<T>;
63
63
  events(): {};
64
64
  slots(): {};
65
- bindings(): "selected";
65
+ bindings(): "";
66
66
  exports(): {};
67
67
  }
68
68
  interface $$IsomorphicComponent {
@@ -42,6 +42,8 @@ export { default as Connector } from './Connector.svelte';
42
42
  export * from './Connector.svelte';
43
43
  export { default as Dagre } from './Dagre.svelte';
44
44
  export * from './Dagre.svelte';
45
+ export { default as Ellipse } from './Ellipse.svelte';
46
+ export * from './Ellipse.svelte';
45
47
  export { default as Frame } from './Frame.svelte';
46
48
  export * from './Frame.svelte';
47
49
  export { default as ForceSimulation } from './ForceSimulation.svelte';
@@ -102,6 +104,8 @@ export { default as Point } from './Point.svelte';
102
104
  export * from './Point.svelte';
103
105
  export { default as Points } from './Points.svelte';
104
106
  export * from './Points.svelte';
107
+ export { default as Polygon } from './Polygon.svelte';
108
+ export * from './Polygon.svelte';
105
109
  export { default as RadialGradient } from './RadialGradient.svelte';
106
110
  export * from './RadialGradient.svelte';
107
111
  export { default as Rect } from './Rect.svelte';
@@ -42,6 +42,8 @@ export { default as Connector } from './Connector.svelte';
42
42
  export * from './Connector.svelte';
43
43
  export { default as Dagre } from './Dagre.svelte';
44
44
  export * from './Dagre.svelte';
45
+ export { default as Ellipse } from './Ellipse.svelte';
46
+ export * from './Ellipse.svelte';
45
47
  export { default as Frame } from './Frame.svelte';
46
48
  export * from './Frame.svelte';
47
49
  export { default as ForceSimulation } from './ForceSimulation.svelte';
@@ -102,6 +104,8 @@ export { default as Point } from './Point.svelte';
102
104
  export * from './Point.svelte';
103
105
  export { default as Points } from './Points.svelte';
104
106
  export * from './Points.svelte';
107
+ export { default as Polygon } from './Polygon.svelte';
108
+ export * from './Polygon.svelte';
105
109
  export { default as RadialGradient } from './RadialGradient.svelte';
106
110
  export * from './RadialGradient.svelte';
107
111
  export { default as Rect } from './Rect.svelte';
@@ -3,17 +3,19 @@
3
3
 
4
4
  export type CanvasLayerProps = {
5
5
  type: 'canvas';
6
- } & Omit<ComponentProps<typeof Canvas>, 'type'>;
6
+ } & Omit<ComponentProps<typeof Canvas>, 'type' | 'onpointermove'>;
7
7
 
8
8
  export type HtmlLayerProps = {
9
9
  type: 'html';
10
- } & Omit<ComponentProps<typeof Html>, 'type'>;
10
+ } & Omit<ComponentProps<typeof Html>, 'type' | 'onpointermove'>;
11
11
 
12
12
  export type SvgLayerProps = {
13
13
  type: 'svg';
14
- } & Omit<ComponentProps<typeof Svg>, 'type'>;
14
+ } & Omit<ComponentProps<typeof Svg>, 'type' | 'onpointermove'>;
15
15
 
16
- export type LayerProps = CanvasLayerProps | HtmlLayerProps | SvgLayerProps;
16
+ export type LayerProps = (CanvasLayerProps | HtmlLayerProps | SvgLayerProps) & {
17
+ onpointermove?: (e: PointerEvent) => void;
18
+ };
17
19
  </script>
18
20
 
19
21
  <script lang="ts">
@@ -1,14 +1,16 @@
1
1
  import type { ComponentProps } from 'svelte';
2
2
  export type CanvasLayerProps = {
3
3
  type: 'canvas';
4
- } & Omit<ComponentProps<typeof Canvas>, 'type'>;
4
+ } & Omit<ComponentProps<typeof Canvas>, 'type' | 'onpointermove'>;
5
5
  export type HtmlLayerProps = {
6
6
  type: 'html';
7
- } & Omit<ComponentProps<typeof Html>, 'type'>;
7
+ } & Omit<ComponentProps<typeof Html>, 'type' | 'onpointermove'>;
8
8
  export type SvgLayerProps = {
9
9
  type: 'svg';
10
- } & Omit<ComponentProps<typeof Svg>, 'type'>;
11
- export type LayerProps = CanvasLayerProps | HtmlLayerProps | SvgLayerProps;
10
+ } & Omit<ComponentProps<typeof Svg>, 'type' | 'onpointermove'>;
11
+ export type LayerProps = (CanvasLayerProps | HtmlLayerProps | SvgLayerProps) & {
12
+ onpointermove?: (e: PointerEvent) => void;
13
+ };
12
14
  import Canvas from './Canvas.svelte';
13
15
  import Html from './Html.svelte';
14
16
  import Svg from './Svg.svelte';
@@ -23,7 +23,7 @@
23
23
  bind:this={ref}
24
24
  class={cls(
25
25
  layerClass('tooltip-list'),
26
- 'grid grid-cols-[1fr_auto] gap-x-2 gap-y-1 items-center',
26
+ 'grid grid-cols-[1fr_auto] gap-x-2 gap-y-1 items-start',
27
27
  className
28
28
  )}
29
29
  {...restProps}
@@ -24,6 +24,8 @@ export function getComputedStyles(canvas, { styles, classes } = {}) {
24
24
  if (styles) {
25
25
  Object.assign(svg.style, styles);
26
26
  }
27
+ // Make sure `<svg>` is not visible
28
+ svg.style.display = 'none';
27
29
  if (classes) {
28
30
  svg.setAttribute('class', cls(classes)
29
31
  .split(' ')
@@ -145,6 +147,19 @@ export function renderCircle(ctx, coords, styleOptions = {}) {
145
147
  }, styleOptions);
146
148
  ctx.closePath();
147
149
  }
150
+ export function renderEllipse(ctx, coords, styleOptions = {}) {
151
+ ctx.beginPath();
152
+ ctx.ellipse(coords.cx, coords.cy, coords.rx, coords.ry, 0, 0, 2 * Math.PI);
153
+ render(ctx, {
154
+ fill: (ctx) => {
155
+ ctx.fill();
156
+ },
157
+ stroke: (ctx) => {
158
+ ctx.stroke();
159
+ },
160
+ }, styleOptions);
161
+ ctx.closePath();
162
+ }
148
163
  /** Clear canvas accounting for Canvas `context.translate(...)` */
149
164
  export function clearCanvasContext(ctx, options) {
150
165
  // Clear with negative offset due to Canvas `context.translate(...)`
@@ -17,5 +17,15 @@ export declare function spikePath({ x, y, width, height, }: {
17
17
  width: number;
18
18
  height: number;
19
19
  }): string;
20
+ /** Create rounded polygon path
21
+ *
22
+ * @param coords - Array of points (x, y)
23
+ * @param radius - Radius of the curve
24
+ * @returns String of path data
25
+ */
26
+ export declare function roundedPolygonPath(coords: {
27
+ x: number;
28
+ y: number;
29
+ }[], radius: number): string;
20
30
  /** Flatten all `y` coordinates to `0` */
21
31
  export declare function flattenPathData(pathData: string, yOverride?: number): string;
@@ -36,6 +36,36 @@ export function spikePath({ x, y, width, height, }) {
36
36
  `;
37
37
  return pathData;
38
38
  }
39
+ /** Create rounded polygon path
40
+ *
41
+ * @param coords - Array of points (x, y)
42
+ * @param radius - Radius of the curve
43
+ * @returns String of path data
44
+ */
45
+ export function roundedPolygonPath(coords, radius) {
46
+ if (radius === 0) {
47
+ // Simple polygon with straight lines
48
+ return `M${coords[0].x},${coords[0].y}${coords
49
+ .slice(1)
50
+ .map((p) => `L${p.x},${p.y}`)
51
+ .join('')}Z`;
52
+ }
53
+ let path = '';
54
+ const length = coords.length + 1;
55
+ for (let i = 0; i < length; i++) {
56
+ const a = coords[i % coords.length];
57
+ const b = coords[(i + 1) % coords.length];
58
+ const t = Math.min(radius / Math.hypot(b.x - a.x, b.y - a.y), 0.5);
59
+ if (i == 0)
60
+ path += `M${a.x * (1 - t) + b.x * t},${a.y * (1 - t) + b.y * t}`;
61
+ if (i > 0)
62
+ path += `Q${a.x},${a.y} ${a.x * (1 - t) + b.x * t},${a.y * (1 - t) + b.y * t}`;
63
+ if (i < length - 1)
64
+ path += `L${a.x * t + b.x * (1 - t)},${a.y * t + b.y * (1 - t)}`;
65
+ }
66
+ path += 'Z';
67
+ return path;
68
+ }
39
69
  /** Flatten all `y` coordinates to `0` */
40
70
  export function flattenPathData(pathData, yOverride = 0) {
41
71
  let result = pathData;
@@ -0,0 +1,43 @@
1
+ /** Get points to create a polygon with given number of points and radius
2
+ *
3
+ * @param count - Number of points
4
+ * @param radius - Radius of the polygon
5
+ * @returns Array of points (angle, radius)
6
+ */
7
+ export declare function polygonPoints(count: number, radius: number, rotate?: number): {
8
+ angle: number;
9
+ radius: number;
10
+ }[];
11
+ /** Create polygon
12
+ *
13
+ * @param cx - Center x coordinate
14
+ * @param cy - Center y coordinate
15
+ * @param count - Number of points
16
+ * @param radius - Radius of the polygon
17
+ * @param rotate - Rotation of the polygon (degrees)
18
+ * @param inset - Percent to inset odd points (<1 inset, >1 outset)
19
+ * @param scaleX - Horizontal stretch factor
20
+ * @param scaleY - Vertical stretch factor
21
+ * @param skewX - Skew angle in degrees along the X axis
22
+ * @param skewY - Skew angle in degrees along the Y axis
23
+ * @param tiltX - Tilt factor for x-coordinates (0 = no tilt, positive moves points top => down, negative moves points bottom => up)
24
+ * @param tiltY - Tilt factor for y-coordinates (0 = no tilt, positive moves points left => right, negative moves points right => left)
25
+ * @returns Array of points (x, y)
26
+ */
27
+ export declare function polygon(options: {
28
+ cx: number;
29
+ cy: number;
30
+ count: number;
31
+ radius: number;
32
+ rotate?: number;
33
+ inset?: number;
34
+ scaleX?: number;
35
+ scaleY?: number;
36
+ skewX?: number;
37
+ skewY?: number;
38
+ tiltX?: number;
39
+ tiltY?: number;
40
+ }): {
41
+ x: number;
42
+ y: number;
43
+ }[];
@@ -0,0 +1,59 @@
1
+ import { range } from 'd3-array';
2
+ import { degreesToRadians } from './math.js';
3
+ /** Get points to create a polygon with given number of points and radius
4
+ *
5
+ * @param count - Number of points
6
+ * @param radius - Radius of the polygon
7
+ * @returns Array of points (angle, radius)
8
+ */
9
+ export function polygonPoints(count, radius, rotate = 0) {
10
+ const angle = 360 / count;
11
+ return range(count).map((index) => {
12
+ return {
13
+ angle: degreesToRadians(angle * index) + degreesToRadians(rotate),
14
+ radius,
15
+ };
16
+ });
17
+ }
18
+ /** Create polygon
19
+ *
20
+ * @param cx - Center x coordinate
21
+ * @param cy - Center y coordinate
22
+ * @param count - Number of points
23
+ * @param radius - Radius of the polygon
24
+ * @param rotate - Rotation of the polygon (degrees)
25
+ * @param inset - Percent to inset odd points (<1 inset, >1 outset)
26
+ * @param scaleX - Horizontal stretch factor
27
+ * @param scaleY - Vertical stretch factor
28
+ * @param skewX - Skew angle in degrees along the X axis
29
+ * @param skewY - Skew angle in degrees along the Y axis
30
+ * @param tiltX - Tilt factor for x-coordinates (0 = no tilt, positive moves points top => down, negative moves points bottom => up)
31
+ * @param tiltY - Tilt factor for y-coordinates (0 = no tilt, positive moves points left => right, negative moves points right => left)
32
+ * @returns Array of points (x, y)
33
+ */
34
+ export function polygon(options) {
35
+ const { cx, cy, count, radius, rotate = 0, inset = 1, scaleX = 1, scaleY = 1, skewX = 0, skewY = 0, tiltX = 0, tiltY = 0, } = options;
36
+ const skewXRad = degreesToRadians(skewX);
37
+ const skewYRad = degreesToRadians(skewY);
38
+ return polygonPoints(count, radius, rotate).map(({ angle, radius }, i) => {
39
+ // inset
40
+ const insetScale = i % 2 == 0 ? 1 : 1 - inset;
41
+ // scale
42
+ let x = radius * insetScale * Math.cos(angle) * scaleX;
43
+ let y = radius * insetScale * Math.sin(angle) * scaleY;
44
+ // tilt
45
+ const normalizedY = (y + radius) / (2 * radius);
46
+ const normalizedX = (x + radius) / (2 * radius);
47
+ const tiltScaleX = tiltX > 0 ? 1 + tiltX * (1 - normalizedY) : 1 - tiltX * normalizedY;
48
+ const tiltScaleY = tiltY > 0 ? 1 + tiltY * (1 - normalizedX) : 1 - tiltY * normalizedX;
49
+ x *= tiltScaleX;
50
+ y *= tiltScaleY;
51
+ // skew
52
+ const xSkewed = x + Math.tan(skewXRad) * y;
53
+ const ySkewed = y + Math.tan(skewYRad) * x;
54
+ return {
55
+ x: cx + xSkewed,
56
+ y: cy + ySkewed,
57
+ };
58
+ });
59
+ }
@@ -9,5 +9,5 @@ export declare function aspectTile(tile: TileFunc, width: number, height: number
9
9
  /**
10
10
  * Show if the node (a) is a child of the selected (b), or any parent above selected
11
11
  */
12
- export declare function isNodeVisible(a: HierarchyNode<any>, b: HierarchyNode<any> | null): boolean;
12
+ export declare function isNodeVisible(a: HierarchyNode<any>, b: HierarchyNode<any> | null | undefined): boolean;
13
13
  export {};