konva 10.8.0 → 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.8.0
8
+ * Konva JavaScript Framework v10.9.0
9
9
  * http://konvajs.org/
10
10
  * Licensed under the MIT
11
- * Date: Mon Oct 12 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.8.0',
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
  }
@@ -10232,16 +10255,18 @@ js: import "konva/skia-backend";
10232
10255
  const layersToDraw = new Set();
10233
10256
  // an animation may stop or restart itself, or another one, inside its
10234
10257
  // function: each one runs at most once per frame, a stopped one not at all
10235
- Array.from(this.animations).forEach((anim) => {
10236
- if (!this.animations.has(anim)) {
10237
- return;
10238
- }
10239
- anim._updateFrameObject(now());
10240
- // the function returns false to skip the redraw
10241
- if (anim.func && anim.func.call(anim, anim.frame) === false) {
10242
- return;
10243
- }
10244
- 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
+ });
10245
10270
  });
10246
10271
  layersToDraw.forEach((layer) => layer.batchDraw());
10247
10272
  }
@@ -11014,6 +11039,32 @@ js: import "konva/skia-backend";
11014
11039
 
11015
11040
  // what is core parts of Konva?
11016
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,
11017
11068
  Util,
11018
11069
  Transform,
11019
11070
  Node,
@@ -16737,14 +16788,14 @@ js: import "konva/skia-backend";
16737
16788
  // rendered in another window than the one Konva was imported into
16738
16789
  this._transformWindow = null;
16739
16790
  this._elementsCreated = false;
16740
- this._updateScheduled = false;
16741
- // an attached container's descendants changed after the last layout
16742
- this._subtreeChanged = false;
16791
+ // the layout is stale; the next sync point runs _update() (see _syncUpdate)
16792
+ this._updatePending = false;
16743
16793
  this._createElements();
16744
16794
  // bindings
16745
16795
  this._handleMouseMove = this._handleMouseMove.bind(this);
16746
16796
  this._handleMouseUp = this._handleMouseUp.bind(this);
16747
16797
  this.update = this.update.bind(this);
16798
+ this._syncUpdate = this._syncUpdate.bind(this);
16748
16799
  // update transformer data for certain attr changes
16749
16800
  this.on(ATTR_CHANGE_LIST, (event) => {
16750
16801
  if (event.type === 'ignoreStrokeChange')
@@ -16800,12 +16851,7 @@ js: import "konva/skia-backend";
16800
16851
  return true;
16801
16852
  });
16802
16853
  this._nodes = nodes = filteredNodes;
16803
- if (nodes.length === 1 && this.useSingleNodeRotation()) {
16804
- this.rotation(nodes[0].getAbsoluteRotation());
16805
- }
16806
- else {
16807
- this.rotation(0);
16808
- }
16854
+ this._resetRotation();
16809
16855
  this._nodes.forEach((node) => {
16810
16856
  const onChange = () => {
16811
16857
  var _a;
@@ -16817,6 +16863,7 @@ js: import "konva/skia-backend";
16817
16863
  (_a = this._nodeRectCache) === null || _a === void 0 ? void 0 : _a.delete(node);
16818
16864
  return;
16819
16865
  }
16866
+ // unlike _resetRotation, a multi-node box keeps its rotation here
16820
16867
  if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
16821
16868
  this.rotation(this.nodes()[0].getAbsoluteRotation());
16822
16869
  }
@@ -16841,8 +16888,7 @@ js: import "konva/skia-backend";
16841
16888
  if (node instanceof Container)
16842
16889
  node._addSubtreeObserver(this);
16843
16890
  node.on(`destroy.${this._getEventNamespace()}`, () => {
16844
- // a destroyed node has nothing left to transform
16845
- this.setNodes(this._nodes.filter((n) => n !== node));
16891
+ this._removeNode(node);
16846
16892
  });
16847
16893
  this._proxyDrag(node);
16848
16894
  });
@@ -16888,6 +16934,23 @@ js: import "konva/skia-backend";
16888
16934
  lastPos = null;
16889
16935
  });
16890
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
+ }
16891
16954
  getNodes() {
16892
16955
  return this._nodes || [];
16893
16956
  }
@@ -16911,17 +16974,12 @@ js: import "konva/skia-backend";
16911
16974
  * transformer.detach();
16912
16975
  */
