|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
import { Point } from "./types";
|
|
|
|
|
import { NormalizedZoomValue, Point, Zoom } from "./types";
|
|
|
|
|
import { LINE_CONFIRM_THRESHOLD } from "./constants";
|
|
|
|
|
import { ExcalidrawLinearElement } from "./element/types";
|
|
|
|
|
|
|
|
|
@ -147,13 +147,16 @@ export const centerPoint = (a: Point, b: Point): Point => {
|
|
|
|
|
// to be considered a loop
|
|
|
|
|
export const isPathALoop = (
|
|
|
|
|
points: ExcalidrawLinearElement["points"],
|
|
|
|
|
/** supply if you want the loop detection to account for current zoom */
|
|
|
|
|
zoomValue: Zoom["value"] = 1 as NormalizedZoomValue,
|
|
|
|
|
): boolean => {
|
|
|
|
|
if (points.length >= 3) {
|
|
|
|
|
const [firstPoint, lastPoint] = [points[0], points[points.length - 1]];
|
|
|
|
|
return (
|
|
|
|
|
distance2d(firstPoint[0], firstPoint[1], lastPoint[0], lastPoint[1]) <=
|
|
|
|
|
LINE_CONFIRM_THRESHOLD
|
|
|
|
|
);
|
|
|
|
|
const [first, last] = [points[0], points[points.length - 1]];
|
|
|
|
|
const distance = distance2d(first[0], first[1], last[0], last[1]);
|
|
|
|
|
|
|
|
|
|
// Adjusting LINE_CONFIRM_THRESHOLD to current zoom so that when zoomed in
|
|
|
|
|
// really close we make the threshold smaller, and vice versa.
|
|
|
|
|
return distance <= LINE_CONFIRM_THRESHOLD / zoomValue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|