@krainovsd/graph 0.13.0-beta1 → 0.13.0-beta3
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/lib/cjs/index.cjs +258 -74
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/esm/module/GraphCanvas/constants/link-settings.js +1 -0
- package/lib/esm/module/GraphCanvas/constants/link-settings.js.map +1 -1
- package/lib/esm/module/GraphCanvas/lib/utils/calculate-curve-link-position-by-node.js +42 -0
- package/lib/esm/module/GraphCanvas/lib/utils/calculate-curve-link-position-by-node.js.map +1 -0
- package/lib/esm/module/GraphCanvas/lib/utils/calculate-link-position-by-node.js +37 -35
- package/lib/esm/module/GraphCanvas/lib/utils/calculate-link-position-by-node.js.map +1 -1
- package/lib/esm/module/GraphCanvas/lib/utils/link-by-pointer-getter.js +72 -22
- package/lib/esm/module/GraphCanvas/lib/utils/link-by-pointer-getter.js.map +1 -1
- package/lib/esm/module/GraphCanvas/slices/draw-links.js +70 -17
- package/lib/esm/module/GraphCanvas/slices/draw-links.js.map +1 -1
- package/lib/esm/module/GraphCanvas/slices/init-pointer.js +6 -0
- package/lib/esm/module/GraphCanvas/slices/init-pointer.js.map +1 -1
- package/lib/esm/module/GraphCanvas/slices/init-resize.js +7 -0
- package/lib/esm/module/GraphCanvas/slices/init-resize.js.map +1 -1
- package/lib/esm/module/GraphCanvas/slices/update-link-cache.js +30 -0
- package/lib/esm/module/GraphCanvas/slices/update-link-cache.js.map +1 -1
- package/lib/index.d.ts +22 -7
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"link-settings.js","sources":["../../../../../src/module/GraphCanvas/constants/link-settings.ts"],"sourcesContent":["import type { LinkOptionsInterface, LinkSettingsInterface } from \"../types/link-settings\";\n\nexport const LINK_SETTINGS: Omit<\n Required<LinkSettingsInterface<Record<string, unknown>, Record<string, unknown>>>,\n \"options\"\n> = {\n prettyDraw: true,\n linkScaleSwitch: 1,\n linkColorAfterScaleSwitch: \"#000000FF\",\n linkColorBeforeScaleSwitch: \"#999\",\n linkWidthAfterScaleSwitch: 0.1,\n linkWidthBeforeScaleSwitch: 1,\n arrow: true,\n arrowByHighlight: true,\n particles: true,\n particleFlexSpeed: true,\n particleFlexSpeedCoefficient: 4,\n};\n\nexport const LINK_OPTIONS: Omit<\n Required<LinkOptionsInterface<Record<string, unknown>, Record<string, unknown>>>,\n \"color\" | \"width\" | \"drawLink\" | \"drawExtraLink\" | \"arrowColor\"\n> = {\n alpha: 1,\n arrowAlpha: 1,\n arrowSize: 2,\n arrowBorderColor: \"#000000FF\",\n arrowBorderWidth: 0.1,\n particleAlpha: 1,\n particleColor: \"#000000FF\",\n particleBorderColor: \"#000000FF\",\n particleBorderWidth: 0.1,\n particleCount: 2,\n particleRadius: 0.5,\n particleSteps: 60,\n};\n"],"names":[],"mappings":"AAEa,MAAA,aAAa,GAGtB;AACF,IAAA,UAAU,EAAE,IAAI;AAChB,IAAA,eAAe,EAAE,CAAC;AAClB,IAAA,yBAAyB,EAAE,WAAW;AACtC,IAAA,0BAA0B,EAAE,MAAM;AAClC,IAAA,yBAAyB,EAAE,GAAG;AAC9B,IAAA,0BAA0B,EAAE,CAAC;AAC7B,IAAA,KAAK,EAAE,IAAI;AACX,IAAA,gBAAgB,EAAE,IAAI;AACtB,IAAA,SAAS,EAAE,IAAI;AACf,IAAA,iBAAiB,EAAE,IAAI;AACvB,IAAA,4BAA4B,EAAE,CAAC;;AAGpB,MAAA,YAAY,GAGrB;AACF,IAAA,KAAK,EAAE,CAAC;AACR,IAAA,UAAU,EAAE,CAAC;AACb,IAAA,SAAS,EAAE,CAAC;AACZ,IAAA,gBAAgB,EAAE,WAAW;AAC7B,IAAA,gBAAgB,EAAE,GAAG;AACrB,IAAA,aAAa,EAAE,CAAC;AAChB,IAAA,aAAa,EAAE,WAAW;AAC1B,IAAA,mBAAmB,EAAE,WAAW;AAChC,IAAA,mBAAmB,EAAE,GAAG;AACxB,IAAA,aAAa,EAAE,CAAC;AAChB,IAAA,cAAc,EAAE,GAAG;AACnB,IAAA,aAAa,EAAE,EAAE;;;;;"}
|
|
1
|
+
{"version":3,"file":"link-settings.js","sources":["../../../../../src/module/GraphCanvas/constants/link-settings.ts"],"sourcesContent":["import type { LinkOptionsInterface, LinkSettingsInterface } from \"../types/link-settings\";\n\nexport const LINK_SETTINGS: Omit<\n Required<LinkSettingsInterface<Record<string, unknown>, Record<string, unknown>>>,\n \"options\"\n> = {\n prettyDraw: true,\n curve: false,\n linkScaleSwitch: 1,\n linkColorAfterScaleSwitch: \"#000000FF\",\n linkColorBeforeScaleSwitch: \"#999\",\n linkWidthAfterScaleSwitch: 0.1,\n linkWidthBeforeScaleSwitch: 1,\n arrow: true,\n arrowByHighlight: true,\n particles: true,\n particleFlexSpeed: true,\n particleFlexSpeedCoefficient: 4,\n};\n\nexport const LINK_OPTIONS: Omit<\n Required<LinkOptionsInterface<Record<string, unknown>, Record<string, unknown>>>,\n \"color\" | \"width\" | \"drawLink\" | \"drawExtraLink\" | \"arrowColor\"\n> = {\n alpha: 1,\n arrowAlpha: 1,\n arrowSize: 2,\n arrowBorderColor: \"#000000FF\",\n arrowBorderWidth: 0.1,\n particleAlpha: 1,\n particleColor: \"#000000FF\",\n particleBorderColor: \"#000000FF\",\n particleBorderWidth: 0.1,\n particleCount: 2,\n particleRadius: 0.5,\n particleSteps: 60,\n};\n"],"names":[],"mappings":"AAEa,MAAA,aAAa,GAGtB;AACF,IAAA,UAAU,EAAE,IAAI;AAChB,IAAA,KAAK,EAAE,KAAK;AACZ,IAAA,eAAe,EAAE,CAAC;AAClB,IAAA,yBAAyB,EAAE,WAAW;AACtC,IAAA,0BAA0B,EAAE,MAAM;AAClC,IAAA,yBAAyB,EAAE,GAAG;AAC9B,IAAA,0BAA0B,EAAE,CAAC;AAC7B,IAAA,KAAK,EAAE,IAAI;AACX,IAAA,gBAAgB,EAAE,IAAI;AACtB,IAAA,SAAS,EAAE,IAAI;AACf,IAAA,iBAAiB,EAAE,IAAI;AACvB,IAAA,4BAA4B,EAAE,CAAC;;AAGpB,MAAA,YAAY,GAGrB;AACF,IAAA,KAAK,EAAE,CAAC;AACR,IAAA,UAAU,EAAE,CAAC;AACb,IAAA,SAAS,EAAE,CAAC;AACZ,IAAA,gBAAgB,EAAE,WAAW;AAC7B,IAAA,gBAAgB,EAAE,GAAG;AACrB,IAAA,aAAa,EAAE,CAAC;AAChB,IAAA,aAAa,EAAE,WAAW;AAC1B,IAAA,mBAAmB,EAAE,WAAW;AAChC,IAAA,mBAAmB,EAAE,GAAG;AACxB,IAAA,aAAa,EAAE,CAAC;AAChB,IAAA,cAAc,EAAE,GAAG;AACnB,IAAA,aAAa,EAAE,EAAE;;;;;"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { COMMON_SETTINGS } from '../../constants/common-settings.js';
|
|
2
|
+
import '@krainovsd/js-helpers';
|
|
3
|
+
import 'd3-array';
|
|
4
|
+
|
|
5
|
+
function calculateCurveLinkPositionByNode(xStart, yStart, xEnd, yEnd, sourceNode, targetNode, groupIndex, arrowSize = 0) {
|
|
6
|
+
const sourceRadius = (sourceNode._radius ?? COMMON_SETTINGS.nodeRadius) + (sourceNode._borderWidth ?? 0) * 0.5;
|
|
7
|
+
const targetRadius = (targetNode._radius ?? COMMON_SETTINGS.nodeRadius) + (targetNode._borderWidth ?? 0) * 0.5;
|
|
8
|
+
const effectiveTargetRadius = targetRadius + arrowSize * 0.9;
|
|
9
|
+
const mx = (xStart + xEnd) * 0.5;
|
|
10
|
+
const my = (yStart + yEnd) * 0.5;
|
|
11
|
+
const dx = xEnd - xStart;
|
|
12
|
+
const dy = yEnd - yStart;
|
|
13
|
+
const lenSq = dx * dx + dy * dy;
|
|
14
|
+
const len = lenSq > 0 ? Math.sqrt(lenSq) : 1;
|
|
15
|
+
const nx = -dy / len;
|
|
16
|
+
const ny = dx / len;
|
|
17
|
+
const offset = 0.12 * len * groupIndex;
|
|
18
|
+
const xControl = mx + nx * offset;
|
|
19
|
+
const yControl = my + ny * offset;
|
|
20
|
+
const [xStartP, yStartP] = adjustPoint(xStart, yStart, xControl, yControl, sourceRadius);
|
|
21
|
+
const [xEndP, yEndP] = adjustPoint(xEnd, yEnd, xControl, yControl, effectiveTargetRadius);
|
|
22
|
+
const [xEndArrow, yEndArrow] = adjustPoint(xEnd, yEnd, xControl, yControl, targetRadius);
|
|
23
|
+
return {
|
|
24
|
+
xStart: xStartP,
|
|
25
|
+
yStart: yStartP,
|
|
26
|
+
xEnd: xEndP,
|
|
27
|
+
yEnd: yEndP,
|
|
28
|
+
xControl,
|
|
29
|
+
yControl,
|
|
30
|
+
xEndArrow,
|
|
31
|
+
yEndArrow,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function adjustPoint(x, y, xControl, yControl, radius) {
|
|
35
|
+
const dxToControl = xControl - x;
|
|
36
|
+
const dyToControl = yControl - y;
|
|
37
|
+
const distToControl = Math.sqrt(dxToControl * dxToControl + dyToControl * dyToControl) || 1;
|
|
38
|
+
return [x + (dxToControl / distToControl) * radius, y + (dyToControl / distToControl) * radius];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { calculateCurveLinkPositionByNode };
|
|
42
|
+
//# sourceMappingURL=calculate-curve-link-position-by-node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculate-curve-link-position-by-node.js","sources":["../../../../../../src/module/GraphCanvas/lib/utils/calculate-curve-link-position-by-node.ts"],"sourcesContent":["import { COMMON_SETTINGS } from \"../../constants\";\nimport type { NodeInterface } from \"../../types\";\n\nexport function calculateCurveLinkPositionByNode<NodeData extends Record<string, unknown>>(\n xStart: number,\n yStart: number,\n xEnd: number,\n yEnd: number,\n sourceNode: NodeInterface<NodeData>,\n targetNode: NodeInterface<NodeData>,\n groupIndex: number,\n arrowSize: number = 0,\n) {\n const sourceRadius =\n (sourceNode._radius ?? COMMON_SETTINGS.nodeRadius) + (sourceNode._borderWidth ?? 0) * 0.5;\n const targetRadius =\n (targetNode._radius ?? COMMON_SETTINGS.nodeRadius) + (targetNode._borderWidth ?? 0) * 0.5;\n const effectiveTargetRadius = targetRadius + arrowSize * 0.9;\n\n const mx = (xStart + xEnd) * 0.5;\n const my = (yStart + yEnd) * 0.5;\n const dx = xEnd - xStart;\n const dy = yEnd - yStart;\n const lenSq = dx * dx + dy * dy;\n const len = lenSq > 0 ? Math.sqrt(lenSq) : 1;\n\n const nx = -dy / len;\n const ny = dx / len;\n\n const offset = 0.12 * len * groupIndex;\n\n const xControl = mx + nx * offset;\n const yControl = my + ny * offset;\n\n const [xStartP, yStartP] = adjustPoint(xStart, yStart, xControl, yControl, sourceRadius);\n const [xEndP, yEndP] = adjustPoint(xEnd, yEnd, xControl, yControl, effectiveTargetRadius);\n const [xEndArrow, yEndArrow] = adjustPoint(xEnd, yEnd, xControl, yControl, targetRadius);\n\n return {\n xStart: xStartP,\n yStart: yStartP,\n xEnd: xEndP,\n yEnd: yEndP,\n xControl,\n yControl,\n xEndArrow,\n yEndArrow,\n };\n}\n\nfunction adjustPoint(\n x: number,\n y: number,\n xControl: number,\n yControl: number,\n radius: number,\n): [number, number] {\n const dxToControl = xControl - x;\n const dyToControl = yControl - y;\n const distToControl = Math.sqrt(dxToControl * dxToControl + dyToControl * dyToControl) || 1;\n\n return [x + (dxToControl / distToControl) * radius, y + (dyToControl / distToControl) * radius];\n}\n"],"names":[],"mappings":";;;;SAGgB,gCAAgC,CAC9C,MAAc,EACd,MAAc,EACd,IAAY,EACZ,IAAY,EACZ,UAAmC,EACnC,UAAmC,EACnC,UAAkB,EAClB,YAAoB,CAAC,EAAA;IAErB,MAAM,YAAY,GAChB,CAAC,UAAU,CAAC,OAAO,IAAI,eAAe,CAAC,UAAU,IAAI,CAAC,UAAU,CAAC,YAAY,IAAI,CAAC,IAAI,GAAG;IAC3F,MAAM,YAAY,GAChB,CAAC,UAAU,CAAC,OAAO,IAAI,eAAe,CAAC,UAAU,IA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,MAAM,EAAE,OAAO;AACf,QAAA,IAAI,EAAE,KAAK;AACX,QAAA,IAAI,EAAE,KAAK;QACX,QAAQ;QACR,QAAQ;QACR,SAAS;QACT,SAAS;KACV;AACH;AAEA,SAAS,WAAW,CAClB,CAAS,EACT,CAAS,EACT,QAAgB,EAChB,QAAgB,EAChB,MAAc,EAAA;AAEd,IAAA,MAAM,WAAW,GAAG,QAAQ,GAAG,CAAC;AAChC,IAAA,MAAM,WAAW,GAAG,QAAQ,GAAG,CAAC;AAChC,IAAA,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,GAAG,WAAW,GAAG,WAAW,GAAG,WAAW,CAAC,IAAI,CAAC;IAE3F,OAAO,CAAC,CAAC,GAAG,CAAC,WAAW,GAAG,aAAa,IAAI,MAAM,EAAE,CAAC,GAAG,CAAC,WAAW,GAAG,aAAa,IAAI,MAAM,CAAC;AACjG;;;;"}
|
|
@@ -2,50 +2,52 @@ import { COMMON_SETTINGS } from '../../constants/common-settings.js';
|
|
|
2
2
|
import '@krainovsd/js-helpers';
|
|
3
3
|
import 'd3-array';
|
|
4
4
|
|
|
5
|
-
function calculateLinkPositionByNode(
|
|
6
|
-
const source = link.source;
|
|
7
|
-
const target = link.target;
|
|
8
|
-
if (typeof source != "object" || typeof target != "object")
|
|
9
|
-
return null;
|
|
5
|
+
function calculateLinkPositionByNode(xStart, yStart, xEnd, yEnd, sourceNode, targetNode, arrowSize = 0) {
|
|
10
6
|
const sourcePoint = getLinkPoint({
|
|
7
|
+
xStart,
|
|
8
|
+
yStart,
|
|
9
|
+
xEnd,
|
|
10
|
+
yEnd,
|
|
11
11
|
arrowSize: 0,
|
|
12
|
-
node:
|
|
13
|
-
oppositeNode: target,
|
|
14
|
-
});
|
|
12
|
+
node: sourceNode});
|
|
15
13
|
const targetPoint = getLinkPoint({
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
xStart: xEnd,
|
|
15
|
+
yStart: yEnd,
|
|
16
|
+
xEnd: xStart,
|
|
17
|
+
yEnd: yStart,
|
|
18
|
+
arrowSize: arrowSize > 0 ? arrowSize * 0.9 : 0,
|
|
19
|
+
node: targetNode});
|
|
20
|
+
const targetPointArrow = getLinkPoint({
|
|
21
|
+
xStart: xEnd,
|
|
22
|
+
yStart: yEnd,
|
|
23
|
+
xEnd: xStart,
|
|
24
|
+
yEnd: yStart,
|
|
25
|
+
arrowSize: 0,
|
|
26
|
+
node: targetNode});
|
|
22
27
|
const distance = targetPoint && sourcePoint
|
|
23
28
|
? Math.sqrt((targetPoint.x - sourcePoint.x) ** 2 + (targetPoint.y - sourcePoint.y) ** 2)
|
|
24
29
|
: 0;
|
|
25
30
|
return {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
xStart: sourcePoint.x,
|
|
32
|
+
yStart: sourcePoint.y,
|
|
33
|
+
xEnd: targetPoint.x,
|
|
34
|
+
yEnd: targetPoint.y,
|
|
35
|
+
xEndArrow: targetPointArrow.x,
|
|
36
|
+
yEndArrow: targetPointArrow.y,
|
|
30
37
|
distance,
|
|
31
38
|
};
|
|
32
39
|
}
|
|
33
40
|
function getLinkPoint(opts) {
|
|
34
|
-
if (opts.node.x == undefined ||
|
|
35
|
-
opts.node.y == undefined ||
|
|
36
|
-
opts.oppositeNode.x == undefined ||
|
|
37
|
-
opts.oppositeNode.y == undefined)
|
|
38
|
-
return null;
|
|
39
41
|
let nodePoint;
|
|
40
42
|
switch (opts.node._shape) {
|
|
41
43
|
case "circle": {
|
|
42
44
|
nodePoint = getCircleIntersection({
|
|
43
|
-
x: opts.
|
|
44
|
-
y: opts.
|
|
45
|
+
x: opts.xStart,
|
|
46
|
+
y: opts.yStart,
|
|
45
47
|
radius: (opts.node._radius ?? COMMON_SETTINGS.nodeRadius) +
|
|
46
48
|
(opts.node._borderWidth ? opts.node._borderWidth / 2 : 0),
|
|
47
|
-
oppositeX: opts.
|
|
48
|
-
oppositeY: opts.
|
|
49
|
+
oppositeX: opts.xEnd,
|
|
50
|
+
oppositeY: opts.yEnd,
|
|
49
51
|
arrowSize: opts.arrowSize,
|
|
50
52
|
});
|
|
51
53
|
break;
|
|
@@ -54,23 +56,23 @@ function getLinkPoint(opts) {
|
|
|
54
56
|
case "text": {
|
|
55
57
|
nodePoint = getRectangleIntersection({
|
|
56
58
|
arrowSize: opts.arrowSize,
|
|
57
|
-
x: opts.
|
|
58
|
-
y: opts.
|
|
59
|
+
x: opts.xStart,
|
|
60
|
+
y: opts.yStart,
|
|
59
61
|
height: (opts.node._height ?? COMMON_SETTINGS.nodeSize) + (opts.node._borderWidth ?? 0),
|
|
60
62
|
width: (opts.node._width ?? COMMON_SETTINGS.nodeSize) + (opts.node._borderWidth ?? 0),
|
|
61
|
-
oppositeX: opts.
|
|
62
|
-
oppositeY: opts.
|
|
63
|
+
oppositeX: opts.xEnd,
|
|
64
|
+
oppositeY: opts.yEnd,
|
|
63
65
|
borderRadius: opts.node._shape === "text" ? 0 : (opts.node._borderRadius ?? 0),
|
|
64
66
|
});
|
|
65
67
|
break;
|
|
66
68
|
}
|
|
67
69
|
default: {
|
|
68
70
|
nodePoint = getCircleIntersection({
|
|
69
|
-
x: opts.
|
|
70
|
-
y: opts.
|
|
71
|
+
x: opts.xStart,
|
|
72
|
+
y: opts.yStart,
|
|
71
73
|
radius: opts.node._radius ?? COMMON_SETTINGS.nodeRadius,
|
|
72
|
-
oppositeX: opts.
|
|
73
|
-
oppositeY: opts.
|
|
74
|
+
oppositeX: opts.xEnd,
|
|
75
|
+
oppositeY: opts.yEnd,
|
|
74
76
|
arrowSize: opts.arrowSize,
|
|
75
77
|
});
|
|
76
78
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calculate-link-position-by-node.js","sources":["../../../../../../src/module/GraphCanvas/lib/utils/calculate-link-position-by-node.ts"],"sourcesContent":["import { COMMON_SETTINGS } from \"../../constants\";\nimport type { LinkInterface, NodeInterface } from \"../../types\";\n\ntype Point = {\n x: number;\n y: number;\n};\n\nexport function calculateLinkPositionByNode<\n NodeData extends Record<string, unknown>,\n LinkData extends Record<string, unknown>,\n>(link: LinkInterface<NodeData, LinkData>, arrowSize = 0) {\n const source = link.source;\n const target = link.target;\n\n if (typeof source != \"object\" || typeof target != \"object\") return null;\n\n const sourcePoint = getLinkPoint({\n arrowSize: 0,\n node: source,\n oppositeNode: target,\n });\n const targetPoint = getLinkPoint({\n arrowSize: arrowSize > 0 ? arrowSize * 0.85 : 0,\n node: target,\n oppositeNode: source,\n });\n\n if (!sourcePoint || !targetPoint) return null;\n\n const distance =\n targetPoint && sourcePoint\n ? Math.sqrt((targetPoint.x - sourcePoint.x) ** 2 + (targetPoint.y - sourcePoint.y) ** 2)\n : 0;\n\n return {\n x1: sourcePoint.x,\n y1: sourcePoint.y,\n x2: targetPoint.x,\n y2: targetPoint.y,\n distance,\n };\n}\n\ntype GetLinkPointProps<NodeData extends Record<string, unknown>> = {\n arrowSize: number;\n node: NodeInterface<NodeData>;\n oppositeNode: NodeInterface<NodeData>;\n};\nfunction getLinkPoint<NodeData extends Record<string, unknown>>(opts: GetLinkPointProps<NodeData>) {\n if (\n opts.node.x == undefined ||\n opts.node.y == undefined ||\n opts.oppositeNode.x == undefined ||\n opts.oppositeNode.y == undefined\n )\n return null;\n\n let nodePoint: Point;\n switch (opts.node._shape) {\n case \"circle\": {\n nodePoint = getCircleIntersection({\n x: opts.node.x,\n y: opts.node.y,\n radius:\n (opts.node._radius ?? COMMON_SETTINGS.nodeRadius) +\n (opts.node._borderWidth ? opts.node._borderWidth / 2 : 0),\n oppositeX: opts.oppositeNode.x,\n oppositeY: opts.oppositeNode.y,\n arrowSize: opts.arrowSize,\n });\n break;\n }\n case \"square\":\n case \"text\": {\n nodePoint = getRectangleIntersection({\n arrowSize: opts.arrowSize,\n x: opts.node.x,\n y: opts.node.y,\n height: (opts.node._height ?? COMMON_SETTINGS.nodeSize) + (opts.node._borderWidth ?? 0),\n width: (opts.node._width ?? COMMON_SETTINGS.nodeSize) + (opts.node._borderWidth ?? 0),\n oppositeX: opts.oppositeNode.x,\n oppositeY: opts.oppositeNode.y,\n borderRadius: opts.node._shape === \"text\" ? 0 : (opts.node._borderRadius ?? 0),\n });\n break;\n }\n default: {\n nodePoint = getCircleIntersection({\n x: opts.node.x,\n y: opts.node.y,\n radius: opts.node._radius ?? COMMON_SETTINGS.nodeRadius,\n oppositeX: opts.oppositeNode.x,\n oppositeY: opts.oppositeNode.y,\n arrowSize: opts.arrowSize,\n });\n }\n }\n\n return nodePoint;\n}\n\ntype GetCircleIntersection = {\n x: number;\n y: number;\n radius: number;\n oppositeX: number;\n oppositeY: number;\n arrowSize: number;\n};\n\nfunction getCircleIntersection(opts: GetCircleIntersection): Point {\n const dx = opts.oppositeX - opts.x;\n const dy = opts.oppositeY - opts.y;\n const dr = Math.sqrt(dx * dx + dy * dy);\n\n const radius = opts.radius + opts.arrowSize;\n\n return {\n x: opts.x + (dx * radius) / dr,\n y: opts.y + (dy * radius) / dr,\n };\n}\n\ntype GetRectangleIntersection = {\n x: number;\n y: number;\n width: number;\n height: number;\n oppositeX: number;\n oppositeY: number;\n arrowSize: number;\n borderRadius: number;\n};\n\nfunction getRectangleIntersection(opts: GetRectangleIntersection): Point {\n const halfWidth = opts.width / 2;\n const halfHeight = opts.height / 2;\n\n const dx = opts.oppositeX - opts.x;\n const dy = opts.oppositeY - opts.y;\n\n let relX: number, relY: number;\n\n if (Math.abs(dx) <= halfWidth && Math.abs(dy) <= halfHeight) {\n const distToRight = halfWidth - dx;\n const distToLeft = halfWidth + dx;\n const distToTop = halfHeight - dy;\n const distToBottom = halfHeight + dy;\n\n const minDist = Math.min(distToRight, distToLeft, distToTop, distToBottom);\n\n relX = minDist === distToRight ? halfWidth : minDist === distToLeft ? -halfWidth : dx;\n relY = minDist === distToTop ? halfHeight : minDist === distToBottom ? -halfHeight : dy;\n } else {\n const absDx = Math.abs(dx);\n const absDy = Math.abs(dy);\n\n if (halfWidth * absDy < halfHeight * absDx) {\n relX = dx > 0 ? halfWidth : -halfWidth;\n relY = (relX * dy) / dx;\n if (Math.abs(relY) > halfHeight) {\n relY = dy > 0 ? halfHeight : -halfHeight;\n relX = (relY * dx) / dy;\n }\n } else {\n relY = dy > 0 ? halfHeight : -halfHeight;\n relX = (relY * dx) / dy;\n if (Math.abs(relX) > halfWidth) {\n relX = dx > 0 ? halfWidth : -halfWidth;\n relY = (relX * dy) / dx;\n }\n }\n }\n\n if (opts.borderRadius != undefined) {\n const { x, y } = squareRadiusFix(relX, relY, halfWidth, halfHeight, opts.borderRadius);\n if (x != undefined) relX = x;\n if (y != undefined) relY = y;\n }\n\n if (opts.arrowSize > 0) {\n const edgeDist = Math.sqrt(relX * relX + relY * relY);\n const scale = (edgeDist + opts.arrowSize) / edgeDist;\n relX *= scale;\n relY *= scale;\n }\n\n return {\n x: opts.x + relX,\n y: opts.y + relY,\n };\n}\n\n// eslint-disable-next-line no-warning-comments\n// TODO: Need upgrade\nfunction squareRadiusFix(\n relX: number,\n relY: number,\n halfWidth: number,\n halfHeight: number,\n borderRadius: number,\n) {\n const epsilon = 1e-6;\n const absX = Math.abs(relX);\n const absY = Math.abs(relY);\n\n let side: string | null = null;\n if (absX >= halfWidth - epsilon) {\n side = relX > 0 ? \"right\" : \"left\";\n } else if (absY >= halfHeight - epsilon) {\n side = relY > 0 ? \"top\" : \"bottom\";\n }\n\n if (side) {\n const topBound = halfHeight - borderRadius;\n const bottomBound = -topBound;\n const rightBound = halfWidth - borderRadius;\n const leftBound = -rightBound;\n\n switch (side) {\n case \"right\":\n if (relY > topBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"right-top\");\n } else if (relY < bottomBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"right-bottom\");\n }\n break;\n case \"left\":\n if (relY > topBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"left-top\");\n } else if (relY < bottomBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"left-bottom\");\n }\n break;\n case \"top\":\n if (relX > rightBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"top-right\");\n } else if (relX < leftBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"top-left\");\n }\n break;\n case \"bottom\":\n if (relX > rightBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"bottom-right\");\n } else if (relX < leftBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"bottom-left\");\n }\n break;\n default:\n break;\n }\n }\n\n return { x: undefined, y: undefined };\n}\n\nfunction checkIntersection(\n relX: number,\n relY: number,\n halfWidth: number,\n halfHeight: number,\n r: number,\n corner: Parameters<typeof intersectWithCircle>[5],\n) {\n const intersect = intersectWithCircle(relX, relY, halfWidth, halfHeight, r, corner);\n\n return intersect ?? { x: undefined, y: undefined };\n}\n\nfunction intersectWithCircle(\n relX: number,\n relY: number,\n halfWidth: number,\n halfHeight: number,\n r: number,\n corner:\n | \"right-top\"\n | \"right-bottom\"\n | \"left-top\"\n | \"left-bottom\"\n | \"top-right\"\n | \"top-left\"\n | \"bottom-right\"\n | \"bottom-left\",\n) {\n switch (corner) {\n case \"top-right\":\n corner = \"right-top\";\n break;\n case \"bottom-right\":\n corner = \"right-bottom\";\n break;\n case \"top-left\":\n corner = \"left-top\";\n break;\n case \"bottom-left\":\n corner = \"left-bottom\";\n break;\n default:\n break;\n }\n\n let cx: number, cy: number;\n switch (corner) {\n case \"right-top\":\n cx = halfWidth - r;\n cy = halfHeight - r;\n break;\n case \"right-bottom\":\n cx = halfWidth - r;\n cy = -halfHeight + r;\n break;\n case \"left-top\":\n cx = -halfWidth + r;\n cy = halfHeight - r;\n break;\n case \"left-bottom\":\n cx = -halfWidth + r;\n cy = -halfHeight + r;\n break;\n default:\n return null;\n }\n\n const a = relX ** 2 + relY ** 2;\n if (a === 0) return null;\n\n const b = -2 * (relX * cx + relY * cy);\n const c = cx ** 2 + cy ** 2 - r ** 2;\n const discriminant = b ** 2 - 4 * a * c;\n\n if (discriminant < 0) return null;\n const sqrtD = Math.sqrt(discriminant);\n\n const t2 = (-b - sqrtD) / (2 * a);\n const t1 = (-b + sqrtD) / (2 * a);\n const t = t2 > 0 ? t2 : t1 > 0 ? t1 : null;\n\n if (t === null || t >= 1) return null;\n\n const x = t * relX;\n const y = t * relY;\n\n let valid = false;\n switch (corner) {\n case \"right-top\":\n valid = x >= cx && y >= cy;\n break;\n case \"right-bottom\":\n valid = x >= cx && y <= cy;\n break;\n case \"left-top\":\n valid = x <= cx && y >= cy;\n break;\n case \"left-bottom\":\n valid = x <= cx && y <= cy;\n break;\n default:\n break;\n }\n\n return valid ? { x, y } : null;\n}\n"],"names":[],"mappings":";;;;SAQgB,2BAA2B,CAGzC,IAAuC,EAAE,SAAS,GAAG,CAAC,EAAA;AACtD,IAAA,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM;AAC1B,IAAA,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM;IAE1B,IAAI,OAAO,MAAM,IAAI,QAAQ,IAAI,OAAO,MAAM,IAAI,QAAQ;AAAE,QAAA,OAAO,IAAI;IAEvE,MAAM,WAAW,GAAG,YAAY,CAAC;AAC/B,QAAA,SAAS,EAAE,CAAC;AACZ,QAAA,IAAI,EAAE,MAAM;AACZ,QAAA,YAAY,EAAE,MAAM;AACrB,KAAA,CAAC;IACF,MAAM,WAAW,GAAG,YAAY,CAAC;AAC/B,QAAA,SAAS,EAAE,SAAS,GAAG,CAAC,GAAG,SAAS,GAAG,IAAI,GAAG,CAAC;AAC/C,QAAA,IAAI,EAAE,MAAM;AACZ,QAAA,YAAY,EAAE,MAAM;AACrB,KAAA,CAAC;AAEF,IAAA,IAAI,CAAC,WAAW,IAAI,CAAC,WAAW;AAAE,QAAA,OAAO,IAAI;AAE7C,IAAA,MAAM,QAAQ,GACZ,WAAW,IAA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,QAAA,KAAK,WAAW;YACd,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;YAC1B;AACF,QAAA,KAAK,cAAc;YACjB,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;YAC1B;AACF,QAAA,KAAK,UAAU;YACb,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;YAC1B;AACF,QAAA,KAAK,aAAa;YAChB,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;YAC1B;;AAKJ,IAAA,OAAO,KAAK,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,IAAI;AAChC;;;;"}
|
|
1
|
+
{"version":3,"file":"calculate-link-position-by-node.js","sources":["../../../../../../src/module/GraphCanvas/lib/utils/calculate-link-position-by-node.ts"],"sourcesContent":["import { COMMON_SETTINGS } from \"../../constants\";\nimport type { NodeInterface } from \"../../types\";\n\ntype Point = {\n x: number;\n y: number;\n};\n\nexport function calculateLinkPositionByNode<NodeData extends Record<string, unknown>>(\n xStart: number,\n yStart: number,\n xEnd: number,\n yEnd: number,\n sourceNode: NodeInterface<NodeData>,\n targetNode: NodeInterface<NodeData>,\n arrowSize = 0,\n) {\n const sourcePoint = getLinkPoint({\n xStart,\n yStart,\n xEnd,\n yEnd,\n arrowSize: 0,\n node: sourceNode,\n oppositeNode: targetNode,\n });\n const targetPoint = getLinkPoint({\n xStart: xEnd,\n yStart: yEnd,\n xEnd: xStart,\n yEnd: yStart,\n arrowSize: arrowSize > 0 ? arrowSize * 0.9 : 0,\n node: targetNode,\n oppositeNode: sourceNode,\n });\n const targetPointArrow = getLinkPoint({\n xStart: xEnd,\n yStart: yEnd,\n xEnd: xStart,\n yEnd: yStart,\n arrowSize: 0,\n node: targetNode,\n oppositeNode: sourceNode,\n });\n\n const distance =\n targetPoint && sourcePoint\n ? Math.sqrt((targetPoint.x - sourcePoint.x) ** 2 + (targetPoint.y - sourcePoint.y) ** 2)\n : 0;\n\n return {\n xStart: sourcePoint.x,\n yStart: sourcePoint.y,\n xEnd: targetPoint.x,\n yEnd: targetPoint.y,\n xEndArrow: targetPointArrow.x,\n yEndArrow: targetPointArrow.y,\n distance,\n };\n}\n\ntype GetLinkPointProps<NodeData extends Record<string, unknown>> = {\n xStart: number;\n yStart: number;\n xEnd: number;\n yEnd: number;\n arrowSize: number;\n node: NodeInterface<NodeData>;\n oppositeNode: NodeInterface<NodeData>;\n};\nfunction getLinkPoint<NodeData extends Record<string, unknown>>(opts: GetLinkPointProps<NodeData>) {\n let nodePoint: Point;\n switch (opts.node._shape) {\n case \"circle\": {\n nodePoint = getCircleIntersection({\n x: opts.xStart,\n y: opts.yStart,\n radius:\n (opts.node._radius ?? COMMON_SETTINGS.nodeRadius) +\n (opts.node._borderWidth ? opts.node._borderWidth / 2 : 0),\n oppositeX: opts.xEnd,\n oppositeY: opts.yEnd,\n arrowSize: opts.arrowSize,\n });\n break;\n }\n case \"square\":\n case \"text\": {\n nodePoint = getRectangleIntersection({\n arrowSize: opts.arrowSize,\n x: opts.xStart,\n y: opts.yStart,\n height: (opts.node._height ?? COMMON_SETTINGS.nodeSize) + (opts.node._borderWidth ?? 0),\n width: (opts.node._width ?? COMMON_SETTINGS.nodeSize) + (opts.node._borderWidth ?? 0),\n oppositeX: opts.xEnd,\n oppositeY: opts.yEnd,\n borderRadius: opts.node._shape === \"text\" ? 0 : (opts.node._borderRadius ?? 0),\n });\n break;\n }\n default: {\n nodePoint = getCircleIntersection({\n x: opts.xStart,\n y: opts.yStart,\n radius: opts.node._radius ?? COMMON_SETTINGS.nodeRadius,\n oppositeX: opts.xEnd,\n oppositeY: opts.yEnd,\n arrowSize: opts.arrowSize,\n });\n }\n }\n\n return nodePoint;\n}\n\ntype GetCircleIntersection = {\n x: number;\n y: number;\n radius: number;\n oppositeX: number;\n oppositeY: number;\n arrowSize: number;\n};\n\nfunction getCircleIntersection(opts: GetCircleIntersection): Point {\n const dx = opts.oppositeX - opts.x;\n const dy = opts.oppositeY - opts.y;\n const dr = Math.sqrt(dx * dx + dy * dy);\n\n const radius = opts.radius + opts.arrowSize;\n\n return {\n x: opts.x + (dx * radius) / dr,\n y: opts.y + (dy * radius) / dr,\n };\n}\n\ntype GetRectangleIntersection = {\n x: number;\n y: number;\n width: number;\n height: number;\n oppositeX: number;\n oppositeY: number;\n arrowSize: number;\n borderRadius: number;\n};\n\nfunction getRectangleIntersection(opts: GetRectangleIntersection): Point {\n const halfWidth = opts.width / 2;\n const halfHeight = opts.height / 2;\n\n const dx = opts.oppositeX - opts.x;\n const dy = opts.oppositeY - opts.y;\n\n let relX: number, relY: number;\n\n if (Math.abs(dx) <= halfWidth && Math.abs(dy) <= halfHeight) {\n const distToRight = halfWidth - dx;\n const distToLeft = halfWidth + dx;\n const distToTop = halfHeight - dy;\n const distToBottom = halfHeight + dy;\n\n const minDist = Math.min(distToRight, distToLeft, distToTop, distToBottom);\n\n relX = minDist === distToRight ? halfWidth : minDist === distToLeft ? -halfWidth : dx;\n relY = minDist === distToTop ? halfHeight : minDist === distToBottom ? -halfHeight : dy;\n } else {\n const absDx = Math.abs(dx);\n const absDy = Math.abs(dy);\n\n if (halfWidth * absDy < halfHeight * absDx) {\n relX = dx > 0 ? halfWidth : -halfWidth;\n relY = (relX * dy) / dx;\n if (Math.abs(relY) > halfHeight) {\n relY = dy > 0 ? halfHeight : -halfHeight;\n relX = (relY * dx) / dy;\n }\n } else {\n relY = dy > 0 ? halfHeight : -halfHeight;\n relX = (relY * dx) / dy;\n if (Math.abs(relX) > halfWidth) {\n relX = dx > 0 ? halfWidth : -halfWidth;\n relY = (relX * dy) / dx;\n }\n }\n }\n\n if (opts.borderRadius != undefined) {\n const { x, y } = squareRadiusFix(relX, relY, halfWidth, halfHeight, opts.borderRadius);\n if (x != undefined) relX = x;\n if (y != undefined) relY = y;\n }\n\n if (opts.arrowSize > 0) {\n const edgeDist = Math.sqrt(relX * relX + relY * relY);\n const scale = (edgeDist + opts.arrowSize) / edgeDist;\n relX *= scale;\n relY *= scale;\n }\n\n return {\n x: opts.x + relX,\n y: opts.y + relY,\n };\n}\n\n// eslint-disable-next-line no-warning-comments\n// TODO: Need upgrade\nfunction squareRadiusFix(\n relX: number,\n relY: number,\n halfWidth: number,\n halfHeight: number,\n borderRadius: number,\n) {\n const epsilon = 1e-6;\n const absX = Math.abs(relX);\n const absY = Math.abs(relY);\n\n let side: string | null = null;\n if (absX >= halfWidth - epsilon) {\n side = relX > 0 ? \"right\" : \"left\";\n } else if (absY >= halfHeight - epsilon) {\n side = relY > 0 ? \"top\" : \"bottom\";\n }\n\n if (side) {\n const topBound = halfHeight - borderRadius;\n const bottomBound = -topBound;\n const rightBound = halfWidth - borderRadius;\n const leftBound = -rightBound;\n\n switch (side) {\n case \"right\":\n if (relY > topBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"right-top\");\n } else if (relY < bottomBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"right-bottom\");\n }\n break;\n case \"left\":\n if (relY > topBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"left-top\");\n } else if (relY < bottomBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"left-bottom\");\n }\n break;\n case \"top\":\n if (relX > rightBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"top-right\");\n } else if (relX < leftBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"top-left\");\n }\n break;\n case \"bottom\":\n if (relX > rightBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"bottom-right\");\n } else if (relX < leftBound) {\n return checkIntersection(relX, relY, halfWidth, halfHeight, borderRadius, \"bottom-left\");\n }\n break;\n default:\n break;\n }\n }\n\n return { x: undefined, y: undefined };\n}\n\nfunction checkIntersection(\n relX: number,\n relY: number,\n halfWidth: number,\n halfHeight: number,\n r: number,\n corner: Parameters<typeof intersectWithCircle>[5],\n) {\n const intersect = intersectWithCircle(relX, relY, halfWidth, halfHeight, r, corner);\n\n return intersect ?? { x: undefined, y: undefined };\n}\n\nfunction intersectWithCircle(\n relX: number,\n relY: number,\n halfWidth: number,\n halfHeight: number,\n r: number,\n corner:\n | \"right-top\"\n | \"right-bottom\"\n | \"left-top\"\n | \"left-bottom\"\n | \"top-right\"\n | \"top-left\"\n | \"bottom-right\"\n | \"bottom-left\",\n) {\n switch (corner) {\n case \"top-right\":\n corner = \"right-top\";\n break;\n case \"bottom-right\":\n corner = \"right-bottom\";\n break;\n case \"top-left\":\n corner = \"left-top\";\n break;\n case \"bottom-left\":\n corner = \"left-bottom\";\n break;\n default:\n break;\n }\n\n let cx: number, cy: number;\n switch (corner) {\n case \"right-top\":\n cx = halfWidth - r;\n cy = halfHeight - r;\n break;\n case \"right-bottom\":\n cx = halfWidth - r;\n cy = -halfHeight + r;\n break;\n case \"left-top\":\n cx = -halfWidth + r;\n cy = halfHeight - r;\n break;\n case \"left-bottom\":\n cx = -halfWidth + r;\n cy = -halfHeight + r;\n break;\n default:\n return null;\n }\n\n const a = relX ** 2 + relY ** 2;\n if (a === 0) return null;\n\n const b = -2 * (relX * cx + relY * cy);\n const c = cx ** 2 + cy ** 2 - r ** 2;\n const discriminant = b ** 2 - 4 * a * c;\n\n if (discriminant < 0) return null;\n const sqrtD = Math.sqrt(discriminant);\n\n const t2 = (-b - sqrtD) / (2 * a);\n const t1 = (-b + sqrtD) / (2 * a);\n const t = t2 > 0 ? t2 : t1 > 0 ? t1 : null;\n\n if (t === null || t >= 1) return null;\n\n const x = t * relX;\n const y = t * relY;\n\n let valid = false;\n switch (corner) {\n case \"right-top\":\n valid = x >= cx && y >= cy;\n break;\n case \"right-bottom\":\n valid = x >= cx && y <= cy;\n break;\n case \"left-top\":\n valid = x <= cx && y >= cy;\n break;\n case \"left-bottom\":\n valid = x <= cx && y <= cy;\n break;\n default:\n break;\n }\n\n return valid ? { x, y } : null;\n}\n"],"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GAAG,CAAC,CAAC;AACjC,IAAA,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,CAAC,GAAG,CAAC,CAAC;IACjC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI;AAE1C,IAAA,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,IAAI,CAAC;AAAE,QAAA,OAAO,IAAI;AAErC,IAAA,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI;AAClB,IAAA,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI;IAElB,IAAI,KAAK,GAAG,KAAK;IACjB,QAAQ,MAAM;AACZ,QAAA,KAAK,WAAW;YACd,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;YAC1B;AACF,QAAA,KAAK,cAAc;YACjB,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;YAC1B;AACF,QAAA,KAAK,UAAU;YACb,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;YAC1B;AACF,QAAA,KAAK,aAAa;YAChB,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;YAC1B;;AAKJ,IAAA,OAAO,KAAK,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,IAAI;AAChC;;;;"}
|
|
@@ -2,31 +2,34 @@ import { isObject } from '@krainovsd/js-helpers';
|
|
|
2
2
|
import { greatest } from 'd3-array';
|
|
3
3
|
import { pointerGetter } from './pointer-getter.js';
|
|
4
4
|
|
|
5
|
-
function linkByPointerGetter({ areaRect, areaTransform, mouseEvent, links, linkHoverExtraZone, }) {
|
|
5
|
+
function linkByPointerGetter({ areaRect, areaTransform, mouseEvent, links, linkHoverExtraZone, curve, }) {
|
|
6
6
|
if (!areaRect)
|
|
7
7
|
return undefined;
|
|
8
8
|
const [pointerX, pointerY] = pointerGetter(mouseEvent, areaRect, areaTransform);
|
|
9
9
|
return greatest(links, (link) => {
|
|
10
|
-
if (
|
|
11
|
-
|
|
10
|
+
if (!isObject(link.source) ||
|
|
11
|
+
!isObject(link.target) ||
|
|
12
|
+
link.source.visible === false ||
|
|
13
|
+
link.target.visible === false)
|
|
14
|
+
return;
|
|
15
|
+
const x1 = (link._x1 ?? link.source.x);
|
|
16
|
+
const y1 = (link._y1 ?? link.source.y);
|
|
17
|
+
const x2 = (link._x2 ?? link.target.x);
|
|
18
|
+
const y2 = (link._y2 ?? link.target.y);
|
|
19
|
+
const cx = link._cx ?? 0;
|
|
20
|
+
const cy = link._cy ?? 0;
|
|
21
|
+
return curve
|
|
22
|
+
? hitTestQuadratic(pointerX, pointerY, x1, y1, cx, cy, x2, y2, linkHoverExtraZone)
|
|
23
|
+
? link.index
|
|
24
|
+
: undefined
|
|
25
|
+
: isNearLink(pointerX, pointerY, x1, y1, x2, y2, linkHoverExtraZone)
|
|
26
|
+
? link.index
|
|
27
|
+
: undefined;
|
|
12
28
|
});
|
|
13
29
|
}
|
|
14
|
-
function isNearLink(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
link.source.visible === false ||
|
|
18
|
-
link.target.visible === false)
|
|
19
|
-
return false;
|
|
20
|
-
const x1 = link.source.x;
|
|
21
|
-
const y1 = link.source.y;
|
|
22
|
-
const x2 = link.target.x;
|
|
23
|
-
const y2 = link.target.y;
|
|
24
|
-
const distance = distanceToLine(mouseX, mouseY, x1, y1, x2, y2);
|
|
25
|
-
return distance <= threshold;
|
|
26
|
-
}
|
|
27
|
-
function distanceToLine(x, y, x1, y1, x2, y2) {
|
|
28
|
-
const A = x - x1;
|
|
29
|
-
const B = y - y1;
|
|
30
|
+
function isNearLink(px, py, x1, y1, x2, y2, threshold = 2) {
|
|
31
|
+
const A = px - x1;
|
|
32
|
+
const B = py - y1;
|
|
30
33
|
const C = x2 - x1;
|
|
31
34
|
const D = y2 - y1;
|
|
32
35
|
const dot = A * C + B * D;
|
|
@@ -48,9 +51,56 @@ function distanceToLine(x, y, x1, y1, x2, y2) {
|
|
|
48
51
|
xx = x1 + param * C;
|
|
49
52
|
yy = y1 + param * D;
|
|
50
53
|
}
|
|
51
|
-
const dx =
|
|
52
|
-
const dy =
|
|
53
|
-
return Math.sqrt(dx * dx + dy * dy);
|
|
54
|
+
const dx = px - xx;
|
|
55
|
+
const dy = py - yy;
|
|
56
|
+
return Math.sqrt(dx * dx + dy * dy) <= threshold;
|
|
57
|
+
}
|
|
58
|
+
function hitTestQuadratic(px, py, x0, y0, xc, yc, x2, y2, tol) {
|
|
59
|
+
const bb = bezierBBox(x0, y0, xc, yc, x2, y2);
|
|
60
|
+
if (px < bb.minX - tol || px > bb.maxX + tol || py < bb.minY - tol || py > bb.maxY + tol) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const chord = Math.hypot(x2 - x0, y2 - y0);
|
|
64
|
+
const contLen = Math.hypot(xc - x0, yc - y0) + Math.hypot(x2 - xc, y2 - yc);
|
|
65
|
+
const approxLen = (chord + contLen) / 2;
|
|
66
|
+
const steps = Math.max(16, Math.ceil(approxLen / 1));
|
|
67
|
+
const tol2 = tol * tol;
|
|
68
|
+
for (let i = 0; i <= steps; i++) {
|
|
69
|
+
const t = i / steps;
|
|
70
|
+
const mt = 1 - t;
|
|
71
|
+
const x = mt * mt * x0 + 2 * mt * t * xc + t * t * x2;
|
|
72
|
+
const y = mt * mt * y0 + 2 * mt * t * yc + t * t * y2;
|
|
73
|
+
const dx = px - x;
|
|
74
|
+
const dy = py - y;
|
|
75
|
+
if (dx * dx + dy * dy <= tol2)
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
function bezierBBox(x0, y0, x1, y1, x2, y2) {
|
|
81
|
+
function extrema(p0, p1, p2) {
|
|
82
|
+
const denom = p0 - 2 * p1 + p2;
|
|
83
|
+
if (denom === 0)
|
|
84
|
+
return [];
|
|
85
|
+
const t = (p0 - p1) / denom;
|
|
86
|
+
return t > 0 && t < 1 ? [t] : [];
|
|
87
|
+
}
|
|
88
|
+
const ts = [...extrema(x0, x1, x2), ...extrema(y0, y1, y2), 0, 1];
|
|
89
|
+
let maxX = -Infinity, maxY = -Infinity, minX = Infinity, minY = Infinity;
|
|
90
|
+
for (const t of ts) {
|
|
91
|
+
const mt = 1 - t;
|
|
92
|
+
const x = mt * mt * x0 + 2 * mt * t * x1 + t * t * x2;
|
|
93
|
+
const y = mt * mt * y0 + 2 * mt * t * y1 + t * t * y2;
|
|
94
|
+
if (x < minX)
|
|
95
|
+
minX = x;
|
|
96
|
+
if (x > maxX)
|
|
97
|
+
maxX = x;
|
|
98
|
+
if (y < minY)
|
|
99
|
+
minY = y;
|
|
100
|
+
if (y > maxY)
|
|
101
|
+
maxY = y;
|
|
102
|
+
}
|
|
103
|
+
return { minX, minY, maxX, maxY };
|
|
54
104
|
}
|
|
55
105
|
|
|
56
106
|
export { linkByPointerGetter };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"link-by-pointer-getter.js","sources":["../../../../../../src/module/GraphCanvas/lib/utils/link-by-pointer-getter.ts"],"sourcesContent":["import { isObject } from \"@krainovsd/js-helpers\";\nimport { greatest } from \"d3-array\";\nimport type { ZoomTransform } from \"d3-zoom\";\nimport type { LinkInterface } from \"../../types\";\nimport { pointerGetter } from \"./pointer-getter\";\n\nexport type LinkByPointerGetterOptions<\n NodeData extends Record<string, unknown>,\n LinkData extends Record<string, unknown>,\n> = {\n mouseEvent: MouseEvent | TouchEvent;\n areaRect: DOMRect | undefined;\n areaTransform: ZoomTransform;\n links: LinkInterface<NodeData, LinkData>[];\n linkHoverExtraZone: number;\n};\n\nexport function linkByPointerGetter<\n NodeData extends Record<string, unknown>,\n LinkData extends Record<string, unknown>,\n>({\n areaRect,\n areaTransform,\n mouseEvent,\n links,\n linkHoverExtraZone,\n}: LinkByPointerGetterOptions<NodeData, LinkData>): LinkInterface<NodeData, LinkData> | undefined {\n if (!areaRect) return undefined;\n\n const [pointerX, pointerY] = pointerGetter(mouseEvent, areaRect, areaTransform);\n\n return greatest(links, (link) => {\n if (
|
|
1
|
+
{"version":3,"file":"link-by-pointer-getter.js","sources":["../../../../../../src/module/GraphCanvas/lib/utils/link-by-pointer-getter.ts"],"sourcesContent":["import { isObject } from \"@krainovsd/js-helpers\";\nimport { greatest } from \"d3-array\";\nimport type { ZoomTransform } from \"d3-zoom\";\nimport type { LinkInterface } from \"../../types\";\nimport { pointerGetter } from \"./pointer-getter\";\n\nexport type LinkByPointerGetterOptions<\n NodeData extends Record<string, unknown>,\n LinkData extends Record<string, unknown>,\n> = {\n mouseEvent: MouseEvent | TouchEvent;\n areaRect: DOMRect | undefined;\n areaTransform: ZoomTransform;\n links: LinkInterface<NodeData, LinkData>[];\n linkHoverExtraZone: number;\n curve: boolean;\n};\n\nexport function linkByPointerGetter<\n NodeData extends Record<string, unknown>,\n LinkData extends Record<string, unknown>,\n>({\n areaRect,\n areaTransform,\n mouseEvent,\n links,\n linkHoverExtraZone,\n curve,\n}: LinkByPointerGetterOptions<NodeData, LinkData>): LinkInterface<NodeData, LinkData> | undefined {\n if (!areaRect) return undefined;\n\n const [pointerX, pointerY] = pointerGetter(mouseEvent, areaRect, areaTransform);\n\n return greatest(links, (link) => {\n if (\n !isObject(link.source) ||\n !isObject(link.target) ||\n link.source.visible === false ||\n link.target.visible === false\n )\n return;\n\n const x1 = (link._x1 ?? link.source.x) as number;\n const y1 = (link._y1 ?? link.source.y) as number;\n const x2 = (link._x2 ?? link.target.x) as number;\n const y2 = (link._y2 ?? link.target.y) as number;\n const cx = link._cx ?? 0;\n const cy = link._cy ?? 0;\n\n return curve\n ? hitTestQuadratic(pointerX, pointerY, x1, y1, cx, cy, x2, y2, linkHoverExtraZone)\n ? link.index\n : undefined\n : isNearLink(pointerX, pointerY, x1, y1, x2, y2, linkHoverExtraZone)\n ? link.index\n : undefined;\n });\n}\n\nfunction isNearLink(\n px: number,\n py: number,\n x1: number,\n y1: number,\n x2: number,\n y2: number,\n threshold = 2,\n) {\n const A = px - x1;\n const B = py - y1;\n const C = x2 - x1;\n const D = y2 - y1;\n\n const dot = A * C + B * D;\n const lenSq = C * C + D * D;\n let param = -1;\n\n if (lenSq !== 0) {\n param = dot / lenSq;\n }\n\n let xx, yy;\n\n if (param < 0) {\n xx = x1;\n yy = y1;\n } else if (param > 1) {\n xx = x2;\n yy = y2;\n } else {\n xx = x1 + param * C;\n yy = y1 + param * D;\n }\n\n const dx = px - xx;\n const dy = py - yy;\n\n return Math.sqrt(dx * dx + dy * dy) <= threshold;\n}\n\nfunction hitTestQuadratic(\n px: number,\n py: number,\n x0: number,\n y0: number,\n xc: number,\n yc: number,\n x2: number,\n y2: number,\n tol: number,\n): boolean {\n const bb = bezierBBox(x0, y0, xc, yc, x2, y2);\n if (px < bb.minX - tol || px > bb.maxX + tol || py < bb.minY - tol || py > bb.maxY + tol) {\n return false;\n }\n\n const chord = Math.hypot(x2 - x0, y2 - y0);\n const contLen = Math.hypot(xc - x0, yc - y0) + Math.hypot(x2 - xc, y2 - yc);\n const approxLen = (chord + contLen) / 2;\n const steps = Math.max(16, Math.ceil(approxLen / 1));\n\n const tol2 = tol * tol;\n for (let i = 0; i <= steps; i++) {\n const t = i / steps;\n const mt = 1 - t;\n const x = mt * mt * x0 + 2 * mt * t * xc + t * t * x2;\n const y = mt * mt * y0 + 2 * mt * t * yc + t * t * y2;\n const dx = px - x;\n const dy = py - y;\n if (dx * dx + dy * dy <= tol2) return true;\n }\n return false;\n}\n\nfunction bezierBBox(\n x0: number,\n y0: number,\n x1: number,\n y1: number,\n x2: number,\n y2: number,\n): { minX: number; maxX: number; minY: number; maxY: number } {\n function extrema(p0: number, p1: number, p2: number): number[] {\n const denom = p0 - 2 * p1 + p2;\n if (denom === 0) return [];\n const t = (p0 - p1) / denom;\n return t > 0 && t < 1 ? [t] : [];\n }\n\n const ts = [...extrema(x0, x1, x2), ...extrema(y0, y1, y2), 0, 1];\n\n let maxX = -Infinity,\n maxY = -Infinity,\n minX = Infinity,\n minY = Infinity;\n\n for (const t of ts) {\n const mt = 1 - t;\n const x = mt * mt * x0 + 2 * mt * t * x1 + t * t * x2;\n const y = mt * mt * y0 + 2 * mt * t * y1 + t * t * y2;\n if (x < minX) minX = x;\n if (x > maxX) maxX = x;\n if (y < minY) minY = y;\n if (y > maxY) maxY = y;\n }\n\n return { minX, minY, maxX, maxY };\n}\n"],"names":[],"mappings":";;;;AAkBgB,SAAA,mBAAmB,CAGjC,EACA,QAAQ,EACR,aAAa,EACb,UAAU,EACV,KAAK,EACL,kBAAkB,EAClB,KAAK,GAC0C,EAAA;AAC/C,IAAA,IAAI,CAAC,QAAQ;AAAE,QAAA,OAAO,SAAS;AAE/B,IAAA,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,GAAG,aAAa,CAAC,UAAU,EAAE,QAAQ,EAAE,aAAa,CAAC;AAE/E,IAAA,OAAO,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,KAAI;AAC9B,QAAA,IACE,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC;AACtB,YAAA,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC;AACtB,YAAA,IAAI,CAAC,MAAM,CAAC,OAAO,KAAK,KAAK;AAC7B,YAAA,IAAI,CAAC,MAAM,CAAC,OAAO,KAAK,KAAK;YAE7B;AAEF,QAAA,MAAM,EAAE,IAAI,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAW;AAChD,QAAA,MAAM,EAAE,IAAI,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAW;AAChD,QAAA,MAAM,EAAE,IAAI,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAW;AAChD,QAAA,MAAM,EAAE,IAAI,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,MAAM,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G,CAAC,GAAG,OAAO,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,GAAG,OAAO,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AAEjE,IAAA,IAAI,IAAI,GAAG,CAAC,QAAQ,EAClB,IAAI,GAAG,CAAC,QAAQ,EAChB,IAAI,GAAG,QAAQ,EACf,IAAI,GAAG,QAAQ;AAEjB,IAAA,KAAK,MAAM,CAAC,IAAI,EAAE,EAAE;AAClB,QAAA,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC;QAChB,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE;QACrD,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE;QACrD,IAAI,CAAC,GAAG,IAAI;YAAE,IAAI,GAAG,CAAC;QACtB,IAAI,CAAC,GAAG,IAAI;YAAE,IAAI,GAAG,CAAC;QACtB,IAAI,CAAC,GAAG,IAAI;YAAE,IAAI,GAAG,CAAC;QACtB,IAAI,CAAC,GAAG,IAAI;YAAE,IAAI,GAAG,CAAC;;IAGxB,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;AACnC;;;;"}
|
|
@@ -3,6 +3,7 @@ import 'd3-array';
|
|
|
3
3
|
import { calculateLinkPositionByNode } from '../lib/utils/calculate-link-position-by-node.js';
|
|
4
4
|
import { getParticlePosition } from '../lib/utils/get-particle-position.js';
|
|
5
5
|
import { linkFade, linkHighlight } from '../lib/utils/link-highlight.js';
|
|
6
|
+
import { calculateCurveLinkPositionByNode } from '../lib/utils/calculate-curve-link-position-by-node.js';
|
|
6
7
|
|
|
7
8
|
function getDrawLink() {
|
|
8
9
|
return function drawLink(link) {
|
|
@@ -119,23 +120,62 @@ function getDrawLink() {
|
|
|
119
120
|
let yStart = link.source.y;
|
|
120
121
|
let xEnd = link.target.x;
|
|
121
122
|
let yEnd = link.target.y;
|
|
123
|
+
let xControl = 0;
|
|
124
|
+
let yControl = 0;
|
|
125
|
+
let xEndArrow = xEnd;
|
|
126
|
+
let yEndArrow = yEnd;
|
|
122
127
|
let linkDistance = 0;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
(this.linkSettings.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
xStart = position.
|
|
130
|
-
xEnd = position.
|
|
131
|
-
yStart = position.
|
|
132
|
-
yEnd = position.
|
|
128
|
+
const isHasArrow = this.linkSettings.arrow && arrowAlpha > 0;
|
|
129
|
+
if (!this.linkSettings.curve) {
|
|
130
|
+
if (this.linkSettings.prettyDraw ||
|
|
131
|
+
this.linkSettings.particleFlexSpeed ||
|
|
132
|
+
(this.linkSettings.arrow && arrowAlpha > 0)) {
|
|
133
|
+
const position = calculateLinkPositionByNode(xStart, yStart, xEnd, yEnd, link.source, link.target, isHasArrow ? arrowSize : 0);
|
|
134
|
+
xStart = position.xStart;
|
|
135
|
+
xEnd = position.xEnd;
|
|
136
|
+
yStart = position.yStart;
|
|
137
|
+
yEnd = position.yEnd;
|
|
138
|
+
xEndArrow = position.xEndArrow;
|
|
139
|
+
yEndArrow = position.yEndArrow;
|
|
133
140
|
linkDistance = position.distance;
|
|
134
141
|
}
|
|
142
|
+
link._x1 = xStart;
|
|
143
|
+
link._y1 = yStart;
|
|
144
|
+
link._x2 = xEnd;
|
|
145
|
+
link._y2 = yEnd;
|
|
146
|
+
link._ax = xEndArrow;
|
|
147
|
+
link._ay = yEndArrow;
|
|
148
|
+
this.context.moveTo(xStart, yStart);
|
|
149
|
+
this.context.lineTo(xEnd, yEnd);
|
|
150
|
+
this.context.stroke();
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
const position = calculateCurveLinkPositionByNode(xStart, yStart, xEnd, yEnd, link.source, link.target, link._groupIndex ?? 0, isHasArrow ? arrowSize : 0);
|
|
154
|
+
xStart = position.xStart;
|
|
155
|
+
yStart = position.yStart;
|
|
156
|
+
xEnd = position.xEnd;
|
|
157
|
+
yEnd = position.yEnd;
|
|
158
|
+
xControl = position.xControl;
|
|
159
|
+
yControl = position.yControl;
|
|
160
|
+
xEndArrow = position.xEndArrow;
|
|
161
|
+
yEndArrow = position.yEndArrow;
|
|
162
|
+
link._x1 = position.xStart;
|
|
163
|
+
link._y1 = position.yStart;
|
|
164
|
+
link._x2 = position.xEnd;
|
|
165
|
+
link._y2 = position.yEnd;
|
|
166
|
+
link._cx = position.xControl;
|
|
167
|
+
link._cy = position.yControl;
|
|
168
|
+
link._ax = position.xEndArrow;
|
|
169
|
+
link._ay = position.yEndArrow;
|
|
170
|
+
this.context.beginPath();
|
|
171
|
+
this.context.moveTo(position.xStart, position.yStart);
|
|
172
|
+
this.context.quadraticCurveTo(position.xControl, position.yControl, position.xEnd, position.yEnd);
|
|
173
|
+
this.context.setLineDash([]);
|
|
174
|
+
// if (isDashed) {
|
|
175
|
+
// this.context.setLineDash([10, 5]);
|
|
176
|
+
// }
|
|
177
|
+
this.context.stroke();
|
|
135
178
|
}
|
|
136
|
-
this.context.moveTo(xStart, yStart);
|
|
137
|
-
this.context.lineTo(xEnd, yEnd);
|
|
138
|
-
this.context.stroke();
|
|
139
179
|
/** Particle */
|
|
140
180
|
if (this.linkSettings.particles &&
|
|
141
181
|
((this.highlightedNode &&
|
|
@@ -203,15 +243,28 @@ function getDrawLink() {
|
|
|
203
243
|
}
|
|
204
244
|
/** Arrow */
|
|
205
245
|
if (this.linkSettings.arrow && arrowAlpha > 0) {
|
|
246
|
+
let angle = 0;
|
|
247
|
+
if (!this.linkSettings.curve) {
|
|
248
|
+
angle = Math.atan2(yEndArrow - yStart, xEndArrow - xStart);
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
const tangentX = 2 * (xEndArrow - xControl);
|
|
252
|
+
const tangentY = 2 * (yEndArrow - yControl);
|
|
253
|
+
if (Math.abs(tangentX) < 0.001 && Math.abs(tangentY) < 0.001) {
|
|
254
|
+
angle = Math.atan2(yEndArrow - yControl, xEndArrow - xControl);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
angle = Math.atan2(tangentY, tangentX);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
206
260
|
this.context.beginPath();
|
|
207
261
|
this.context.globalAlpha = arrowAlpha;
|
|
208
262
|
this.context.strokeStyle = arrowBorderColor;
|
|
209
263
|
this.context.lineWidth = arrowBorderWidth;
|
|
210
264
|
this.context.fillStyle = arrowColor;
|
|
211
|
-
|
|
212
|
-
this.context.
|
|
213
|
-
this.context.lineTo(
|
|
214
|
-
this.context.lineTo(xEnd - arrowSize * Math.cos(angle + Math.PI / 6), yEnd - arrowSize * Math.sin(angle + Math.PI / 6));
|
|
265
|
+
this.context.moveTo(xEndArrow, yEndArrow);
|
|
266
|
+
this.context.lineTo(xEndArrow - arrowSize * Math.cos(angle - Math.PI / 6), yEndArrow - arrowSize * Math.sin(angle - Math.PI / 6));
|
|
267
|
+
this.context.lineTo(xEndArrow - arrowSize * Math.cos(angle + Math.PI / 6), yEndArrow - arrowSize * Math.sin(angle + Math.PI / 6));
|
|
215
268
|
this.context.closePath();
|
|
216
269
|
this.context.fill();
|
|
217
270
|
if (arrowBorderWidth > 0) {
|