@vielzeug/wire 26.11.23 → 26.11.25

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/bezier.cjs CHANGED
@@ -1,2 +1,2 @@
1
- const e=require("./_sides.cjs"),t=require("./errors.cjs");var n=40;function bezierRoute(r,i,a={}){let o=a.handleLength??n;if(!Number.isFinite(o)||o<0)throw new t.WireConfigError(`Bezier handleLength must be a finite non-negative number, received ${String(o)}.`);let s=e.SIDE_VECTOR[r.side],c=e.SIDE_VECTOR[i.side];return{c1:{x:r.x+s.x*o,y:r.y+s.y*o},c2:{x:i.x+c.x*o,y:i.y+c.y*o},from:{x:r.x,y:r.y},to:{x:i.x,y:i.y}}}exports.bezierRoute=bezierRoute;
1
+ const e=require("./_sides.cjs"),t=require("./errors.cjs");var n=40,r=1.5;function bezierRoute(i,a,o={}){let s=o.handleLength??n;if(!Number.isFinite(s)||s<0)throw new t.WireConfigError(`Bezier handleLength must be a finite non-negative number, received ${String(s)}.`);let c=e.SIDE_VECTOR[i.side],l=e.SIDE_VECTOR[a.side],u=s;if(o.selfLoop&&i.side===a.side){let e=Math.hypot(a.x-i.x,a.y-i.y);u=Math.max(s,e*r)}return{c1:{x:i.x+c.x*u,y:i.y+c.y*u},c2:{x:a.x+l.x*u,y:a.y+l.y*u},from:{x:i.x,y:i.y},to:{x:a.x,y:a.y}}}exports.bezierRoute=bezierRoute;
2
2
  //# sourceMappingURL=bezier.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"bezier.cjs","names":["WireConfigError","SIDE_VECTOR","require__sides","require_errors","DEFAULT_HANDLE_LENGTH","handleLength","options","fromVector","from","toVector","to"],"sources":["../src/bezier.ts"],"sourcesContent":["import { SIDE_VECTOR } from './_sides.js';\nimport { WireConfigError } from './errors.js';\nimport type { CubicBezier, PortAnchor } from './types.js';\n\n/** Default control-point distance for `bezierRoute()` when `handleLength` is omitted. */\nconst DEFAULT_HANDLE_LENGTH = 40;\n\n/** Configuration for `bezierRoute()`. */\nexport interface BezierRouteOptions {\n /** Distance each control point extends outward from its port along the port side. Defaults to 40. */\n handleLength?: number;\n}\n\n/**\n * Builds a cubic bezier segment between two ports.\n *\n * Control points extend outward from each port along its side, so the curve leaves\n * and enters ports perpendicular to the node edge.\n */\nexport function bezierRoute(from: PortAnchor, to: PortAnchor, options: BezierRouteOptions = {}): CubicBezier {\n const handleLength = options.handleLength ?? DEFAULT_HANDLE_LENGTH;\n if (!Number.isFinite(handleLength) || handleLength < 0) {\n throw new WireConfigError(\n `Bezier handleLength must be a finite non-negative number, received ${String(handleLength)}.`,\n );\n }\n\n const fromVector = SIDE_VECTOR[from.side];\n const toVector = SIDE_VECTOR[to.side];\n\n return {\n c1: { x: from.x + fromVector.x * handleLength, y: from.y + fromVector.y * handleLength },\n c2: { x: to.x + toVector.x * handleLength, y: to.y + toVector.y * handleLength },\n from: { x: from.x, y: from.y },\n to: { x: to.x, y: to.y },\n };\n}\n"],"mappings":"A,0DAKA,IAAMI,EAAwB,GAc9B,SAAgB,YAAY,EAAkB,EAAgB,EAA8B,CAAC,EAAgB,CAC3G,IAAMC,EAAeC,EAAQ,cAAgBF,EAC7C,GAAI,CAAC,OAAO,SAASC,CAAY,GAAKA,EAAe,EACnD,MAAM,IAAIL,EAAAA,gBACR,sEAAsE,OAAOK,CAAY,EAAE,EAC7F,EAGF,IAAME,EAAaN,EAAAA,YAAYO,EAAK,MAC9BC,EAAWR,EAAAA,YAAYS,EAAG,MAEhC,MAAO,CACL,GAAI,CAAE,EAAGF,EAAK,EAAID,EAAW,EAAIF,EAAc,EAAGG,EAAK,EAAID,EAAW,EAAIF,CAAa,EACvF,GAAI,CAAE,EAAGK,EAAG,EAAID,EAAS,EAAIJ,EAAc,EAAGK,EAAG,EAAID,EAAS,EAAIJ,CAAa,EAC/E,KAAM,CAAE,EAAGG,EAAK,EAAG,EAAGA,EAAK,CAAE,EAC7B,GAAI,CAAE,EAAGE,EAAG,EAAG,EAAGA,EAAG,CAAE,CACzB,CACF,C"}
1
+ {"version":3,"file":"bezier.cjs","names":["WireConfigError","SIDE_VECTOR","require__sides","require_errors","DEFAULT_HANDLE_LENGTH","SELF_LOOP_HANDLE_RATIO","handleLength","options","fromVector","from","toVector","to","effectiveHandle","spacing"],"sources":["../src/bezier.ts"],"sourcesContent":["import { SIDE_VECTOR } from './_sides.js';\nimport { WireConfigError } from './errors.js';\nimport type { CubicBezier, PortAnchor } from './types.js';\n\n/** Default control-point distance for `bezierRoute()` when `handleLength` is omitted. */\nconst DEFAULT_HANDLE_LENGTH = 40;\n\n/** Configuration for `bezierRoute()`. */\nexport interface BezierRouteOptions {\n /** Distance each control point extends outward from its port along the port side. Defaults to 40. */\n handleLength?: number;\n /**\n * Widen the handles so an edge whose endpoints sit on the same side of one\n * node forms a visible loop instead of collapsing onto that side. When the\n * ports share a side, each handle grows to `1.5 *` the distance between the\n * ports (never smaller than `handleLength`), keeping the loop symmetric and\n * deterministic. Ignored when the ports face different sides. Defaults to false.\n */\n selfLoop?: boolean;\n}\n\n/** Multiplier applied to the port spacing when `selfLoop` widens the handles. */\nconst SELF_LOOP_HANDLE_RATIO = 1.5;\n\n/**\n * Builds a cubic bezier segment between two ports.\n *\n * Control points extend outward from each port along its side, so the curve leaves\n * and enters ports perpendicular to the node edge. With `selfLoop` and both ports\n * on the same side, the handles stretch to `1.5 *` the port spacing so the curve\n * bulges outward into a loop.\n */\nexport function bezierRoute(from: PortAnchor, to: PortAnchor, options: BezierRouteOptions = {}): CubicBezier {\n const handleLength = options.handleLength ?? DEFAULT_HANDLE_LENGTH;\n if (!Number.isFinite(handleLength) || handleLength < 0) {\n throw new WireConfigError(\n `Bezier handleLength must be a finite non-negative number, received ${String(handleLength)}.`,\n );\n }\n\n const fromVector = SIDE_VECTOR[from.side];\n const toVector = SIDE_VECTOR[to.side];\n\n let effectiveHandle = handleLength;\n if (options.selfLoop && from.side === to.side) {\n const spacing = Math.hypot(to.x - from.x, to.y - from.y);\n effectiveHandle = Math.max(handleLength, spacing * SELF_LOOP_HANDLE_RATIO);\n }\n\n return {\n c1: { x: from.x + fromVector.x * effectiveHandle, y: from.y + fromVector.y * effectiveHandle },\n c2: { x: to.x + toVector.x * effectiveHandle, y: to.y + toVector.y * effectiveHandle },\n from: { x: from.x, y: from.y },\n to: { x: to.x, y: to.y },\n };\n}\n"],"mappings":"A,0DAKA,IAAMI,EAAwB,GAiBxBC,EAAyB,IAU/B,SAAgB,YAAY,EAAkB,EAAgB,EAA8B,CAAC,EAAgB,CAC3G,IAAMC,EAAeC,EAAQ,cAAgBH,EAC7C,GAAI,CAAC,OAAO,SAASE,CAAY,GAAKA,EAAe,EACnD,MAAM,IAAIN,EAAAA,gBACR,sEAAsE,OAAOM,CAAY,EAAE,EAC7F,EAGF,IAAME,EAAaP,EAAAA,YAAYQ,EAAK,MAC9BC,EAAWT,EAAAA,YAAYU,EAAG,MAE5BC,EAAkBN,EACtB,GAAIC,EAAQ,UAAYE,EAAK,OAASE,EAAG,KAAM,CAC7C,IAAME,EAAU,KAAK,MAAMF,EAAG,EAAIF,EAAK,EAAGE,EAAG,EAAIF,EAAK,CAAC,EACvD,EAAkB,KAAK,IAAIH,EAAcO,EAAUR,CAAsB,CAC3E,CAEA,MAAO,CACL,GAAI,CAAE,EAAGI,EAAK,EAAID,EAAW,EAAII,EAAiB,EAAGH,EAAK,EAAID,EAAW,EAAII,CAAgB,EAC7F,GAAI,CAAE,EAAGD,EAAG,EAAID,EAAS,EAAIE,EAAiB,EAAGD,EAAG,EAAID,EAAS,EAAIE,CAAgB,EACrF,KAAM,CAAE,EAAGH,EAAK,EAAG,EAAGA,EAAK,CAAE,EAC7B,GAAI,CAAE,EAAGE,EAAG,EAAG,EAAGA,EAAG,CAAE,CACzB,CACF,C"}
package/dist/bezier.d.ts CHANGED
@@ -3,12 +3,22 @@ import type { CubicBezier, PortAnchor } from './types.js';
3
3
  export interface BezierRouteOptions {
4
4
  /** Distance each control point extends outward from its port along the port side. Defaults to 40. */
5
5
  handleLength?: number;
6
+ /**
7
+ * Widen the handles so an edge whose endpoints sit on the same side of one
8
+ * node forms a visible loop instead of collapsing onto that side. When the
9
+ * ports share a side, each handle grows to `1.5 *` the distance between the
10
+ * ports (never smaller than `handleLength`), keeping the loop symmetric and
11
+ * deterministic. Ignored when the ports face different sides. Defaults to false.
12
+ */
13
+ selfLoop?: boolean;
6
14
  }
7
15
  /**
8
16
  * Builds a cubic bezier segment between two ports.
9
17
  *
10
18
  * Control points extend outward from each port along its side, so the curve leaves
11
- * and enters ports perpendicular to the node edge.
19
+ * and enters ports perpendicular to the node edge. With `selfLoop` and both ports
20
+ * on the same side, the handles stretch to `1.5 *` the port spacing so the curve
21
+ * bulges outward into a loop.
12
22
  */
13
23
  export declare function bezierRoute(from: PortAnchor, to: PortAnchor, options?: BezierRouteOptions): CubicBezier;
14
24
  //# sourceMappingURL=bezier.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bezier.d.ts","sourceRoot":"","sources":["../src/bezier.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAK1D,yCAAyC;AACzC,MAAM,WAAW,kBAAkB;IACjC,qGAAqG;IACrG,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,EAAE,OAAO,GAAE,kBAAuB,GAAG,WAAW,CAiB3G"}
