konva 10.6.0 → 10.7.1

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 (53) hide show
  1. package/README.md +12 -8
  2. package/konva.js +617 -231
  3. package/konva.min.js +4 -4
  4. package/lib/Canvas.d.ts +9 -2
  5. package/lib/Canvas.js +83 -5
  6. package/lib/Container.d.ts +3 -2
  7. package/lib/Container.js +27 -12
  8. package/lib/Context.d.ts +5 -2
  9. package/lib/Context.js +22 -1
  10. package/lib/Global.d.ts +2 -1
  11. package/lib/Global.js +8 -3
  12. package/lib/Group.d.ts +7 -1
  13. package/lib/Group.js +61 -0
  14. package/lib/Layer.d.ts +1 -1
  15. package/lib/Layer.js +4 -2
  16. package/lib/Node.d.ts +12 -7
  17. package/lib/Node.js +53 -44
  18. package/lib/Shape.d.ts +9 -15
  19. package/lib/Shape.js +145 -85
  20. package/lib/Stage.d.ts +0 -2
  21. package/lib/Stage.js +2 -13
  22. package/lib/Util.d.ts +1 -0
  23. package/lib/Util.js +25 -0
  24. package/lib/shapes/Arc.js +3 -3
  25. package/lib/shapes/Arrow.d.ts +1 -0
  26. package/lib/shapes/Arrow.js +7 -1
  27. package/lib/shapes/Circle.d.ts +1 -0
  28. package/lib/shapes/Circle.js +4 -1
  29. package/lib/shapes/Ellipse.d.ts +1 -0
  30. package/lib/shapes/Ellipse.js +4 -1
  31. package/lib/shapes/Image.d.ts +2 -1
  32. package/lib/shapes/Image.js +6 -3
  33. package/lib/shapes/Label.js +18 -20
  34. package/lib/shapes/Line.d.ts +1 -0
  35. package/lib/shapes/Line.js +4 -1
  36. package/lib/shapes/Path.js +1 -1
  37. package/lib/shapes/Rect.d.ts +1 -0
  38. package/lib/shapes/Rect.js +4 -1
  39. package/lib/shapes/RegularPolygon.js +1 -1
  40. package/lib/shapes/Ring.d.ts +7 -0
  41. package/lib/shapes/Ring.js +8 -1
  42. package/lib/shapes/Sprite.d.ts +2 -1
  43. package/lib/shapes/Sprite.js +6 -3
  44. package/lib/shapes/Star.d.ts +6 -0
  45. package/lib/shapes/Star.js +5 -1
  46. package/lib/shapes/Text.d.ts +5 -2
  47. package/lib/shapes/Text.js +49 -22
  48. package/lib/shapes/TextPath.d.ts +7 -0
  49. package/lib/shapes/TextPath.js +16 -1
  50. package/lib/shapes/Transformer.d.ts +10 -2
  51. package/lib/shapes/Transformer.js +55 -6
  52. package/lib/shapes/Wedge.js +1 -1
  53. package/package.json +2 -18
@@ -4,6 +4,7 @@ import { Node } from "../Node.js";
4
4
  import { Shape } from "../Shape.js";
5
5
  import { Rect } from "./Rect.js";
6
6
  import { Group } from "../Group.js";
7
+ import { Container } from "../Container.js";
7
8
  import { Konva } from "../Global.js";
8
9
  import { getBooleanValidator, getNumberValidator } from "../Validators.js";
9
10
  import { _registerNode } from "../Global.js";
