From 7951f04664cacd284b37462ae83fb6cf341aa7c7 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 24 Jun 2024 16:36:38 +0200 Subject: [PATCH 1/2] change init active selection --- .../vanilla/src/testcases/clearCache.ts | 27 ++++++++++++------- src/shapes/ActiveSelection.ts | 12 ++++++--- src/shapes/Group.ts | 17 +++++++++++- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/.codesandbox/templates/vanilla/src/testcases/clearCache.ts b/.codesandbox/templates/vanilla/src/testcases/clearCache.ts index d5ccb3e7561..cbe30683173 100644 --- a/.codesandbox/templates/vanilla/src/testcases/clearCache.ts +++ b/.codesandbox/templates/vanilla/src/testcases/clearCache.ts @@ -1,14 +1,23 @@ import * as fabric from 'fabric'; export async function testCase(canvas: fabric.Canvas) { + const utahCommonDefaults = { + originX: 'center' as const, + originY: 'center' as const, + }; + + fabric.ActiveSelection.ownDefaults = { + ...fabric.ActiveSelection.ownDefaults, + ...utahCommonDefaults, + }; + + fabric.Rect.ownDefaults = { + ...fabric.Rect.ownDefaults, + ...utahCommonDefaults, + }; + const rect = new fabric.Rect({ width: 500, height: 500, fill: 'blue' }); - rect.on('moving', function (opt) { - if (this.fill === 'blue') { - this.fill = 'red'; - } else { - this.fill = 'blue'; - } - this.dirty = true; - }); - canvas.add(rect); + const rect2 = new fabric.Rect({ width: 500, height: 500, fill: 'blue' }); + + canvas.add(rect, rect2); } diff --git a/src/shapes/ActiveSelection.ts b/src/shapes/ActiveSelection.ts index 474d65f0cec..837ca763088 100644 --- a/src/shapes/ActiveSelection.ts +++ b/src/shapes/ActiveSelection.ts @@ -59,13 +59,17 @@ export class ActiveSelection extends Group { constructor( objects: FabricObject[] = [], - { layoutManager, ...options }: Partial = {} + options: Partial = {} ) { - super(objects, { - layoutManager: layoutManager ?? new ActiveSelectionLayoutManager(), - }); + super(); Object.assign(this, ActiveSelection.ownDefaults); this.setOptions(options); + const { left, top, layoutManager } = options; + this.groupInit(objects, { + left, + top, + layoutManager: layoutManager ?? new ActiveSelectionLayoutManager(), + }); } /** diff --git a/src/shapes/Group.ts b/src/shapes/Group.ts index c9e85c282b6..ecabc3f9ef8 100644 --- a/src/shapes/Group.ts +++ b/src/shapes/Group.ts @@ -140,6 +140,21 @@ export class Group super(); Object.assign(this, Group.ownDefaults); this.setOptions(options); + this.groupInit(objects, options); + } + + /** + * Shared code between group and active selection + * Meant to be used by the constructor. + */ + protected groupInit( + objects: FabricObject[], + options: { + layoutManager?: LayoutManager; + top?: number; + left?: number; + } + ) { this._objects = [...objects]; // Avoid unwanted mutations of Collection to affect the caller this.__objectSelectionTracker = this.__objectSelectionMonitor.bind( @@ -156,7 +171,7 @@ export class Group }); // perform initial layout - this.layoutManager = options.layoutManager || new LayoutManager(); + this.layoutManager = options.layoutManager ?? new LayoutManager(); this.layoutManager.performLayout({ type: LAYOUT_TYPE_INITIALIZATION, target: this, From 7c9b499291211fa7130ecca2f344fd9c8a8b3704 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 24 Jun 2024 14:39:40 +0000 Subject: [PATCH 2/2] update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e881f403251..915a359b21c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [next] +- fix(Activeselection): Activeselection default initialization [#9940](https://github.com/fabricjs/fabric.js/pull/9940) + ## [6.0.0-rc3] - fix(StaticCanvas): fully clean the cache canvas to avoid leaving trailing pixels [#9779](https://github.com/fabricjs/fabric.js/pull/9779)