konva 10.7.1 → 10.9.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.
package/konva.js CHANGED
@@ -5,10 +5,10 @@
5
5
  })(this, (function () { 'use strict';
6
6
 
7
7
  /*
8
- * Konva JavaScript Framework v10.7.1
8
+ * Konva JavaScript Framework v10.9.0
9
9
  * http://konvajs.org/
10
10
  * Licensed under the MIT
11
- * Date: Mon Oct 05 2026
11
+ * Date: Wed Oct 14 2026
12
12
  *
13
13
  * Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
14
14
  * Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva)
@@ -35,7 +35,7 @@
35
35
  : {};
36
36
  const Konva$2 = {
37
37
  _global: glob,
38
- version: '10.7.1',
38
+ version: '10.9.0',
39
39
  isBrowser: detectBrowser(),
40
40
  isUnminified: /param/.test(function (param) { }.toString()),
41
41
  dblClickWindow: 400,
@@ -3239,14 +3239,44 @@ js: import "konva/skia-backend";
3239
3239
  this.fire('absoluteTransformChange');
3240
3240
  }
3241
3241
  }
3242
- static _runAfterAbsTransformCascade(cb) {
3243
- if (Node._absTransformCascadeDepth > 0) {
3244
- Node._pendingAfterCascade.push(cb);
3242
+ static _runAfterUpdateBatch(cb) {
3243
+ if (Node._updateBatchDepth > 0) {
3244
+ Node._pendingUpdates.add(cb);
3245
3245
  }
3246
3246
  else {
3247
3247
  cb();
3248
3248
  }
3249
3249
  }
3250
+ static _flushUpdateBatch() {
3251
+ if (Node._updateBatchDepth > 0)
3252
+ return;
3253
+ const callbacks = Node._pendingUpdates;
3254
+ if (!callbacks.size)
3255
+ return;
3256
+ Node._pendingUpdates = new Set();
3257
+ callbacks.forEach((callback) => callback());
3258
+ }
3259
+ static _updateBatch(fn) {
3260
+ Node._updateBatchDepth++;
3261
+ try {
3262
+ return fn();
3263
+ }
3264
+ finally {
3265
+ Node._updateBatchDepth--;
3266
+ Node._flushUpdateBatch();
3267
+ }
3268
+ }
3269
+ static _beginUpdateBatch() {
3270
+ Node._updateBatchDepth++;
3271
+ let finished = false;
3272
+ return () => {
3273
+ if (finished)
3274
+ return;
3275
+ finished = true;
3276
+ Node._updateBatchDepth--;
3277
+ Node._flushUpdateBatch();
3278
+ };
3279
+ }
3250
3280
  /**
3251
3281
  * clear cached canvas
3252
3282
  * @method
@@ -4037,18 +4067,20 @@ js: import "konva/skia-backend";
4037
4067
  func();
4038
4068
  return;
4039
4069
  }
4040
- this._batchingTransformChange = true;
4041
- try {
4042
- func();
4043
- }
4044
- finally {
4045
- this._batchingTransformChange = false;
4046
- if (this._needClearTransformCache) {
4047
- this._needClearTransformCache = false;
4048
- this._clearCache(TRANSFORM);
4049
- this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
4070
+ Node._updateBatch(() => {
4071
+ this._batchingTransformChange = true;
4072
+ try {
4073
+ func();
4074
+ }
4075
+ finally {
4076
+ this._batchingTransformChange = false;
4077
+ if (this._needClearTransformCache) {
4078
+ this._needClearTransformCache = false;
4079
+ this._clearCache(TRANSFORM);
4080
+ this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
4081
+ }
4050
4082
  }
4051
- }
4083
+ });
4052
4084
  }
4053
4085
  setPosition(pos) {
4054
4086
  this._batchTransformChanges(() => {
@@ -5465,12 +5497,13 @@ js: import "konva/skia-backend";
5465
5497
  }
5466
5498
  // Number of registered observers. The ancestor walk is skipped while it is 0.
5467
5499
  Node._subtreeObserverCount = 0;
5468
- // Perf: lets a listener inside an absoluteTransform cascade (e.g. Konva
5469
- // .Transformer reacting to a stage drag fan-out across N attached nodes)
5470
- // defer work until the cascade ends — collapses O(N) listener-driven calls
5471
- // into one. Counter is mutated by Container's _clearSelfAndDescendantCache.
5472
- Node._absTransformCascadeDepth = 0;
5473
- Node._pendingAfterCascade = [];
5500
+ // Perf: while a batch is open, work that depends on many nodes (a Konva
5501
+ // .Transformer refresh) is queued and runs once when the outermost scope
5502
+ // ends, instead of once per node change. Scopes are the callers of
5503
+ // _updateBatch() and _beginUpdateBatch().
5504
+ Node._updateBatchDepth = 0;
5505
+ // a Set, so a callback queued once per change runs once per batch
5506
+ Node._pendingUpdates = new Set();
5474
5507
  // keyed by prototype: subclasses that share a nodeType (every shape class)
5475
5508
  // have their own listeners
5476
5509
  Node.protoListenerMap = new WeakMap();
@@ -6099,17 +6132,7 @@ js: import "konva/skia-backend";
6099
6132
  * @name Konva.Container#removeChildren
6100
6133
  */
6101
6134
  removeChildren() {
6102
- this.children.forEach((child) => {
6103
- // reset parent to prevent many _setChildrenIndices calls
6104
- child.parent = null;
6105
- child.index = 0;
6106
- child.remove();
6107
- });
6108
- this.children = [];
6109
- // because all children were detached from parent, request draw via container
6110
- this._notifySubtreeChange();
6111
- this._requestDraw();
6112
- return this;
6135
+ return this._clearChildren('remove');
6113
6136
  }
6114
6137
  /**
6115
6138
  * destroy all children nodes.
@@ -6117,17 +6140,22 @@ js: import "konva/skia-backend";
6117
6140
  * @name Konva.Container#destroyChildren
6118
6141
  */
6119
6142
  destroyChildren() {
6120
- this.children.forEach((child) => {
6121
- // reset parent to prevent many _setChildrenIndices calls
6122
- child.parent = null;
6123
- child.index = 0;
6124
- child.destroy();
6143
+ return this._clearChildren('destroy');
6144
+ }
6145
+ _clearChildren(dispose) {
6146
+ return Node._updateBatch(() => {
6147
+ this.children.forEach((child) => {
6148
+ // reset parent to prevent many _setChildrenIndices calls
6149
+ child.parent = null;
6150
+ child.index = 0;
6151
+ child[dispose]();
6152
+ });
6153
+ this.children = [];
6154
+ // because all children were detached from parent, request draw via container
6155
+ this._notifySubtreeChange();
6156
+ this._requestDraw();
6157
+ return this;
6125
6158
  });
6126
- this.children = [];
6127
- // because all children were detached from parent, request draw via container
6128
- this._notifySubtreeChange();
6129
- this._requestDraw();
6130
- return this;
6131
6159
  }
6132
6160
  /**
6133
6161
  * add a child and children into container
@@ -6332,12 +6360,12 @@ js: import "konva/skia-backend";
6332
6360
  }
6333
6361
  _clearSelfAndDescendantCache(attr) {
6334
6362
  var _a;
6335
- // Perf: bracket absoluteTransform cascades with a depth counter so
6336
- // Node._runAfterAbsTransformCascade can defer work until the outermost
6337
- // walk returns. try/finally so depth/flush survive listener exceptions.
6363
+ // Perf: an absoluteTransform cascade is an update batch, so a listener on
6364
+ // one of N descendants defers its work until the outermost walk returns.
6365
+ // try/finally so depth/flush survive listener exceptions.
6338
6366
  const isAbsTransform = attr === undefined || attr === 'absoluteTransform';
6339
6367
  if (isAbsTransform)
6340
- Node._absTransformCascadeDepth++;
6368
+ Node._updateBatchDepth++;
6341
6369
  try {
6342
6370
  super._clearSelfAndDescendantCache(attr);
6343
6371
  // a cached container is drawn from its bitmap, so its descendants keep
@@ -6350,13 +6378,8 @@ js: import "konva/skia-backend";
6350
6378
  });
6351
6379
  }
6352
6380
  finally {
6353
- if (isAbsTransform && --Node._absTransformCascadeDepth === 0) {
6354
- const callbacks = Node._pendingAfterCascade;
6355
- if (callbacks.length) {
6356
- Node._pendingAfterCascade = [];
6357
- for (let i = 0; i < callbacks.length; i++)
6358
- callbacks[i]();
6359
- }
6381
+ if (isAbsTransform && --Node._updateBatchDepth === 0) {
6382
+ Node._flushUpdateBatch();
6360
6383
  }
6361
6384
  }
6362
6385
  }
@@ -8086,12 +8109,16 @@ js: import "konva/skia-backend";
8086
8109
  const forDrawing = config._forDrawing;
8087
8110
  // Painted bounds are measured in the canvas that draws the shape
8088
8111
  // (relativeTo): non-scaling strokes and shadows use that canvas's axes.
8089
- // The identity transform normalizes negative sizes.
8090
- const fillRect = !forDrawing
8091
- ? this.getSelfRect()
8092
- : this.strokeScaleEnabled()
8093
- ? new Transform()._getTransformedRect(this._getSelfRectForDrawing())
8094
- : this._transformedRect(this._getSelfRectForDrawing(), relativeTo);
8112
+ let fillRect = forDrawing
8113
+ ? this._getSelfRectForDrawing()
8114
+ : this.getSelfRect();
8115
+ if (forDrawing && !this.strokeScaleEnabled()) {
8116
+ fillRect = this._transformedRect(fillRect, relativeTo);
8117
+ }
8118
+ else if (fillRect.width < 0 || fillRect.height < 0) {
8119
+ // The identity transform normalizes negative sizes.
8120
+ fillRect = new Transform()._getTransformedRect(fillRect);
8121
+ }
8095
8122
  const applyStroke = !config.skipStroke && this.hasStroke();
8096
8123
  const strokeWidth = applyStroke
8097
8124
  ? forDrawing
@@ -10228,16 +10255,18 @@ js: import "konva/skia-backend";
10228
10255
  const layersToDraw = new Set();
10229
10256
  // an animation may stop or restart itself, or another one, inside its
10230
10257
  // function: each one runs at most once per frame, a stopped one not at all
10231
- Array.from(this.animations).forEach((anim) => {
10232
- if (!this.animations.has(anim)) {
10233
- return;
10234
- }
10235
- anim._updateFrameObject(now());
10236
- // the function returns false to skip the redraw
10237
- if (anim.func && anim.func.call(anim, anim.frame) === false) {
10238
- return;
10239
- }
10240
- anim.layers.forEach((layer) => layer && layersToDraw.add(layer));
10258
+ Node._updateBatch(() => {
10259
+ Array.from(this.animations).forEach((anim) => {
10260
+ if (!this.animations.has(anim)) {
10261
+ return;
10262
+ }
10263
+ anim._updateFrameObject(now());
10264
+ // the function returns false to skip the redraw
10265
+ if (anim.func && anim.func.call(anim, anim.frame) === false) {
10266
+ return;
10267
+ }
10268
+ anim.layers.forEach((layer) => layer && layersToDraw.add(layer));
10269
+ });
10241
10270
  });
10242
10271
  layersToDraw.forEach((layer) => layer.batchDraw());
10243
10272
  }
@@ -11010,6 +11039,32 @@ js: import "konva/skia-backend";
11010
11039
 
11011
11040
  // what is core parts of Konva?
11012
11041
  const Konva$1 = Util._assign(Konva$2, {
11042
+ /**
11043
+ * Defer automatic Konva.Transformer updates across many synchronous node changes
11044
+ * and run them once when the returned function is called.
11045
+ * A Transformer normally updates after every change of an attached node, so a loop
11046
+ * over N selected nodes costs N updates, each measuring all N nodes. In a batch it costs one.
11047
+ * A single setAttrs(), position(), destroyChildren(), removeChildren() call or animation frame is
11048
+ * already batched; use this for anything else, for example a React commit or an undo.
11049
+ * Attributes and change events stay synchronous. Inside the batch, the Transformer's own
11050
+ * x(), y(), width(), height() and getClientRect() are computed on read; its anchors are laid out
11051
+ * when the batch finishes, or earlier if something draws or measures them.
11052
+ * Change events fire inside the batch, so a listener sees the new attributes and the
11053
+ * Transformer's box, but not its anchors; call transformer.update() there if it needs them.
11054
+ * Batches nest. Call the returned function in a finally block, and do not keep a batch
11055
+ * open across an await or an animation frame.
11056
+ * @method
11057
+ * @name Konva.beginUpdateBatch
11058
+ * @returns {Function} finishes the batch and runs the pending updates
11059
+ * @example
11060
+ * const finish = Konva.beginUpdateBatch();
11061
+ * try {
11062
+ * selectedNodes.forEach((node) => node.width(100));
11063
+ * } finally {
11064
+ * finish();
11065
+ * }
11066
+ */
11067
+ beginUpdateBatch: Node._beginUpdateBatch,
11013
11068
  Util,
11014
11069
  Transform,
11015
11070
  Node,
@@ -16459,6 +16514,7 @@ js: import "konva/skia-backend";
16459
16514
  'rotateAnchorOffsetChange',
16460
16515
  'rotateAnchorAngleChange',
16461
16516
  'rotateEnabledChange',
16517
+ 'rotateModeChange',
16462
16518
  'enabledAnchorsChange',
16463
16519
  'anchorSizeChange',
16464
16520
  'borderEnabledChange',
@@ -16507,10 +16563,37 @@ js: import "konva/skia-backend";
16507
16563
  'bottom-right': 135,
16508
16564
  };
16509
16565
  const TOUCH_DEVICE = 'ontouchstart' in Konva$2._global;
16566
+ // invisible zones just outside the corners, the rotate area of rotateMode 'corners'
16567
+ const ROTATE_ZONE_NAMES = [
16568
+ 'rotater-top-left',
16569
+ 'rotater-top-right',
16570
+ 'rotater-bottom-left',
16571
+ 'rotater-bottom-right',
16572
+ ];
16573
+ function isRotateAnchor(anchorName) {
16574
+ return anchorName.startsWith('rotater');
16575
+ }
16576
+ // with Shift pressed the rotation snaps to this step
16577
+ const SHIFT_ROTATION_STEP = Math.PI / 12;
16578
+ // curved double arrow drawn for the top-right corner, rotated for others
16579
+ function getRotateZoneCursor(deg, fallback) {
16580
+ const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24">' +
16581
+ `<g transform="rotate(${deg} 12 12)" stroke-linejoin="round">` +
16582
+ '<path d="M5 8 A11 11 0 0 1 16 19" fill="none" stroke="white" stroke-width="4"/>' +
16583
+ '<path d="M2 8 L6 4.5 L6 11.5 Z M16 22 L12.5 18 L19.5 18 Z" fill="black" stroke="white"/>' +
16584
+ '<path d="M5 8 A11 11 0 0 1 16 19" fill="none" stroke="black" stroke-width="1.5"/>' +
16585
+ '</g></svg>';
16586
+ return `url("data:image/svg+xml,${encodeURIComponent(svg)}") 12 12, ${fallback}`;
16587
+ }
16510
16588
  function getCursor(anchorName, rad, rotateCursor) {
16511
16589
  if (anchorName === 'rotater') {
16512
16590
  return rotateCursor;
16513
16591
  }
16592
+ if (isRotateAnchor(anchorName)) {
16593
+ const corner = anchorName.slice('rotater-'.length);
16594
+ const deg = Math.round(Util.radToDeg(rad) + ANGLES[corner] - 45);
16595
+ return getRotateZoneCursor(deg, rotateCursor);
16596
+ }
16514
16597
  rad += Util.degToRad(ANGLES[anchorName] || 0);
16515
16598
  const angle = ((Util.radToDeg(rad) % 360) + 360) % 360;
16516
16599
  if (Util._inRange(angle, 315 + 22.5, 360) || Util._inRange(angle, 0, 22.5)) {
@@ -16651,6 +16734,7 @@ js: import "konva/skia-backend";
16651
16734
  * @param {Number} [config.rotateAnchorOffset] Default is 50
16652
16735
  * @param {Number} [config.rotateAnchorAngle] Angle of the rotate anchor around the box, in degrees. Default is 0 (top)
16653
16736
  * @param {String} [config.rotateAnchorCursor] Default is crosshair
16737
+ * @param {String} [config.rotateMode] How the user rotates: 'handle' (the rotate anchor), 'corners' (invisible zones just outside the corners) or 'both'. Default is 'handle'
16654
16738
  * @param {Number} [config.padding] Default is 0
16655
16739
  * @param {Boolean} [config.borderEnabled] Should we draw border? Default is true
16656
16740
  * @param {String} [config.borderStroke] Border stroke color
@@ -16662,7 +16746,7 @@ js: import "konva/skia-backend";
16662
16746
  * @param {Number} [config.anchorStrokeWidth] Anchor stroke size
16663
16747
  * @param {Number} [config.anchorSize] Default is 10
16664
16748
  * @param {Boolean} [config.keepRatio] Should we keep ratio when we are moving edges? Default is true
16665
- * @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? Default is 'default'
16749
+ * @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? 'none' also turns off the Shift rotation snap. Default is 'default'
16666
16750
  * @param {Boolean} [config.centeredScaling] Should we resize relative to node's center? Default is false
16667
16751
  * @param {Array} [config.enabledAnchors] Array of names of enabled handles
16668
16752
  * @param {Boolean} [config.flipEnabled] Can we flip/mirror shape on transform?. True by default
@@ -16704,14 +16788,14 @@ js: import "konva/skia-backend";
16704
16788
  // rendered in another window than the one Konva was imported into
16705
16789
  this._transformWindow = null;
16706
16790
  this._elementsCreated = false;
16707
- this._updateScheduled = false;
16708
- // an attached container's descendants changed after the last layout
16709
- this._subtreeChanged = false;
16791
+ // the layout is stale; the next sync point runs _update() (see _syncUpdate)
16792
+ this._updatePending = false;
16710
16793
  this._createElements();
16711
16794
  // bindings
16712
16795
  this._handleMouseMove = this._handleMouseMove.bind(this);
16713
16796
  this._handleMouseUp = this._handleMouseUp.bind(this);
16714
16797
  this.update = this.update.bind(this);
16798
+ this._syncUpdate = this._syncUpdate.bind(this);
16715
16799
  // update transformer data for certain attr changes
16716
16800
  this.on(ATTR_CHANGE_LIST, (event) => {
16717
16801
  if (event.type === 'ignoreStrokeChange')
@@ -16767,12 +16851,7 @@ js: import "konva/skia-backend";
16767
16851
  return true;
16768
16852
  });
16769
16853
  this._nodes = nodes = filteredNodes;
16770
- if (nodes.length === 1 && this.useSingleNodeRotation()) {
16771
- this.rotation(nodes[0].getAbsoluteRotation());
16772
- }
16773
- else {
16774
- this.rotation(0);
16775
- }
16854
+ this._resetRotation();
16776
16855
  this._nodes.forEach((node) => {
16777
16856
  const onChange = () => {
16778
16857
  var _a;
@@ -16784,6 +16863,7 @@ js: import "konva/skia-backend";
16784
16863
  (_a = this._nodeRectCache) === null || _a === void 0 ? void 0 : _a.delete(node);
16785
16864
  return;
16786
16865
  }
16866
+ // unlike _resetRotation, a multi-node box keeps its rotation here
16787
16867
  if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
16788
16868
  this.rotation(this.nodes()[0].getAbsoluteRotation());
16789
16869
  }
@@ -16808,8 +16888,7 @@ js: import "konva/skia-backend";
16808
16888
  if (node instanceof Container)
16809
16889
  node._addSubtreeObserver(this);
16810
16890
  node.on(`destroy.${this._getEventNamespace()}`, () => {
16811
- // a destroyed node has nothing left to transform
16812
- this.setNodes(this._nodes.filter((n) => n !== node));
16891
+ this._removeNode(node);
16813
16892
  });
16814
16893
  this._proxyDrag(node);
16815
16894
  });
@@ -16855,6 +16934,23 @@ js: import "konva/skia-backend";
16855
16934
  lastPos = null;
16856
16935
  });
16857
16936
  }
16937
+ _detachNode(node) {
16938
+ node.off('.' + this._getEventNamespace());
16939
+ node._removeSubtreeObserver(this);
16940
+ }
16941
+ _resetRotation() {
16942
+ const single = this._nodes.length === 1 && this.useSingleNodeRotation();
16943
+ this.rotation(single ? this._nodes[0].getAbsoluteRotation() : 0);
16944
+ }
16945
+ // Drops one destroyed node. Re-attaching the survivors instead costs
16946
+ // O(N) listener rebinds per destroyed node, O(N^2) for a deleted selection.
16947
+ _removeNode(node) {
16948
+ this._detachNode(node);
16949
+ this._nodes = this._nodes.filter((n) => n !== node);
16950
+ this._resetRotation();
16951
+ this._resetTransformCache(node);
16952
+ this._scheduleUpdate();
16953
+ }
16858
16954
  getNodes() {
16859
16955
  return this._nodes || [];
16860
16956
  }
@@ -16878,17 +16974,12 @@ js: import "konva/skia-backend";
16878
16974
  * transformer.detach();
16879
16975
  */
16880
16976
  detach() {
16881
- var _a;
16882
- // remove events
16883
- if (this._nodes) {
16884
- this._nodes.forEach((node) => {
16885
- node.off('.' + this._getEventNamespace());
16886
- node._removeSubtreeObserver(this);
16887
- });
16888
- }
16977
+ var _a, _b;
16978
+ (_a = this._nodes) === null || _a === void 0 ? void 0 : _a.forEach((node) => this._detachNode(node));
16889
16979
  this._nodes = [];
16980
+ this._updatePending = false;
16890
16981
  this._resetTransformCache();
16891
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
16982
+ (_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
16892
16983
  }
16893
16984
  /**
16894
16985
  * bind events to the Transformer. You can use events: `transform`, `transformstart`, `transformend`, `dragstart`, `dragmove`, `dragend`
@@ -16916,37 +17007,42 @@ js: import "konva/skia-backend";
16916
17007
  // Called for every change below an attached container. A burst of child
16917
17008
  // changes only drops cached values here. The new bounds are measured once,
16918
17009
  // before the next frame, or earlier if a draw or transform step comes first
16919
- // (_syncSubtreeChange).
17010
+ // (_syncUpdate).
16920
17011
  _onSubtreeChange(node) {
16921
17012
  var _a, _b;
16922
17013
  if (this._fitting)
16923
17014
  return;
16924
17015
  // nothing was measured since the last change, so nothing cached is stale
16925
- if (this._subtreeChanged && !this._cache[NODES_RECT])
17016
+ const measured = !this._updatePending || !!this._cache[NODES_RECT];
17017
+ if (measured)
17018
+ this._resetTransformCache(node);
17019
+ if (Node._updateBatchDepth > 0) {
17020
+ // the batch flush lays the new bounds out, no frame needed
17021
+ this._scheduleUpdate();
16926
17022
  return;
16927
- this._resetTransformCache(node);
16928
- if (!this._subtreeChanged) {
16929
- this._subtreeChanged = true;
17023
+ }
17024
+ if (!this._updatePending) {
17025
+ this._updatePending = true;
16930
17026
  // queued ahead of the draw the change requests, so that draw shows the
16931
17027
  // new layout. Updating inside the draw would request one more frame.
16932
- Util.requestAnimFrame(() => this._syncSubtreeChange(), (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow());
17028
+ Util.requestAnimFrame(this._syncUpdate, (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow());
16933
17029
  // the transformer can be on another layer than the changed node
16934
17030
  (_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
16935
17031
  }
16936
17032
  }
16937
- _syncSubtreeChange() {
16938
- var _a;
17033
+ // Runs a deferred update before anything draws or measures the anchors.
17034
+ _syncUpdate() {
16939
17035
  // while dragging, the dragmove handler updates the layout
16940
- if (this._subtreeChanged && ((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) && !this.isDragging()) {
17036
+ if (this._updatePending && !this._fitting && !this.isDragging()) {
16941
17037
  this._update();
16942
17038
  }
16943
17039
  }
16944
17040
  drawScene(can, top) {
16945
- this._syncSubtreeChange();
17041
+ this._syncUpdate();
16946
17042
  return super.drawScene(can, top);
16947
17043
  }
16948
17044
  drawHit(can, top) {
16949
- this._syncSubtreeChange();
17045
+ this._syncUpdate();
16950
17046
  return super.drawHit(can, top);
16951
17047
  }
16952
17048
  _getNodeRect() {
@@ -17063,18 +17159,27 @@ js: import "konva/skia-backend";
17063
17159
  }
17064
17160
  _createElements() {
17065
17161
  this._createBack();
17162
+ // below the resize anchors, so a resize anchor wins where they overlap
17163
+ ROTATE_ZONE_NAMES.forEach((name) => {
17164
+ // invisible, but its fill keeps it in the hit graph
17165
+ this._createAnchor(name, '_rotate-zone').setAttrs({
17166
+ fill: 'transparent',
17167
+ strokeEnabled: false,
17168
+ visible: false,
17169
+ });
17170
+ });
17066
17171
  ANCHORS_NAMES.forEach((name) => {
17067
17172
  this._createAnchor(name);
17068
17173
  });
17069
17174
  this._createAnchor('rotater');
17070
17175
  this._elementsCreated = true;
17071
17176
  }
17072
- _createAnchor(name) {
17177
+ _createAnchor(name, className = '_anchor') {
17073
17178
  const anchor = new Rect({
17074
17179
  stroke: 'rgb(0, 161, 255)',
17075
17180
  fill: 'white',
17076
17181
  strokeWidth: 1,
17077
- name: name + ' _anchor',
17182
+ name: name + ' ' + className,
17078
17183
  dragDistance: 0,
17079
17184
  // make it draggable,
17080
17185
  // so activating the anchor will not start drag&drop of any parent
@@ -17097,11 +17202,7 @@ js: import "konva/skia-backend";
17097
17202
  anchor.on('mouseenter', () => {
17098
17203
  if (this.isTransforming())
17099
17204
  return;
17100
- const rad = Konva$2.getAngle(this.rotation());
17101
- const rotateCursor = this.rotateAnchorCursor();
17102
- const cursor = getCursor(name, rad, rotateCursor);
17103
- anchor.getStage().content &&
17104
- (anchor.getStage().content.style.cursor = cursor);
17205
+ this._setAnchorCursor(name);
17105
17206
  this._cursorChange = true;
17106
17207
  });
17107
17208
  anchor.on('mouseout', () => {
@@ -17112,6 +17213,14 @@ js: import "konva/skia-backend";
17112
17213
  (anchor.getStage().content.style.cursor = '');
17113
17214
  });
17114
17215
  this.add(anchor);
17216
+ return anchor;
17217
+ }
17218
+ _setAnchorCursor(name, rad = Konva$2.getAngle(this.rotation())) {
17219
+ var _a;
17220
+ const content = (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a.content;
17221
+ if (content) {
17222
+ content.style.cursor = getCursor(name, rad, this.rotateAnchorCursor());
17223
+ }
17115
17224
  }
17116
17225
  _createBack() {
17117
17226
  const back = new Shape({
@@ -17127,7 +17236,9 @@ js: import "konva/skia-backend";
17127
17236
  const height = shape.height();
17128
17237
  ctx.beginPath();
17129
17238
  ctx.rect(-padding, -padding, width + padding * 2, height + padding * 2);
17130
- if (tr.rotateEnabled() && tr.rotateLineVisible()) {
17239
+ if (tr.rotateEnabled() &&
17240
+ tr.rotateLineVisible() &&
17241
+ tr.rotateMode() !== 'corners') {
17131
17242
  const line = getRotaterLine(tr, width, height);
17132
17243
  ctx.moveTo(line.edgeX, line.edgeY);
17133
17244
  ctx.lineTo(line.endX, line.endY);
@@ -17199,6 +17310,13 @@ js: import "konva/skia-backend";
17199
17310
  x: pos.x - ap.x,
17200
17311
  y: pos.y - ap.y,
17201
17312
  };
17313
+ if (isRotateAnchor(this._movingAnchorName)) {
17314
+ const center = getCenter(attrs);
17315
+ this._rotateStart = {
17316
+ pointerAngle: Math.atan2(pos.y - center.y, pos.x - center.x),
17317
+ rotation: attrs.rotation,
17318
+ };
17319
+ }
17202
17320
  activeTransformers.add(this);
17203
17321
  this._fire('transformstart', { evt: e.evt, target: this.getNode() });
17204
17322
  this._nodes.forEach((target) => {
@@ -17211,7 +17329,7 @@ js: import "konva/skia-backend";
17211
17329
  }
17212
17330
  _moveTransform(e) {
17213
17331
  // anchors must show the current bounds before they are measured against
17214
- this._syncSubtreeChange();
17332
+ this._syncUpdate();
17215
17333
  let x, y, newHypotenuse;
17216
17334
  const anchorNode = this._anchors[this._movingAnchorName];
17217
17335
  const stage = anchorNode.getStage();
@@ -17234,19 +17352,33 @@ js: import "konva/skia-backend";
17234
17352
  if (oldAbs.x === newAbs.x && oldAbs.y === newAbs.y) {
17235
17353
  return;
17236
17354
  }
17237
- // rotater is working very differently, so do it first
17238
- if (this._movingAnchorName === 'rotater') {
17355
+ // rotation is working very differently, so do it first
17356
+ if (isRotateAnchor(this._movingAnchorName)) {
17239
17357
  const attrs = this._getNodeRect();
17240
- x = anchorNode.x() - attrs.width / 2;
17241
- y = -anchorNode.y() + attrs.height / 2;
17242
- // Calculate angle from center to current anchor position
17243
- // Offset by rotateAnchorAngle so we measure rotation from the anchor's starting position
17244
- const rotateAnchorAngleRad = Util.degToRad(this.rotateAnchorAngle());
17245
- let delta = Math.atan2(-y, x) + Math.PI / 2 - rotateAnchorAngleRad;
17246
- const oldRotation = Konva$2.getAngle(this.rotation());
17247
- const newRotation = oldRotation + delta;
17248
- const tol = Konva$2.getAngle(this.rotationSnapTolerance());
17249
- const snappedRot = getSnap(this.rotationSnaps(), newRotation, tol);
17358
+ const center = getCenter(attrs);
17359
+ const start = this._rotateStart;
17360
+ // the grabbed point turns around the center by as much as the box,
17361
+ // so the box follows the pointer wherever it was grabbed
17362
+ const point = {
17363
+ x: newAbs.x + this._anchorDragOffset.x,
17364
+ y: newAbs.y + this._anchorDragOffset.y,
17365
+ };
17366
+ const pointerAngle = Math.atan2(point.y - center.y, point.x - center.x);
17367
+ const newRotation = start.rotation + pointerAngle - start.pointerAngle;
17368
+ let snappedRot;
17369
+ if (e.shiftKey && this.shiftBehavior() !== 'none') {
17370
+ snappedRot =
17371
+ Math.round(newRotation / SHIFT_ROTATION_STEP) * SHIFT_ROTATION_STEP;
17372
+ }
17373
+ else {
17374
+ const tol = Konva$2.getAngle(this.rotationSnapTolerance());
17375
+ snappedRot = getSnap(this.rotationSnaps(), newRotation, tol);
17376
+ }
17377
+ // a corner zone cursor shows the direction of the turned corner,
17378
+ // set before the transform event so its handlers can override it
17379
+ if (this._movingAnchorName !== 'rotater') {
17380
+ this._setAnchorCursor(this._movingAnchorName, snappedRot);
17381
+ }
17250
17382
  const diff = snappedRot - attrs.rotation;
17251
17383
  const shape = rotateAroundCenter(attrs, diff);
17252
17384
  this._fitNodesInto(shape, e);
@@ -17631,21 +17763,10 @@ js: import "konva/skia-backend";
17631
17763
  layersToDraw.add(this.getLayer());
17632
17764
  layersToDraw.forEach((layer) => layer && layer.batchDraw());
17633
17765
  }
17634
- // Inside an absoluteTransform cascade, queue a single update to run when
17635
- // the cascade ends; outside a cascade, run synchronously so reads of
17636
- // transformer state stay consistent.
17766
+ // deferred to the batch flush when one is open, else synchronous
17637
17767
  _scheduleUpdate() {
17638
- if (this._updateScheduled)
17639
- return;
17640
- this._updateScheduled = true;
17641
- Node._runAfterAbsTransformCascade(() => {
17642
- var _a;
17643
- this._updateScheduled = false;
17644
- if (!((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) || this._fitting || this.isDragging()) {
17645
- return;
17646
- }
17647
- this._update();
17648
- });
17768
+ this._updatePending = true;
17769
+ Node._runAfterUpdateBatch(this._syncUpdate);
17649
17770
  }
17650
17771
  /**
17651
17772
  * force update of Konva.Transformer.
@@ -17667,7 +17788,7 @@ js: import "konva/skia-backend";
17667
17788
  }
17668
17789
  _update() {
17669
17790
  var _a;
17670
- this._subtreeChanged = false;
17791
+ this._updatePending = false;
17671
17792
  const attrs = this._getNodeRect();
17672
17793
  this._updateElements(attrs);
17673
17794
  const draggable = this.nodes().some((node) => node.draggable());
@@ -17676,7 +17797,7 @@ js: import "konva/skia-backend";
17676
17797
  }
17677
17798
  const styleFunc = this.anchorStyleFunc();
17678
17799
  if (styleFunc) {
17679
- Object.values(this._anchors).forEach((node) => styleFunc(node));
17800
+ Object.values(this._anchors).forEach((node) => node.hasName('_anchor') && styleFunc(node));
17680
17801
  }
17681
17802
  (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
17682
17803
  }
@@ -17700,7 +17821,7 @@ js: import "konva/skia-backend";
17700
17821
  const resizeEnabled = this.resizeEnabled();
17701
17822
  const padding = this.padding();
17702
17823
  const anchorSize = this.anchorSize();
17703
- const anchors = Object.values(this._anchors);
17824
+ const anchors = Object.values(this._anchors).filter((node) => node.hasName('_anchor'));
17704
17825
  anchors.forEach((node) => {
17705
17826
  node.setAttrs({
17706
17827
  width: anchorSize,
@@ -17765,11 +17886,25 @@ js: import "konva/skia-backend";
17765
17886
  offsetY: anchorSize / 2 - padding,
17766
17887
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
17767
17888
  });
17889
+ const rotateMode = this.rotateMode();
17768
17890
  const rotaterLine = getRotaterLine(this, width, height);
17769
17891
  this._anchors['rotater'].setAttrs({
17770
17892
  x: rotaterLine.endX,
17771
17893
  y: rotaterLine.endY,
17772
- visible: this.rotateEnabled(),
17894
+ visible: this.rotateEnabled() && rotateMode !== 'corners',
17895
+ });
17896
+ // each zone is a square that touches its corner from the outside
17897
+ const zoneSize = anchorSize * 2;
17898
+ const zonesVisible = this.rotateEnabled() && rotateMode !== 'handle';
17899
+ const near = -padding - zoneSize;
17900
+ ROTATE_ZONE_NAMES.forEach((name) => {
17901
+ this._anchors[name].setAttrs({
17902
+ x: name.includes('left') ? near : width + padding,
17903
+ y: name.includes('top') ? near : height + padding,
17904
+ width: zoneSize,
17905
+ height: zoneSize,
17906
+ visible: zonesVisible,
17907
+ });
17773
17908
  });
17774
17909
  this._back.setAttrs({
17775
17910
  width: width,
@@ -17833,6 +17968,7 @@ js: import "konva/skia-backend";
17833
17968
  return node;
17834
17969
  }
17835
17970
  getClientRect(config) {
17971
+ this._syncUpdate();
17836
17972
  if (this.nodes().length > 0) {
17837
17973
  return super.getClientRect(config);
17838
17974
  }
@@ -18008,6 +18144,23 @@ js: import "konva/skia-backend";
18008
18144
  * transformer.rotateAnchorCursor('grab');
18009
18145
  */
18010
18146
  Factory.addGetterSetter(Transformer, 'rotateAnchorCursor', 'crosshair');
18147
+ /**
18148
+ * get/set how the user rotates the nodes. 'handle' shows the rotate anchor,
18149
+ * 'corners' makes invisible zones just outside the box corners rotate,
18150
+ * 'both' enables both. Hold Shift while rotating to snap to 15 degree steps, unless shiftBehavior is 'none'.
18151
+ * While a corner zone rotates, `getActiveAnchor()` returns its name, such as 'rotater-top-left'.
18152
+ * @name Konva.Transformer#rotateMode
18153
+ * @method
18154
+ * @param {String} mode
18155
+ * @returns {String}
18156
+ * @example
18157
+ * // get
18158
+ * var rotateMode = transformer.rotateMode();
18159
+ *
18160
+ * // set
18161
+ * transformer.rotateMode('corners');
18162
+ */
18163
+ Factory.addGetterSetter(Transformer, 'rotateMode', 'handle');
18011
18164
  /**
18012
18165
  * get/set distance for rotation tolerance
18013
18166
  * @name Konva.Transformer#rotationSnapTolerance