1
+ {"version":3,"file":"bezier.d.ts","sourceRoot":"","sources":["../src/bezier.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAK1D,yCAAyC;AACzC,MAAM,WAAW,kBAAkB;IACjC,qGAAqG;IACrG,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAKD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,EAAE,OAAO,GAAE,kBAAuB,GAAG,WAAW,CAuB3G"}
package/dist/bezier.js CHANGED
@@ -1,27 +1,31 @@
1
1
  import { SIDE_VECTOR as e } from "./_sides.js";
2
2
  import { WireConfigError as t } from "./errors.js";
3
3
  // src/bezier.ts
4
- var n = 40;
5
- function bezierRoute(r, i, a = {}) {
6
- let o = a.handleLength ?? n;
7
- if (!Number.isFinite(o) || o < 0) throw new t(`Bezier handleLength must be a finite non-negative number, received ${String(o)}.`);
8
- let s = e[r.side], c = e[i.side];
4
+ var n = 40, r = 1.5;
5
+ function bezierRoute(i, a, o = {}) {
6
+ let s = o.handleLength ?? n;
7
+ if (!Number.isFinite(s) || s < 0) throw new t(`Bezier handleLength must be a finite non-negative number, received ${String(s)}.`);
8
+ let c = e[i.side], l = e[a.side], u = s;
9
+ if (o.selfLoop && i.side === a.side) {
10
+ let e = Math.hypot(a.x - i.x, a.y - i.y);
11
+ u = Math.max(s, e * r);
12
+ }
9
13
  return {
10
14
  c1: {
11
- x: r.x + s.x * o,
12
- y: r.y + s.y * o
15
+ x: i.x + c.x * u,
16
+ y: i.y + c.y * u
13
17
  },
14
18
  c2: {
15
- x: i.x + c.x * o,
16
- y: i.y + c.y * o
19
+ x: a.x + l.x * u,
20
+ y: a.y + l.y * u
17
21
  },
18
22
  from: {
19
- x: r.x,
20
- y: r.y
21
- },
22
- to: {
23
23
  x: i.x,
24
24
  y: i.y
25
+ },
26
+ to: {
27
+ x: a.x,
28
+ y: a.y
25
29
  }
26
30
  };
27
31
  }
@@ -1 +1 @@
1
- {"version":3,"file":"bezier.js","names":["DEFAULT_HANDLE_LENGTH","handleLength","options","WireConfigError","fromVector","SIDE_VECTOR","from","toVector","to"],"sources":["../src/bezier.ts"],"sourcesContent":["import { SIDE_VECTOR } from './_sides.js';\nimport { WireConfigError } from './errors.js';\nimport type { CubicBezier, PortAnchor } from './types.js';\n\n/** Default control-point distance for `bezierRoute()` when `handleLength` is omitted. */\nconst DEFAULT_HANDLE_LENGTH = 40;\n\n/** Configuration for `bezierRoute()`. */\nexport interface BezierRouteOptions {\n /** Distance each control point extends outward from its port along the port side. Defaults to 40. */\n handleLength?: number;\n}\n\n/**\n * Builds a cubic bezier segment between two ports.\n *\n * Control points extend outward from each port along its side, so the curve leaves\n * and enters ports perpendicular to the node edge.\n */\nexport function bezierRoute(from: PortAnchor, to: PortAnchor, options: BezierRouteOptions = {}): CubicBezier {\n const handleLength = options.handleLength ?? DEFAULT_HANDLE_LENGTH;\n if (!Number.isFinite(handleLength) || handleLength < 0) {\n throw new WireConfigError(\n `Bezier handleLength must be a finite non-negative number, received ${String(handleLength)}.`,\n );\n }\n\n const fromVector = SIDE_VECTOR[from.side];\n const toVector = SIDE_VECTOR[to.side];\n\n return {\n c1: { x: from.x + fromVector.x * handleLength, y: from.y + fromVector.y * handleLength },\n c2: { x: to.x + toVector.x * handleLength, y: to.y + toVector.y * handleLength },\n from: { x: from.x, y: from.y },\n to: { x: to.x, y: to.y },\n };\n}\n"],"mappings":";;;AAKA,IAAMA,IAAwB;AAc9B,SAAgB,YAAY,GAAkB,GAAgB,IAA8B,CAAC,GAAgB;CAC3G,IAAMC,IAAeC,EAAQ,gBAAgBF;CAC7C,IAAI,CAAC,OAAO,SAASC,CAAY,KAAKA,IAAe,GACnD,MAAM,IAAIE,EACR,sEAAsE,OAAOF,CAAY,EAAE,EAC7F;CAGF,IAAMG,IAAaC,EAAYC,EAAK,OAC9BC,IAAWF,EAAYG,EAAG;CAEhC,OAAO;EACL,IAAI;GAAE,GAAGF,EAAK,IAAIF,EAAW,IAAIH;GAAc,GAAGK,EAAK,IAAIF,EAAW,IAAIH;EAAa;EACvF,IAAI;GAAE,GAAGO,EAAG,IAAID,EAAS,IAAIN;GAAc,GAAGO,EAAG,IAAID,EAAS,IAAIN;EAAa;EAC/E,MAAM;GAAE,GAAGK,EAAK;GAAG,GAAGA,EAAK;EAAE;EAC7B,IAAI;GAAE,GAAGE,EAAG;GAAG,GAAGA,EAAG;EAAE;CACzB;AACF"}
1
+ {"version":3,"file":"bezier.js","names":["DEFAULT_HANDLE_LENGTH","SELF_LOOP_HANDLE_RATIO","handleLength","options","WireConfigError","fromVector","SIDE_VECTOR","from","toVector","to","effectiveHandle","spacing"],"sources":["../src/bezier.ts"],"sourcesContent":["import { SIDE_VECTOR } from './_sides.js';\nimport { WireConfigError } from './errors.js';\nimport type { CubicBezier, PortAnchor } from './types.js';\n\n/** Default control-point distance for `bezierRoute()` when `handleLength` is omitted. */\nconst DEFAULT_HANDLE_LENGTH = 40;\n\n/** Configuration for `bezierRoute()`. */\nexport interface BezierRouteOptions {\n /** Distance each control point extends outward from its port along the port side. Defaults to 40. */\n handleLength?: number;\n /**\n * Widen the handles so an edge whose endpoints sit on the same side of one\n * node forms a visible loop instead of collapsing onto that side. When the\n * ports share a side, each handle grows to `1.5 *` the distance between the\n * ports (never smaller than `handleLength`), keeping the loop symmetric and\n * deterministic. Ignored when the ports face different sides. Defaults to false.\n */\n selfLoop?: boolean;\n}\n\n/** Multiplier applied to the port spacing when `selfLoop` widens the handles. */\nconst SELF_LOOP_HANDLE_RATIO = 1.5;\n\n/**\n * Builds a cubic bezier segment between two ports.\n *\n * Control points extend outward from each port along its side, so the curve leaves\n * and enters ports perpendicular to the node edge. With `selfLoop` and both ports\n * on the same side, the handles stretch to `1.5 *` the port spacing so the curve\n * bulges outward into a loop.\n */\nexport function bezierRoute(from: PortAnchor, to: PortAnchor, options: BezierRouteOptions = {}): CubicBezier {\n const handleLength = options.handleLength ?? DEFAULT_HANDLE_LENGTH;\n if (!Number.isFinite(handleLength) || handleLength < 0) {\n throw new WireConfigError(\n `Bezier handleLength must be a finite non-negative number, received ${String(handleLength)}.`,\n );\n }\n\n const fromVector = SIDE_VECTOR[from.side];\n const toVector = SIDE_VECTOR[to.side];\n\n let effectiveHandle = handleLength;\n if (options.selfLoop && from.side === to.side) {\n const spacing = Math.hypot(to.x - from.x, to.y - from.y);\n effectiveHandle = Math.max(handleLength, spacing * SELF_LOOP_HANDLE_RATIO);\n }\n\n return {\n c1: { x: from.x + fromVector.x * effectiveHandle, y: from.y + fromVector.y * effectiveHandle },\n c2: { x: to.x + toVector.x * effectiveHandle, y: to.y + toVector.y * effectiveHandle },\n from: { x: from.x, y: from.y },\n to: { x: to.x, y: to.y },\n };\n}\n"],"mappings":";;;AAKA,IAAMA,IAAwB,IAiBxBC,IAAyB;AAU/B,SAAgB,YAAY,GAAkB,GAAgB,IAA8B,CAAC,GAAgB;CAC3G,IAAMC,IAAeC,EAAQ,gBAAgBH;CAC7C,IAAI,CAAC,OAAO,SAASE,CAAY,KAAKA,IAAe,GACnD,MAAM,IAAIE,EACR,sEAAsE,OAAOF,CAAY,EAAE,EAC7F;CAGF,IAAMG,IAAaC,EAAYC,EAAK,OAC9BC,IAAWF,EAAYG,EAAG,OAE5BC,IAAkBR;CACtB,IAAIC,EAAQ,YAAYI,EAAK,SAASE,EAAG,MAAM;EAC7C,IAAME,IAAU,KAAK,MAAMF,EAAG,IAAIF,EAAK,GAAGE,EAAG,IAAIF,EAAK,CAAC;EACvD,IAAkB,KAAK,IAAIL,GAAcS,IAAUV,CAAsB;CAC3E;CAEA,OAAO;EACL,IAAI;GAAE,GAAGM,EAAK,IAAIF,EAAW,IAAIK;GAAiB,GAAGH,EAAK,IAAIF,EAAW,IAAIK;EAAgB;EAC7F,IAAI;GAAE,GAAGD,EAAG,IAAID,EAAS,IAAIE;GAAiB,GAAGD,EAAG,IAAID,EAAS,IAAIE;EAAgB;EACrF,MAAM;GAAE,GAAGH,EAAK;GAAG,GAAGA,EAAK;EAAE;EAC7B,IAAI;GAAE,GAAGE,EAAG;GAAG,GAAGA,EAAG;EAAE;CACzB;AACF"}
package/dist/edit.cjs ADDED
@@ -0,0 +1,2 @@
1
+ const e=require("./errors.cjs");var t=10,n=6,r=4,i=64;function cubicAt(e,t){let n=1-t,r=n*n*n,i=3*n*n*t,a=3*n*t*t,o=t*t*t;return{x:r*e.from.x+i*e.c1.x+a*e.c2.x+o*e.to.x,y:r*e.from.y+i*e.c1.y+a*e.c2.y+o*e.to.y}}function requirePoint(t,n){if(!Number.isFinite(t.x)||!Number.isFinite(t.y))throw new e.WireConfigError(`Wire edit ${n} must have finite coordinates, received (${t.x}, ${t.y}).`)}function requirePositive(t,n){if(!Number.isFinite(t)||t<=0)throw new e.WireConfigError(`Wire edit ${n} must be a finite positive number, received ${String(t)}.`)}function requireUnitT(t){if(!Number.isFinite(t)||t<0||t>1)throw new e.WireConfigError(`Wire edit t must be a finite number in [0, 1], received ${String(t)}.`)}function arrowhead(r,i,a={}){requirePoint(r,`arrowhead tip`),requirePoint(i,`arrowhead direction`);let o=Math.hypot(i.x,i.y);if(o===0)throw new e.WireConfigError(`Wire edit arrowhead direction must be non-zero.`);let s=a.length??t,c=a.width??n;requirePositive(s,`arrowhead length`),requirePositive(c,`arrowhead width`);let l=i.x/o,u=i.y/o,d=-u,f=l,p=r.x-l*s,m=r.y-u*s,h=c/2;return[{x:r.x,y:r.y},{x:p+d*h,y:m+f*h},{x:p-d*h,y:m-f*h}]}function polylineLengths(e){let t=Array(e.length);t[0]=0;for(let n=1;n<e.length;n++)t[n]=t[n-1]+Math.hypot(e[n].x-e[n-1].x,e[n].y-e[n-1].y);return t}function pointAtPolylineLength(e,t,n){let r=t[t.length-1];if(r===0)return{x:e[0].x,y:e[0].y};let i=Math.min(Math.max(n,0),r),a=1,o=t.length-1;for(;a<o;){let e=a+o>>1;t[e]<i?a=e+1:o=e}let s=e[a-1],c=e[a],l=t[a]-t[a-1];if(l===0)return{x:s.x,y:s.y};let u=(i-t[a-1])/l;return{x:s.x+(c.x-s.x)*u,y:s.y+(c.y-s.y)*u}}function bezierTable(e){let t=Array(65),n=Array(65);for(let n=0;n<=i;n++)t[n]=cubicAt(e,n/i);n[0]=0;for(let e=1;e<=i;e++)n[e]=n[e-1]+Math.hypot(t[e].x-t[e-1].x,t[e].y-t[e-1].y);return{cumulative:n,samples:t,total:n[i]}}function uAtBezierLength(e,t){let n=Math.min(Math.max(t,0),e.total),r=1,a=i;for(;r<a;){let t=r+a>>1;e.cumulative[t]<n?r=t+1:a=t}let o=e.cumulative[r]-e.cumulative[r-1],s=o===0?0:(n-e.cumulative[r-1])/o;return(r-1+s)/i}function sampledLengthAt(e,t){let n=Math.min(Math.max(t,0),1)*i,r=Math.min(Math.floor(n),63),a=n-r;return e.cumulative[r]+a*(e.cumulative[r+1]-e.cumulative[r])}function requireCurve(e,t){requirePoint(e.from,`${t} from`),requirePoint(e.c1,`${t} c1`),requirePoint(e.c2,`${t} c2`),requirePoint(e.to,`${t} to`)}function pointOnPolyline(t,n){if(t.length<2)throw new e.WireConfigError(`Wire edit pointOnPolyline requires at least 2 points, received ${t.length}.`);for(let e=0;e<t.length;e++)requirePoint(t[e],`pointOnPolyline points[${e}]`);requireUnitT(n);let r=polylineLengths(t);return pointAtPolylineLength(t,r,n*r[r.length-1])}function pointOnBezier(e,t){requireCurve(e,`pointOnBezier`),requireUnitT(t);let n=bezierTable(e);return n.total===0?{x:e.from.x,y:e.from.y}:cubicAt(e,uAtBezierLength(n,t*n.total))}function distanceToSegment(e,t,n){let r=n.x-t.x,i=n.y-t.y,a=r*r+i*i;if(a===0)return{distance:Math.hypot(e.x-t.x,e.y-t.y),ratio:0};let o=Math.min(Math.max(((e.x-t.x)*r+(e.y-t.y)*i)/a,0),1),s=t.x+r*o,c=t.y+i*o;return{distance:Math.hypot(e.x-s,e.y-c),ratio:o}}function hitPolyline(t,n,i={}){if(t.length<2)throw new e.WireConfigError(`Wire edit hitPolyline requires at least 2 points, received ${t.length}.`);for(let e=0;e<t.length;e++)requirePoint(t[e],`hitPolyline points[${e}]`);requirePoint(n,`hitPolyline point`);let a=i.threshold??r;requirePositive(a,`hitPolyline threshold`);let o=polylineLengths(t),s=o[o.length-1],c=1/0,l=0;for(let e=1;e<t.length;e++){let r=distanceToSegment(n,t[e-1],t[e]);r.distance<c&&(c=r.distance,l=o[e-1]+r.ratio*(o[e]-o[e-1]))}return c>a?null:{distance:c,t:s===0?0:l/s}}function hitBezier(e,t,n={}){requireCurve(e,`hitBezier`),requirePoint(t,`hitBezier point`);let a=n.threshold??r;requirePositive(a,`hitBezier threshold`);let o=bezierTable(e);if(o.total===0){let n=Math.hypot(t.x-e.from.x,t.y-e.from.y);return n<=a?{distance:n,t:0}:null}let s=0,c=1/0;for(let e=0;e<=i;e++){let n=Math.hypot(t.x-o.samples[e].x,t.y-o.samples[e].y);n<c&&(c=n,s=e)}let l=Math.max(0,(s-1)/i),u=Math.min(1,(s+1)/i);for(let n=0;n<32;n++){let n=l+(u-l)/3,r=u-(u-l)/3,i=cubicAt(e,n),a=cubicAt(e,r);Math.hypot(t.x-i.x,t.y-i.y)<Math.hypot(t.x-a.x,t.y-a.y)?u=r:l=n}let d=(l+u)/2,f=cubicAt(e,d),p=Math.hypot(t.x-f.x,t.y-f.y);return p>a?null:{distance:p,t:sampledLengthAt(o,d)/o.total}}exports.arrowhead=arrowhead,exports.hitBezier=hitBezier,exports.hitPolyline=hitPolyline,exports.pointOnBezier=pointOnBezier,exports.pointOnPolyline=pointOnPolyline;
2
+ //# sourceMappingURL=edit.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.cjs","names":["WireConfigError","require_errors","DEFAULT_ARROWHEAD_LENGTH","DEFAULT_ARROWHEAD_WIDTH","DEFAULT_HIT_THRESHOLD","BEZIER_SAMPLES","v","u","a","b","c","d","curve","value","label","tip","direction","magnitude","length","options","width","ux","uy","px","py","baseX","baseY","half","cumulative","points","i","total","clamped","target","low","high","mid","start","end","segmentLength","ratio","samples","table","span","scaled","cell","t","abx","aby","segmentLengthSq","point","projX","projY","threshold","bestDistance","bestLength","hit","distance","bestIndex","lo","hi","m1","m2","p1","p2","closest"],"sources":["../src/edit.ts"],"sourcesContent":["import { WireConfigError } from './errors.js';\nimport type { CubicBezier, Point } from './types.js';\n\nconst DEFAULT_ARROWHEAD_LENGTH = 10;\nconst DEFAULT_ARROWHEAD_WIDTH = 6;\nconst DEFAULT_HIT_THRESHOLD = 4;\nconst BEZIER_SAMPLES = 64;\n\n/** Evaluates the cubic bezier at raw parameter `u` in `[0, 1]`. */\nfunction cubicAt(curve: CubicBezier, u: number): Point {\n const v = 1 - u;\n const a = v * v * v;\n const b = 3 * v * v * u;\n const c = 3 * v * u * u;\n const d = u * u * u;\n return {\n x: a * curve.from.x + b * curve.c1.x + c * curve.c2.x + d * curve.to.x,\n y: a * curve.from.y + b * curve.c1.y + c * curve.c2.y + d * curve.to.y,\n };\n}\n\n/** Options for {@link arrowhead}. */\nexport interface ArrowheadOptions {\n /** Distance from the tip back along the shaft. Default 10. */\n readonly length?: number;\n /** Full width of the base, perpendicular to the shaft. Default 6. */\n readonly width?: number;\n}\n\n/** Options for the hit-test helpers. */\nexport interface HitOptions {\n /** Maximum distance from the shape for a hit. Default 4. */\n readonly threshold?: number;\n}\n\n/** Where a point hit a shape. */\nexport interface HitResult {\n /** Euclidean distance from the query point to the closest point on the shape. */\n readonly distance: number;\n /** Position along the shape, normalized by arc length in `[0, 1]`. */\n readonly t: number;\n}\n\nfunction requirePoint(value: Point, label: string): void {\n if (!Number.isFinite(value.x) || !Number.isFinite(value.y)) {\n throw new WireConfigError(`Wire edit ${label} must have finite coordinates, received (${value.x}, ${value.y}).`);\n }\n}\n\nfunction requirePositive(value: number, label: string): void {\n if (!Number.isFinite(value) || value <= 0) {\n throw new WireConfigError(`Wire edit ${label} must be a finite positive number, received ${String(value)}.`);\n }\n}\n\nfunction requireUnitT(t: number): void {\n if (!Number.isFinite(t) || t < 0 || t > 1) {\n throw new WireConfigError(`Wire edit t must be a finite number in [0, 1], received ${String(t)}.`);\n }\n}\n\n/**\n * Build a filled triangle that caps a line at `tip`, pointing along\n * `direction`. The apex sits at `tip`; the base sits `length` behind it\n * along `-direction`, `width` apart. `direction` may be any non-zero\n * vector and is normalized internally.\n *\n * @param tip - The apex of the arrowhead.\n * @param direction - The direction the line arrives from, pointing toward the tip.\n * @param options - Head `length` (default 10) and `width` (default 6).\n * @returns `[tip, baseLeft, baseRight]` as three points.\n * @throws {@link WireConfigError} when a point is non-finite, `direction` is\n * zero, or `length`/`width` are not finite positive numbers.\n */\nexport function arrowhead(tip: Point, direction: Point, options: ArrowheadOptions = {}): Point[] {\n requirePoint(tip, 'arrowhead tip');\n requirePoint(direction, 'arrowhead direction');\n const magnitude = Math.hypot(direction.x, direction.y);\n if (magnitude === 0) {\n throw new WireConfigError('Wire edit arrowhead direction must be non-zero.');\n }\n const length = options.length ?? DEFAULT_ARROWHEAD_LENGTH;\n const width = options.width ?? DEFAULT_ARROWHEAD_WIDTH;\n requirePositive(length, 'arrowhead length');\n requirePositive(width, 'arrowhead width');\n const ux = direction.x / magnitude;\n const uy = direction.y / magnitude;\n // Perpendicular unit vector for the base offsets.\n const px = -uy;\n const py = ux;\n const baseX = tip.x - ux * length;\n const baseY = tip.y - uy * length;\n const half = width / 2;\n return [\n { x: tip.x, y: tip.y },\n { x: baseX + px * half, y: baseY + py * half },\n { x: baseX - px * half, y: baseY - py * half },\n ];\n}\n\nfunction polylineLengths(points: readonly Point[]): number[] {\n const cumulative = new Array<number>(points.length);\n cumulative[0] = 0;\n for (let i = 1; i < points.length; i++) {\n cumulative[i] = cumulative[i - 1] + Math.hypot(points[i].x - points[i - 1].x, points[i].y - points[i - 1].y);\n }\n return cumulative;\n}\n\nfunction pointAtPolylineLength(points: readonly Point[], cumulative: readonly number[], target: number): Point {\n const total = cumulative[cumulative.length - 1];\n if (total === 0) {\n return { x: points[0].x, y: points[0].y };\n }\n const clamped = Math.min(Math.max(target, 0), total);\n let low = 1;\n let high = cumulative.length - 1;\n while (low < high) {\n const mid = (low + high) >> 1;\n if (cumulative[mid] < clamped) {\n low = mid + 1;\n } else {\n high = mid;\n }\n }\n const start = points[low - 1];\n const end = points[low];\n const segmentLength = cumulative[low] - cumulative[low - 1];\n if (segmentLength === 0) {\n return { x: start.x, y: start.y };\n }\n const ratio = (clamped - cumulative[low - 1]) / segmentLength;\n return { x: start.x + (end.x - start.x) * ratio, y: start.y + (end.y - start.y) * ratio };\n}\n\n/** Sampled arc-length lookup table for one cubic bezier. */\ninterface BezierTable {\n readonly cumulative: number[];\n readonly samples: Point[];\n readonly total: number;\n}\n\nfunction bezierTable(curve: CubicBezier): BezierTable {\n const samples = new Array<Point>(BEZIER_SAMPLES + 1);\n const cumulative = new Array<number>(BEZIER_SAMPLES + 1);\n for (let i = 0; i <= BEZIER_SAMPLES; i++) {\n samples[i] = cubicAt(curve, i / BEZIER_SAMPLES);\n }\n cumulative[0] = 0;\n for (let i = 1; i <= BEZIER_SAMPLES; i++) {\n cumulative[i] = cumulative[i - 1] + Math.hypot(samples[i].x - samples[i - 1].x, samples[i].y - samples[i - 1].y);\n }\n return { cumulative, samples, total: cumulative[BEZIER_SAMPLES] };\n}\n\n/** Raw bezier parameter `u` whose sampled arc length is `target`. */\nfunction uAtBezierLength(table: BezierTable, target: number): number {\n const clamped = Math.min(Math.max(target, 0), table.total);\n let low = 1;\n let high = BEZIER_SAMPLES;\n while (low < high) {\n const mid = (low + high) >> 1;\n if (table.cumulative[mid] < clamped) {\n low = mid + 1;\n } else {\n high = mid;\n }\n }\n const span = table.cumulative[low] - table.cumulative[low - 1];\n const ratio = span === 0 ? 0 : (clamped - table.cumulative[low - 1]) / span;\n return (low - 1 + ratio) / BEZIER_SAMPLES;\n}\n\n/** Sampled arc length up to raw parameter `u`, interpolated within its cell. */\nfunction sampledLengthAt(table: BezierTable, u: number): number {\n const scaled = Math.min(Math.max(u, 0), 1) * BEZIER_SAMPLES;\n const cell = Math.min(Math.floor(scaled), BEZIER_SAMPLES - 1);\n const ratio = scaled - cell;\n return table.cumulative[cell] + ratio * (table.cumulative[cell + 1] - table.cumulative[cell]);\n}\n\nfunction requireCurve(curve: CubicBezier, label: string): void {\n requirePoint(curve.from, `${label} from`);\n requirePoint(curve.c1, `${label} c1`);\n requirePoint(curve.c2, `${label} c2`);\n requirePoint(curve.to, `${label} to`);\n}\n\n/**\n * Point on a polyline at parameter `t`, where `t` is normalized by total\n * arc length (not by segment index): `t = 0.5` is the point halfway along\n * the wire's length. Exact for polylines.\n *\n * @param points - Two or more finite points.\n * @param t - Position in `[0, 1]` along the total length.\n * @returns The point at that length. A degenerate all-duplicate polyline\n * returns its first point for every `t`.\n * @throws {@link WireConfigError} when `points` has fewer than two finite\n * points or `t` is outside `[0, 1]`.\n */\nexport function pointOnPolyline(points: readonly Point[], t: number): Point {\n if (points.length < 2) {\n throw new WireConfigError(`Wire edit pointOnPolyline requires at least 2 points, received ${points.length}.`);\n }\n for (let i = 0; i < points.length; i++) {\n requirePoint(points[i], `pointOnPolyline points[${i}]`);\n }\n requireUnitT(t);\n const cumulative = polylineLengths(points);\n return pointAtPolylineLength(points, cumulative, t * cumulative[cumulative.length - 1]);\n}\n\n/**\n * Point on a cubic bezier at parameter `t`, where `t` is normalized by arc\n * length: `t = 0.5` is the point halfway along the curve's drawn length,\n * not the raw bezier midpoint.\n *\n * The arc-length mapping is approximated with a fixed lookup table of 64\n * samples, so `t` is accurate to within roughly one sample cell of the\n * true length fraction — sub-pixel for typical wire sizes.\n *\n * @param curve - A cubic bezier with finite endpoints and control points.\n * @param t - Position in `[0, 1]` along the curve length.\n * @returns The point at that length. A degenerate zero-length curve returns\n * its start for every `t`.\n * @throws {@link WireConfigError} when any curve point is non-finite or `t`\n * is outside `[0, 1]`.\n */\nexport function pointOnBezier(curve: CubicBezier, t: number): Point {\n requireCurve(curve, 'pointOnBezier');\n requireUnitT(t);\n const table = bezierTable(curve);\n if (table.total === 0) {\n return { x: curve.from.x, y: curve.from.y };\n }\n return cubicAt(curve, uAtBezierLength(table, t * table.total));\n}\n\nfunction distanceToSegment(point: Point, a: Point, b: Point): { distance: number; ratio: number } {\n const abx = b.x - a.x;\n const aby = b.y - a.y;\n const segmentLengthSq = abx * abx + aby * aby;\n if (segmentLengthSq === 0) {\n return { distance: Math.hypot(point.x - a.x, point.y - a.y), ratio: 0 };\n }\n const ratio = Math.min(Math.max(((point.x - a.x) * abx + (point.y - a.y) * aby) / segmentLengthSq, 0), 1);\n const projX = a.x + abx * ratio;\n const projY = a.y + aby * ratio;\n return { distance: Math.hypot(point.x - projX, point.y - projY), ratio };\n}\n\n/**\n * Closest point on a polyline to `point`, parameterized by arc length.\n * Exact: every segment is tested analytically.\n *\n * @param points - Two or more finite points.\n * @param point - The query point.\n * @param options - `threshold` (default 4): the maximum distance for a hit.\n * @returns The length-normalized `t` and `distance` of the closest point, or\n * `null` when the closest point is farther away than `threshold`.\n * @throws {@link WireConfigError} when `points` has fewer than two finite\n * points, `point` is non-finite, or `threshold` is not finite positive.\n */\nexport function hitPolyline(points: readonly Point[], point: Point, options: HitOptions = {}): HitResult | null {\n if (points.length < 2) {\n throw new WireConfigError(`Wire edit hitPolyline requires at least 2 points, received ${points.length}.`);\n }\n for (let i = 0; i < points.length; i++) {\n requirePoint(points[i], `hitPolyline points[${i}]`);\n }\n requirePoint(point, 'hitPolyline point');\n const threshold = options.threshold ?? DEFAULT_HIT_THRESHOLD;\n requirePositive(threshold, 'hitPolyline threshold');\n const cumulative = polylineLengths(points);\n const total = cumulative[cumulative.length - 1];\n let bestDistance = Infinity;\n let bestLength = 0;\n for (let i = 1; i < points.length; i++) {\n const hit = distanceToSegment(point, points[i - 1], points[i]);\n if (hit.distance < bestDistance) {\n bestDistance = hit.distance;\n bestLength = cumulative[i - 1] + hit.ratio * (cumulative[i] - cumulative[i - 1]);\n }\n }\n if (bestDistance > threshold) {\n return null;\n }\n return { distance: bestDistance, t: total === 0 ? 0 : bestLength / total };\n}\n\n/**\n * Closest point on a cubic bezier to `point`, parameterized by arc length.\n * Uses the same 64-sample lookup table as {@link pointOnBezier}: the nearest\n * sample is refined by ternary search on the true curve, so the reported\n * `distance` is accurate; `t` carries the table's arc-length tolerance.\n *\n * @param curve - A cubic bezier with finite endpoints and control points.\n * @param point - The query point.\n * @param options - `threshold` (default 4): the maximum distance for a hit.\n * @returns The length-normalized `t` and `distance` of the closest point, or\n * `null` when the closest point is farther away than `threshold`.\n * @throws {@link WireConfigError} when any curve point is non-finite,\n * `point` is non-finite, or `threshold` is not finite positive.\n */\nexport function hitBezier(curve: CubicBezier, point: Point, options: HitOptions = {}): HitResult | null {\n requireCurve(curve, 'hitBezier');\n requirePoint(point, 'hitBezier point');\n const threshold = options.threshold ?? DEFAULT_HIT_THRESHOLD;\n requirePositive(threshold, 'hitBezier threshold');\n const table = bezierTable(curve);\n if (table.total === 0) {\n const distance = Math.hypot(point.x - curve.from.x, point.y - curve.from.y);\n return distance <= threshold ? { distance, t: 0 } : null;\n }\n let bestIndex = 0;\n let bestDistance = Infinity;\n for (let i = 0; i <= BEZIER_SAMPLES; i++) {\n const distance = Math.hypot(point.x - table.samples[i].x, point.y - table.samples[i].y);\n if (distance < bestDistance) {\n bestDistance = distance;\n bestIndex = i;\n }\n }\n // Refine within the neighbouring samples with ternary search, clamped to the\n // curve so an endpoint sample never extrapolates past t=0 or t=1.\n let lo = Math.max(0, (bestIndex - 1) / BEZIER_SAMPLES);\n let hi = Math.min(1, (bestIndex + 1) / BEZIER_SAMPLES);\n for (let i = 0; i < 32; i++) {\n const m1 = lo + (hi - lo) / 3;\n const m2 = hi - (hi - lo) / 3;\n const p1 = cubicAt(curve, m1);\n const p2 = cubicAt(curve, m2);\n const d1 = Math.hypot(point.x - p1.x, point.y - p1.y);\n const d2 = Math.hypot(point.x - p2.x, point.y - p2.y);\n if (d1 < d2) {\n hi = m2;\n } else {\n lo = m1;\n }\n }\n const u = (lo + hi) / 2;\n const closest = cubicAt(curve, u);\n const distance = Math.hypot(point.x - closest.x, point.y - closest.y);\n if (distance > threshold) {\n return null;\n }\n return { distance, t: sampledLengthAt(table, u) / table.total };\n}\n"],"mappings":"A,gCAGA,IAAME,EAA2B,GAC3BC,EAA0B,EAC1BC,EAAwB,EACxBC,E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M,EAAIc,EAAG,CAC1C,EACP,EAAKF,EAEL,EAAKD,CAET,CACA,IAAMtD,GAAKoD,EAAKC,GAAM,EAChBK,EAAU,QAAQrD,EAAOL,CAAC,EAC1BkD,EAAW,KAAK,MAAMP,EAAM,EAAIe,EAAQ,EAAGf,EAAM,EAAIe,EAAQ,CAAC,EAIpE,OAHIR,EAAWJ,EACN,KAEF,CAAE,WAAU,EAAG,gBAAgBX,EAAOnC,CAAC,EAAImC,EAAM,KAAM,CAChE,C"}
package/dist/edit.d.ts ADDED
@@ -0,0 +1,93 @@
1
+ import type { CubicBezier, Point } from './types.js';
2
+ /** Options for {@link arrowhead}. */
3
+ export interface ArrowheadOptions {
4
+ /** Distance from the tip back along the shaft. Default 10. */
5
+ readonly length?: number;
6
+ /** Full width of the base, perpendicular to the shaft. Default 6. */
7
+ readonly width?: number;
8
+ }
9
+ /** Options for the hit-test helpers. */
10
+ export interface HitOptions {
11
+ /** Maximum distance from the shape for a hit. Default 4. */
12
+ readonly threshold?: number;
13
+ }
14
+ /** Where a point hit a shape. */
15
+ export interface HitResult {
16
+ /** Euclidean distance from the query point to the closest point on the shape. */
17
+ readonly distance: number;
18
+ /** Position along the shape, normalized by arc length in `[0, 1]`. */
19
+ readonly t: number;
20
+ }
21
+ /**
22
+ * Build a filled triangle that caps a line at `tip`, pointing along
23
+ * `direction`. The apex sits at `tip`; the base sits `length` behind it
24
+ * along `-direction`, `width` apart. `direction` may be any non-zero
25
+ * vector and is normalized internally.
26
+ *
27
+ * @param tip - The apex of the arrowhead.
28
+ * @param direction - The direction the line arrives from, pointing toward the tip.
29
+ * @param options - Head `length` (default 10) and `width` (default 6).
30
+ * @returns `[tip, baseLeft, baseRight]` as three points.
31
+ * @throws {@link WireConfigError} when a point is non-finite, `direction` is
32
+ * zero, or `length`/`width` are not finite positive numbers.
33
+ */
34
+ export declare function arrowhead(tip: Point, direction: Point, options?: ArrowheadOptions): Point[];
35
+ /**
36
+ * Point on a polyline at parameter `t`, where `t` is normalized by total
37
+ * arc length (not by segment index): `t = 0.5` is the point halfway along
38
+ * the wire's length. Exact for polylines.
39
+ *
40
+ * @param points - Two or more finite points.
41
+ * @param t - Position in `[0, 1]` along the total length.
42
+ * @returns The point at that length. A degenerate all-duplicate polyline
43
+ * returns its first point for every `t`.
44
+ * @throws {@link WireConfigError} when `points` has fewer than two finite
45
+ * points or `t` is outside `[0, 1]`.
46
+ */
47
+ export declare function pointOnPolyline(points: readonly Point[], t: number): Point;
48
+ /**
49
+ * Point on a cubic bezier at parameter `t`, where `t` is normalized by arc
50
+ * length: `t = 0.5` is the point halfway along the curve's drawn length,
51
+ * not the raw bezier midpoint.
52
+ *
53
+ * The arc-length mapping is approximated with a fixed lookup table of 64
54
+ * samples, so `t` is accurate to within roughly one sample cell of the
55
+ * true length fraction — sub-pixel for typical wire sizes.
56
+ *
57
+ * @param curve - A cubic bezier with finite endpoints and control points.
58
+ * @param t - Position in `[0, 1]` along the curve length.
59
+ * @returns The point at that length. A degenerate zero-length curve returns
60
+ * its start for every `t`.
61
+ * @throws {@link WireConfigError} when any curve point is non-finite or `t`
62
+ * is outside `[0, 1]`.
63
+ */
64
+ export declare function pointOnBezier(curve: CubicBezier, t: number): Point;
65
+ /**
66
+ * Closest point on a polyline to `point`, parameterized by arc length.
67
+ * Exact: every segment is tested analytically.
68
+ *
69
+ * @param points - Two or more finite points.
70
+ * @param point - The query point.
71
+ * @param options - `threshold` (default 4): the maximum distance for a hit.
72
+ * @returns The length-normalized `t` and `distance` of the closest point, or
73
+ * `null` when the closest point is farther away than `threshold`.
74
+ * @throws {@link WireConfigError} when `points` has fewer than two finite
75
+ * points, `point` is non-finite, or `threshold` is not finite positive.
76
+ */
77
+ export declare function hitPolyline(points: readonly Point[], point: Point, options?: HitOptions): HitResult | null;
78
+ /**
79
+ * Closest point on a cubic bezier to `point`, parameterized by arc length.
80
+ * Uses the same 64-sample lookup table as {@link pointOnBezier}: the nearest
81
+ * sample is refined by ternary search on the true curve, so the reported
82
+ * `distance` is accurate; `t` carries the table's arc-length tolerance.
83
+ *
84
+ * @param curve - A cubic bezier with finite endpoints and control points.
85
+ * @param point - The query point.
86
+ * @param options - `threshold` (default 4): the maximum distance for a hit.
87
+ * @returns The length-normalized `t` and `distance` of the closest point, or
88
+ * `null` when the closest point is farther away than `threshold`.
89
+ * @throws {@link WireConfigError} when any curve point is non-finite,
90
+ * `point` is non-finite, or `threshold` is not finite positive.
91
+ */
92
+ export declare function hitBezier(curve: CubicBezier, point: Point, options?: HitOptions): HitResult | null;
93
+ //# sourceMappingURL=edit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.d.ts","sourceRoot":"","sources":["../src/edit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAoBrD,qCAAqC;AACrC,MAAM,WAAW,gBAAgB;IAC/B,8DAA8D;IAC9D,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,wCAAwC;AACxC,MAAM,WAAW,UAAU;IACzB,4DAA4D;IAC5D,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,iCAAiC;AACjC,MAAM,WAAW,SAAS;IACxB,iFAAiF;IACjF,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,sEAAsE;IACtE,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB;AAoBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,GAAE,gBAAqB,GAAG,KAAK,EAAE,CAwB/F;AA0FD;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,KAAK,CAU1E;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,GAAG,KAAK,CAQlE;AAeD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,GAAE,UAAe,GAAG,SAAS,GAAG,IAAI,CAyB9G;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,GAAE,UAAe,GAAG,SAAS,GAAG,IAAI,CA2CtG"}
package/dist/edit.js ADDED
@@ -0,0 +1,170 @@
1
+ import { WireConfigError as e } from "./errors.js";
2
+ // src/edit.ts
3
+ var t = 10, n = 6, r = 4, i = 64;
4
+ function cubicAt(e, t) {
5
+ let n = 1 - t, r = n * n * n, i = 3 * n * n * t, a = 3 * n * t * t, o = t * t * t;
6
+ return {
7
+ x: r * e.from.x + i * e.c1.x + a * e.c2.x + o * e.to.x,
8
+ y: r * e.from.y + i * e.c1.y + a * e.c2.y + o * e.to.y
9
+ };
10
+ }
11
+ function requirePoint(t, n) {
12
+ if (!Number.isFinite(t.x) || !Number.isFinite(t.y)) throw new e(`Wire edit ${n} must have finite coordinates, received (${t.x}, ${t.y}).`);
13
+ }
14
+ function requirePositive(t, n) {
15
+ if (!Number.isFinite(t) || t <= 0) throw new e(`Wire edit ${n} must be a finite positive number, received ${String(t)}.`);
16
+ }
17
+ function requireUnitT(t) {
18
+ if (!Number.isFinite(t) || t < 0 || t > 1) throw new e(`Wire edit t must be a finite number in [0, 1], received ${String(t)}.`);
19
+ }
20
+ function arrowhead(r, i, a = {}) {
21
+ requirePoint(r, "arrowhead tip"), requirePoint(i, "arrowhead direction");
22
+ let o = Math.hypot(i.x, i.y);
23
+ if (o === 0) throw new e("Wire edit arrowhead direction must be non-zero.");
24
+ let s = a.length ?? t, c = a.width ?? n;
25
+ requirePositive(s, "arrowhead length"), requirePositive(c, "arrowhead width");
26
+ let l = i.x / o, u = i.y / o, d = -u, f = l, p = r.x - l * s, m = r.y - u * s, h = c / 2;
27
+ return [
28
+ {
29
+ x: r.x,
30
+ y: r.y
31
+ },
32
+ {
33
+ x: p + d * h,
34
+ y: m + f * h
35
+ },
36
+ {
37
+ x: p - d * h,
38
+ y: m - f * h
39
+ }
40
+ ];
41
+ }
42
+ function polylineLengths(e) {
43
+ let t = Array(e.length);
44
+ t[0] = 0;
45
+ for (let n = 1; n < e.length; n++) t[n] = t[n - 1] + Math.hypot(e[n].x - e[n - 1].x, e[n].y - e[n - 1].y);
46
+ return t;
47
+ }
48
+ function pointAtPolylineLength(e, t, n) {
49
+ let r = t[t.length - 1];
50
+ if (r === 0) return {
51
+ x: e[0].x,
52
+ y: e[0].y
53
+ };
54
+ let i = Math.min(Math.max(n, 0), r), a = 1, o = t.length - 1;
55
+ for (; a < o;) {
56
+ let e = a + o >> 1;
57
+ t[e] < i ? a = e + 1 : o = e;
58
+ }
59
+ let s = e[a - 1], c = e[a], l = t[a] - t[a - 1];
60
+ if (l === 0) return {
61
+ x: s.x,
62
+ y: s.y
63
+ };
64
+ let u = (i - t[a - 1]) / l;
65
+ return {
66
+ x: s.x + (c.x - s.x) * u,
67
+ y: s.y + (c.y - s.y) * u
68
+ };
69
+ }
70
+ function bezierTable(e) {
71
+ let t = Array(65), n = Array(65);
72
+ for (let n = 0; n <= i; n++) t[n] = cubicAt(e, n / i);
73
+ n[0] = 0;
74
+ for (let e = 1; e <= i; e++) n[e] = n[e - 1] + Math.hypot(t[e].x - t[e - 1].x, t[e].y - t[e - 1].y);
75
+ return {
76
+ cumulative: n,
77
+ samples: t,
78
+ total: n[i]
79
+ };
80
+ }
81
+ function uAtBezierLength(e, t) {
82
+ let n = Math.min(Math.max(t, 0), e.total), r = 1, a = i;
83
+ for (; r < a;) {
84
+ let t = r + a >> 1;
85
+ e.cumulative[t] < n ? r = t + 1 : a = t;
86
+ }
87
+ let o = e.cumulative[r] - e.cumulative[r - 1], s = o === 0 ? 0 : (n - e.cumulative[r - 1]) / o;
88
+ return (r - 1 + s) / i;
89
+ }
90
+ function sampledLengthAt(e, t) {
91
+ let n = Math.min(Math.max(t, 0), 1) * i, r = Math.min(Math.floor(n), 63), a = n - r;
92
+ return e.cumulative[r] + a * (e.cumulative[r + 1] - e.cumulative[r]);
93
+ }
94
+ function requireCurve(e, t) {
95
+ requirePoint(e.from, `${t} from`), requirePoint(e.c1, `${t} c1`), requirePoint(e.c2, `${t} c2`), requirePoint(e.to, `${t} to`);
96
+ }
97
+ function pointOnPolyline(t, n) {
98
+ if (t.length < 2) throw new e(`Wire edit pointOnPolyline requires at least 2 points, received ${t.length}.`);
99
+ for (let e = 0; e < t.length; e++) requirePoint(t[e], `pointOnPolyline points[${e}]`);
100
+ requireUnitT(n);
101
+ let r = polylineLengths(t);
102
+ return pointAtPolylineLength(t, r, n * r[r.length - 1]);
103
+ }
104
+ function pointOnBezier(e, t) {
105
+ requireCurve(e, "pointOnBezier"), requireUnitT(t);
106
+ let n = bezierTable(e);
107
+ return n.total === 0 ? {
108
+ x: e.from.x,
109
+ y: e.from.y
110
+ } : cubicAt(e, uAtBezierLength(n, t * n.total));
111
+ }
112
+ function distanceToSegment(e, t, n) {
113
+ let r = n.x - t.x, i = n.y - t.y, a = r * r + i * i;
114
+ if (a === 0) return {
115
+ distance: Math.hypot(e.x - t.x, e.y - t.y),
116
+ ratio: 0
117
+ };
118
+ let o = Math.min(Math.max(((e.x - t.x) * r + (e.y - t.y) * i) / a, 0), 1), s = t.x + r * o, c = t.y + i * o;
119
+ return {
120
+ distance: Math.hypot(e.x - s, e.y - c),
121
+ ratio: o
122
+ };
123
+ }
124
+ function hitPolyline(t, n, i = {}) {
125
+ if (t.length < 2) throw new e(`Wire edit hitPolyline requires at least 2 points, received ${t.length}.`);
126
+ for (let e = 0; e < t.length; e++) requirePoint(t[e], `hitPolyline points[${e}]`);
127
+ requirePoint(n, "hitPolyline point");
128
+ let a = i.threshold ?? r;
129
+ requirePositive(a, "hitPolyline threshold");
130
+ let o = polylineLengths(t), s = o[o.length - 1], c = Infinity, l = 0;
131
+ for (let e = 1; e < t.length; e++) {
132
+ let r = distanceToSegment(n, t[e - 1], t[e]);
133
+ r.distance < c && (c = r.distance, l = o[e - 1] + r.ratio * (o[e] - o[e - 1]));
134
+ }
135
+ return c > a ? null : {
136
+ distance: c,
137
+ t: s === 0 ? 0 : l / s
138
+ };
139
+ }
140
+ function hitBezier(e, t, n = {}) {
141
+ requireCurve(e, "hitBezier"), requirePoint(t, "hitBezier point");
142
+ let a = n.threshold ?? r;
143
+ requirePositive(a, "hitBezier threshold");
144
+ let o = bezierTable(e);
145
+ if (o.total === 0) {
146
+ let n = Math.hypot(t.x - e.from.x, t.y - e.from.y);
147
+ return n <= a ? {
148
+ distance: n,
149
+ t: 0
150
+ } : null;
151
+ }
152
+ let s = 0, c = Infinity;
153
+ for (let e = 0; e <= i; e++) {
154
+ let n = Math.hypot(t.x - o.samples[e].x, t.y - o.samples[e].y);
155
+ n < c && (c = n, s = e);
156
+ }
157
+ let l = Math.max(0, (s - 1) / i), u = Math.min(1, (s + 1) / i);
158
+ for (let n = 0; n < 32; n++) {
159
+ let n = l + (u - l) / 3, r = u - (u - l) / 3, i = cubicAt(e, n), a = cubicAt(e, r);
160
+ Math.hypot(t.x - i.x, t.y - i.y) < Math.hypot(t.x - a.x, t.y - a.y) ? u = r : l = n;
161
+ }
162
+ let d = (l + u) / 2, f = cubicAt(e, d), p = Math.hypot(t.x - f.x, t.y - f.y);
163
+ return p > a ? null : {
164
+ distance: p,
165
+ t: sampledLengthAt(o, d) / o.total
166
+ };
167
+ }
168
+ export { arrowhead, hitBezier, hitPolyline, pointOnBezier, pointOnPolyline };
169
+
170
+ //# sourceMappingURL=edit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.js","names":["DEFAULT_ARROWHEAD_LENGTH","DEFAULT_ARROWHEAD_WIDTH","DEFAULT_HIT_THRESHOLD","BEZIER_SAMPLES","v","u","a","b","c","d","curve","value","WireConfigError","label","tip","direction","magnitude","length","options","width","ux","uy","px","py","baseX","baseY","half","cumulative","points","i","total","clamped","target","low","high","mid","start","end","segmentLength","ratio","samples","table","span","scaled","cell","t","abx","aby","segmentLengthSq","point","projX","projY","threshold","bestDistance","bestLength","hit","distance","bestIndex","lo","hi","m1","m2","p1","p2","closest"],"sources":["../src/edit.ts"],"sourcesContent":["import { WireConfigError } from './errors.js';\nimport type { CubicBezier, Point } from './types.js';\n\nconst DEFAULT_ARROWHEAD_LENGTH = 10;\nconst DEFAULT_ARROWHEAD_WIDTH = 6;\nconst DEFAULT_HIT_THRESHOLD = 4;\nconst BEZIER_SAMPLES = 64;\n\n/** Evaluates the cubic bezier at raw parameter `u` in `[0, 1]`. */\nfunction cubicAt(curve: CubicBezier, u: number): Point {\n const v = 1 - u;\n const a = v * v * v;\n const b = 3 * v * v * u;\n const c = 3 * v * u * u;\n const d = u * u * u;\n return {\n x: a * curve.from.x + b * curve.c1.x + c * curve.c2.x + d * curve.to.x,\n y: a * curve.from.y + b * curve.c1.y + c * curve.c2.y + d * curve.to.y,\n };\n}\n\n/** Options for {@link arrowhead}. */\nexport interface ArrowheadOptions {\n /** Distance from the tip back along the shaft. Default 10. */\n readonly length?: number;\n /** Full width of the base, perpendicular to the shaft. Default 6. */\n readonly width?: number;\n}\n\n/** Options for the hit-test helpers. */\nexport interface HitOptions {\n /** Maximum distance from the shape for a hit. Default 4. */\n readonly threshold?: number;\n}\n\n/** Where a point hit a shape. */\nexport interface HitResult {\n /** Euclidean distance from the query point to the closest point on the shape. */\n readonly distance: number;\n /** Position along the shape, normalized by arc length in `[0, 1]`. */\n readonly t: number;\n}\n\nfunction requirePoint(value: Point, label: string): void {\n if (!Number.isFinite(value.x) || !Number.isFinite(value.y)) {\n throw new WireConfigError(`Wire edit ${label} must have finite coordinates, received (${value.x}, ${value.y}).`);\n }\n}\n\nfunction requirePositive(value: number, label: string): void {\n if (!Number.isFinite(value) || value <= 0) {\n throw new WireConfigError(`Wire edit ${label} must be a finite positive number, received ${String(value)}.`);\n }\n}\n\nfunction requireUnitT(t: number): void {\n if (!Number.isFinite(t) || t < 0 || t > 1) {\n throw new WireConfigError(`Wire edit t must be a finite number in [0, 1], received ${String(t)}.`);\n }\n}\n\n/**\n * Build a filled triangle that caps a line at `tip`, pointing along\n * `direction`. The apex sits at `tip`; the base sits `length` behind it\n * along `-direction`, `width` apart. `direction` may be any non-zero\n * vector and is normalized internally.\n *\n * @param tip - The apex of the arrowhead.\n * @param direction - The direction the line arrives from, pointing toward the tip.\n * @param options - Head `length` (default 10) and `width` (default 6).\n * @returns `[tip, baseLeft, baseRight]` as three points.\n * @throws {@link WireConfigError} when a point is non-finite, `direction` is\n * zero, or `length`/`width` are not finite positive numbers.\n */\nexport function arrowhead(tip: Point, direction: Point, options: ArrowheadOptions = {}): Point[] {\n requirePoint(tip, 'arrowhead tip');\n requirePoint(direction, 'arrowhead direction');\n const magnitude = Math.hypot(direction.x, direction.y);\n if (magnitude === 0) {\n throw new WireConfigError('Wire edit arrowhead direction must be non-zero.');\n }\n const length = options.length ?? DEFAULT_ARROWHEAD_LENGTH;\n const width = options.width ?? DEFAULT_ARROWHEAD_WIDTH;\n requirePositive(length, 'arrowhead length');\n requirePositive(width, 'arrowhead width');\n const ux = direction.x / magnitude;\n const uy = direction.y / magnitude;\n // Perpendicular unit vector for the base offsets.\n const px = -uy;\n const py = ux;\n const baseX = tip.x - ux * length;\n const baseY = tip.y - uy * length;\n const half = width / 2;\n return [\n { x: tip.x, y: tip.y },\n { x: baseX + px * half, y: baseY + py * half },\n { x: baseX - px * half, y: baseY - py * half },\n ];\n}\n\nfunction polylineLengths(points: readonly Point[]): number[] {\n const cumulative = new Array<number>(points.length);\n cumulative[0] = 0;\n for (let i = 1; i < points.length; i++) {\n cumulative[i] = cumulative[i - 1] + Math.hypot(points[i].x - points[i - 1].x, points[i].y - points[i - 1].y);\n }\n return cumulative;\n}\n\nfunction pointAtPolylineLength(points: readonly Point[], cumulative: readonly number[], target: number): Point {\n const total = cumulative[cumulative.length - 1];\n if (total === 0) {\n return { x: points[0].x, y: points[0].y };\n }\n const clamped = Math.min(Math.max(target, 0), total);\n let low = 1;\n let high = cumulative.length - 1;\n while (low < high) {\n const mid = (low + high) >> 1;\n if (cumulative[mid] < clamped) {\n low = mid + 1;\n } else {\n high = mid;\n }\n }\n const start = points[low - 1];\n const end = points[low];\n const segmentLength = cumulative[low] - cumulative[low - 1];\n if (segmentLength === 0) {\n return { x: start.x, y: start.y };\n }\n const ratio = (clamped - cumulative[low - 1]) / segmentLength;\n return { x: start.x + (end.x - start.x) * ratio, y: start.y + (end.y - start.y) * ratio };\n}\n\n/** Sampled arc-length lookup table for one cubic bezier. */\ninterface BezierTable {\n readonly cumulative: number[];\n readonly samples: Point[];\n readonly total: number;\n}\n\nfunction bezierTable(curve: CubicBezier): BezierTable {\n const samples = new Array<Point>(BEZIER_SAMPLES + 1);\n const cumulative = new Array<number>(BEZIER_SAMPLES + 1);\n for (let i = 0; i <= BEZIER_SAMPLES; i++) {\n samples[i] = cubicAt(curve, i / BEZIER_SAMPLES);\n }\n cumulative[0] = 0;\n for (let i = 1; i <= BEZIER_SAMPLES; i++) {\n cumulative[i] = cumulative[i - 1] + Math.hypot(samples[i].x - samples[i - 1].x, samples[i].y - samples[i - 1].y);\n }\n return { cumulative, samples, total: cumulative[BEZIER_SAMPLES] };\n}\n\n/** Raw bezier parameter `u` whose sampled arc length is `target`. */\nfunction uAtBezierLength(table: BezierTable, target: number): number {\n const clamped = Math.min(Math.max(target, 0), table.total);\n let low = 1;\n let high = BEZIER_SAMPLES;\n while (low < high) {\n const mid = (low + high) >> 1;\n if (table.cumulative[mid] < clamped) {\n low = mid + 1;\n } else {\n high = mid;\n }\n }\n const span = table.cumulative[low] - table.cumulative[low - 1];\n const ratio = span === 0 ? 0 : (clamped - table.cumulative[low - 1]) / span;\n return (low - 1 + ratio) / BEZIER_SAMPLES;\n}\n\n/** Sampled arc length up to raw parameter `u`, interpolated within its cell. */\nfunction sampledLengthAt(table: BezierTable, u: number): number {\n const scaled = Math.min(Math.max(u, 0), 1) * BEZIER_SAMPLES;\n const cell = Math.min(Math.floor(scaled), BEZIER_SAMPLES - 1);\n const ratio = scaled - cell;\n return table.cumulative[cell] + ratio * (table.cumulative[cell + 1] - table.cumulative[cell]);\n}\n\nfunction requireCurve(curve: CubicBezier, label: string): void {\n requirePoint(curve.from, `${label} from`);\n requirePoint(curve.c1, `${label} c1`);\n requirePoint(curve.c2, `${label} c2`);\n requirePoint(curve.to, `${label} to`);\n}\n\n/**\n * Point on a polyline at parameter `t`, where `t` is normalized by total\n * arc length (not by segment index): `t = 0.5` is the point halfway along\n * the wire's length. Exact for polylines.\n *\n * @param points - Two or more finite points.\n * @param t - Position in `[0, 1]` along the total length.\n * @returns The point at that length. A degenerate all-duplicate polyline\n * returns its first point for every `t`.\n * @throws {@link WireConfigError} when `points` has fewer than two finite\n * points or `t` is outside `[0, 1]`.\n */\nexport function pointOnPolyline(points: readonly Point[], t: number): Point {\n if (points.length < 2) {\n throw new WireConfigError(`Wire edit pointOnPolyline requires at least 2 points, received ${points.length}.`);\n }\n for (let i = 0; i < points.length; i++) {\n requirePoint(points[i], `pointOnPolyline points[${i}]`);\n }\n requireUnitT(t);\n const cumulative = polylineLengths(points);\n return pointAtPolylineLength(points, cumulative, t * cumulative[cumulative.length - 1]);\n}\n\n/**\n * Point on a cubic bezier at parameter `t`, where `t` is normalized by arc\n * length: `t = 0.5` is the point halfway along the curve's drawn length,\n * not the raw bezier midpoint.\n *\n * The arc-length mapping is approximated with a fixed lookup table of 64\n * samples, so `t` is accurate to within roughly one sample cell of the\n * true length fraction — sub-pixel for typical wire sizes.\n *\n * @param curve - A cubic bezier with finite endpoints and control points.\n * @param t - Position in `[0, 1]` along the curve length.\n * @returns The point at that length. A degenerate zero-length curve returns\n * its start for every `t`.\n * @throws {@link WireConfigError} when any curve point is non-finite or `t`\n * is outside `[0, 1]`.\n */\nexport function pointOnBezier(curve: CubicBezier, t: number): Point {\n requireCurve(curve, 'pointOnBezier');\n requireUnitT(t);\n const table = bezierTable(curve);\n if (table.total === 0) {\n return { x: curve.from.x, y: curve.from.y };\n }\n return cubicAt(curve, uAtBezierLength(table, t * table.total));\n}\n\nfunction distanceToSegment(point: Point, a: Point, b: Point): { distance: number; ratio: number } {\n const abx = b.x - a.x;\n const aby = b.y - a.y;\n const segmentLengthSq = abx * abx + aby * aby;\n if (segmentLengthSq === 0) {\n return { distance: Math.hypot(point.x - a.x, point.y - a.y), ratio: 0 };\n }\n const ratio = Math.min(Math.max(((point.x - a.x) * abx + (point.y - a.y) * aby) / segmentLengthSq, 0), 1);\n const projX = a.x + abx * ratio;\n const projY = a.y + aby * ratio;\n return { distance: Math.hypot(point.x - projX, point.y - projY), ratio };\n}\n\n/**\n * Closest point on a polyline to `point`, parameterized by arc length.\n * Exact: every segment is tested analytically.\n *\n * @param points - Two or more finite points.\n * @param point - The query point.\n * @param options - `threshold` (default 4): the maximum distance for a hit.\n * @returns The length-normalized `t` and `distance` of the closest point, or\n * `null` when the closest point is farther away than `threshold`.\n * @throws {@link WireConfigError} when `points` has fewer than two finite\n * points, `point` is non-finite, or `threshold` is not finite positive.\n */\nexport function hitPolyline(points: readonly Point[], point: Point, options: HitOptions = {}): HitResult | null {\n if (points.length < 2) {\n throw new WireConfigError(`Wire edit hitPolyline requires at least 2 points, received ${points.length}.`);\n }\n for (let i = 0; i < points.length; i++) {\n requirePoint(points[i], `hitPolyline points[${i}]`);\n }\n requirePoint(point, 'hitPolyline point');\n const threshold = options.threshold ?? DEFAULT_HIT_THRESHOLD;\n requirePositive(threshold, 'hitPolyline threshold');\n const cumulative = polylineLengths(points);\n const total = cumulative[cumulative.length - 1];\n let bestDistance = Infinity;\n let bestLength = 0;\n for (let i = 1; i < points.length; i++) {\n const hit = distanceToSegment(point, points[i - 1], points[i]);\n if (hit.distance < bestDistance) {\n bestDistance = hit.distance;\n bestLength = cumulative[i - 1] + hit.ratio * (cumulative[i] - cumulative[i - 1]);\n }\n }\n if (bestDistance > threshold) {\n return null;\n }\n return { distance: bestDistance, t: total === 0 ? 0 : bestLength / total };\n}\n\n/**\n * Closest point on a cubic bezier to `point`, parameterized by arc length.\n * Uses the same 64-sample lookup table as {@link pointOnBezier}: the nearest\n * sample is refined by ternary search on the true curve, so the reported\n * `distance` is accurate; `t` carries the table's arc-length tolerance.\n *\n * @param curve - A cubic bezier with finite endpoints and control points.\n * @param point - The query point.\n * @param options - `threshold` (default 4): the maximum distance for a hit.\n * @returns The length-normalized `t` and `distance` of the closest point, or\n * `null` when the closest point is farther away than `threshold`.\n * @throws {@link WireConfigError} when any curve point is non-finite,\n * `point` is non-finite, or `threshold` is not finite positive.\n */\nexport function hitBezier(curve: CubicBezier, point: Point, options: HitOptions = {}): HitResult | null {\n requireCurve(curve, 'hitBezier');\n requirePoint(point, 'hitBezier point');\n const threshold = options.threshold ?? DEFAULT_HIT_THRESHOLD;\n requirePositive(threshold, 'hitBezier threshold');\n const table = bezierTable(curve);\n if (table.total === 0) {\n const distance = Math.hypot(point.x - curve.from.x, point.y - curve.from.y);\n return distance <= threshold ? { distance, t: 0 } : null;\n }\n let bestIndex = 0;\n let bestDistance = Infinity;\n for (let i = 0; i <= BEZIER_SAMPLES; i++) {\n const distance = Math.hypot(point.x - table.samples[i].x, point.y - table.samples[i].y);\n if (distance < bestDistance) {\n bestDistance = distance;\n bestIndex = i;\n }\n }\n // Refine within the neighbouring samples with ternary search, clamped to the\n // curve so an endpoint sample never extrapolates past t=0 or t=1.\n let lo = Math.max(0, (bestIndex - 1) / BEZIER_SAMPLES);\n let hi = Math.min(1, (bestIndex + 1) / BEZIER_SAMPLES);\n for (let i = 0; i < 32; i++) {\n const m1 = lo + (hi - lo) / 3;\n const m2 = hi - (hi - lo) / 3;\n const p1 = cubicAt(curve, m1);\n const p2 = cubicAt(curve, m2);\n const d1 = Math.hypot(point.x - p1.x, point.y - p1.y);\n const d2 = Math.hypot(point.x - p2.x, point.y - p2.y);\n if (d1 < d2) {\n hi = m2;\n } else {\n lo = m1;\n }\n }\n const u = (lo + hi) / 2;\n const closest = cubicAt(curve, u);\n const distance = Math.hypot(point.x - closest.x, point.y - closest.y);\n if (distance > threshold) {\n return null;\n }\n return { distance, t: sampledLengthAt(table, u) / table.total };\n}\n"],"mappings":";;AAGA,IAAMA,IAA2B,IAC3BC,IAA0B,GAC1BC,IAAwB,GACxBC,IAAiB;AAGvB,SAAS,QAAQ,GAAoB,GAAkB;CACrD,IAAMC,IAAI,IAAIC,GACRC,IAAIF,IAAIA,IAAIA,GACZG,IAAI,IAAIH,IAAIA,IAAIC,GAChBG,IAAI,IAAIJ,IAAIC,IAAIA,GAChBI,IAAIJ,IAAIA,IAAIA;CAClB,OAAO;EACL,GAAGC,IAAII,EAAM,KAAK,IAAIH,IAAIG,EAAM,GAAG,IAAIF,IAAIE,EAAM,GAAG,IAAID,IAAIC,EAAM,GAAG;EACrE,GAAGJ,IAAII,EAAM,KAAK,IAAIH,IAAIG,EAAM,GAAG,IAAIF,IAAIE,EAAM,GAAG,IAAID,IAAIC,EAAM,GAAG;CACvE;AACF;AAwBA,SAAS,aAAa,GAAc,GAAqB;CACvD,IAAI,CAAC,OAAO,SAASC,EAAM,CAAC,KAAK,CAAC,OAAO,SAASA,EAAM,CAAC,GACvD,MAAM,IAAIC,EAAgB,aAAaC,EAAM,2CAA2CF,EAAM,EAAE,IAAIA,EAAM,EAAE,GAAG;AAEnH;AAEA,SAAS,gBAAgB,GAAe,GAAqB;CAC3D,IAAI,CAAC,OAAO,SAASA,CAAK,KAAKA,KAAS,GACtC,MAAM,IAAIC,EAAgB,aAAaC,EAAM,8CAA8C,OAAOF,CAAK,EAAE,EAAE;AAE/G;AAEA,SAAS,aAAa,GAAiB;CACrC,IAAI,CAAC,OAAO,SAAS,CAAC,KAAK,IAAI,KAAK,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AAI,IAAIF,IAAY,KAAKtD,CAAc;CACrD,KAAK,IAAI0B,IAAI,GAAGA,IAAI,IAAI,KAAK;EAC3B,IAAM+B,IAAKF,KAAMC,IAAKD,KAAM,GACtBG,IAAKF,KAAMA,IAAKD,KAAM,GACtBI,IAAK,QAAQpD,GAAOkD,CAAE,GACtBG,IAAK,QAAQrD,GAAOmD,CAAE;EAG5B,AAFW,KAAK,MAAMZ,EAAM,IAAIa,EAAG,GAAGb,EAAM,IAAIa,EAAG,CAE/C,IADO,KAAK,MAAMb,EAAM,IAAIc,EAAG,GAAGd,EAAM,IAAIc,EAAG,CAC1C,IACP,IAAKF,IAEL,IAAKD;CAET;CACA,IAAMvD,KAAKqD,IAAKC,KAAM,GAChBK,IAAU,QAAQtD,GAAOL,CAAC,GAC1BmD,IAAW,KAAK,MAAMP,EAAM,IAAIe,EAAQ,GAAGf,EAAM,IAAIe,EAAQ,CAAC;CAIpE,OAHIR,IAAWJ,IACN,OAEF;EAAE;EAAU,GAAG,gBAAgBX,GAAOpC,CAAC,IAAIoC,EAAM;CAAM;AAChE"}
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./bezier.cjs"),t=require("./errors.cjs"),n=require("./graph.cjs"),r=require("./orthogonal.cjs"),i=require("./path.cjs"),a=require("./ports.cjs");exports.WireConfigError=t.WireConfigError,exports.WireError=t.WireError,exports.WireGraphError=t.WireGraphError,exports.bezierRoute=e.bezierRoute,exports.bezierToSvgPath=i.bezierToSvgPath,exports.findCyclicGroups=n.findCyclicGroups,exports.orthogonalRoute=r.orthogonalRoute,exports.polylineToSvgPath=i.polylineToSvgPath,exports.portAnchor=a.portAnchor,exports.topologicalOrder=n.topologicalOrder;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./bezier.cjs"),t=require("./edit.cjs"),n=require("./errors.cjs"),r=require("./graph.cjs"),i=require("./orthogonal.cjs"),a=require("./path.cjs"),o=require("./ports.cjs");exports.WireConfigError=n.WireConfigError,exports.WireError=n.WireError,exports.WireGraphError=n.WireGraphError,exports.arrowhead=t.arrowhead,exports.bezierRoute=e.bezierRoute,exports.bezierToSvgPath=a.bezierToSvgPath,exports.findCyclicGroups=r.findCyclicGroups,exports.floatingSide=o.floatingSide,exports.hitBezier=t.hitBezier,exports.hitPolyline=t.hitPolyline,exports.orthogonalRoute=i.orthogonalRoute,exports.pointOnBezier=t.pointOnBezier,exports.pointOnPolyline=t.pointOnPolyline,exports.polylineToSvgPath=a.polylineToSvgPath,exports.portAnchor=o.portAnchor,exports.topologicalOrder=r.topologicalOrder;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  export { type BezierRouteOptions, bezierRoute } from './bezier.js';
2
+ export { type ArrowheadOptions, arrowhead, type HitOptions, type HitResult, hitBezier, hitPolyline, pointOnBezier, pointOnPolyline, } from './edit.js';
2
3
  export { WireConfigError, WireError, WireGraphError } from './errors.js';
3
4
  export { findCyclicGroups, topologicalOrder } from './graph.js';
4
5
  export { type OrthogonalRouteOptions, orthogonalRoute } from './orthogonal.js';
5
- export { bezierToSvgPath, polylineToSvgPath } from './path.js';
6
- export { portAnchor } from './ports.js';
6
+ export { bezierToSvgPath, type PolylinePathOptions, polylineToSvgPath } from './path.js';
7
+ export { floatingSide, portAnchor } from './ports.js';
7
8
  export type { CubicBezier, Edge, Point, PortAnchor, PortSide, Rect } from './types.js';
8
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,kBAAkB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAChE,OAAO,EAAE,KAAK,sBAAsB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,YAAY,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,kBAAkB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EACL,KAAK,gBAAgB,EACrB,SAAS,EACT,KAAK,UAAU,EACf,KAAK,SAAS,EACd,SAAS,EACT,WAAW,EACX,aAAa,EACb,eAAe,GAChB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAChE,OAAO,EAAE,KAAK,sBAAsB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,KAAK,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACtD,YAAY,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { bezierRoute as e } from "./bezier.js";
2
- import { WireConfigError as t, WireError as n, WireGraphError as r } from "./errors.js";
3
- import { findCyclicGroups as i, topologicalOrder as a } from "./graph.js";
4
- import { orthogonalRoute as o } from "./orthogonal.js";
5
- import { bezierToSvgPath as s, polylineToSvgPath as c } from "./path.js";
6
- import { portAnchor as l } from "./ports.js";
7
- export { t as WireConfigError, n as WireError, r as WireGraphError, e as bezierRoute, s as bezierToSvgPath, i as findCyclicGroups, o as orthogonalRoute, c as polylineToSvgPath, l as portAnchor, a as topologicalOrder };
2
+ import { arrowhead as t, hitBezier as n, hitPolyline as r, pointOnBezier as i, pointOnPolyline as a } from "./edit.js";
3
+ import { WireConfigError as o, WireError as s, WireGraphError as c } from "./errors.js";
4
+ import { findCyclicGroups as l, topologicalOrder as u } from "./graph.js";
5
+ import { orthogonalRoute as d } from "./orthogonal.js";
6
+ import { bezierToSvgPath as f, polylineToSvgPath as p } from "./path.js";
7
+ import { floatingSide as m, portAnchor as h } from "./ports.js";
8
+ export { o as WireConfigError, s as WireError, c as WireGraphError, t as arrowhead, e as bezierRoute, f as bezierToSvgPath, l as findCyclicGroups, m as floatingSide, n as hitBezier, r as hitPolyline, d as orthogonalRoute, i as pointOnBezier, a as pointOnPolyline, p as polylineToSvgPath, h as portAnchor, u as topologicalOrder };
package/dist/path.cjs CHANGED
@@ -1,2 +1,2 @@
1
- const e=require("./_format.cjs");function polylineToSvgPath(t){let n=t[0];if(!n)return``;let r=[`M ${e.fmt(n.x)} ${e.fmt(n.y)}`];for(let n=1;n<t.length;n+=1){let i=t[n];r.push(`L ${e.fmt(i.x)} ${e.fmt(i.y)}`)}return r.join(` `)}function bezierToSvgPath(t){return`M ${e.fmt(t.from.x)} ${e.fmt(t.from.y)} C ${e.fmt(t.c1.x)} ${e.fmt(t.c1.y)}, ${e.fmt(t.c2.x)} ${e.fmt(t.c2.y)}, ${e.fmt(t.to.x)} ${e.fmt(t.to.y)}`}exports.bezierToSvgPath=bezierToSvgPath,exports.polylineToSvgPath=polylineToSvgPath;
1
+ const e=require("./_format.cjs"),t=require("./errors.cjs");var n=1e-9;function polylineToSvgPath(r,i={}){let a=i.cornerRadius??0;if(!Number.isFinite(a)||a<0)throw new t.WireConfigError(`Path cornerRadius must be a finite non-negative number, received ${String(a)}.`);let o=r[0];if(!o)return``;let s=r[r.length-1],c=[`M ${e.fmt(o.x)} ${e.fmt(o.y)}`];if(a===0){for(let t=1;t<r.length;t+=1){let n=r[t];c.push(`L ${e.fmt(n.x)} ${e.fmt(n.y)}`)}return c.join(` `)}for(let t=1;t<r.length-1;t+=1){let i=r[t-1],o=r[t],s=r[t+1],l=Math.hypot(o.x-i.x,o.y-i.y),u=Math.hypot(s.x-o.x,s.y-o.y);if(l===0||u===0)continue;let d=(o.x-i.x)/l,f=(o.y-i.y)/l,p=(s.x-o.x)/u,m=(s.y-o.y)/u;if(Math.abs(d*m-f*p)<n)continue;let h=Math.min(a,l/2,u/2);c.push(`L ${e.fmt(o.x-d*h)} ${e.fmt(o.y-f*h)}`),c.push(`Q ${e.fmt(o.x)} ${e.fmt(o.y)}, ${e.fmt(o.x+p*h)} ${e.fmt(o.y+m*h)}`)}return c.push(`L ${e.fmt(s.x)} ${e.fmt(s.y)}`),c.join(` `)}function bezierToSvgPath(t){return`M ${e.fmt(t.from.x)} ${e.fmt(t.from.y)} C ${e.fmt(t.c1.x)} ${e.fmt(t.c1.y)}, ${e.fmt(t.c2.x)} ${e.fmt(t.c2.y)}, ${e.fmt(t.to.x)} ${e.fmt(t.to.y)}`}exports.bezierToSvgPath=bezierToSvgPath,exports.polylineToSvgPath=polylineToSvgPath;
2
2
  //# sourceMappingURL=path.cjs.map
package/dist/path.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"path.cjs","names":["fmt","require__format","first","points","parts","i","point","curve"],"sources":["../src/path.ts"],"sourcesContent":["import { fmt } from './_format.js';\nimport type { CubicBezier, Point } from './types.js';\n\n/** Formats a polyline as an SVG path `d` string using absolute moveto and lineto commands. */\nexport function polylineToSvgPath(points: readonly Point[]): string {\n const first = points[0];\n if (!first) return '';\n const parts = [`M ${fmt(first.x)} ${fmt(first.y)}`];\n for (let i = 1; i < points.length; i += 1) {\n const point = points[i] as Point;\n parts.push(`L ${fmt(point.x)} ${fmt(point.y)}`);\n }\n return parts.join(' ');\n}\n\n/** Formats a cubic bezier segment as an SVG path `d` string using absolute curveto commands. */\nexport function bezierToSvgPath(curve: CubicBezier): string {\n return `M ${fmt(curve.from.x)} ${fmt(curve.from.y)} C ${fmt(curve.c1.x)} ${fmt(curve.c1.y)}, ${fmt(curve.c2.x)} ${fmt(curve.c2.y)}, ${fmt(curve.to.x)} ${fmt(curve.to.y)}`;\n}\n"],"mappings":"A,iCAIA,SAAgB,kBAAkB,EAAkC,CAClE,IAAME,EAAQC,EAAO,GACrB,GAAI,CAACD,EAAO,MAAO,GACnB,IAAME,EAAQ,CAAC,KAAKJ,EAAAA,IAAIE,EAAM,CAAC,EAAE,GAAGF,EAAAA,IAAIE,EAAM,CAAC,GAAG,EAClD,IAAK,IAAIG,EAAI,EAAGA,EAAIF,EAAO,OAAQ,GAAK,EAAG,CACzC,IAAMG,EAAQH,EAAOE,GACrBD,EAAM,KAAK,KAAKJ,EAAAA,IAAIM,EAAM,CAAC,EAAE,GAAGN,EAAAA,IAAIM,EAAM,CAAC,GAAG,CAChD,CACA,OAAOF,EAAM,KAAK,GAAG,CACvB,CAGA,SAAgB,gBAAgB,EAA4B,CAC1D,MAAO,KAAKJ,EAAAA,IAAIO,EAAM,KAAK,CAAC,EAAE,GAAGP,EAAAA,IAAIO,EAAM,KAAK,CAAC,EAAE,KAAKP,EAAAA,IAAIO,EAAM,GAAG,CAAC,EAAE,GAAGP,EAAAA,IAAIO,EAAM,GAAG,CAAC,EAAE,IAAIP,EAAAA,IAAIO,EAAM,GAAG,CAAC,EAAE,GAAGP,EAAAA,IAAIO,EAAM,GAAG,CAAC,EAAE,IAAIP,EAAAA,IAAIO,EAAM,GAAG,CAAC,EAAE,GAAGP,EAAAA,IAAIO,EAAM,GAAG,CAAC,GACzK,C"}
1
+ {"version":3,"file":"path.cjs","names":["WireConfigError","fmt","require__format","require_errors","COLLINEAR_EPSILON","cornerRadius","options","first","points","last","parts","i","point","prev","cur","next","inLen","outLen","inX","inY","outX","outY","radius","curve"],"sources":["../src/path.ts"],"sourcesContent":["import { fmt } from './_format.js';\nimport { WireConfigError } from './errors.js';\nimport type { CubicBezier, Point } from './types.js';\n\n/** Configuration for `polylineToSvgPath()`. */\nexport interface PolylinePathOptions {\n /**\n * Radius used to round interior corners with quadratic arcs (`Q` commands).\n * Each corner is clamped to half of its shortest adjacent run, so tight\n * bends never overlap. `0` or omitted keeps sharp `L` corners.\n */\n cornerRadius?: number;\n}\n\n/** Below this normalized cross product, three points count as collinear. */\nconst COLLINEAR_EPSILON = 1e-9;\n\n/**\n * Formats a polyline as an SVG path `d` string using absolute moveto and lineto\n * commands. With `cornerRadius`, interior corners are replaced by entry/exit runs\n * joined by a quadratic `Q` arc; the corner point itself is not emitted.\n */\nexport function polylineToSvgPath(points: readonly Point[], options: PolylinePathOptions = {}): string {\n const cornerRadius = options.cornerRadius ?? 0;\n if (!Number.isFinite(cornerRadius) || cornerRadius < 0) {\n throw new WireConfigError(\n `Path cornerRadius must be a finite non-negative number, received ${String(cornerRadius)}.`,\n );\n }\n const first = points[0];\n if (!first) return '';\n const last = points[points.length - 1] as Point;\n const parts = [`M ${fmt(first.x)} ${fmt(first.y)}`];\n if (cornerRadius === 0) {\n for (let i = 1; i < points.length; i += 1) {\n const point = points[i] as Point;\n parts.push(`L ${fmt(point.x)} ${fmt(point.y)}`);\n }\n return parts.join(' ');\n }\n // Rounded mode: interior corners become entry/exit runs joined by a Q arc;\n // the corner point itself is never emitted, collinear points pass through.\n for (let i = 1; i < points.length - 1; i += 1) {\n const prev = points[i - 1] as Point;\n const cur = points[i] as Point;\n const next = points[i + 1] as Point;\n const inLen = Math.hypot(cur.x - prev.x, cur.y - prev.y);\n const outLen = Math.hypot(next.x - cur.x, next.y - cur.y);\n // Zero-length runs and near-collinear points have no corner to round.\n if (inLen === 0 || outLen === 0) continue;\n const inX = (cur.x - prev.x) / inLen;\n const inY = (cur.y - prev.y) / inLen;\n const outX = (next.x - cur.x) / outLen;\n const outY = (next.y - cur.y) / outLen;\n if (Math.abs(inX * outY - inY * outX) < COLLINEAR_EPSILON) continue;\n const radius = Math.min(cornerRadius, inLen / 2, outLen / 2);\n parts.push(`L ${fmt(cur.x - inX * radius)} ${fmt(cur.y - inY * radius)}`);\n parts.push(`Q ${fmt(cur.x)} ${fmt(cur.y)}, ${fmt(cur.x + outX * radius)} ${fmt(cur.y + outY * radius)}`);\n }\n parts.push(`L ${fmt(last.x)} ${fmt(last.y)}`);\n return parts.join(' ');\n}\n\n/** Formats a cubic bezier segment as an SVG path `d` string using absolute curveto commands. */\nexport function bezierToSvgPath(curve: CubicBezier): string {\n return `M ${fmt(curve.from.x)} ${fmt(curve.from.y)} C ${fmt(curve.c1.x)} ${fmt(curve.c1.y)}, ${fmt(curve.c2.x)} ${fmt(curve.c2.y)}, ${fmt(curve.to.x)} ${fmt(curve.to.y)}`;\n}\n"],"mappings":"A,2DAeA,IAAMI,EAAoB,KAO1B,SAAgB,kBAAkB,EAA0B,EAA+B,CAAC,EAAW,CACrG,IAAMC,EAAeC,EAAQ,cAAgB,EAC7C,GAAI,CAAC,OAAO,SAASD,CAAY,GAAKA,EAAe,EACnD,MAAM,IAAIL,EAAAA,gBACR,oEAAoE,OAAOK,CAAY,EAAE,EAC3F,EAEF,IAAME,EAAQC,EAAO,GACrB,GAAI,CAACD,EAAO,MAAO,GACnB,IAAME,EAAOD,EAAOA,EAAO,OAAS,GAC9BE,EAAQ,CAAC,KAAKT,EAAAA,IAAIM,EAAM,CAAC,EAAE,GAAGN,EAAAA,IAAIM,EAAM,CAAC,GAAG,EAClD,GAAIF,IAAiB,EAAG,CACtB,IAAK,IAAIM,EAAI,EAAGA,EAAIH,EAAO,OAAQ,GAAK,EAAG,CACzC,IAAMI,EAAQJ,EAAOG,GACrBD,EAAM,KAAK,KAAKT,EAAAA,IAAIW,EAAM,CAAC,EAAE,GAAGX,EAAAA,IAAIW,EAAM,CAAC,GAAG,CAChD,CACA,OAAOF,EAAM,KAA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AE,GAAGtB,EAAAA,IAAIsB,EAAM,KAAK,CAAC,EAAE,KAAKtB,EAAAA,IAAIsB,EAAM,GAAG,CAAC,EAAE,GAAGtB,EAAAA,IAAIsB,EAAM,GAAG,CAAC,EAAE,IAAItB,EAAAA,IAAIsB,EAAM,GAAG,CAAC,EAAE,GAAGtB,EAAAA,IAAIsB,EAAM,GAAG,CAAC,EAAE,IAAItB,EAAAA,IAAIsB,EAAM,GAAG,CAAC,EAAE,GAAGtB,EAAAA,IAAIsB,EAAM,GAAG,CAAC,GACzK,C"}
package/dist/path.d.ts CHANGED
@@ -1,6 +1,19 @@
1
1
  import type { CubicBezier, Point } from './types.js';
