collision.test.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { arrayToMap } from "@excalidraw/common";
  2. import { type GlobalPoint, type LocalPoint, pointFrom } from "@excalidraw/math";
  3. import { Excalidraw } from "@excalidraw/excalidraw";
  4. import { API } from "@excalidraw/excalidraw/tests/helpers/api";
  5. import { UI } from "@excalidraw/excalidraw/tests/helpers/ui";
  6. import "@excalidraw/utils/test-utils";
  7. import { render } from "@excalidraw/excalidraw/tests/test-utils";
  8. import * as distance from "../src/distance";
  9. import { hitElementItself } from "../src/collision";
  10. describe("check rotated elements can be hit:", () => {
  11. beforeEach(async () => {
  12. localStorage.clear();
  13. await render(<Excalidraw handleKeyboardGlobally={true} />);
  14. });
  15. it("arrow", () => {
  16. UI.createElement("arrow", {
  17. x: 0,
  18. y: 0,
  19. width: 124,
  20. height: 302,
  21. angle: 1.8700426423973724,
  22. points: [
  23. [0, 0],
  24. [120, -198],
  25. [-4, -302],
  26. ] as LocalPoint[],
  27. });
  28. const hit = hitElementItself({
  29. point: pointFrom<GlobalPoint>(88, -68),
  30. element: window.h.elements[0],
  31. threshold: 10,
  32. elementsMap: window.h.scene.getNonDeletedElementsMap(),
  33. });
  34. expect(hit).toBe(true);
  35. });
  36. });
  37. describe("hitElementItself cache", () => {
  38. beforeEach(async () => {
  39. // reset cache
  40. hitElementItself({
  41. point: pointFrom<GlobalPoint>(50, 50),
  42. element: API.createElement({
  43. type: "rectangle",
  44. x: 0,
  45. y: 0,
  46. width: 100,
  47. height: 100,
  48. backgroundColor: "#ffffff",
  49. }),
  50. threshold: Infinity,
  51. elementsMap: new Map([]),
  52. });
  53. localStorage.clear();
  54. await render(<Excalidraw handleKeyboardGlobally={true} />);
  55. });
  56. it("reuses cached result when threshold increases", () => {
  57. const element = API.createElement({
  58. type: "rectangle",
  59. x: 0,
  60. y: 0,
  61. width: 100,
  62. height: 100,
  63. backgroundColor: "#ffffff",
  64. });
  65. const elementsMap = arrayToMap([element]);
  66. const point = pointFrom<GlobalPoint>(100.5, 50);
  67. const distanceSpy = jest.spyOn(distance, "distanceToElement");
  68. expect(
  69. hitElementItself({
  70. point,
  71. element,
  72. threshold: 1,
  73. elementsMap,
  74. }),
  75. ).toBe(true);
  76. expect(distanceSpy).toHaveBeenCalledTimes(1);
  77. expect(
  78. hitElementItself({
  79. point,
  80. element,
  81. threshold: 10,
  82. elementsMap,
  83. }),
  84. ).toBe(true);
  85. expect(distanceSpy).toHaveBeenCalledTimes(1);
  86. distanceSpy.mockRestore();
  87. });
  88. it("does not reuse cache when threshold decreases", () => {
  89. const element = API.createElement({
  90. type: "rectangle",
  91. x: 0,
  92. y: 0,
  93. width: 100,
  94. height: 100,
  95. backgroundColor: "transparent",
  96. });
  97. const elementsMap = arrayToMap([element]);
  98. const point = pointFrom<GlobalPoint>(105, 50);
  99. const distanceSpy = jest.spyOn(distance, "distanceToElement");
  100. expect(
  101. hitElementItself({
  102. point,
  103. element,
  104. threshold: 10,
  105. elementsMap,
  106. }),
  107. ).toBe(true);
  108. expect(distanceSpy).toHaveBeenCalledTimes(1);
  109. expect(
  110. hitElementItself({
  111. point,
  112. element,
  113. threshold: 6,
  114. elementsMap,
  115. }),
  116. ).toBe(true);
  117. expect(distanceSpy).toHaveBeenCalledTimes(2);
  118. distanceSpy.mockRestore();
  119. });
  120. it("invalidates cache when element version changes", () => {
  121. const element = API.createElement({
  122. type: "rectangle",
  123. x: 0,
  124. y: 0,
  125. width: 100,
  126. height: 100,
  127. backgroundColor: "#ffffff",
  128. });
  129. const elementsMap = arrayToMap([element]);
  130. const point = pointFrom<GlobalPoint>(100.5, 50);
  131. const distanceSpy = jest.spyOn(distance, "distanceToElement");
  132. expect(
  133. hitElementItself({
  134. point,
  135. element,
  136. threshold: 1,
  137. elementsMap,
  138. }),
  139. ).toBe(true);
  140. expect(distanceSpy).toHaveBeenCalledTimes(1);
  141. const movedElement = {
  142. ...element,
  143. version: element.version + 1,
  144. versionNonce: element.versionNonce + 1,
  145. };
  146. expect(
  147. hitElementItself({
  148. point,
  149. element: movedElement,
  150. threshold: 1,
  151. elementsMap,
  152. }),
  153. ).toBe(true);
  154. expect(distanceSpy).toHaveBeenCalledTimes(2);
  155. distanceSpy.mockRestore();
  156. });
  157. it("override does not affect caching", () => {
  158. const element = API.createElement({
  159. type: "rectangle",
  160. x: 0,
  161. y: 0,
  162. width: 100,
  163. height: 100,
  164. backgroundColor: "transparent",
  165. });
  166. const elementsMap = arrayToMap([element]);
  167. const point = pointFrom<GlobalPoint>(50, 50);
  168. const distanceSpy = jest.spyOn(distance, "distanceToElement");
  169. expect(
  170. hitElementItself({
  171. point,
  172. element,
  173. threshold: 10,
  174. elementsMap,
  175. }),
  176. ).toBe(false);
  177. expect(distanceSpy).toHaveBeenCalledTimes(1);
  178. expect(
  179. hitElementItself({
  180. point,
  181. element,
  182. threshold: 10,
  183. elementsMap,
  184. overrideShouldTestInside: true,
  185. }),
  186. ).toBe(true);
  187. });
  188. });