@@ -44,6 +45,7 @@ const TRANSFORM_CHANGE_STR = [
44
45
  'transformsEnabledChange',
45
46
  'strokeWidthChange',
46
47
  'strokeChange',
48
+ 'selfRectFuncChange',
47
49
  'strokeLinearGradientColorStopsChange',
48
50
  'strokeScaleEnabledChange',
49
51
  'strokeEnabledChange',
@@ -259,6 +261,8 @@ export class Transformer extends Group {
259
261
  this._transformWindow = null;
260
262
  this._elementsCreated = false;
261
263
  this._updateScheduled = false;
264
+ // an attached container's descendants changed after the last layout
265
+ this._subtreeChanged = false;
262
266
  this._createElements();
263
267
  // bindings
264
268
  this._handleMouseMove = this._handleMouseMove.bind(this);
@@ -357,6 +361,8 @@ export class Transformer extends Group {
357
361
  }
358
362
  node.on(TRANSFORM_CHANGE_STR.map((e) => e + `.${this._getEventNamespace()}`).join(' '), onChange);
359
363
  node.on(`absoluteTransformChange.${this._getEventNamespace()}`, onChange);
364
+ if (node instanceof Container)
365
+ node._addSubtreeObserver(this);
360
366
  node.on(`destroy.${this._getEventNamespace()}`, () => {
361
367
  // a destroyed node has nothing left to transform
362
368
  this.setNodes(this._nodes.filter((n) => n !== node));
@@ -433,6 +439,7 @@ export class Transformer extends Group {
433
439
  if (this._nodes) {
434
440
  this._nodes.forEach((node) => {
435
441
  node.off('.' + this._getEventNamespace());
442
+ node._removeSubtreeObserver(this);
436
443
  });
437
444
  }
438
445
  this._nodes = [];
@@ -462,6 +469,42 @@ export class Transformer extends Group {
462
469
  this._clearCache('transform');
463
470
  this._clearSelfAndDescendantCache('absoluteTransform');
464
471
  }
472
+ // Called for every change below an attached container. A burst of child
473
+ // changes only drops cached values here. The new bounds are measured once,
474
+ // before the next frame, or earlier if a draw or transform step comes first
475
+ // (_syncSubtreeChange).
476
+ _onSubtreeChange(node) {
477
+ var _a, _b;
478
+ if (this._fitting)
479
+ return;
480
+ // nothing was measured since the last change, so nothing cached is stale
481
+ if (this._subtreeChanged && !this._cache[NODES_RECT])
482
+ return;
483
+ this._resetTransformCache(node);
484
+ if (!this._subtreeChanged) {
485
+ this._subtreeChanged = true;
486
+ // queued ahead of the draw the change requests, so that draw shows the
487
+ // new layout. Updating inside the draw would request one more frame.
488
+ Util.requestAnimFrame(() => this._syncSubtreeChange(), (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow());
489
+ // the transformer can be on another layer than the changed node
490
+ (_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
491
+ }
492
+ }
493
+ _syncSubtreeChange() {
494
+ var _a;
495
+ // while dragging, the dragmove handler updates the layout
496
+ if (this._subtreeChanged && ((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) && !this.isDragging()) {
497
+ this._update();
498
+ }
499
+ }
500
+ drawScene(can, top) {
501
+ this._syncSubtreeChange();
502
+ return super.drawScene(can, top);
503
+ }
504
+ drawHit(can, top) {
505
+ this._syncSubtreeChange();
506
+ return super.drawHit(can, top);
507
+ }
465
508
  _getNodeRect() {
466
509
  return this._getCache(NODES_RECT, this.__getNodeRect);
467
510
  }
@@ -487,13 +530,14 @@ export class Transformer extends Group {
487
530
  // Bounds may only be reused when they are a pure function of the node's
488
531
  // own size: then every change reaches us either through _setAttr or
489
532
  // through the size probe below. A shape with its own getSelfRect - Line
490
- // mutates its points array in place, and a user subclass can measure
491
- // anything at all - may change with no signal, so it is measured every
492
- // time. Groups depend on their descendants, and a cached ancestor
493
- // suppresses descendant transform-change notifications, so both are
494
- // measured too.
533
+ // mutates its points array in place, and a user subclass or selfRectFunc
534
+ // can measure anything at all - may change with no signal, so it is
535
+ // measured every time. Groups depend on their descendants, and a cached
536
+ // ancestor suppresses descendant transform-change notifications, so
537
+ // both are measured too.
495
538
  const cacheable = node instanceof Shape &&
496
539
  node.getSelfRect === Shape.prototype.getSelfRect &&
540
+ !node.attrs.selfRectFunc &&
497
541
  !node._hasCachedAncestor();
498
542
  // width() and height() are the only inputs of the default getSelfRect and
499
543
  // they can come from outside the attrs (an Image source that loads or
@@ -722,6 +766,8 @@ export class Transformer extends Group {
722
766
  (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._batchEvents(() => this._moveTransform(e));
723
767
  }
724
768
  _moveTransform(e) {
769
+ // anchors must show the current bounds before they are measured against
770
+ this._syncSubtreeChange();
725
771
  let x, y, newHypotenuse;
726
772
  const anchorNode = this._anchors[this._movingAnchorName];
727
773
  const stage = anchorNode.getStage();
@@ -1159,7 +1205,9 @@ export class Transformer extends Group {
1159
1205
  }
1160
1206
  /**
1161
1207
  * force update of Konva.Transformer.
1162
- * Use it when you updated attached Konva.Group and now you need to reset transformer size
1208
+ * The transformer follows attribute changes of attached nodes and, for an attached group, any change of its descendants.
1209
+ * Use it after a change that sets no attribute, for example a `Line` points array mutated in place
1210
+ * or a custom `getSelfRect()` that depends on outside state.
1163
1211
  * @method
1164
1212
  * @name Konva.Transformer#forceUpdate
1165
1213
  */
@@ -1175,6 +1223,7 @@ export class Transformer extends Group {
1175
1223
  }
1176
1224
  _update() {
1177
1225
  var _a;
1226
+ this._subtreeChanged = false;
1178
1227
  const attrs = this._getNodeRect();
1179
1228
  this._updateElements(attrs);
1180
1229
  const draggable = this.nodes().some((node) => node.draggable());
@@ -49,7 +49,7 @@ export class Wedge extends Shape {
49
49
  Wedge.prototype.className = 'Wedge';
50
50
  Wedge.prototype._centroid = true;
51
51
  Wedge.prototype._attrsAffectingSize = ['radius'];
52
- _registerNode(Wedge);
52
+ _registerNode(Wedge, true);
53
53
  /**
54
54
  * get/set radius
55
55
  * @name Konva.Wedge#radius
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "konva",
3
- "version": "10.6.0",
3
+ "version": "10.7.1",
4
4
  "description": "HTML5 2d canvas library for interactive graphics, design editors, whiteboards, and diagrams.",
5
5
  "author": "Anton Lavrenov",
6
6
  "type": "module",
@@ -54,8 +54,7 @@
54
54
  "tsc": "tsc",
55
55
  "rollup": "rollup -c",
56
56
  "clean": "rimraf ./lib ./test-build",
57
- "watch": "rollup -c -w",
58
- "size": "size-limit"
57
+ "watch": "rollup -c -w"
59
58
  },
60
59
  "targets": {
61
60
  "none": {}
@@ -74,22 +73,8 @@
74
73
  "url": "https://github.com/sponsors/lavrton"
75
74
  }
76
75
  ],
77
- "size-limit": [
78
- {
79
- "limit": "48 KB",
80
- "path": "./lib/index.js"
81
- },
82
- {
83
- "limit": "26 KB",
84
- "path": "./lib/Core.js"
85
- },
86
- {
87
- "path": "./konva.min.js"
88
- }
89
- ],
90
76
  "devDependencies": {
91
77
  "@parcel/transformer-image": "2.15.4",
92
- "@size-limit/preset-big-lib": "^11.2.0",
93
78
  "@types/mocha": "^10.0.10",
94
79
  "canvas": "^3.2.1",
95
80
  "chai": "6.2.2",
@@ -108,7 +93,6 @@
108
93
  "rimraf": "^6.1.2",
109
94
  "rollup": "^4.55.1",
110
95
  "rollup-plugin-typescript2": "^0.37.0",
111
- "size-limit": "^11.2.0",
112
96
  "skia-canvas": "^3.0.4",
113
97
  "stats.js": "0.17.0",
114
98
  "tsx": "^4.21.0",