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(Canvas): invalidate _objectsToRender on stack change #9387

Merged
merged 6 commits into from
Sep 27, 2023
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
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(Canvas): invalidate `_objectsToRender` on stack change [#9387](https://github.com/fabricjs/fabric.js/pull/9387)
- ci(e2e): fix babel compiling error [#9388](https://github.com/fabricjs/fabric.js/pull/9388)
- Breaking: Remove node 14 [#9383](https://github.com/fabricjs/fabric.js/pull/9383)
- chore(): Rename exports that conflicts with JS/WEB api ( Object, Text, Image ). Kept backward compatibility with deprecation notice [#9172](https://github.com/fabricjs/fabric.js/pull/9172)
Expand Down
9 changes: 7 additions & 2 deletions src/canvas/SelectableCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>
* @type FabricObject[]
* @private
*/
_objectsToRender?: FabricObject[] = [];
_objectsToRender?: FabricObject[];

/**
* hold a referenfce to a data structure that contains information
* hold a reference to a data structure that contains information
* on the current on going transform
* @type
* @private
Expand Down Expand Up @@ -344,6 +344,11 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>
super._onObjectRemoved(obj);
}

_onStackOrderChanged() {
this._objectsToRender = undefined;
super._onStackOrderChanged();
}

/**
* Divides objects in two groups, one to render immediately
* and one to render as activeGroup.
Expand Down
60 changes: 60 additions & 0 deletions src/canvas/__tests__/SelectableCanvas.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { FabricObject } from '../../shapes/Object/FabricObject';
import { Canvas } from '../Canvas';

describe('Canvas', () => {
describe('invalidating `_objectsToRender`', () => {
test('initial state', () => {
const canvas = new Canvas();
expect(canvas._objectsToRender).toBeUndefined();
});

test('mousedown', () => {
const canvas = new Canvas();
canvas.add(new FabricObject({ width: 10, height: 10 }));
canvas._objectsToRender = [];
canvas
.getSelectionElement()
.dispatchEvent(new MouseEvent('mousedown', { clientX: 5, clientY: 5 }));
expect(canvas._objectsToRender).toBeUndefined();
});

test('object added/removed', () => {
const canvas = new Canvas();
canvas._objectsToRender = [];
const object = new FabricObject({ width: 10, height: 10 });
canvas.add(object);
expect(canvas._objectsToRender).toBeUndefined();
canvas._objectsToRender = [];
canvas.remove(object);
expect(canvas._objectsToRender).toBeUndefined();
});

test('stack change', () => {
const canvas = new Canvas();
const object = new FabricObject({ width: 10, height: 10 });
const object2 = new FabricObject({
left: 5,
top: 5,
width: 10,
height: 10,
});
canvas.add(object, object2);

canvas._objectsToRender = [];
canvas.sendObjectBackwards(object2);
expect(canvas._objectsToRender).toBeUndefined();

canvas._objectsToRender = [];
canvas.bringObjectForward(object2);
expect(canvas._objectsToRender).toBeUndefined();

canvas._objectsToRender = [];
canvas.bringObjectToFront(object);
expect(canvas._objectsToRender).toBeUndefined();

canvas._objectsToRender = [];
canvas.sendObjectToBack(object);
expect(canvas._objectsToRender).toBeUndefined();
});
});
});
Loading