Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Group, Controls): Fix interactive group actions when negative scaling is involved #9811

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codesandbox/templates/vanilla/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fabric from 'fabric';
import './styles.css';
import { testCase } from './testcases/nestedSelections';
import { testCase } from './testcases/flippedInteractive';

const el = document.getElementById('canvas');
const canvas = (window.canvas = new fabric.Canvas(el));
Expand Down
69 changes: 69 additions & 0 deletions .codesandbox/templates/vanilla/src/testcases/flippedInteractive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as fabric from 'fabric';

export function testCase(canvas: fabric.Canvas, objectCaching = true) {
function renderNamedControl(
this: fabric.Control,
ctx: CanvasRenderingContext2D,
left: number,
top: number,
styleOverride: any,
fabricObject: fabric.FabricObject
) {
ctx.save();
ctx.font = '12px Arial';
ctx.fillStyle = 'black';
ctx.fillText(this.name || '??', left - 12, top - 3);
ctx.restore();
}

canvas.preserveObjectStacking = true;
const rect = new fabric.Rect({
left: 100,
top: 50,
width: 100,
height: 100,
flipX: false,
fill: 'blue',
padding: 20,
});

const controls = rect.controls;

Object.entries(controls).forEach(([key, control]) => {
control.name = key;
control.render = renderNamedControl;
});

const rect2 = new fabric.Rect({
top: 200,
width: 50,
height: 50,
fill: 'red',
flipX: true,
opacity: 1,
controls,
padding: 20,
});

const g = new fabric.Group([rect], {
subTargetCheck: true,
interactive: true,
objectCaching,
skewX: 0,
angle: 90,
flipY: true,
controls,
});
const g2 = new fabric.Group([rect2], {
top: 100,
left: 100,
subTargetCheck: true,
interactive: true,
objectCaching,
angle: 0,
flipY: false,
controls,
});
canvas.add(g);
canvas.add(g2);
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- fix(Group, Controls): Fix interactive group actions when negative scaling is involved [#9811](https://github.com/fabricjs/fabric.js/pull/9811)
- fix(): Replace 'hasOwn' with 'in' operator in typeAssertions check [#9812](https://github.com/fabricjs/fabric.js/pull/9812)

## [6.0.0-rc1]
Expand Down
9 changes: 7 additions & 2 deletions src/shapes/Object/InteractiveObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,13 @@ export class InteractiveFabricObject<
]),
transformOptions = this.group
? qrDecompose(this.calcTransformMatrix())
: undefined,
dim = this._calculateCurrentDimensions(transformOptions),
: undefined;
// decomposing could bring negative scaling and `_calculateCurrentDimensions` can't take it
if (transformOptions) {
transformOptions.scaleX = Math.abs(transformOptions.scaleX);
transformOptions.scaleY = Math.abs(transformOptions.scaleY);
}
const dim = this._calculateCurrentDimensions(transformOptions),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having _getTransformedDimensions returning always absolute numbers could be the more correct solution but i don't want to shake that now for an edge case fix.
There could be something that is relying on the case of negative dimensions.
having this.scaleX or this.scaleY negative is not supported in general and this is the only place where we use the options for calcDimensions with unchecked values

coords: Record<string, TOCoord> = {};

this.forEachControl((control, key) => {
Expand Down
3 changes: 3 additions & 0 deletions src/shapes/Object/ObjectOrigin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export class ObjectOrigin<EventSpec>
*/
_getTransformedDimensions(options: any = {}): Point {
const dimOptions = {
// if scaleX or scaleY are negative numbers,
// this will return dimensions that are negative.
// and this will break assumptions around the codebase
scaleX: this.scaleX,
scaleY: this.scaleY,
skewX: this.skewX,
Expand Down
57 changes: 57 additions & 0 deletions test/visual/control_rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,63 @@
fabricClass: 'Canvas',
});

function controlboxFlippedXInGroup(canvas, callback) {
var rect = new fabric.Rect({
width: 90, height: 90, padding: 9, angle: 0, flipX: true,
cornerSize: 12, cornerColor: 'green', cornerStrokeColor: 'blue',
transparentCorners: false, borderScaleFactor: 3,
fill: 'red', top: 50, left: 35,
});
var group = new fabric.Group([rect], {
interactive: true,
subTargetCheck: true,
});
canvas.add(group);
canvas.setActiveObject(rect);
canvas.renderAll();
callback(canvas.lowerCanvasEl);
}

tests.push({
test: 'controlbox with flipped X in group',
code: controlboxFlippedXInGroup,
golden: 'controls11group.png',
percentage: 0.004,
width: 150,
height: 180,
fabricClass: 'Canvas',
});

function controlboxFlippedYInRotatedGroup(canvas, callback) {
var rect = new fabric.Rect({
width: 90, height: 90, padding: 9, angle: 0, flipX: false,
cornerSize: 12, cornerColor: 'green', cornerStrokeColor: 'blue',
transparentCorners: false, borderScaleFactor: 3,
fill: 'red', top: 50, left: 35,
});
var group = new fabric.Group([rect], {
angle: 90,
left: 110,
flipY: true,
interactive: true,
subTargetCheck: true,
});
canvas.add(group);
canvas.setActiveObject(rect);
canvas.renderAll();
callback(canvas.lowerCanvasEl);
}

tests.push({
test: 'controlbox with flipped Y in rotated group',
code: controlboxFlippedYInRotatedGroup,
golden: 'controls11group90r.png',
percentage: 0.004,
width: 180,
height: 180,
fabricClass: 'Canvas',
});

function controlboxOpacitySingle(canvas, callback) {
var rect = new fabric.Rect({
width: 90, height: 90, padding: 3, opacity: 0.4,
Expand Down
Binary file added test/visual/golden/controls11group.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/visual/golden/controls11group90r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading