konva 10.3.3 → 10.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +1 -1
  2. package/konva.js +1576 -1636
  3. package/konva.min.js +4 -4
  4. package/lib/Animation.d.ts +63 -3
  5. package/lib/Animation.js +86 -65
  6. package/lib/BezierFunctions.d.ts +10 -0
  7. package/lib/BezierFunctions.js +32 -0
  8. package/lib/Canvas.d.ts +53 -0
  9. package/lib/Canvas.js +93 -34
  10. package/lib/Container.d.ts +156 -9
  11. package/lib/Container.js +260 -25
  12. package/lib/Context.d.ts +247 -0
  13. package/lib/Context.js +312 -74
  14. package/lib/Core.js +1 -0
  15. package/lib/DragAndDrop.d.ts +2 -0
  16. package/lib/DragAndDrop.js +92 -21
  17. package/lib/Factory.d.ts +24 -1
  18. package/lib/Factory.js +14 -20
  19. package/lib/FastLayer.d.ts +14 -1
  20. package/lib/FastLayer.js +14 -1
  21. package/lib/Global.d.ts +114 -3
  22. package/lib/Global.js +134 -5
  23. package/lib/Group.d.ts +11 -0
  24. package/lib/Group.js +11 -0
  25. package/lib/Layer.d.ts +128 -3
  26. package/lib/Layer.js +260 -49
  27. package/lib/Node.d.ts +703 -32
  28. package/lib/Node.js +1545 -202
  29. package/lib/PointerEvents.d.ts +2 -1
  30. package/lib/PointerEvents.js +19 -1
  31. package/lib/Shape.d.ts +99 -14
  32. package/lib/Shape.js +1101 -67
  33. package/lib/Stage.d.ts +85 -2
  34. package/lib/Stage.js +205 -14
  35. package/lib/Tween.d.ts +159 -4
  36. package/lib/Tween.js +243 -24
  37. package/lib/Util.d.ts +148 -4
  38. package/lib/Util.js +354 -82
  39. package/lib/Validators.d.ts +0 -2
  40. package/lib/Validators.js +3 -25
  41. package/lib/_CoreInternals.d.ts +12 -6
  42. package/lib/_CoreInternals.js +1 -0
  43. package/lib/_FullInternals.d.ts +17 -6
  44. package/lib/_FullInternals.js +7 -0
  45. package/lib/canvas-backend.js +6 -0
  46. package/lib/filters/Blur.d.ts +18 -0
  47. package/lib/filters/Blur.js +97 -24
  48. package/lib/filters/Brighten.d.ts +21 -0
  49. package/lib/filters/Brighten.js +24 -0
  50. package/lib/filters/Brightness.d.ts +11 -0
  51. package/lib/filters/Brightness.js +16 -0
  52. package/lib/filters/Contrast.d.ts +10 -0
  53. package/lib/filters/Contrast.js +21 -0
  54. package/lib/filters/Emboss.d.ts +20 -0
  55. package/lib/filters/Emboss.js +68 -8
  56. package/lib/filters/Enhance.d.ts +14 -0
  57. package/lib/filters/Enhance.js +34 -0
  58. package/lib/filters/Grayscale.d.ts +9 -0
  59. package/lib/filters/Grayscale.js +12 -0
  60. package/lib/filters/HSL.d.ts +17 -0
  61. package/lib/filters/HSL.js +48 -5
  62. package/lib/filters/HSV.d.ts +18 -0
  63. package/lib/filters/HSV.js +49 -5
  64. package/lib/filters/Invert.d.ts +9 -0
  65. package/lib/filters/Invert.js +12 -0
  66. package/lib/filters/Kaleidoscope.d.ts +12 -0
  67. package/lib/filters/Kaleidoscope.js +83 -0
  68. package/lib/filters/Mask.d.ts +11 -0
  69. package/lib/filters/Mask.js +18 -0
  70. package/lib/filters/Noise.d.ts +19 -0
  71. package/lib/filters/Noise.js +19 -0
  72. package/lib/filters/Pixelate.d.ts +20 -0
  73. package/lib/filters/Pixelate.js +28 -1
  74. package/lib/filters/Posterize.d.ts +21 -0
  75. package/lib/filters/Posterize.js +22 -0
  76. package/lib/filters/RGB.d.ts +21 -0
  77. package/lib/filters/RGB.js +43 -28
  78. package/lib/filters/RGBA.d.ts +23 -0
  79. package/lib/filters/RGBA.js +28 -41
  80. package/lib/filters/Sepia.d.ts +9 -0
  81. package/lib/filters/Sepia.js +10 -0
  82. package/lib/filters/Solarize.d.ts +10 -0
  83. package/lib/filters/Solarize.js +11 -0
  84. package/lib/filters/Threshold.d.ts +22 -0
  85. package/lib/filters/Threshold.js +22 -0
  86. package/lib/shapes/Arc.d.ts +40 -0
  87. package/lib/shapes/Arc.js +89 -6
  88. package/lib/shapes/Arrow.d.ts +24 -1
  89. package/lib/shapes/Arrow.js +83 -0
  90. package/lib/shapes/Circle.d.ts +18 -0
  91. package/lib/shapes/Circle.js +36 -9
  92. package/lib/shapes/Ellipse.d.ts +34 -2
  93. package/lib/shapes/Ellipse.js +67 -9
  94. package/lib/shapes/Image.d.ts +49 -0
  95. package/lib/shapes/Image.js +151 -22
  96. package/lib/shapes/Label.d.ts +65 -1
  97. package/lib/shapes/Label.js +113 -20
  98. package/lib/shapes/Line.d.ts +50 -8
  99. package/lib/shapes/Line.js +116 -20
  100. package/lib/shapes/Path.d.ts +56 -6
  101. package/lib/shapes/Path.js +203 -128
  102. package/lib/shapes/Rect.d.ts +18 -0
  103. package/lib/shapes/Rect.js +36 -0
  104. package/lib/shapes/RegularPolygon.d.ts +22 -0
  105. package/lib/shapes/RegularPolygon.js +70 -0
  106. package/lib/shapes/Ring.d.ts +19 -0
  107. package/lib/shapes/Ring.js +51 -5
  108. package/lib/shapes/Sprite.d.ts +78 -0
  109. package/lib/shapes/Sprite.js +238 -25
  110. package/lib/shapes/Star.d.ts +23 -0
  111. package/lib/shapes/Star.js +62 -0
  112. package/lib/shapes/Text.d.ts +59 -1
  113. package/lib/shapes/Text.js +481 -97
  114. package/lib/shapes/TextPath.d.ts +49 -3
  115. package/lib/shapes/TextPath.js +278 -34
  116. package/lib/shapes/Transformer.d.ts +124 -12
  117. package/lib/shapes/Transformer.js +864 -140
  118. package/lib/shapes/Wedge.d.ts +22 -0
  119. package/lib/shapes/Wedge.js +67 -3
  120. package/lib/skia-backend.js +9 -0
  121. package/lib/types.d.ts +2 -23
  122. package/lib/types.js +1 -21
  123. package/package.json +12 -16
@@ -25,6 +25,7 @@ const ATTR_CHANGE_LIST = [
25
25
  'anchorCornerRadiusChange',
26
26
  'ignoreStrokeChange',
27
27
  'anchorStyleFuncChange',
28
+ 'paddingChange',
28
29
  ]
29
30
  .map((e) => e + `.${EVENTS_NAME}`)
30
31
  .join(' ');
@@ -61,30 +62,39 @@ function getCursor(anchorName, rad, rotateCursor) {
61
62
  rad += Util.degToRad(ANGLES[anchorName] || 0);
62
63
  const angle = ((Util.radToDeg(rad) % 360) + 360) % 360;
63
64
  if (Util._inRange(angle, 315 + 22.5, 360) || Util._inRange(angle, 0, 22.5)) {
65
+ // TOP
64
66
  return 'ns-resize';
65
67
  }
66
68
  else if (Util._inRange(angle, 45 - 22.5, 45 + 22.5)) {
69
+ // TOP - RIGHT
67
70
  return 'nesw-resize';
68
71
  }
69
72
  else if (Util._inRange(angle, 90 - 22.5, 90 + 22.5)) {
73
+ // RIGHT
70
74
  return 'ew-resize';
71
75
  }
72
76
  else if (Util._inRange(angle, 135 - 22.5, 135 + 22.5)) {
77
+ // BOTTOM - RIGHT
73
78
  return 'nwse-resize';
74
79
  }
75
80
  else if (Util._inRange(angle, 180 - 22.5, 180 + 22.5)) {
81
+ // BOTTOM
76
82
  return 'ns-resize';
77
83
  }
78
84
  else if (Util._inRange(angle, 225 - 22.5, 225 + 22.5)) {
85
+ // BOTTOM - LEFT
79
86
  return 'nesw-resize';
80
87
  }
81
88
  else if (Util._inRange(angle, 270 - 22.5, 270 + 22.5)) {
89
+ // RIGHT
82
90
  return 'ew-resize';
83
91
  }
84
92
  else if (Util._inRange(angle, 315 - 22.5, 315 + 22.5)) {
93
+ // BOTTOM - RIGHT
85
94
  return 'nwse-resize';
86
95
  }
87
96
  else {
97
+ // how can we can there?
88
98
  Util.error('Transformer has unknown angle for cursor detection: ' + angle);
89
99
  return 'pointer';
90
100
  }
@@ -140,24 +150,95 @@ function getSnap(snaps, newRotationRad, tol) {
140
150
  }
141
151
  return snapped;
142
152
  }
