From bea4307d78d93692a84f57b8b7573b12dd58ca21 Mon Sep 17 00:00:00 2001 From: Hikaru Y Date: Sun, 15 Oct 2023 09:11:23 +0900 Subject: [PATCH] Another approach to preventing selected shapes from shifting (#2735) * Revert "Fix selected shapes shifting to canvas origin" This reverts commit d81b96fed0adb7db2fb9847c52d7cb73f83b4b32. This introduced a regression where the selection of objects was cleared when it should not have been. For example, if multiple objects were selected and the mouse was released while moving them around, the selection would be cleared. * Another approach to preventing selected shapes from shifting Prevent fabric objects from shifting to the wrong position when the active selection contains multiple objects by calculating their coordinates relative to the canvas, as in the case of the fabric.Group. --- ts/image-occlusion/shapes/to-cloze.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ts/image-occlusion/shapes/to-cloze.ts b/ts/image-occlusion/shapes/to-cloze.ts index bd7666bb8..6ace61ea5 100644 --- a/ts/image-occlusion/shapes/to-cloze.ts +++ b/ts/image-occlusion/shapes/to-cloze.ts @@ -36,18 +36,25 @@ export function exportShapesToClozeDeletions(occludeInactive: boolean): { */ function baseShapesFromFabric(occludeInactive: boolean): ShapeOrShapes[] { const canvas = globalThis.canvas as Canvas; - - // Prevents multiple shapes in 'activeSelection' from shifting to the canvas origin - canvas.discardActiveObject(); - makeMaskTransparent(canvas, false); + const activeObject = canvas.getActiveObject(); + const selectionContainingMultipleObjects = activeObject instanceof fabric.ActiveSelection + && (activeObject.size() > 1) + ? activeObject + : null; const objects = canvas.getObjects() as FabricObject[]; return objects .map((object) => { + // If the object is in the active selection containing multiple objects, + // we need to calculate its x and y coordinates relative to the canvas. + const parent = selectionContainingMultipleObjects?.contains(object) + ? selectionContainingMultipleObjects + : undefined; return fabricObjectToBaseShapeOrShapes( canvas, object, occludeInactive, + parent, ); }) .filter((o): o is ShapeOrShapes => o !== null);