Skip to content

Commit 4cad96f

Browse files
committed
Avoid crash when node inside transformer was destroyed. close #1957
1 parent c621b39 commit 4cad96f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/shapes/Transformer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,11 @@ export class Transformer extends Group {
788788
Math.pow(comparePoint.y - anchorNode.y(), 2)
789789
);
790790

791-
const reverseX = this.findOne('.top-left')!.x() > comparePoint.x ? -1 : 1;
791+
const reverseX =
792+
this.findOne('.top-left')!.x() > comparePoint.x ? -1 : 1;
792793

793-
const reverseY = this.findOne('.top-left')!.y() > comparePoint.y ? -1 : 1;
794+
const reverseY =
795+
this.findOne('.top-left')!.y() > comparePoint.y ? -1 : 1;
794796

795797
x = newHypotenuse * this.cos * reverseX;
796798
y = newHypotenuse * this.sin * reverseY;
@@ -1107,6 +1109,16 @@ export class Transformer extends Group {
11071109
const delta = newTr.multiply(oldTr.invert());
11081110

11091111
this._nodes.forEach((node) => {
1112+
// check to close this issue: https://github.com/konvajs/konva/issues/1957
1113+
// a node can be destroyed during the transformation
1114+
// probably a developer must remove it from transformer
1115+
if (!node.getStage()) {
1116+
// do we need a helping message?
1117+
// Util.error(
1118+
// 'Node is not attached to the stage. This is not allowed. Please attach the node to the stage before transforming. If node was destroyed, make sure to remove it from transformer.'
1119+
// );
1120+
return;
1121+
}
11101122
// for each node we have the same [delta transform]
11111123
// the equations is
11121124
// [delta transform] * [parent transform] * [old local transform] = [parent transform] * [new local transform]

test/unit/Transformer-test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5141,4 +5141,43 @@ describe('Transformer', function () {
51415141
'Event listeners should be cleaned up properly'
51425142
);
51435143
});
5144+
5145+
it.only('should handle transformation when node is not on stage (case for node destroy)', function () {
5146+
var stage = addStage();
5147+
var layer = new Konva.Layer();
5148+
stage.add(layer);
5149+
5150+
// Create a rect but don't add it to stage yet
5151+
var rect = new Konva.Rect({
5152+
x: 50,
5153+
y: 50,
5154+
width: 100,
5155+
height: 100,
5156+
fill: 'yellow',
5157+
});
5158+
5159+
var tr = new Konva.Transformer({
5160+
nodes: [rect],
5161+
});
5162+
layer.add(tr);
5163+
5164+
// Should not throw error when drawing with node not on stage
5165+
layer.draw();
5166+
5167+
// Transformer should have zero size when node is not on stage
5168+
5169+
// Try to move an anchor to trigger transformation - should throw error
5170+
simulateMouseDown(tr, {
5171+
x: 50,
5172+
y: 50,
5173+
});
5174+
simulateMouseMove(tr, {
5175+
x: 60,
5176+
y: 60,
5177+
});
5178+
simulateMouseUp(tr, {
5179+
x: 60,
5180+
y: 60,
5181+
});
5182+
});
51445183
});

0 commit comments

Comments
 (0)