bounds.test.ts 2.8 KB

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