From 0d5091555c96150ef34509ab2f13013abfeb2a9d Mon Sep 17 00:00:00 2001 From: Ryan Di Date: Thu, 27 Feb 2025 19:09:22 +1100 Subject: [PATCH] simplify path threshold adaptive to zoom --- packages/excalidraw/lasso/index.ts | 1 + packages/excalidraw/lasso/worker.ts | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/excalidraw/lasso/index.ts b/packages/excalidraw/lasso/index.ts index 24fc33f53..c5fffe14a 100644 --- a/packages/excalidraw/lasso/index.ts +++ b/packages/excalidraw/lasso/index.ts @@ -168,6 +168,7 @@ export class LassoTrail extends AnimatedTrail { elementsSegments: this.elementsSegments, intersectedElements: this.intersectedElements, enclosedElements: this.enclosedElements, + simplifyDistance: 5 / this.app.state.zoom.value, }; this.worker?.postMessage(message); diff --git a/packages/excalidraw/lasso/worker.ts b/packages/excalidraw/lasso/worker.ts index d9a9f7196..aa1379c90 100644 --- a/packages/excalidraw/lasso/worker.ts +++ b/packages/excalidraw/lasso/worker.ts @@ -79,6 +79,7 @@ export type LassoWorkerInput = { elementsSegments: ElementsSegments; intersectedElements: Set; enclosedElements: Set; + simplifyDistance: number; }; export type LassoWorkerOutput = { @@ -92,9 +93,10 @@ export const updateSelection = (input: LassoWorkerInput): LassoWorkerOutput => { elementsSegments, intersectedElements, enclosedElements, + simplifyDistance, } = input; // simplify the path to reduce the number of points - const path = simplify(lassoPath, 2) as GlobalPoint[]; + let path = simplify(lassoPath, simplifyDistance) as GlobalPoint[]; // close the path to form a polygon for enclosure check const closedPath = polygonFromPoints(path); // as the path might not enclose a shape anymore, clear before checking