konva 10.4.0 → 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.
@@ -274,21 +274,24 @@ export class Transformer extends Group {
274
274
  }
275
275
  this._nodes.forEach((node) => {
276
276
  const onChange = () => {
277
+ var _a;
277
278
  // Perf: skip the changes _fitNodesInto makes itself — it does the
278
279
  // final _resetTransformCache + update, so per-attr fan-out here is
279
280
  // pure waste (was O(N*events) per resize step). A change made between
280
281
  // two pointer moves (e.g. a deferred state update) must get through.
281
- if (this._fitting)
282
+ if (this._fitting) {
283
+ (_a = this._nodeRectCache) === null || _a === void 0 ? void 0 : _a.delete(node);
282
284
  return;
285
+ }
283
286
  if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
284
287
  this.rotation(this.nodes()[0].getAbsoluteRotation());
285
288
  }
286
289
  // Cache reset stays synchronous so sync reads of tr.x() etc. are fresh.
287
- this._resetTransformCache();
290
+ this._resetTransformCache(node);
288
291
  if (!this.isDragging()) {
289
292
  // Perf: an ancestor cascade (e.g. stage drag) fires
290
293
  // absoluteTransformChange on every attached node — without batching,
291
- // update() runs once per node and each call walks all N nodes via
294
+ // a refresh runs once per node and each call walks all N nodes via
292
295
  // __getNodeRect, giving O(N^2) per drag frame.
293
296
  this._scheduleUpdate();
294
297
  }
@@ -394,7 +397,12 @@ export class Transformer extends Group {
394
397
  * console.log('transform started');
395
398
  * });
396
399
  */
397
- _resetTransformCache() {
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();
398
406
  this._clearCache(NODES_RECT);
399
407
  this._clearCache('transform');
400
408
  this._clearSelfAndDescendantCache('absoluteTransform');
@@ -403,6 +411,7 @@ export class Transformer extends Group {
403
411
  return this._getCache(NODES_RECT, this.__getNodeRect);
404
412
  }
405
413
  __getNodeRect() {
414
+ var _a;
406
415
  const node = this.getNode();
407
416
  if (!node) {
408
417
  return {
@@ -413,35 +422,76 @@ export class Transformer extends Group {
413
422
  rotation: 0,
414
423
  };
415
424
  }
416
- const totalPoints = [];
417
- this.nodes().map((node) => {
418
- const box = node.getClientRect({
419
- skipTransform: true,
420
- skipShadow: true,
421
- skipStroke: this.ignoreStroke(),
422
- });
423
- const points = [
424
- { x: box.x, y: box.y },
425
- { x: box.x + box.width, y: box.y },
426
- { x: box.x + box.width, y: box.y + box.height },
427
- { x: box.x, y: box.y + box.height },
428
- ];
429
- const trans = node.getAbsoluteTransform();
430
- points.forEach(function (point) {
431
- const transformed = trans.point(point);
432
- totalPoints.push(transformed);
433
- });
434
- });
425
+ const rotation = Konva.getAngle(this.rotation());
426
+ const ignoreStroke = this.ignoreStroke();
435
427
  const tr = new Transform();
436
- tr.rotate(-Konva.getAngle(this.rotation()));
428
+ tr.rotate(-rotation);
429
+ const cache = this._nodeRectCache || (this._nodeRectCache = new Map());
437
430
  let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
438
- totalPoints.forEach(function (point) {
439
- const transformed = tr.point(point);
440
- minX = Math.min(minX, transformed.x);
441
- minY = Math.min(minY, transformed.y);
442
- maxX = Math.max(maxX, transformed.x);
443
- maxY = Math.max(maxY, transformed.y);
444
- });
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);
489
+ }
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
+ }
445
495
  tr.invert();
446
496
  const p = tr.point({ x: minX, y: minY });
447
497
  return {
@@ -613,6 +663,7 @@ export class Transformer extends Group {
613
663
  win.addEventListener('touchmove', this._handleMouseMove);
614
664
  win.addEventListener('mouseup', this._handleMouseUp, true);
615
665
  win.addEventListener('touchend', this._handleMouseUp, true);
666
+ win.addEventListener('touchcancel', this._handleMouseUp, true);
616
667
  }
617
668
  this._transforming = true;
618
669
  // the pointer that hit the anchor (a touchstart can land several fingers)
@@ -630,6 +681,10 @@ export class Transformer extends Group {
630
681
  });
631
682
  }
632
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) {
633
688
  let x, y, newHypotenuse;
634
689
  const anchorNode = this._anchors[this._movingAnchorName];
635
690
  const stage = anchorNode.getStage();
@@ -838,7 +893,10 @@ export class Transformer extends Group {
838
893
  return;
839
894
  }
840
895
  }
841
- this._removeEvents(e);
896
+ if (stage)
897
+ stage._batchEvents(() => this._removeEvents(e));
898
+ else
899
+ this._removeEvents(e);
842
900
  }
843
901
  // the transformer positions itself in absolute coordinates (see
844
902
  // _getNodeRect), whatever the transforms of its ancestors are
@@ -857,6 +915,7 @@ export class Transformer extends Group {
857
915
  win.removeEventListener('touchmove', this._handleMouseMove);
858
916
  win.removeEventListener('mouseup', this._handleMouseUp, true);
859
917
  win.removeEventListener('touchend', this._handleMouseUp, true);
918
+ win.removeEventListener('touchcancel', this._handleMouseUp, true);
860
919
  }
861
920
  const node = this.getNode();
862
921
  activeTransformers.delete(this);
@@ -1063,7 +1122,7 @@ export class Transformer extends Group {
1063
1122
  if (!((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) || this._fitting || this.isDragging()) {
1064
1123
  return;
1065
1124
  }
1066
- this.update();
1125
+ this._update();
1067
1126
  });
1068
1127
  }
1069
1128
  /**
@@ -1077,8 +1136,39 @@ export class Transformer extends Group {
1077
1136
  this.update();
1078
1137
  }
1079
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() {
1080
1145
  var _a;
1081
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 };
1082
1172
  this.rotation(Util._getRotation(attrs.rotation));
1083
1173
  const width = attrs.width;
1084
1174
  const height = attrs.height;
@@ -1198,17 +1288,9 @@ export class Transformer extends Group {
1198
1288
  stroke: this.borderStroke(),
1199
1289
  strokeWidth: this.borderStrokeWidth(),
1200
1290
  dash: this.borderDash(),
1201
- draggable: this.nodes().some((node) => node.draggable()),
1202
1291
  x: 0,
1203
1292
  y: 0,
1204
1293
  });
1205
- const styleFunc = this.anchorStyleFunc();
1206
- if (styleFunc) {
1207
- anchors.forEach((node) => {
1208
- styleFunc(node);
1209
- });
1210
- }
1211
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
1212
1294
  }
1213
1295
  /**
1214
1296
  * determine if transformer is in active transform
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "konva",
3
- "version": "10.4.0",
3
+ "version": "10.5.0",
4
4
  "description": "HTML5 2d canvas library for interactive graphics, design editors, whiteboards, and diagrams.",
5
5
  "author": "Anton Lavrenov",
6
6
  "type": "module",