align.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { Pen, Node, Rect, PenType, getRect } from '@topology/core';
  2. export function alignNodes(pens, rect, align)
  3. {
  4. for (const item of pens)
  5. {
  6. if (!(item instanceof Node))
  7. {
  8. continue;
  9. }
  10. switch (align)
  11. {
  12. case 'left':
  13. item.rect.x = rect.x;
  14. break;
  15. case 'right':
  16. item.rect.x = rect.ex - item.rect.width;
  17. break;
  18. case 'top':
  19. item.rect.y = rect.y;
  20. break;
  21. case 'bottom':
  22. item.rect.y = rect.ey - item.rect.height;
  23. break;
  24. case 'center':
  25. item.rect.x = rect.center.x - item.rect.width / 2;
  26. break;
  27. case 'middle':
  28. item.rect.y = rect.center.y - item.rect.height / 2;
  29. break;
  30. }
  31. item.rect.floor();
  32. item.rect.calcCenter();
  33. item.init();
  34. item.calcChildrenRect();
  35. }
  36. }
  37. export function spaceBetween(pens, width)
  38. {
  39. let space = 0;
  40. let cnt = 0;
  41. for (const item of pens)
  42. {
  43. if (!(item instanceof Node))
  44. {
  45. continue;
  46. }
  47. space += item.rect.width;
  48. ++cnt;
  49. }
  50. space = (width - space) / (cnt - 1);
  51. let left = 0;
  52. for (const item of pens)
  53. {
  54. if (!(item instanceof Node))
  55. {
  56. continue;
  57. }
  58. if (!left)
  59. {
  60. left = item.rect.x;
  61. }
  62. item.rect.x = left;
  63. left += item.rect.width + space;
  64. item.rect.floor();
  65. item.rect.calcCenter();
  66. item.init();
  67. item.calcChildrenRect();
  68. }
  69. }