bounds.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { pointFrom } from "@excalidraw/math";
  2. import { arrayToMap, ROUNDNESS } from "@excalidraw/common";
  3. import type { LocalPoint } from "@excalidraw/math";
  4. import { getElementAbsoluteCoords, getElementBounds } from "../src/bounds";
  5. import type { ExcalidrawElement, ExcalidrawLinearElement } from "../src/types";
  6. const _ce = ({
  7. x,
  8. y,
  9. w,
  10. h,
  11. a,
  12. t,
  13. }: {
  14. x: number;
  15. y: number;
  16. w: number;
  17. h: number;
  18. a?: number;
  19. t?: string;
  20. }) =>
  21. ({
  22. type: t || "rectangle",
  23. strokeColor: "#000",
  24. backgroundColor: "#000",
  25. fillStyle: "solid",
  26. strokeWidth: 1,
  27. roundness: { type: ROUNDNESS.PROPORTIONAL_RADIUS },
  28. roughness: 0,
  29. opacity: 1,
  30. x,
  31. y,
  32. width: w,
  33. height: h,
  34. angle: a,
  35. } as ExcalidrawElement);
  36. describe("getElementAbsoluteCoords", () => {
  37. it("test x1 coordinate", () => {
  38. const element = _ce({ x: 10, y: 20, w: 10, h: 0 });
  39. const [x1] = getElementAbsoluteCoords(element, arrayToMap([element]));
  40. expect(x1).toEqual(10);
  41. });
  42. it("test x2 coordinate", () => {
  43. const element = _ce({ x: 10, y: 20, w: 10, h: 0 });
  44. const [, , x2] = getElementAbsoluteCoords(element, arrayToMap([element]));
  45. expect(x2).toEqual(20);
  46. });
  47. it("test y1 coordinate", () => {
  48. const element = _ce({ x: 0, y: 10, w: 0, h: 10 });
  49. const [, y1] = getElementAbsoluteCoords(element, arrayToMap([element]));
  50. expect(y1).toEqual(10);
  51. });
  52. it("test y2 coordinate", () => {
  53. const element = _ce({ x: 0, y: 10, w: 0, h: 10 });
  54. const [, , , y2] = getElementAbsoluteCoords(element, arrayToMap([element]));
  55. expect(y2).toEqual(20);
  56. });
  57. });
  58. describe("getElementBounds", () => {
  59. it("rectangle", () => {
  60. const element = _ce({
  61. x: 40,
  62. y: 30,
  63. w: 20,
  64. h: 10,
  65. a: Math.PI / 4,
  66. t: "rectangle",
  67. });
  68. const [x1, y1, x2, y2] = getElementBounds(element, arrayToMap([element]));
  69. expect(x1).toEqual(39.39339828220179);
  70. expect(y1).toEqual(24.393398282201787);
  71. expect(x2).toEqual(60.60660171779821);
  72. expect(y2).toEqual(45.60660171779821);
  73. });
  74. it("diamond", () => {
  75. const element = _ce({
  76. x: 40,
  77. y: 30,
  78. w: 20,
  79. h: 10,
  80. a: Math.PI / 4,
  81. t: "diamond",
  82. });
  83. const [x1, y1, x2, y2] = getElementBounds(element, arrayToMap([element]));
  84. expect(x1).toEqual(42.928932188134524);
  85. expect(y1).toEqual(27.928932188134524);
  86. expect(x2).toEqual(57.071067811865476);
  87. expect(y2).toEqual(42.071067811865476);
  88. });
  89. it("ellipse", () => {
  90. const element = _ce({
  91. x: 40,
  92. y: 30,
  93. w: 20,
  94. h: 10,
  95. a: Math.PI / 4,
  96. t: "ellipse",
  97. });
  98. const [x1, y1, x2, y2] = getElementBounds(element, arrayToMap([element]));
  99. expect(x1).toEqual(42.09430584957905);
  100. expect(y1).toEqual(27.09430584957905);
  101. expect(x2).toEqual(57.90569415042095);
  102. expect(y2).toEqual(42.90569415042095);
  103. });
  104. it("curved line", () => {
  105. const element = {
  106. ..._ce({
  107. t: "line",
  108. x: 449.58203125,
  109. y: 186.0625,
  110. w: 170.12890625,
  111. h: 92.48828125,
  112. a: 0.6447741904932416,
  113. }),
  114. points: [
  115. pointFrom<LocalPoint>(0, 0),
  116. pointFrom<LocalPoint>(67.33984375, 92.48828125),
  117. pointFrom<LocalPoint>(-102.7890625, 52.15625),
  118. ],
  119. } as ExcalidrawLinearElement;
  120. const [x1, y1, x2, y2] = getElementBounds(element, arrayToMap([element]));
  121. expect(x1).toEqual(360.3176068760539);
  122. expect(y1).toEqual(185.90654264413516);
  123. expect(x2).toEqual(480.87005902729743);
  124. expect(y2).toEqual(320.4751269334226);
  125. });
  126. });