143
- let activeTransformersCount = 0;
153
+ const activeTransformers = new Set();
154
+ /**
155
+ * Transformer constructor. Transformer is a special type of group that allow you transform Konva
156
+ * primitives and shapes. Transforming tool is not changing `width` and `height` properties of nodes
157
+ * when you resize them. Instead it changes `scaleX` and `scaleY` properties.
158
+ * @constructor
159
+ * @memberof Konva
160
+ * @param {Object} config
161
+ * @param {Boolean} [config.resizeEnabled] Default is true
162
+ * @param {Boolean} [config.rotateEnabled] Default is true
163
+ * @param {Boolean} [config.rotateLineVisible] Default is true
164
+ * @param {Array} [config.rotationSnaps] Array of angles for rotation snaps. Default is []
165
+ * @param {Number} [config.rotationSnapTolerance] Snapping tolerance. If closer than this it will snap. Default is 5
166
+ * @param {Number} [config.rotateAnchorOffset] Default is 50
167
+ * @param {Number} [config.rotateAnchorAngle] Angle of the rotate anchor around the box, in degrees. Default is 0 (top)
168
+ * @param {String} [config.rotateAnchorCursor] Default is crosshair
169
+ * @param {Number} [config.padding] Default is 0
170
+ * @param {Boolean} [config.borderEnabled] Should we draw border? Default is true
171
+ * @param {String} [config.borderStroke] Border stroke color
172
+ * @param {Number} [config.borderStrokeWidth] Border stroke size
173
+ * @param {Array} [config.borderDash] Array for border dash.
174
+ * @param {String} [config.anchorFill] Anchor fill color
175
+ * @param {String} [config.anchorStroke] Anchor stroke color
176
+ * @param {Number} [config.anchorCornerRadius] Anchor corner radius
177
+ * @param {Number} [config.anchorStrokeWidth] Anchor stroke size
178
+ * @param {Number} [config.anchorSize] Default is 10
179
+ * @param {Boolean} [config.keepRatio] Should we keep ratio when we are moving edges? Default is true
180
+ * @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? Default is 'default'
181
+ * @param {Boolean} [config.centeredScaling] Should we resize relative to node's center? Default is false
182
+ * @param {Array} [config.enabledAnchors] Array of names of enabled handles
183
+ * @param {Boolean} [config.flipEnabled] Can we flip/mirror shape on transform?. True by default
184
+ * @param {Function} [config.boundBoxFunc] Bounding box function, see {@link Konva.Transformer#boundBoxFunc}
185
+ * @param {Function} [config.anchorDragBoundFunc] Function bounding the position of a dragged anchor, see {@link Konva.Transformer#anchorDragBoundFunc}
186
+ * @param {Function} [config.anchorStyleFunc] Function styling every anchor, see {@link Konva.Transformer#anchorStyleFunc}
187
+ * @param {Boolean} [config.ignoreStroke] Should we ignore stroke size? Default is false
188
+ * @param {Boolean} [config.useSingleNodeRotation] When just one node attached, should we use its rotation for transformer?
189
+ * @param {Boolean} [config.shouldOverdrawWholeArea] Should we fill whole transformer area with fake transparent shape to enable dragging from empty spaces?
190
+ * @example
191
+ * var transformer = new Konva.Transformer({
192
+ * nodes: [rectangle],
193
+ * rotateAnchorOffset: 60,
194
+ * enabledAnchors: ['top-left', 'top-right', 'bottom-left', 'bottom-right']
195
+ * });
196
+ * layer.add(transformer);
197
+ */
144
198
  export class Transformer extends Group {
199
+ // the hit graph of a layer is not drawn while a transformer on it, or of
200
+ // a node on it, is transforming (see Layer.shouldDrawHit)
201
+ static _isLayerTransforming(layer) {
202
+ for (const tr of activeTransformers) {
203
+ if (tr.getLayer() === layer ||
204
+ tr._nodes.some((node) => node.getLayer() === layer)) {
205
+ return true;
206
+ }
207
+ }
208
+ return false;
209
+ }
145
210
  constructor(config) {
211
+ // call super constructor
146
212
  super(config);
147
213
  this._movingAnchorName = null;
214
+ this._anchors = {};
148
215
  this._transforming = false;
216
+ // true while _fitNodesInto writes the new attrs into the nodes
217
+ this._fitting = false;
218
+ // the window the transform events are listened to. The stage may be
219
+ // rendered in another window than the one Konva was imported into
149
220
  this._transformWindow = null;
150
221
  this._elementsCreated = false;
151
222
  this._updateScheduled = false;
152
223
  this._createElements();
224
+ // bindings
153
225
  this._handleMouseMove = this._handleMouseMove.bind(this);
154
226
  this._handleMouseUp = this._handleMouseUp.bind(this);
155
227
  this.update = this.update.bind(this);
228
+ // update transformer data for certain attr changes
156
229
  this.on(ATTR_CHANGE_LIST, this.update);
157
230
  if (this.getNode()) {
158
231
  this.update();
159
232
  }
160
233
  }
234
+ /**
235
+ * alias to `tr.nodes([shape])`/ This method is deprecated and will be removed soon.
236
+ * @method
237
+ * @name Konva.Transformer#attachTo
238
+ * @returns {Konva.Transformer}
239
+ * @example
240
+ * transformer.attachTo(shape);
241
+ */
161
242
  attachTo(node) {
162
243
  this.setNode(node);
163
244
  return this;
@@ -177,6 +258,7 @@ export class Transformer extends Group {
177
258
  this.detach();
178
259
  }
179
260
  const filteredNodes = nodes.filter((node) => {
261
+ // check if ancestor of the transformer
180
262
  if (node.isAncestorOf(this)) {
181
263
  Util.error('Konva.Transformer cannot be an a child of the node you are trying to attach');
182
264
  return false;
@@ -192,13 +274,25 @@ export class Transformer extends Group {
192
274
  }
193
275
  this._nodes.forEach((node) => {
194
276
  const onChange = () => {
195
- if (this._transforming)
277
+ var _a;
278
+ // Perf: skip the changes _fitNodesInto makes itself — it does the
279
+ // final _resetTransformCache + update, so per-attr fan-out here is
280
+ // pure waste (was O(N*events) per resize step). A change made between
281
+ // two pointer moves (e.g. a deferred state update) must get through.
282
+ if (this._fitting) {
283
+ (_a = this._nodeRectCache) === null || _a === void 0 ? void 0 : _a.delete(node);
196
284
  return;
285
+ }
197
286
  if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
198
287
  this.rotation(this.nodes()[0].getAbsoluteRotation());
199
288
  }
200
- this._resetTransformCache();
289
+ // Cache reset stays synchronous so sync reads of tr.x() etc. are fresh.
290
+ this._resetTransformCache(node);
201
291
  if (!this.isDragging()) {
292
+ // Perf: an ancestor cascade (e.g. stage drag) fires
293
+ // absoluteTransformChange on every attached node — without batching,
294
+ // a refresh runs once per node and each call walks all N nodes via
295
+ // __getNodeRect, giving O(N^2) per drag frame.
202
296
  this._scheduleUpdate();
203
297
  }
204
298
  };
@@ -210,11 +304,16 @@ export class Transformer extends Group {
210
304
  }
211
305
  node.on(TRANSFORM_CHANGE_STR.map((e) => e + `.${this._getEventNamespace()}`).join(' '), onChange);
212
306
  node.on(`absoluteTransformChange.${this._getEventNamespace()}`, onChange);
307
+ node.on(`destroy.${this._getEventNamespace()}`, () => {
308
+ // a destroyed node has nothing left to transform
309
+ this.setNodes(this._nodes.filter((n) => n !== node));
310
+ });
213
311
  this._proxyDrag(node);
214
312
  });
215
313
  this._resetTransformCache();
216
- const elementsCreated = !!this.findOne('.top-left');
217
- if (elementsCreated) {
314
+ // we may need it if we set node in initial props
315
+ // so elements are not defined yet
316
+ if (this._elementsCreated) {
218
317
  this.update();
219
318
  }
220
319
  return this;
@@ -223,7 +322,9 @@ export class Transformer extends Group {
223
322
  let lastPos;
224
323
  node.on(`dragstart.${this._getEventNamespace()}`, (e) => {
225
324
  lastPos = node.getAbsolutePosition();
226
- if (!this.isDragging() && node !== this.findOne('.back')) {
325
+ // actual dragging of Transformer doesn't make sense
326
+ // but we need to make sure it also has all drag events
327
+ if (!this.isDragging() && node !== this._back) {
227
328
  this.startDrag(e, false);
228
329
  }
229
330
  });
@@ -254,10 +355,27 @@ export class Transformer extends Group {
254
355
  getNodes() {
255
356
  return this._nodes || [];
256
357
  }
358
+ /**
359
+ * return the name of current active anchor
360
+ * @method
361
+ * @name Konva.Transformer#getActiveAnchor
362
+ * @returns {String | Null}
363
+ * @example
364
+ * transformer.getActiveAnchor();
365
+ */
257
366
  getActiveAnchor() {
258
367
  return this._movingAnchorName;
259
368
  }
369
+ /**
370
+ * detach transformer from an attached node
371
+ * @method
372
+ * @name Konva.Transformer#detach
373
+ * @returns {Konva.Transformer}
374
+ * @example
375
+ * transformer.detach();
376
+ */
260
377
  detach() {
378
+ // remove events
261
379
  if (this._nodes) {
262
380
  this._nodes.forEach((node) => {
263
381
  node.off('.' + this._getEventNamespace());
@@ -266,7 +384,25 @@ export class Transformer extends Group {
266
384
  this._nodes = [];
267
385
  this._resetTransformCache();
268
386
  }
269
- _resetTransformCache() {
387
+ /**
388
+ * bind events to the Transformer. You can use events: `transform`, `transformstart`, `transformend`, `dragstart`, `dragmove`, `dragend`
389
+ * @method
390
+ * @name Konva.Transformer#on
391
+ * @param {String} evtStr e.g. 'transform'
392
+ * @param {Function} handler The handler function. The first argument of that function is event object. Event object has `target` as main target of the event, `currentTarget` as current node listener and `evt` as native browser event.
393
+ * @returns {Konva.Transformer}
394
+ * @example
395
+ * // add click listener
396
+ * tr.on('transformstart', function() {
397
+ * console.log('transform started');
398
+ * });
399
+ */
400
+ _resetTransformCache(changedNode) {
401
+ var _a, _b;
402
+ if (changedNode)
403
+ (_a = this._nodeRectCache) === null || _a === void 0 ? void 0 : _a.delete(changedNode);
404
+ else
405
+ (_b = this._nodeRectCache) === null || _b === void 0 ? void 0 : _b.clear();
270
406
  this._clearCache(NODES_RECT);
271
407
  this._clearCache('transform');
272
408
  this._clearSelfAndDescendantCache('absoluteTransform');
@@ -274,31 +410,8 @@ export class Transformer extends Group {
274
410
  _getNodeRect() {
275
411
  return this._getCache(NODES_RECT, this.__getNodeRect);
276
412
  }
277
- __getNodeShape(node, rot = this.rotation(), relative) {
278
- const rect = node.getClientRect({
279
- skipTransform: true,
280
- skipShadow: true,
281
- skipStroke: this.ignoreStroke(),
282
- });
283
- const absScale = node.getAbsoluteScale(relative);
284
- const absPos = node.getAbsolutePosition(relative);
285
- const dx = rect.x * absScale.x - node.offsetX() * absScale.x;
286
- const dy = rect.y * absScale.y - node.offsetY() * absScale.y;
287
- const rotation = (Konva.getAngle(node.getAbsoluteRotation()) + Math.PI * 2) %
288
- (Math.PI * 2);
289
- const box = {
290
- x: absPos.x + dx * Math.cos(rotation) + dy * Math.sin(-rotation),
291
- y: absPos.y + dy * Math.cos(rotation) + dx * Math.sin(rotation),
292
- width: rect.width * absScale.x,
293
- height: rect.height * absScale.y,
294
- rotation: rotation,
295
- };
296
- return rotateAroundPoint(box, -Konva.getAngle(rot), {
297
- x: 0,
298
- y: 0,
299
- });
300
- }
301
413
  __getNodeRect() {
414
+ var _a;
302
415
  const node = this.getNode();
303
416
  if (!node) {
304
417
  return {
@@ -309,39 +422,76 @@ export class Transformer extends Group {
309
422
  rotation: 0,
310
423
  };
311
424
  }
312
- const totalPoints = [];
313
- this.nodes().map((node) => {
314
- const box = node.getClientRect({
315
- skipTransform: true,
316
- skipShadow: true,
317
- skipStroke: this.ignoreStroke(),
318
- });
319
- const points = [
320
- { x: box.x, y: box.y },
321
- { x: box.x + box.width, y: box.y },
322
- { x: box.x + box.width, y: box.y + box.height },
323
- { x: box.x, y: box.y + box.height },
324
- ];
325
- const trans = node.getAbsoluteTransform();
326
- points.forEach(function (point) {
327
- const transformed = trans.point(point);
328
- totalPoints.push(transformed);
329
- });
330
- });
425
+ const rotation = Konva.getAngle(this.rotation());
426
+ const ignoreStroke = this.ignoreStroke();
331
427
  const tr = new Transform();
332
- tr.rotate(-Konva.getAngle(this.rotation()));
428
+ tr.rotate(-rotation);
429
+ const cache = this._nodeRectCache || (this._nodeRectCache = new Map());
333
430
  let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
334
- totalPoints.forEach(function (point) {
335
- const transformed = tr.point(point);
336
- if (minX === undefined) {
337
- minX = maxX = transformed.x;
338
- minY = maxY = transformed.y;
431
+ for (const node of this.nodes()) {
432
+ // Bounds may only be reused when they are a pure function of the node's
433
+ // own size: then every change reaches us either through _setAttr or
434
+ // through the size probe below. A shape with its own getSelfRect - Line
435
+ // mutates its points array in place, and a user subclass can measure
436
+ // anything at all - may change with no signal, so it is measured every
437
+ // time. Groups depend on their descendants, and a cached ancestor
438
+ // suppresses descendant transform-change notifications, so both are
439
+ // measured too.
440
+ const cacheable = node instanceof Shape &&
441
+ node.getSelfRect === Shape.prototype.getSelfRect &&
442
+ !node._hasCachedAncestor();
443
+ // width() and height() are the only inputs of the default getSelfRect and
444
+ // they can come from outside the attrs (an Image source that loads or
445
+ // resizes, Text metrics), so the attr revision alone cannot key them.
446
+ const width = cacheable ? node.width() : 0;
447
+ const height = cacheable ? node.height() : 0;
448
+ if (cacheable)
449
+ (_a = node._attrsVersion) !== null && _a !== void 0 ? _a : (node._attrsVersion = 0);
450
+ let bounds = cacheable ? cache.get(node) : undefined;
451
+ if (!bounds ||
452
+ bounds.rotation !== rotation ||
453
+ bounds.ignoreStroke !== ignoreStroke ||
454
+ bounds.version !== node._attrsVersion ||
455
+ bounds.width !== width ||
456
+ bounds.height !== height) {
457
+ const box = node.getClientRect({
458
+ skipTransform: true,
459
+ skipShadow: true,
460
+ skipStroke: ignoreStroke,
461
+ });
462
+ const points = [
463
+ { x: box.x, y: box.y },
464
+ { x: box.x + box.width, y: box.y },
465
+ { x: box.x + box.width, y: box.y + box.height },
466
+ { x: box.x, y: box.y + box.height },
467
+ ];
468
+ const trans = node.getAbsoluteTransform();
469
+ bounds = {
470
+ rotation,
471
+ ignoreStroke,
472
+ width,
473
+ height,
474
+ version: node._attrsVersion || 0,
475
+ minX: Infinity,
476
+ minY: Infinity,
477
+ maxX: -Infinity,
478
+ maxY: -Infinity,
479
+ };
480
+ for (const point of points) {
481
+ const projected = tr.point(trans.point(point));
482
+ bounds.minX = Math.min(bounds.minX, projected.x);
483
+ bounds.minY = Math.min(bounds.minY, projected.y);
484
+ bounds.maxX = Math.max(bounds.maxX, projected.x);
485
+ bounds.maxY = Math.max(bounds.maxY, projected.y);
486
+ }
487
+ if (cacheable)
488
+ cache.set(node, bounds);
339
489
  }
340
- minX = Math.min(minX, transformed.x);
341
- minY = Math.min(minY, transformed.y);
342
- maxX = Math.max(maxX, transformed.x);
343
- maxY = Math.max(maxY, transformed.y);
344
- });
490
+ minX = Math.min(minX, bounds.minX);
491
+ minY = Math.min(minY, bounds.minY);
492
+ maxX = Math.max(maxX, bounds.maxX);
493
+ maxY = Math.max(maxY, bounds.maxY);
494
+ }
345
495
  tr.invert();
346
496
  const p = tr.point({ x: minX, y: minY });
347
497
  return {
@@ -379,9 +529,12 @@ export class Transformer extends Group {
379
529
  strokeWidth: 1,
380
530
  name: name + ' _anchor',
381
531
  dragDistance: 0,
532
+ // make it draggable,
533
+ // so activating the anchor will not start drag&drop of any parent
382
534
  draggable: true,
383
535
  hitStrokeWidth: TOUCH_DEVICE ? 10 : 'auto',
384
536
  });
537
+ this._anchors[name] = anchor;
385
538
  const self = this;
386
539
  anchor.on('mousedown touchstart', function (e) {
387
540
  self._handleMouseDown(e);
@@ -393,6 +546,7 @@ export class Transformer extends Group {
393
546
  anchor.on('dragend', (e) => {
394
547
  e.cancelBubble = true;
395
548
  });
549
+ // add hover styling
396
550
  anchor.on('mouseenter', () => {
397
551
  const rad = Konva.getAngle(this.rotation());
398
552
  const rotateCursor = this.rotateAnchorCursor();
@@ -421,13 +575,17 @@ export class Transformer extends Group {
421
575
  ctx.beginPath();
422
576
  ctx.rect(-padding, -padding, width + padding * 2, height + padding * 2);
423
577
  if (tr.rotateEnabled() && tr.rotateLineVisible()) {
578
+ // Calculate rotation line position based on rotateAnchorAngle
424
579
  const rotateAnchorAngle = tr.rotateAnchorAngle();
425
580
  const rotateAnchorOffset = tr.rotateAnchorOffset();
426
581
  const rad = Util.degToRad(rotateAnchorAngle);
582
+ // Direction vector (0 degrees = up/top)
427
583
  const dirX = Math.sin(rad);
428
584
  const dirY = -Math.cos(rad);
585
+ // Center of the box
429
586
  const cx = width / 2;
430
587
  const cy = height / 2;
588
+ // Find intersection with box edge
431
589
  let t = Infinity;
432
590
  if (dirY < 0) {
433
591
  t = Math.min(t, -cy / dirY);
@@ -441,8 +599,10 @@ export class Transformer extends Group {
441
599
  else if (dirX > 0) {
442
600
  t = Math.min(t, (width - cx) / dirX);
443
601
  }
602
+ // Edge point (start of line)
444
603
  const edgeX = cx + dirX * t;
445
604
  const edgeY = cy + dirY * t;
605
+ // End point with offset
446
606
  const sign = Util._sign(height);
447
607
  const endX = edgeX + dirX * rotateAnchorOffset * sign;
448
608
  const endY = edgeY + dirY * rotateAnchorOffset * sign;
@@ -461,8 +621,12 @@ export class Transformer extends Group {
461
621
  ctx.fillStrokeShape(shape);
462
622
  },
463
623
  });
624
+ this._back = back;
464
625
  this.add(back);
465
626
  this._proxyDrag(back);
627
+ // do not bubble drag from the back shape
628
+ // because we already "drag" whole transformer
629
+ // so we don't want to trigger drag twice on transformer
466
630
  back.on('dragstart', (e) => {
467
631
  e.cancelBubble = true;
468
632
  });
@@ -472,12 +636,15 @@ export class Transformer extends Group {
472
636
  back.on('dragend', (e) => {
473
637
  e.cancelBubble = true;
474
638
  });
639
+ // force self update when we drag with shouldOverDrawWholeArea setting
475
640
  this.on('dragmove', (e) => {
476
641
  this.update();
477
642
  });
478
643
  }
479
644
  _handleMouseDown(e) {
480
- var _a;
645
+ var _a, _b;
646
+ // do nothing if we already transforming
647
+ // that is possible to trigger with multitouch
481
648
  if (this._transforming) {
482
649
  return;
483
650
  }
@@ -488,6 +655,7 @@ export class Transformer extends Group {
488
655
  const hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
489
656
  this.sin = Math.abs(height / hypotenuse);
490
657
  this.cos = Math.abs(width / hypotenuse);
658
+ // the window that owns the stage is the one that gets the pointer events
491
659
  const win = (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow();
492
660
  this._transformWindow = win || null;
493
661
  if (win) {
@@ -495,26 +663,36 @@ export class Transformer extends Group {
495
663
  win.addEventListener('touchmove', this._handleMouseMove);
496
664
  win.addEventListener('mouseup', this._handleMouseUp, true);
497
665
  win.addEventListener('touchend', this._handleMouseUp, true);
666
+ win.addEventListener('touchcancel', this._handleMouseUp, true);
498
667
  }
499
668
  this._transforming = true;
669
+ // the pointer that hit the anchor (a touchstart can land several fingers)
670
+ this._pointerId = (_b = e.pointerId) !== null && _b !== void 0 ? _b : Util._getFirstPointerId(e.evt);
500
671
  const ap = e.target.getAbsolutePosition();
501
- const pos = e.target.getStage().getPointerPosition();
672
+ const pos = e.target.getStage()._getPointerById(this._pointerId);
502
673
  this._anchorDragOffset = {
503
674
  x: pos.x - ap.x,
504
675
  y: pos.y - ap.y,
505
676
  };
506
- activeTransformersCount++;
677
+ activeTransformers.add(this);
507
678
  this._fire('transformstart', { evt: e.evt, target: this.getNode() });
508
679
  this._nodes.forEach((target) => {
509
680
  target._fire('transformstart', { evt: e.evt, target });
510
681
  });
511
682
  }
512
683
  _handleMouseMove(e) {
684
+ var _a;
685
+ (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._batchEvents(() => this._moveTransform(e));
686
+ }
687
+ _moveTransform(e) {
513
688
  let x, y, newHypotenuse;
514
- const anchorNode = this.findOne('.' + this._movingAnchorName);
689
+ const anchorNode = this._anchors[this._movingAnchorName];
515
690
  const stage = anchorNode.getStage();
516
691
  stage.setPointersPositions(e);
517
- const pp = stage.getPointerPosition();
692
+ const pp = stage._getPointerById(this._pointerId);
693
+ if (!pp) {
694
+ return;
695
+ }
518
696
  let newNodePos = {
519
697
  x: pp.x - this._anchorDragOffset.x,
520
698
  y: pp.y - this._anchorDragOffset.y,
@@ -525,13 +703,17 @@ export class Transformer extends Group {
525
703
  }
526
704
  anchorNode.setAbsolutePosition(newNodePos);
527
705
  const newAbs = anchorNode.getAbsolutePosition();
706
+ // console.log(oldAbs, newNodePos, newAbs);
528
707
  if (oldAbs.x === newAbs.x && oldAbs.y === newAbs.y) {
529
708
  return;
530
709
  }
710
+ // rotater is working very differently, so do it first
531
711
  if (this._movingAnchorName === 'rotater') {
532
712
  const attrs = this._getNodeRect();
533
713
  x = anchorNode.x() - attrs.width / 2;
534
714
  y = -anchorNode.y() + attrs.height / 2;
715
+ // Calculate angle from center to current anchor position
716
+ // Offset by rotateAnchorAngle so we measure rotation from the anchor's starting position
535
717
  const rotateAnchorAngleRad = Konva.getAngle(this.rotateAnchorAngle());
536
718
  let delta = Math.atan2(-y, x) + Math.PI / 2 - rotateAnchorAngleRad;
537
719
  if (attrs.height < 0) {
@@ -558,6 +740,9 @@ export class Transformer extends Group {
558
740
  keepProportion = this.keepRatio() || e.shiftKey;
559
741
  }
560
742
  let centeredScaling = this.centeredScaling() || e.altKey;
743
+ // with keepRatio, a corner anchor doesn't follow the pointer — the blocks
744
+ // below project it onto the ratio diagonal. _doFitNodesInto must know
745
+ // that to skip the padding flip compensation (issue #1878)
561
746
  let anchorProjected = false;
562
747
  if (this._movingAnchorName === 'top-left') {
563
748
  if (keepProportion) {
@@ -568,21 +753,21 @@ export class Transformer extends Group {
568
753
  y: this.height() / 2,
569
754
  }
570
755
  : {
571
- x: this.findOne('.bottom-right').x(),
572
- y: this.findOne('.bottom-right').y(),
756
+ x: this._anchors['bottom-right'].x(),
757
+ y: this._anchors['bottom-right'].y(),
573
758
  };
574
759
  newHypotenuse = Math.sqrt(Math.pow(comparePoint.x - anchorNode.x(), 2) +
575
760
  Math.pow(comparePoint.y - anchorNode.y(), 2));
576
- const reverseX = this.findOne('.top-left').x() > comparePoint.x ? -1 : 1;
577
- const reverseY = this.findOne('.top-left').y() > comparePoint.y ? -1 : 1;
761
+ const reverseX = this._anchors['top-left'].x() > comparePoint.x ? -1 : 1;
762
+ const reverseY = this._anchors['top-left'].y() > comparePoint.y ? -1 : 1;
578
763
  x = newHypotenuse * this.cos * reverseX;
579
764
  y = newHypotenuse * this.sin * reverseY;
580
- this.findOne('.top-left').x(comparePoint.x - x);
581
- this.findOne('.top-left').y(comparePoint.y - y);
765
+ this._anchors['top-left'].x(comparePoint.x - x);
766
+ this._anchors['top-left'].y(comparePoint.y - y);
582
767
  }
583
768
  }
584
769
  else if (this._movingAnchorName === 'top-center') {
585
- this.findOne('.top-left').y(anchorNode.y());
770
+ this._anchors['top-left'].y(anchorNode.y());
586
771
  }
587
772
  else if (this._movingAnchorName === 'top-right') {
588
773
  if (keepProportion) {
@@ -593,27 +778,27 @@ export class Transformer extends Group {
593
778
  y: this.height() / 2,
594
779
  }
595
780
  : {
596
- x: this.findOne('.bottom-left').x(),
597
- y: this.findOne('.bottom-left').y(),
781
+ x: this._anchors['bottom-left'].x(),
782
+ y: this._anchors['bottom-left'].y(),
598
783
  };
599
784
  newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - comparePoint.x, 2) +
600
785
  Math.pow(comparePoint.y - anchorNode.y(), 2));
601
- const reverseX = this.findOne('.top-right').x() < comparePoint.x ? -1 : 1;
602
- const reverseY = this.findOne('.top-right').y() > comparePoint.y ? -1 : 1;
786
+ const reverseX = this._anchors['top-right'].x() < comparePoint.x ? -1 : 1;
787
+ const reverseY = this._anchors['top-right'].y() > comparePoint.y ? -1 : 1;
603
788
  x = newHypotenuse * this.cos * reverseX;
604
789
  y = newHypotenuse * this.sin * reverseY;
605
- this.findOne('.top-right').x(comparePoint.x + x);
606
- this.findOne('.top-right').y(comparePoint.y - y);
790
+ this._anchors['top-right'].x(comparePoint.x + x);
791
+ this._anchors['top-right'].y(comparePoint.y - y);
607
792
  }
608
793
  var pos = anchorNode.position();
609
- this.findOne('.top-left').y(pos.y);
610
- this.findOne('.bottom-right').x(pos.x);
794
+ this._anchors['top-left'].y(pos.y);
795
+ this._anchors['bottom-right'].x(pos.x);
611
796
  }
612
797
  else if (this._movingAnchorName === 'middle-left') {
613
- this.findOne('.top-left').x(anchorNode.x());
798
+ this._anchors['top-left'].x(anchorNode.x());
614
799
  }
615
800
  else if (this._movingAnchorName === 'middle-right') {
616
- this.findOne('.bottom-right').x(anchorNode.x());
801
+ this._anchors['bottom-right'].x(anchorNode.x());
617
802
  }
618
803
  else if (this._movingAnchorName === 'bottom-left') {
619
804
  if (keepProportion) {
@@ -624,8 +809,8 @@ export class Transformer extends Group {
624
809
  y: this.height() / 2,
625
810
  }
626
811
  : {
627
- x: this.findOne('.top-right').x(),
628
- y: this.findOne('.top-right').y(),
812
+ x: this._anchors['top-right'].x(),
813
+ y: this._anchors['top-right'].y(),
629
814
  };
630
815
  newHypotenuse = Math.sqrt(Math.pow(comparePoint.x - anchorNode.x(), 2) +
631
816
  Math.pow(anchorNode.y() - comparePoint.y, 2));
@@ -637,11 +822,11 @@ export class Transformer extends Group {
637
822
  anchorNode.y(comparePoint.y + y);
638
823
  }
639
824
  pos = anchorNode.position();
640
- this.findOne('.top-left').x(pos.x);
641
- this.findOne('.bottom-right').y(pos.y);
825
+ this._anchors['top-left'].x(pos.x);
826
+ this._anchors['bottom-right'].y(pos.y);
642
827
  }
643
828
  else if (this._movingAnchorName === 'bottom-center') {
644
- this.findOne('.bottom-right').y(anchorNode.y());
829
+ this._anchors['bottom-right'].y(anchorNode.y());
645
830
  }
646
831
  else if (this._movingAnchorName === 'bottom-right') {
647
832
  if (keepProportion) {
@@ -652,17 +837,17 @@ export class Transformer extends Group {
652
837
  y: this.height() / 2,
653
838
  }
654
839
  : {
655
- x: this.findOne('.top-left').x(),
656
- y: this.findOne('.top-left').y(),
840
+ x: this._anchors['top-left'].x(),
841
+ y: this._anchors['top-left'].y(),
657
842
  };
658
843
  newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - comparePoint.x, 2) +
659
844
  Math.pow(anchorNode.y() - comparePoint.y, 2));
660
- const reverseX = this.findOne('.bottom-right').x() < comparePoint.x ? -1 : 1;
661
- const reverseY = this.findOne('.bottom-right').y() < comparePoint.y ? -1 : 1;
845
+ const reverseX = this._anchors['bottom-right'].x() < comparePoint.x ? -1 : 1;
846
+ const reverseY = this._anchors['bottom-right'].y() < comparePoint.y ? -1 : 1;
662
847
  x = newHypotenuse * this.cos * reverseX;
663
848
  y = newHypotenuse * this.sin * reverseY;
664
- this.findOne('.bottom-right').x(comparePoint.x + x);
665
- this.findOne('.bottom-right').y(comparePoint.y + y);
849
+ this._anchors['bottom-right'].x(comparePoint.x + x);
850
+ this._anchors['bottom-right'].y(comparePoint.y + y);
666
851
  }
667
852
  }
668
853
  else {
@@ -671,8 +856,8 @@ export class Transformer extends Group {
671
856
  }
672
857
  centeredScaling = this.centeredScaling() || e.altKey;
673
858
  if (centeredScaling) {
674
- const topLeft = this.findOne('.top-left');
675
- const bottomRight = this.findOne('.bottom-right');
859
+ const topLeft = this._anchors['top-left'];
860
+ const bottomRight = this._anchors['bottom-right'];
676
861
  const topOffsetX = topLeft.x();
677
862
  const topOffsetY = topLeft.y();
678
863
  const bottomOffsetX = this.getWidth() - bottomRight.x();
@@ -686,11 +871,11 @@ export class Transformer extends Group {
686
871
  y: bottomOffsetY,
687
872
  });
688
873
  }
689
- const absPos = this.findOne('.top-left').getAbsolutePosition();
874
+ const absPos = this._anchors['top-left'].getAbsolutePosition();
690
875
  x = absPos.x;
691
876
  y = absPos.y;
692
- const width = this.findOne('.bottom-right').x() - this.findOne('.top-left').x();
693
- const height = this.findOne('.bottom-right').y() - this.findOne('.top-left').y();
877
+ const width = this._anchors['bottom-right'].x() - this._anchors['top-left'].x();
878
+ const height = this._anchors['bottom-right'].y() - this._anchors['top-left'].y();
694
879
  this._fitNodesInto({
695
880
  x: x,
696
881
  y: y,
@@ -700,10 +885,24 @@ export class Transformer extends Group {
700
885
  }, e, anchorProjected);
701
886
  }
702
887
  _handleMouseUp(e) {
703
- this._removeEvents(e);
888
+ // only the pointer that grabbed the anchor ends the transform
889
+ const stage = this.getStage();
890
+ if (stage) {
891
+ stage.setPointersPositions(e);
892
+ if (!stage._changedPointerPositions.some((p) => p.id === this._pointerId)) {
893
+ return;
894
+ }
895
+ }
896
+ if (stage)
897
+ stage._batchEvents(() => this._removeEvents(e));
898
+ else
899
+ this._removeEvents(e);
704
900
  }
705
- getAbsoluteTransform() {
706
- return this.getTransform();
901
+ // the transformer positions itself in absolute coordinates (see
902
+ // _getNodeRect), whatever the transforms of its ancestors are
903
+ getAbsoluteTransform(top) {
904
+ const at = this.getTransform();
905
+ return top ? top.getAbsoluteTransform().copy().invert().multiply(at) : at;
707
906
  }
708
907
  _removeEvents(e) {
709
908
  var _a;
@@ -716,15 +915,18 @@ export class Transformer extends Group {
716
915
  win.removeEventListener('touchmove', this._handleMouseMove);
717
916
  win.removeEventListener('mouseup', this._handleMouseUp, true);
718
917
  win.removeEventListener('touchend', this._handleMouseUp, true);
918
+ win.removeEventListener('touchcancel', this._handleMouseUp, true);
719
919
  }
720
920
  const node = this.getNode();
721
- activeTransformersCount--;
921
+ activeTransformers.delete(this);
722
922
  this._fire('transformend', { evt: e, target: node });
923
+ // redraw layer to restore hit graph
723
924
  (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
724
925
  if (node) {
725
926
  this._nodes.forEach((target) => {
726
927
  var _a;
727
928
  target._fire('transformend', { evt: e, target });
929
+ // redraw layer to restore hit graph
728
930
  (_a = target.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
729
931
  });
730
932
  }
@@ -732,28 +934,39 @@ export class Transformer extends Group {
732
934
  }
733
935
  }
734
936
  _fitNodesInto(newAttrs, evt, anchorProjected = false) {
937
+ // Perf: suspend autoDraw for the whole method so per-attr _setAttr writes
938
+ // don't each trigger _requestDraw -> getLayer parent-chain walks.
939
+ // batchDraw per affected layer is issued at the end.
735
940
  const prevAutoDraw = Konva.autoDrawEnabled;
736
941
  Konva.autoDrawEnabled = false;
942
+ this._fitting = true;
737
943
  try {
738
944
  return this._doFitNodesInto(newAttrs, evt, anchorProjected);
739
945
  }
740
946
  finally {
741
947
  Konva.autoDrawEnabled = prevAutoDraw;
948
+ this._fitting = false;
742
949
  }
743
950
  }
744
951
  _doFitNodesInto(newAttrs, evt, anchorProjected = false) {
745
952
  const oldAttrs = this._getNodeRect();
746
953
  const minSize = 1;
747
- if (Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize)) {
748
- this.update();
749
- return;
750
- }
751
- if (Util._inRange(newAttrs.height, -this.padding() * 2 - minSize, minSize)) {
954
+ // a zero-size box has a singular transform, there is nothing to scale from
955
+ if (!oldAttrs.width ||
956
+ !oldAttrs.height ||
957
+ Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize) ||
958
+ Util._inRange(newAttrs.height, -this.padding() * 2 - minSize, minSize)) {
752
959
  this.update();
753
960
  return;
754
961
  }
755
962
  const t = new Transform();
756
963
  t.rotate(Konva.getAngle(this.rotation()));
964
+ // the ±2*padding shifts below assume the box size passed through zero
965
+ // continuously (the anchor tied to the pointer). A projected anchor snaps
966
+ // the box across zero instead; the shifts then leave the drag state
967
+ // inconsistent with the real anchor position and the box flips back and
968
+ // forth on every following move (issue #1878) — so flip only the anchor
969
+ // name, exactly as when padding is 0.
757
970
  const flipPadding = anchorProjected ? 0 : this.padding() * 2;
758
971
  let widthFlip = null;
759
972
  let heightFlip = null;
@@ -810,6 +1023,8 @@ export class Transformer extends Group {
810
1023
  Util.warn('boundBoxFunc returned falsy. You should return new bound rect from it!');
811
1024
  }
812
1025
  }
1026
+ // a staged flip survived only if the bounded size is still negative on
1027
+ // its axis
813
1028
  for (const flip of [widthFlip, heightFlip]) {
814
1029
  if (flip && newAttrs[flip.axis] < 0 && this._movingAnchorName) {
815
1030
  this._movingAnchorName = this._movingAnchorName.replace(flip.from, flip.to);
@@ -817,6 +1032,10 @@ export class Transformer extends Group {
817
1032
  this._anchorDragOffset.y -= flip.offset.y;
818
1033
  }
819
1034
  }
1035
+ // base size value doesn't really matter
1036
+ // we just need to think about bounding boxes as transforms
1037
+ // but how?
1038
+ // the idea is that we have a transformed rectangle with the size of "baseSize"
820
1039
  const baseSize = 10000000;
821
1040
  const oldTr = new Transform();
822
1041
  oldTr.translate(oldAttrs.x, oldAttrs.y);
@@ -836,14 +1055,35 @@ export class Transformer extends Group {
836
1055
  newTr.rotate(newAttrs.rotation);
837
1056
  newTr.scale(newScaleX, newScaleY);
838
1057
  }
1058
+ // now lets think we had [old transform] and n ow we have [new transform]
1059
+ // Now, the questions is: how can we transform "parent" to go from [old transform] into [new transform]
1060
+ // in equation it will be:
1061
+ // [delta transform] * [old transform] = [new transform]
1062
+ // that means that
1063
+ // [delta transform] = [new transform] * [old transform inverted]
839
1064
  const delta = newTr.multiply(oldTr.invert());
1065
+ // Perf: collect unique affected layers and batchDraw each once at the end
1066
+ // (was once per node — O(N)).
840
1067
  const layersToDraw = new Set();
841
1068
  this._nodes.forEach((node) => {
1069
+ // check to close this issue: https://github.com/konvajs/konva/issues/1957
1070
+ // a node can be destroyed during the transformation
1071
+ // probably a developer must remove it from transformer
842
1072
  if (!node.getStage()) {
1073
+ // do we need a helping message?
1074
+ // Util.error(
1075
+ // '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.'
1076
+ // );
843
1077
  return;
844
1078
  }
1079
+ // for each node we have the same [delta transform]
1080
+ // the equations is
1081
+ // [delta transform] * [parent transform] * [old local transform] = [parent transform] * [new local transform]
1082
+ // and we need to find [new local transform]
1083
+ // [new local] = [parent inverted] * [delta] * [parent] * [old local]
845
1084
  const parentTransform = node.getParent().getAbsoluteTransform();
846
1085
  const localTransform = node.getTransform().copy();
1086
+ // skip offset:
847
1087
  localTransform.translate(node.offsetX(), node.offsetY());
848
1088
  const newLocalTransform = new Transform();
849
1089
  newLocalTransform
@@ -859,6 +1099,7 @@ export class Transformer extends Group {
859
1099
  }
860
1100
  });
861
1101
  this.rotation(Util._getRotation(newAttrs.rotation));
1102
+ // trigger transform event AFTER we update rotation
862
1103
  this._nodes.forEach((node) => {
863
1104
  this._fire('transform', { evt: evt, target: node });
864
1105
  node._fire('transform', { evt: evt, target: node });
@@ -868,6 +1109,9 @@ export class Transformer extends Group {
868
1109
  layersToDraw.add(this.getLayer());
869
1110
  layersToDraw.forEach((layer) => layer && layer.batchDraw());
870
1111
  }
1112
+ // Inside an absoluteTransform cascade, queue a single update to run when
1113
+ // the cascade ends; outside a cascade, run synchronously so reads of
1114
+ // transformer state stay consistent.
871
1115
  _scheduleUpdate() {
872
1116
  if (this._updateScheduled)
873
1117
  return;
@@ -875,23 +1119,56 @@ export class Transformer extends Group {
875
1119
  Node._runAfterAbsTransformCascade(() => {
876
1120
  var _a;
877
1121
  this._updateScheduled = false;
878
- if (!((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) || this._transforming || this.isDragging()) {
1122
+ if (!((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) || this._fitting || this.isDragging()) {
879
1123
  return;
880
1124
  }
881
- this.update();
1125
+ this._update();
882
1126
  });
883
1127
  }
1128
+ /**
1129
+ * force update of Konva.Transformer.
1130
+ * Use it when you updated attached Konva.Group and now you need to reset transformer size
1131
+ * @method
1132
+ * @name Konva.Transformer#forceUpdate
1133
+ */
884
1134
  forceUpdate() {
885
1135
  this._resetTransformCache();
886
1136
  this.update();
887
1137
  }
888
- _batchChangeChild(selector, attrs) {
889
- const anchor = this.findOne(selector);
890
- anchor.setAttrs(attrs);
891
- }
892
1138
  update() {
1139
+ // Explicit updates and Transformer configuration changes also refresh
1140
+ // styles and layout when the selected nodes' bounds are unchanged.
1141
+ this._lastNodeRect = undefined;
1142
+ this._update();
1143
+ }
1144
+ _update() {
893
1145
  var _a;
894
1146
  const attrs = this._getNodeRect();
1147
+ this._updateElements(attrs);
1148
+ const draggable = this.nodes().some((node) => node.draggable());
1149
+ if (this._back.draggable() !== draggable) {
1150
+ this._back.draggable(draggable);
1151
+ }
1152
+ const styleFunc = this.anchorStyleFunc();
1153
+ if (styleFunc) {
1154
+ Object.values(this._anchors).forEach((node) => styleFunc(node));
1155
+ }
1156
+ (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
1157
+ }
1158
+ _updateElements(attrs) {
1159
+ const previous = this._lastNodeRect;
1160
+ // Only element layout is cached. Drag handling, user callbacks and drawing
1161
+ // remain in _update(). Custom styling needs fresh defaults on every update.
1162
+ if (previous &&
1163
+ !this.anchorStyleFunc() &&
1164
+ attrs.x === previous.x &&
1165
+ attrs.y === previous.y &&
1166
+ attrs.width === previous.width &&
1167
+ attrs.height === previous.height &&
1168
+ attrs.rotation === previous.rotation) {
1169
+ return;
1170
+ }
1171
+ this._lastNodeRect = { ...attrs };
895
1172
  this.rotation(Util._getRotation(attrs.rotation));
896
1173
  const width = attrs.width;
897
1174
  const height = attrs.height;
@@ -899,7 +1176,7 @@ export class Transformer extends Group {
899
1176
  const resizeEnabled = this.resizeEnabled();
900
1177
  const padding = this.padding();
901
1178
  const anchorSize = this.anchorSize();
902
- const anchors = this.find('._anchor');
1179
+ const anchors = Object.values(this._anchors);
903
1180
  anchors.forEach((node) => {
904
1181
  node.setAttrs({
905
1182
  width: anchorSize,
@@ -912,115 +1189,127 @@ export class Transformer extends Group {
912
1189
  cornerRadius: this.anchorCornerRadius(),
913
1190
  });
914
1191
  });
915
- this._batchChangeChild('.top-left', {
1192
+ this._anchors['top-left'].setAttrs({
916
1193
  x: 0,
917
1194
  y: 0,
918
1195
  offsetX: anchorSize / 2 + padding,
919
1196
  offsetY: anchorSize / 2 + padding,
920
1197
  visible: resizeEnabled && enabledAnchors.indexOf('top-left') >= 0,
921
1198
  });
922
- this._batchChangeChild('.top-center', {
1199
+ this._anchors['top-center'].setAttrs({
923
1200
  x: width / 2,
924
1201
  y: 0,
925
1202
  offsetY: anchorSize / 2 + padding,
926
1203
  visible: resizeEnabled && enabledAnchors.indexOf('top-center') >= 0,
927
1204
  });
928
- this._batchChangeChild('.top-right', {
1205
+ this._anchors['top-right'].setAttrs({
929
1206
  x: width,
930
1207
  y: 0,
931
1208
  offsetX: anchorSize / 2 - padding,
932
1209
  offsetY: anchorSize / 2 + padding,
933
1210
  visible: resizeEnabled && enabledAnchors.indexOf('top-right') >= 0,
934
1211
  });
935
- this._batchChangeChild('.middle-left', {
1212
+ this._anchors['middle-left'].setAttrs({
936
1213
  x: 0,
937
1214
  y: height / 2,
938
1215
  offsetX: anchorSize / 2 + padding,
939
1216
  visible: resizeEnabled && enabledAnchors.indexOf('middle-left') >= 0,
940
1217
  });
941
- this._batchChangeChild('.middle-right', {
1218
+ this._anchors['middle-right'].setAttrs({
942
1219
  x: width,
943
1220
  y: height / 2,
944
1221
  offsetX: anchorSize / 2 - padding,
945
1222
  visible: resizeEnabled && enabledAnchors.indexOf('middle-right') >= 0,
946
1223
  });
947
- this._batchChangeChild('.bottom-left', {
1224
+ this._anchors['bottom-left'].setAttrs({
948
1225
  x: 0,
949
1226
  y: height,
950
1227
  offsetX: anchorSize / 2 + padding,
951
1228
  offsetY: anchorSize / 2 - padding,
952
1229
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-left') >= 0,
953
1230
  });
954
- this._batchChangeChild('.bottom-center', {
1231
+ this._anchors['bottom-center'].setAttrs({
955
1232
  x: width / 2,
956
1233
  y: height,
957
1234
  offsetY: anchorSize / 2 - padding,
958
1235
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-center') >= 0,
959
1236
  });
960
- this._batchChangeChild('.bottom-right', {
1237
+ this._anchors['bottom-right'].setAttrs({
961
1238
  x: width,
962
1239
  y: height,
963
1240
  offsetX: anchorSize / 2 - padding,
964
1241
  offsetY: anchorSize / 2 - padding,
965
1242
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
966
1243
  });
1244
+ // Calculate rotation anchor position based on rotateAnchorAngle
967
1245
  const rotateAnchorAngle = this.rotateAnchorAngle();
968
1246
  const rotateAnchorOffset = this.rotateAnchorOffset();
969
1247
  const rad = Util.degToRad(rotateAnchorAngle);
1248
+ // Direction vector (0 degrees = up/top)
970
1249
  const dirX = Math.sin(rad);
971
1250
  const dirY = -Math.cos(rad);
1251
+ // Center of the box
972
1252
  const cx = width / 2;
973
1253
  const cy = height / 2;
1254
+ // Find intersection with box edge
1255
+ // Calculate time to hit each edge from center
974
1256
  let t = Infinity;
1257
+ // Handle each direction
975
1258
  if (dirY < 0) {
1259
+ // Moving up, check top edge (y = 0)
976
1260
  t = Math.min(t, -cy / dirY);
977
1261
  }
978
1262
  else if (dirY > 0) {
1263
+ // Moving down, check bottom edge (y = height)
979
1264
  t = Math.min(t, (height - cy) / dirY);
980
1265
  }
981
1266
  if (dirX < 0) {
1267
+ // Moving left, check left edge (x = 0)
982
1268
  t = Math.min(t, -cx / dirX);
983
1269
  }
984
1270
  else if (dirX > 0) {
1271
+ // Moving right, check right edge (x = width)
985
1272
  t = Math.min(t, (width - cx) / dirX);
986
1273
  }
1274
+ // Edge point
987
1275
  const edgeX = cx + dirX * t;
988
1276
  const edgeY = cy + dirY * t;
1277
+ // Final position with offset (accounting for height sign and padding)
989
1278
  const sign = Util._sign(height);
990
- this._batchChangeChild('.rotater', {
1279
+ this._anchors['rotater'].setAttrs({
991
1280
  x: edgeX + dirX * rotateAnchorOffset * sign,
992
1281
  y: edgeY + dirY * rotateAnchorOffset * sign - padding * dirY,
993
1282
  visible: this.rotateEnabled(),
994
1283
  });
995
- this._batchChangeChild('.back', {
1284
+ this._back.setAttrs({
996
1285
  width: width,
997
1286
  height: height,
998
1287
  visible: this.borderEnabled(),
999
1288
  stroke: this.borderStroke(),
1000
1289
  strokeWidth: this.borderStrokeWidth(),
1001
1290
  dash: this.borderDash(),
1002
- draggable: this.nodes().some((node) => node.draggable()),
1003
1291
  x: 0,
1004
1292
  y: 0,
1005
1293
  });
1006
- const styleFunc = this.anchorStyleFunc();
1007
- if (styleFunc) {
1008
- anchors.forEach((node) => {
1009
- styleFunc(node);
1010
- });
1011
- }
1012
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
1013
1294
  }
1295
+ /**
1296
+ * determine if transformer is in active transform
1297
+ * @method
1298
+ * @name Konva.Transformer#isTransforming
1299
+ * @returns {Boolean}
1300
+ */
1014
1301
  isTransforming() {
1015
1302
  return this._transforming;
1016
1303
  }
1304
+ /**
1305
+ * Stop active transform action
1306
+ * @method
1307
+ * @name Konva.Transformer#stopTransform
1308
+ * @returns {Boolean}
1309
+ */
1017
1310
  stopTransform() {
1018
1311
  if (this._transforming) {
1019
1312
  this._removeEvents();
1020
- const anchorNode = this.findOne('.' + this._movingAnchorName);
1021
- if (anchorNode) {
1022
- anchorNode.stopDrag();
1023
- }
1024
1313
  }
1025
1314
  }
1026
1315
  destroy() {
@@ -1032,6 +1321,10 @@ export class Transformer extends Group {
1032
1321
  this._removeEvents();
1033
1322
  return this;
1034
1323
  }
1324
+ // Transformer manages its own internal children (anchors, back shape).
1325
+ // Adding external nodes as children can cause infinite recursion because
1326
+ // the Transformer's absoluteTransform depends on nodes' transforms.
1327
+ // Use tr.nodes([node]) to attach nodes instead.
1035
1328
  add(...children) {
1036
1329
  if (this._elementsCreated) {
1037
1330
  Util.error('You cannot add external nodes to the Transformer. Use tr.nodes([node]) instead.');
@@ -1039,24 +1332,29 @@ export class Transformer extends Group {
1039
1332
  }
1040
1333
  return super.add(...children);
1041
1334
  }
1335
+ // do not work as a container
1336
+ // we will recreate inner nodes manually
1042
1337
  toObject() {
1043
1338
  return Node.prototype.toObject.call(this);
1044
1339
  }
1340
+ // overwrite clone to NOT use method from Container
1045
1341
  clone(obj) {
1046
1342
  const node = Node.prototype.clone.call(this, obj);
1047
1343
  return node;
1048
1344
  }
1049
- getClientRect() {
1345
+ getClientRect(config) {
1050
1346
  if (this.nodes().length > 0) {
1051
- return super.getClientRect();
1347
+ return super.getClientRect(config);
1052
1348
  }
1053
1349
  else {
1350
+ // if we are detached return zero size
1351
+ // so it will be skipped in calculations
1054
1352
  return { x: 0, y: 0, width: 0, height: 0 };
1055
1353
  }
1056
1354
  }
1057
1355
  }
1058
1356
  Transformer.isTransforming = () => {
1059
- return activeTransformersCount > 0;
1357
+ return activeTransformers.size > 0;
1060
1358
  };
1061
1359
  function validateAnchors(val) {
1062
1360
  if (!(val instanceof Array)) {
@@ -1076,36 +1374,462 @@ function validateAnchors(val) {
1076
1374
  }
1077
1375
  Transformer.prototype.className = 'Transformer';
1078
1376
  _registerNode(Transformer);
1377
+ /**
1378
+ * get/set enabled handlers
1379
+ * @name Konva.Transformer#enabledAnchors
1380
+ * @method
1381
+ * @param {Array} array
1382
+ * @returns {Array}
1383
+ * @example
1384
+ * // get list of handlers
1385
+ * var enabledAnchors = transformer.enabledAnchors();
1386
+ *
1387
+ * // set handlers
1388
+ * transformer.enabledAnchors(['top-left', 'top-center', 'top-right', 'middle-right', 'middle-left', 'bottom-left', 'bottom-center', 'bottom-right']);
1389
+ */
1079
1390
  Factory.addGetterSetter(Transformer, 'enabledAnchors', ANCHORS_NAMES, validateAnchors);
1391
+ /**
1392
+ * get/set flip enabled
1393
+ * @name Konva.Transformer#flipEnabled
1394
+ * @method
1395
+ * @param {Boolean} flag
1396
+ * @returns {Boolean}
1397
+ * @example
1398
+ * // get flip enabled property
1399
+ * var flipEnabled = transformer.flipEnabled();
1400
+ *
1401
+ * // set flip enabled property
1402
+ * transformer.flipEnabled(false);
1403
+ */
1080
1404
  Factory.addGetterSetter(Transformer, 'flipEnabled', true, getBooleanValidator());
1405
+ /**
1406
+ * get/set resize ability. If false it will automatically hide resizing handlers
1407
+ * @name Konva.Transformer#resizeEnabled
1408
+ * @method
1409
+ * @param {Boolean} enabled
1410
+ * @returns {Boolean}
1411
+ * @example
1412
+ * // get
1413
+ * var resizeEnabled = transformer.resizeEnabled();
1414
+ *
1415
+ * // set
1416
+ * transformer.resizeEnabled(false);
1417
+ */
1081
1418
  Factory.addGetterSetter(Transformer, 'resizeEnabled', true);
1419
+ /**
1420
+ * get/set anchor size. Default is 10
1421
+ * @name Konva.Transformer#anchorSize
1422
+ * @method
1423
+ * @param {Number} size
1424
+ * @returns {Number}
1425
+ * @example
1426
+ * // get
1427
+ * var anchorSize = transformer.anchorSize();
1428
+ *
1429
+ * // set
1430
+ * transformer.anchorSize(20)
1431
+ */
1082
1432
  Factory.addGetterSetter(Transformer, 'anchorSize', 10, getNumberValidator());
1433
+ /**
1434
+ * get/set ability to rotate.
1435
+ * @name Konva.Transformer#rotateEnabled
1436
+ * @method
1437
+ * @param {Boolean} enabled
1438
+ * @returns {Boolean}
1439
+ * @example
1440
+ * // get
1441
+ * var rotateEnabled = transformer.rotateEnabled();
1442
+ *
1443
+ * // set
1444
+ * transformer.rotateEnabled(false);
1445
+ */
1083
1446
  Factory.addGetterSetter(Transformer, 'rotateEnabled', true);
1447
+ /**
1448
+ * get/set visibility of a little line that connects transformer and rotate anchor.
1449
+ * @name Konva.Transformer#rotateLineVisible
1450
+ * @method
1451
+ * @param {Boolean} enabled
1452
+ * @returns {Boolean}
1453
+ * @example
1454
+ * // get
1455
+ * var rotateLineVisible = transformer.rotateLineVisible();
1456
+ *
1457
+ * // set
1458
+ * transformer.rotateLineVisible(false);
1459
+ */
1084
1460
  Factory.addGetterSetter(Transformer, 'rotateLineVisible', true);
1461
+ /**
1462
+ * get/set rotation snaps angles.
1463
+ * @name Konva.Transformer#rotationSnaps
1464
+ * @method
1465
+ * @param {Array} array
1466
+ * @returns {Array}
1467
+ * @example
1468
+ * // get
1469
+ * var rotationSnaps = transformer.rotationSnaps();
1470
+ *
1471
+ * // set
1472
+ * transformer.rotationSnaps([0, 90, 180, 270]);
1473
+ */
1085
1474
  Factory.addGetterSetter(Transformer, 'rotationSnaps', []);
1475
+ /**
1476
+ * get/set distance for rotation handler
1477
+ * @name Konva.Transformer#rotateAnchorOffset
1478
+ * @method
1479
+ * @param {Number} offset
1480
+ * @returns {Number}
1481
+ * @example
1482
+ * // get
1483
+ * var rotateAnchorOffset = transformer.rotateAnchorOffset();
1484
+ *
1485
+ * // set
1486
+ * transformer.rotateAnchorOffset(100);
1487
+ */
1086
1488
  Factory.addGetterSetter(Transformer, 'rotateAnchorOffset', 50, getNumberValidator());
1489
+ /**
1490
+ * get/set the angle (in degrees) of the rotation anchor position around the bounding box.
1491
+ * 0 = top-center (default), 90 = middle-right, 180 = bottom-center, -90 = middle-left
1492
+ * @name Konva.Transformer#rotateAnchorAngle
1493
+ * @method
1494
+ * @param {Number} angle
1495
+ * @returns {Number}
1496
+ * @example
1497
+ * // get
1498
+ * var rotateAnchorAngle = transformer.rotateAnchorAngle();
1499
+ *
1500
+ * // set rotation anchor to the right side
1501
+ * transformer.rotateAnchorAngle(90);
1502
+ *
1503
+ * // set rotation anchor to the bottom
1504
+ * transformer.rotateAnchorAngle(180);
1505
+ */
1087
1506
  Factory.addGetterSetter(Transformer, 'rotateAnchorAngle', 0, getNumberValidator());
1507
+ /**
1508
+ * get/set rotation anchor cursor
1509
+ * @name Konva.Transformer#rotateAnchorCursor
1510
+ * @method
1511
+ * @param {String} cursorName
1512
+ * @returns {String}
1513
+ * @example
1514
+ * // get
1515
+ * var currentRotationAnchorCursor = transformer.rotateAnchorCursor();
1516
+ *
1517
+ * // set
1518
+ * transformer.rotateAnchorCursor('grab');
1519
+ */
1088
1520
  Factory.addGetterSetter(Transformer, 'rotateAnchorCursor', 'crosshair');
1521
+ /**
1522
+ * get/set distance for rotation tolerance
1523
+ * @name Konva.Transformer#rotationSnapTolerance
1524
+ * @method
1525
+ * @param {Number} tolerance
1526
+ * @returns {Number}
1527
+ * @example
1528
+ * // get
1529
+ * var rotationSnapTolerance = transformer.rotationSnapTolerance();
1530
+ *
1531
+ * // set
1532
+ * transformer.rotationSnapTolerance(100);
1533
+ */
1089
1534
  Factory.addGetterSetter(Transformer, 'rotationSnapTolerance', 5, getNumberValidator());
1535
+ /**
1536
+ * get/set visibility of border
1537
+ * @name Konva.Transformer#borderEnabled
1538
+ * @method
1539
+ * @param {Boolean} enabled
1540
+ * @returns {Boolean}
1541
+ * @example
1542
+ * // get
1543
+ * var borderEnabled = transformer.borderEnabled();
1544
+ *
1545
+ * // set
1546
+ * transformer.borderEnabled(false);
1547
+ */
1090
1548
  Factory.addGetterSetter(Transformer, 'borderEnabled', true);
1549
+ /**
1550
+ * get/set anchor stroke color
1551
+ * @name Konva.Transformer#anchorStroke
1552
+ * @method
1553
+ * @param {String} strokeColor
1554
+ * @returns {String}
1555
+ * @example
1556
+ * // get
1557
+ * var anchorStroke = transformer.anchorStroke();
1558
+ *
1559
+ * // set
1560
+ * transformer.anchorStroke('red');
1561
+ */
1091
1562
  Factory.addGetterSetter(Transformer, 'anchorStroke', 'rgb(0, 161, 255)');
1563
+ /**
1564
+ * get/set anchor stroke width
1565
+ * @name Konva.Transformer#anchorStrokeWidth
1566
+ * @method
1567
+ * @param {Number} anchorStrokeWidth
1568
+ * @returns {Number}
1569
+ * @example
1570
+ * // get
1571
+ * var anchorStrokeWidth = transformer.anchorStrokeWidth();
1572
+ *
1573
+ * // set
1574
+ * transformer.anchorStrokeWidth(3);
1575
+ */
1092
1576
  Factory.addGetterSetter(Transformer, 'anchorStrokeWidth', 1, getNumberValidator());
1577
+ /**
1578
+ * get/set anchor fill color
1579
+ * @name Konva.Transformer#anchorFill
1580
+ * @method
1581
+ * @param {String} anchorFill
1582
+ * @returns {String}
1583
+ * @example
1584
+ * // get
1585
+ * var anchorFill = transformer.anchorFill();
1586
+ *
1587
+ * // set
1588
+ * transformer.anchorFill('red');
1589
+ */
1093
1590
  Factory.addGetterSetter(Transformer, 'anchorFill', 'white');
1591
+ /**
1592
+ * get/set anchor corner radius
1593
+ * @name Konva.Transformer#anchorCornerRadius
1594
+ * @method
1595
+ * @param {Number} radius
1596
+ * @returns {Number}
1597
+ * @example
1598
+ * // get
1599
+ * var anchorCornerRadius = transformer.anchorCornerRadius();
1600
+ *
1601
+ * // set
1602
+ * transformer.anchorCornerRadius(3);
1603
+ */
1094
1604
  Factory.addGetterSetter(Transformer, 'anchorCornerRadius', 0, getNumberValidator());
1605
+ /**
1606
+ * get/set border stroke color
1607
+ * @name Konva.Transformer#borderStroke
1608
+ * @method
1609
+ * @param {Boolean} enabled
1610
+ * @returns {Boolean}
1611
+ * @example
1612
+ * // get
1613
+ * var borderStroke = transformer.borderStroke();
1614
+ *
1615
+ * // set
1616
+ * transformer.borderStroke('red');
1617
+ */
1095
1618
  Factory.addGetterSetter(Transformer, 'borderStroke', 'rgb(0, 161, 255)');
1619
+ /**
1620
+ * get/set border stroke width
1621
+ * @name Konva.Transformer#borderStrokeWidth
1622
+ * @method
1623
+ * @param {Number} strokeWidth
1624
+ * @returns {Number}
1625
+ * @example
1626
+ * // get
1627
+ * var borderStrokeWidth = transformer.borderStrokeWidth();
1628
+ *
1629
+ * // set
1630
+ * transformer.borderStrokeWidth(3);
1631
+ */
1096
1632
  Factory.addGetterSetter(Transformer, 'borderStrokeWidth', 1, getNumberValidator());
1633
+ /**
1634
+ * get/set border dash array
1635
+ * @name Konva.Transformer#borderDash
1636
+ * @method
1637
+ * @param {Array} dash array
1638
+ * @returns {Array}
1639
+ * @example
1640
+ * // get
1641
+ * var borderDash = transformer.borderDash();
1642
+ *
1643
+ * // set
1644
+ * transformer.borderDash([2, 2]);
1645
+ */
1097
1646
  Factory.addGetterSetter(Transformer, 'borderDash');
1647
+ /**
1648
+ * get/set should we keep ratio while resize anchors at corners
1649
+ * @name Konva.Transformer#keepRatio
1650
+ * @method
1651
+ * @param {Boolean} keepRatio
1652
+ * @returns {Boolean}
1653
+ * @example
1654
+ * // get
1655
+ * var keepRatio = transformer.keepRatio();
1656
+ *
1657
+ * // set
1658
+ * transformer.keepRatio(false);
1659
+ */
1098
1660
  Factory.addGetterSetter(Transformer, 'keepRatio', true);
1661
+ /**
1662
+ * get/set how to react on skift key while resizing anchors at corners
1663
+ * @name Konva.Transformer#shiftBehavior
1664
+ * @method
1665
+ * @param {String} shiftBehavior
1666
+ * @returns {String}
1667
+ * @example
1668
+ * // get
1669
+ * var shiftBehavior = transformer.shiftBehavior();
1670
+ *
1671
+ * // set
1672
+ * transformer.shiftBehavior('none');
1673
+ */
1099
1674
  Factory.addGetterSetter(Transformer, 'shiftBehavior', 'default');
1675
+ /**
1676
+ * get/set should we resize relative to node's center?
1677
+ * @name Konva.Transformer#centeredScaling
1678
+ * @method
1679
+ * @param {Boolean} centeredScaling
1680
+ * @returns {Boolean}
1681
+ * @example
1682
+ * // get
1683
+ * var centeredScaling = transformer.centeredScaling();
1684
+ *
1685
+ * // set
1686
+ * transformer.centeredScaling(true);
1687
+ */
1100
1688
  Factory.addGetterSetter(Transformer, 'centeredScaling', false);
1689
+ /**
1690
+ * get/set should we think about stroke while resize? Good to use when a shape has strokeScaleEnabled = false
1691
+ * default is false
1692
+ * @name Konva.Transformer#ignoreStroke
1693
+ * @method
1694
+ * @param {Boolean} ignoreStroke
1695
+ * @returns {Boolean}
1696
+ * @example
1697
+ * // get
1698
+ * var ignoreStroke = transformer.ignoreStroke();
1699
+ *
1700
+ * // set
1701
+ * transformer.ignoreStroke(true);
1702
+ */
1101
1703
  Factory.addGetterSetter(Transformer, 'ignoreStroke', false);
1704
+ /**
1705
+ * get/set padding
1706
+ * @name Konva.Transformer#padding
1707
+ * @method
1708
+ * @param {Number} padding
1709
+ * @returns {Number}
1710
+ * @example
1711
+ * // get
1712
+ * var padding = transformer.padding();
1713
+ *
1714
+ * // set
1715
+ * transformer.padding(10);
1716
+ */
1102
1717
  Factory.addGetterSetter(Transformer, 'padding', 0, getNumberValidator());
1718
+ /**
1719
+ * get/set attached nodes of the Transformer. Transformer will adapt to their size and listen to their events
1720
+ * @method
1721
+ * @name Konva.Transformer#nodes
1722
+ * @returns {Konva.Node}
1723
+ * @example
1724
+ * // get
1725
+ * const nodes = transformer.nodes();
1726
+ *
1727
+ * // set
1728
+ * transformer.nodes([rect, circle]);
1729
+ *
1730
+ * // push new item:
1731
+ * const oldNodes = transformer.nodes();
1732
+ * const newNodes = oldNodes.concat([newShape]);
1733
+ * // it is important to set new array instance (and concat method above will create it)
1734
+ * transformer.nodes(newNodes);
1735
+ */
1103
1736
  Factory.addGetterSetter(Transformer, 'nodes');
1737
+ // @ts-ignore
1738
+ // deprecated
1104
1739
  Factory.addGetterSetter(Transformer, 'node');
1740
+ /**
1741
+ * get/set bounding box function. **IMPORTANT!** boundBoxFunc operates in absolute coordinates.
1742
+ * Both boxes are `{ x, y, width, height, rotation }`, with the rotation in radians.
1743
+ * @name Konva.Transformer#boundBoxFunc
1744
+ * @method
1745
+ * @param {Function} func
1746
+ * @returns {Function}
1747
+ * @example
1748
+ * // get
1749
+ * var boundBoxFunc = transformer.boundBoxFunc();
1750
+ *
1751
+ * // set
1752
+ * transformer.boundBoxFunc(function(oldBox, newBox) {
1753
+ * // width and height of the boxes are corresponding to total absolute width and height of all nodes combined
1754
+ * // so it includes scale of the node.
1755
+ * if (newBox.width > 200) {
1756
+ * return oldBox;
1757
+ * }
1758
+ * return newBox;
1759
+ * });
1760
+ */
1105
1761
  Factory.addGetterSetter(Transformer, 'boundBoxFunc');
1762
+ /**
1763
+ * get/set dragging func for transformer anchors
1764
+ * @name Konva.Transformer#anchorDragBoundFunc
1765
+ * @method
1766
+ * @param {Function} func
1767
+ * @returns {Function}
1768
+ * @example
1769
+ * // get
1770
+ * var anchorDragBoundFunc = transformer.anchorDragBoundFunc();
1771
+ *
1772
+ * // set
1773
+ * transformer.anchorDragBoundFunc(function(oldAbsPos, newAbsPos, event) {
1774
+ * return {
1775
+ * x: 0,
1776
+ * y: newAbsolutePosition.y
1777
+ * }
1778
+ * });
1779
+ */
1106
1780
  Factory.addGetterSetter(Transformer, 'anchorDragBoundFunc');
1781
+ /**
1782
+ * get/set styling function for transformer anchors to overwrite default styles
1783
+ * @name Konva.Transformer#anchorStyleFunc
1784
+ * @method
1785
+ * @param {Function} func
1786
+ * @returns {Function}
1787
+ * @example
1788
+ * // get
1789
+ * var anchorStyleFunc = transformer.anchorStyleFunc();
1790
+ *
1791
+ * // set
1792
+ * transformer.anchorStyleFunc(function(anchor) {
1793
+ * // anchor is a simple Konva.Rect instance
1794
+ * // it will be executed AFTER all attributes are set, like 'anchorStrokeWidth' or 'anchorFill'
1795
+ * if (anchor.hasName('rotater')) {
1796
+ * // make rotater anchor filled black and looks like a circle
1797
+ * anchor.fill('black');
1798
+ * anchor.cornerRadius(anchor.width() / 2);
1799
+ * }
1800
+ * });
1801
+ */
1107
1802
  Factory.addGetterSetter(Transformer, 'anchorStyleFunc');
1803
+ /**
1804
+ * using this setting you can drag transformer group by dragging empty space between attached nodes
1805
+ * shouldOverdrawWholeArea = true may temporary disable all events on attached nodes
1806
+ * @name Konva.Transformer#shouldOverdrawWholeArea
1807
+ * @method
1808
+ * @param {Boolean} shouldOverdrawWholeArea
1809
+ * @returns {Boolean}
1810
+ * @example
1811
+ * // get
1812
+ * var shouldOverdrawWholeArea = transformer.shouldOverdrawWholeArea();
1813
+ *
1814
+ * // set
1815
+ * transformer.shouldOverdrawWholeArea(true);
1816
+ */
1108
1817
  Factory.addGetterSetter(Transformer, 'shouldOverdrawWholeArea', false);
1818
+ /**
1819
+ * If you have just one attached node to Transformer it will set its initial rotation to the rotation of that node.
1820
+ * In some cases you may need to set a different rotation.
1821
+ * @name Konva.Transformer#useSingleNodeRotation
1822
+ * @method
1823
+ * @param {Boolean} useSingleNodeRotation
1824
+ * @returns {Boolean}
1825
+ * @example
1826
+ * // set flag to false
1827
+ * transformer.useSingleNodeRotation(false);
1828
+ * // attach a shape
1829
+ * transformer.nodes([shape]);
1830
+ * transformer.rotation(45);
1831
+ * transformer.update();
1832
+ */
1109
1833
  Factory.addGetterSetter(Transformer, 'useSingleNodeRotation', true);
1110
1834
  Factory.backCompat(Transformer, {
1111
1835
  lineEnabled: 'borderEnabled',