lasso tests

This commit is contained in:
Ryan Di 2025-03-03 11:18:14 +11:00
parent 5d8fd603bb
commit 99c573e88e
4 changed files with 1870 additions and 56 deletions

View file

@ -52,15 +52,27 @@ export class LassoTrail extends AnimatedTrail {
this.intersectedElements.clear();
this.enclosedElements.clear();
try {
this.worker = new Worker(new URL("./worker.ts", import.meta.url), {
type: "module",
});
this.worker.onmessage = (event: MessageEvent<LassoWorkerOutput>) => {
const { selectedElementIds } = event.data;
this.selectElementsFromIds(selectedElementIds);
};
this.worker.onerror = (error) => {
console.error("Worker error:", error);
};
} catch (error) {
console.error("Failed to start worker", error);
}
}
selectElementsFromIds = (ids: string[]) => {
this.app.setState((prevState) => {
const nextSelectedElementIds = selectedElementIds.reduce((acc, id) => {
const nextSelectedElementIds = ids.reduce((acc, id) => {
acc[id] = true;
return acc;
}, {} as Record<ExcalidrawElement["id"], true>);
@ -89,9 +101,7 @@ export class LassoTrail extends AnimatedTrail {
);
const selectedIds = [...Object.keys(nextSelection.selectedElementIds)];
const selectedGroupIds = [
...Object.keys(nextSelection.selectedGroupIds),
];
const selectedGroupIds = [...Object.keys(nextSelection.selectedGroupIds)];
return {
selectedElementIds: nextSelection.selectedElementIds,
@ -110,11 +120,6 @@ export class LassoTrail extends AnimatedTrail {
});
};
this.worker.onerror = (error) => {
console.error("Worker error:", error);
};
}
addPointToPath = (x: number, y: number) => {
super.addPointToPath(x, y);

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,7 @@ export type LassoWorkerInput = {
elementsSegments: ElementsSegmentsMap;
intersectedElements: Set<ExcalidrawElement["id"]>;
enclosedElements: Set<ExcalidrawElement["id"]>;
simplifyDistance: number;
simplifyDistance?: number;
};
export type LassoWorkerOutput = {

View file

@ -86,7 +86,10 @@ export const updateSelection = (input: LassoWorkerInput): LassoWorkerOutput => {
simplifyDistance,
} = input;
// simplify the path to reduce the number of points
const path = simplify(lassoPath, simplifyDistance) as GlobalPoint[];
let path: GlobalPoint[] = lassoPath;
if (simplifyDistance) {
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