bounds.test.ts 2.9 KB

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