util.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { Line } from '@antv/x6-geometry';
  2. export function offset(p1, p2, offset) {
  3. let tx;
  4. if (typeof offset === 'object') {
  5. if (Number.isFinite(offset.y)) {
  6. const line = new Line(p2, p1);
  7. const { start, end } = line.parallel(offset.y);
  8. p2 = start; // eslint-disable-line
  9. p1 = end; // eslint-disable-line
  10. }
  11. tx = offset.x;
  12. }
  13. else {
  14. tx = offset;
  15. }
  16. if (tx == null || !Number.isFinite(tx)) {
  17. return p1;
  18. }
  19. const length = p1.distance(p2);
  20. if (tx === 0 && length > 0) {
  21. return p1;
  22. }
  23. return p1.move(p2, -Math.min(tx, length - 1));
  24. }
  25. export function getStrokeWidth(magnet) {
  26. const stroke = magnet.getAttribute('stroke-width');
  27. if (stroke === null) {
  28. return 0;
  29. }
  30. return parseFloat(stroke) || 0;
  31. }
  32. export function findShapeNode(magnet) {
  33. if (magnet == null) {
  34. return null;
  35. }
  36. let node = magnet;
  37. do {
  38. let tagName = node.tagName;
  39. if (typeof tagName !== 'string')
  40. return null;
  41. tagName = tagName.toUpperCase();
  42. if (tagName === 'G') {
  43. node = node.firstElementChild;
  44. }
  45. else if (tagName === 'TITLE') {
  46. node = node.nextElementSibling;
  47. }
  48. else
  49. break;
  50. } while (node);
  51. return node;
  52. }
  53. //# sourceMappingURL=util.js.map