diagram-js_lib_util_RenderUtil.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {
  2. attr,
  3. create
  4. } from "./chunk-IQOBXLIK.js";
  5. import {
  6. isNumber
  7. } from "./chunk-4AK4GF4H.js";
  8. import "./chunk-2LSFTFF7.js";
  9. // node_modules/.pnpm/diagram-js@11.9.1/node_modules/diagram-js/lib/util/RenderUtil.js
  10. function componentsToPath(elements) {
  11. return elements.flat().join(",").replace(/,?([A-z]),?/g, "$1");
  12. }
  13. function toSVGPoints(points) {
  14. var result = "";
  15. for (var i = 0, p; p = points[i]; i++) {
  16. result += p.x + "," + p.y + " ";
  17. }
  18. return result;
  19. }
  20. function move(point) {
  21. return ["M", point.x, point.y];
  22. }
  23. function lineTo(point) {
  24. return ["L", point.x, point.y];
  25. }
  26. function curveTo(p1, p2, p3) {
  27. return ["C", p1.x, p1.y, p2.x, p2.y, p3.x, p3.y];
  28. }
  29. function drawPath(waypoints, cornerRadius) {
  30. const pointCount = waypoints.length;
  31. const path = [move(waypoints[0])];
  32. for (let i = 1; i < pointCount; i++) {
  33. const pointBefore = waypoints[i - 1];
  34. const point = waypoints[i];
  35. const pointAfter = waypoints[i + 1];
  36. if (!pointAfter || !cornerRadius) {
  37. path.push(lineTo(point));
  38. continue;
  39. }
  40. const effectiveRadius = Math.min(
  41. cornerRadius,
  42. vectorLength(point.x - pointBefore.x, point.y - pointBefore.y),
  43. vectorLength(pointAfter.x - point.x, pointAfter.y - point.y)
  44. );
  45. if (!effectiveRadius) {
  46. path.push(lineTo(point));
  47. continue;
  48. }
  49. const beforePoint = getPointAtLength(point, pointBefore, effectiveRadius);
  50. const beforePoint2 = getPointAtLength(point, pointBefore, effectiveRadius * 0.5);
  51. const afterPoint = getPointAtLength(point, pointAfter, effectiveRadius);
  52. const afterPoint2 = getPointAtLength(point, pointAfter, effectiveRadius * 0.5);
  53. path.push(lineTo(beforePoint));
  54. path.push(curveTo(beforePoint2, afterPoint2, afterPoint));
  55. }
  56. return path;
  57. }
  58. function getPointAtLength(start, end, length) {
  59. const deltaX = end.x - start.x;
  60. const deltaY = end.y - start.y;
  61. const totalLength = vectorLength(deltaX, deltaY);
  62. const percent = length / totalLength;
  63. return {
  64. x: start.x + deltaX * percent,
  65. y: start.y + deltaY * percent
  66. };
  67. }
  68. function vectorLength(x, y) {
  69. return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  70. }
  71. function createLine(points, attrs, radius) {
  72. if (isNumber(attrs)) {
  73. radius = attrs;
  74. attrs = null;
  75. }
  76. if (!attrs) {
  77. attrs = {};
  78. }
  79. const line = create("path", attrs);
  80. if (isNumber(radius)) {
  81. line.dataset.cornerRadius = String(radius);
  82. }
  83. return updateLine(line, points);
  84. }
  85. function updateLine(gfx, points) {
  86. const cornerRadius = parseInt(gfx.dataset.cornerRadius, 10) || 0;
  87. attr(gfx, {
  88. d: componentsToPath(drawPath(points, cornerRadius))
  89. });
  90. return gfx;
  91. }
  92. export {
  93. componentsToPath,
  94. createLine,
  95. toSVGPoints,
  96. updateLine
  97. };
  98. //# sourceMappingURL=diagram-js_lib_util_RenderUtil.js.map