2
- /** Formats a polyline as an SVG path `d` string using absolute moveto and lineto commands. */
3
- export declare function polylineToSvgPath(points: readonly Point[]): string;
2
+ /** Configuration for `polylineToSvgPath()`. */
3
+ export interface PolylinePathOptions {
4
+ /**
5
+ * Radius used to round interior corners with quadratic arcs (`Q` commands).
6
+ * Each corner is clamped to half of its shortest adjacent run, so tight
7
+ * bends never overlap. `0` or omitted keeps sharp `L` corners.
8
+ */
9
+ cornerRadius?: number;
10
+ }
11
+ /**
12
+ * Formats a polyline as an SVG path `d` string using absolute moveto and lineto
13
+ * commands. With `cornerRadius`, interior corners are replaced by entry/exit runs
14
+ * joined by a quadratic `Q` arc; the corner point itself is not emitted.
15
+ */
16
+ export declare function polylineToSvgPath(points: readonly Point[], options?: PolylinePathOptions): string;
4
17
  /** Formats a cubic bezier segment as an SVG path `d` string using absolute curveto commands. */
5
18
  export declare function bezierToSvgPath(curve: CubicBezier): string;
6
19
  //# sourceMappingURL=path.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../src/path.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAErD,8FAA8F;AAC9F,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,MAAM,CASlE;AAED,gGAAgG;AAChG,wBAAgB,eAAe,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAE1D"}
1
+ {"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../src/path.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAErD,+CAA+C;AAC/C,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAKD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,EAAE,OAAO,GAAE,mBAAwB,GAAG,MAAM,CAuCrG;AAED,gGAAgG;AAChG,wBAAgB,eAAe,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAE1D"}