16913
16976
  detach() {
16914
- var _a;
16915
- // remove events
16916
- if (this._nodes) {
16917
- this._nodes.forEach((node) => {
16918
- node.off('.' + this._getEventNamespace());
16919
- node._removeSubtreeObserver(this);
16920
- });
16921
- }
16977
+ var _a, _b;
16978
+ (_a = this._nodes) === null || _a === void 0 ? void 0 : _a.forEach((node) => this._detachNode(node));
16922
16979
  this._nodes = [];
16980
+ this._updatePending = false;
16923
16981
  this._resetTransformCache();
16924
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
16982
+ (_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
16925
16983
  }
16926
16984
  /**
16927
16985
  * bind events to the Transformer. You can use events: `transform`, `transformstart`, `transformend`, `dragstart`, `dragmove`, `dragend`
@@ -16949,37 +17007,42 @@ js: import "konva/skia-backend";
16949
17007
  // Called for every change below an attached container. A burst of child
16950
17008
  // changes only drops cached values here. The new bounds are measured once,
16951
17009
  // before the next frame, or earlier if a draw or transform step comes first
16952
- // (_syncSubtreeChange).
17010
+ // (_syncUpdate).
16953
17011
  _onSubtreeChange(node) {
16954
17012
  var _a, _b;
16955
17013
  if (this._fitting)
16956
17014
  return;
16957
17015
  // nothing was measured since the last change, so nothing cached is stale
16958
- 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();
16959
17022
  return;
16960
- this._resetTransformCache(node);
16961
- if (!this._subtreeChanged) {
16962
- this._subtreeChanged = true;
17023
+ }
17024
+ if (!this._updatePending) {
17025
+ this._updatePending = true;
16963
17026
  // queued ahead of the draw the change requests, so that draw shows the
16964
17027
  // new layout. Updating inside the draw would request one more frame.
16965
- 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());
16966
17029
  // the transformer can be on another layer than the changed node
16967
17030
  (_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
16968
17031
  }
16969
17032
  }
16970
- _syncSubtreeChange() {
16971
- var _a;
17033
+ // Runs a deferred update before anything draws or measures the anchors.
17034
+ _syncUpdate() {
16972
17035
  // while dragging, the dragmove handler updates the layout
16973
- if (this._subtreeChanged && ((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) && !this.isDragging()) {
17036
+ if (this._updatePending && !this._fitting && !this.isDragging()) {
16974
17037
  this._update();
16975
17038
  }
16976
17039
  }
16977
17040
  drawScene(can, top) {
16978
- this._syncSubtreeChange();
17041
+ this._syncUpdate();
16979
17042
  return super.drawScene(can, top);
16980
17043
  }
16981
17044
  drawHit(can, top) {
16982
- this._syncSubtreeChange();
17045
+ this._syncUpdate();
16983
17046
  return super.drawHit(can, top);
16984
17047
  }
16985
17048
  _getNodeRect() {
@@ -17266,7 +17329,7 @@ js: import "konva/skia-backend";
17266
17329
  }
17267
17330
  _moveTransform(e) {
17268
17331
  // anchors must show the current bounds before they are measured against
17269
- this._syncSubtreeChange();
17332
+ this._syncUpdate();
17270
17333
  let x, y, newHypotenuse;
17271
17334
  const anchorNode = this._anchors[this._movingAnchorName];
17272
17335
  const stage = anchorNode.getStage();
@@ -17700,21 +17763,10 @@ js: import "konva/skia-backend";
17700
17763
  layersToDraw.add(this.getLayer());
17701
17764
  layersToDraw.forEach((layer) => layer && layer.batchDraw());
17702
17765
  }
17703
- // Inside an absoluteTransform cascade, queue a single update to run when
17704
- // the cascade ends; outside a cascade, run synchronously so reads of
17705
- // transformer state stay consistent.
17766
+ // deferred to the batch flush when one is open, else synchronous
17706
17767
  _scheduleUpdate() {
17707
- if (this._updateScheduled)
17708
- return;
17709
- this._updateScheduled = true;
17710
- Node._runAfterAbsTransformCascade(() => {
17711
- var _a;
17712
- this._updateScheduled = false;
17713
- if (!((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) || this._fitting || this.isDragging()) {
17714
- return;
17715
- }
17716
- this._update();
17717
- });
17768
+ this._updatePending = true;
17769
+ Node._runAfterUpdateBatch(this._syncUpdate);
17718
17770
  }
17719
17771
  /**
17720
17772
  * force update of Konva.Transformer.
@@ -17736,7 +17788,7 @@ js: import "konva/skia-backend";
17736
17788
  }
17737
17789
  _update() {
17738
17790
  var _a;
17739
- this._subtreeChanged = false;
17791
+ this._updatePending = false;
17740
17792
  const attrs = this._getNodeRect();
17741
17793
  this._updateElements(attrs);
17742
17794
  const draggable = this.nodes().some((node) => node.draggable());
@@ -17916,6 +17968,7 @@ js: import "konva/skia-backend";
17916
17968
  return node;
17917
17969
  }
17918
17970
  getClientRect(config) {
17971
+ this._syncUpdate();
17919
17972
  if (this.nodes().length > 0) {
17920
17973
  return super.getClientRect(config);
17921
17974
  }