konva 8.3.13 → 8.4.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 (55) hide show
  1. package/cmj/Canvas.js +1 -0
  2. package/cmj/Context.d.ts +33 -27
  3. package/cmj/Context.js +6 -2
  4. package/cmj/Global.d.ts +1 -0
  5. package/cmj/Global.js +2 -1
  6. package/cmj/Layer.d.ts +1 -0
  7. package/cmj/Layer.js +4 -0
  8. package/cmj/Node.d.ts +5 -5
  9. package/cmj/Node.js +6 -1
  10. package/cmj/PointerEvents.d.ts +1 -1
  11. package/cmj/Shape.d.ts +3 -3
  12. package/cmj/Stage.js +1 -0
  13. package/cmj/Util.d.ts +5 -0
  14. package/cmj/Util.js +54 -0
  15. package/cmj/_CoreInternals.d.ts +5 -0
  16. package/cmj/_FullInternals.d.ts +5 -0
  17. package/cmj/filters/Kaleidoscope.js +1 -0
  18. package/cmj/shapes/Image.d.ts +2 -0
  19. package/cmj/shapes/Image.js +17 -5
  20. package/cmj/shapes/Rect.d.ts +2 -1
  21. package/cmj/shapes/Rect.js +2 -22
  22. package/cmj/shapes/TextPath.d.ts +1 -0
  23. package/cmj/shapes/TextPath.js +4 -0
  24. package/cmj/shapes/Transformer.d.ts +1 -0
  25. package/cmj/shapes/Transformer.js +4 -0
  26. package/cmj/types.d.ts +1 -1
  27. package/konva.js +147 -47
  28. package/konva.min.js +3 -3
  29. package/lib/Canvas.js +1 -0
  30. package/lib/Context.d.ts +33 -27
  31. package/lib/Context.js +6 -2
  32. package/lib/Global.d.ts +1 -0
  33. package/lib/Global.js +2 -1
  34. package/lib/Layer.d.ts +1 -0
  35. package/lib/Layer.js +4 -0
  36. package/lib/Node.d.ts +5 -5
  37. package/lib/Node.js +6 -1
  38. package/lib/PointerEvents.d.ts +1 -1
  39. package/lib/Shape.d.ts +3 -3
  40. package/lib/Stage.js +1 -0
  41. package/lib/Util.d.ts +5 -0
  42. package/lib/Util.js +54 -0
  43. package/lib/_CoreInternals.d.ts +5 -0
  44. package/lib/_FullInternals.d.ts +5 -0
  45. package/lib/filters/Kaleidoscope.js +1 -0
  46. package/lib/shapes/Image.d.ts +2 -0
  47. package/lib/shapes/Image.js +17 -5
  48. package/lib/shapes/Rect.d.ts +2 -1
  49. package/lib/shapes/Rect.js +2 -22
  50. package/lib/shapes/TextPath.d.ts +1 -0
  51. package/lib/shapes/TextPath.js +4 -0
  52. package/lib/shapes/Transformer.d.ts +1 -0
  53. package/lib/shapes/Transformer.js +4 -0
  54. package/lib/types.d.ts +1 -1
  55. package/package.json +13 -13
package/konva.js CHANGED
@@ -5,10 +5,10 @@
5
5
  })(this, (function () { 'use strict';
6
6
 
7
7
  /*
8
- * Konva JavaScript Framework v8.3.13
8
+ * Konva JavaScript Framework v8.4.0
9
9
  * http://konvajs.org/
10
10
  * Licensed under the MIT
11
- * Date: Mon Oct 03 2022
11
+ * Date: Thu Jan 05 2023
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: '8.3.13',
38
+ version: '8.4.0',
39
39
  isBrowser: detectBrowser(),
40
40
  isUnminified: /param/.test(function (param) { }.toString()),
41
41
  dblClickWindow: 400,
@@ -157,6 +157,17 @@
157
157
  isDragReady() {
158
158
  return !!Konva$2['DD'].node;
159
159
  },
160
+ /**
161
+ * Should Konva release canvas elements on destroy. Default is true.
162
+ * Useful to avoid memory leak issues in Safari on macOS/iOS.
163
+ * @property releaseCanvasOnDestroy
164
+ * @default true
165
+ * @name releaseCanvasOnDestroy
166
+ * @memberof Konva
167
+ * @example
168
+ * Konva.releaseCanvasOnDestroy = true;
169
+ */
170
+ releaseCanvasOnDestroy: true,
160
171
  // user agent
161
172
  document: glob.document,
162
173
  // insert Konva into global namespace (window)
@@ -748,7 +759,9 @@
748
759
  str = str || 'black';
749
760
  return (Util._namedColorToRBA(str) ||
750
761
  Util._hex3ColorToRGBA(str) ||
762
+ Util._hex4ColorToRGBA(str) ||
751
763
  Util._hex6ColorToRGBA(str) ||
764
+ Util._hex8ColorToRGBA(str) ||
752
765
  Util._rgbColorToRGBA(str) ||
753
766
  Util._rgbaColorToRGBA(str) ||
754
767
  Util._hslColorToRGBA(str));
@@ -797,6 +810,17 @@
797
810
  };
798
811
  }
799
812
  },
813
+ // Parse #nnnnnnnn
814
+ _hex8ColorToRGBA(str) {
815
+ if (str[0] === '#' && str.length === 9) {
816
+ return {
817
+ r: parseInt(str.slice(1, 3), 16),
818
+ g: parseInt(str.slice(3, 5), 16),
819
+ b: parseInt(str.slice(5, 7), 16),
820
+ a: parseInt(str.slice(7, 9), 16) / 0xff,
821
+ };
822
+ }
823
+ },
800
824
  // Parse #nnnnnn
801
825
  _hex6ColorToRGBA(str) {
802
826
  if (str[0] === '#' && str.length === 7) {
@@ -808,6 +832,17 @@
808
832
  };
809
833
  }
810
834
  },
835
+ // Parse #nnnn
836
+ _hex4ColorToRGBA(str) {
837
+ if (str[0] === '#' && str.length === 5) {
838
+ return {
839
+ r: parseInt(str[1] + str[1], 16),
840
+ g: parseInt(str[2] + str[2], 16),
841
+ b: parseInt(str[3] + str[3], 16),
842
+ a: parseInt(str[4] + str[4], 16) / 0xff,
843
+ };
844
+ }
845
+ },
811
846
  // Parse #nnn
812
847
  _hex3ColorToRGBA(str) {
813
848
  if (str[0] === '#' && str.length === 4) {
@@ -1071,6 +1106,38 @@
1071
1106
  return evt.changedTouches[0].identifier;
1072
1107
  }
1073
1108
  },
1109
+ releaseCanvas(...canvases) {
1110
+ if (!Konva$2.releaseCanvasOnDestroy)
1111
+ return;
1112
+ canvases.forEach(c => {
1113
+ c.width = 0;
1114
+ c.height = 0;
1115
+ });
1116
+ },
1117
+ drawRoundedRectPath(context, width, height, cornerRadius) {
1118
+ let topLeft = 0;
1119
+ let topRight = 0;
1120
+ let bottomLeft = 0;
1121
+ let bottomRight = 0;
1122
+ if (typeof cornerRadius === 'number') {
1123
+ topLeft = topRight = bottomLeft = bottomRight = Math.min(cornerRadius, width / 2, height / 2);
1124
+ }
1125
+ else {
1126
+ topLeft = Math.min(cornerRadius[0] || 0, width / 2, height / 2);
1127
+ topRight = Math.min(cornerRadius[1] || 0, width / 2, height / 2);
1128
+ bottomRight = Math.min(cornerRadius[2] || 0, width / 2, height / 2);
1129
+ bottomLeft = Math.min(cornerRadius[3] || 0, width / 2, height / 2);
1130
+ }
1131
+ context.moveTo(topLeft, 0);
1132
+ context.lineTo(width - topRight, 0);
1133
+ context.arc(width - topRight, topRight, topRight, (Math.PI * 3) / 2, 0, false);
1134
+ context.lineTo(width, height - bottomRight);
1135
+ context.arc(width - bottomRight, height - bottomRight, bottomRight, 0, Math.PI / 2, false);
1136
+ context.lineTo(bottomLeft, height);
1137
+ context.arc(bottomLeft, height - bottomLeft, bottomLeft, Math.PI / 2, Math.PI, false);
1138
+ context.lineTo(0, topLeft);
1139
+ context.arc(topLeft, topLeft, topLeft, Math.PI, (Math.PI * 3) / 2, false);
1140
+ }
1074
1141
  };
1075
1142
 
1076
1143
  function _formatValue(val) {
@@ -1730,8 +1797,11 @@
1730
1797
  * @method
1731
1798
  * @name Konva.Context#isPointInPath
1732
1799
  */
1733
- isPointInPath(x, y) {
1734
- return this._context.isPointInPath(x, y);
1800
+ isPointInPath(x, y, path, fillRule) {
1801
+ if (path) {
1802
+ return this._context.isPointInPath(path, x, y, fillRule);
1803
+ }
1804
+ return this._context.isPointInPath(x, y, fillRule);
1735
1805
  }
1736
1806
  /**
1737
1807
  * fill function.
@@ -2164,6 +2234,7 @@
2164
2234
  1;
2165
2235
  return devicePixelRatio / backingStoreRatio;
2166
2236
  })();
2237
+ Util.releaseCanvas(canvas);
2167
2238
  return _pixelRatio;
2168
2239
  }
2169
2240
  /**
@@ -2556,7 +2627,11 @@
2556
2627
  * node.clearCache();
2557
2628
  */
2558
2629
  clearCache() {
2559
- this._cache.delete(CANVAS);
2630
+ if (this._cache.has(CANVAS)) {
2631
+ const { scene, filter, hit } = this._cache.get(CANVAS);
2632
+ Util.releaseCanvas(scene, filter, hit);
2633
+ this._cache.delete(CANVAS);
2634
+ }
2560
2635
  this._clearSelfAndDescendantCache();
2561
2636
  this._requestDraw();
2562
2637
  return this;
@@ -3042,6 +3117,7 @@
3042
3117
  */
3043
3118
  destroy() {
3044
3119
  this.remove();
3120
+ this.clearCache();
3045
3121
  return this;
3046
3122
  }
3047
3123
  /**
@@ -5997,6 +6073,7 @@
5997
6073
  if (index > -1) {
5998
6074
  stages.splice(index, 1);
5999
6075
  }
6076
+ Util.releaseCanvas(this.bufferCanvas._canvas, this.bufferHitCanvas._canvas);
6000
6077
  return this;
6001
6078
  }
6002
6079
  /**
@@ -8620,6 +8697,10 @@
8620
8697
  parent.content.appendChild(this.hitCanvas._canvas);
8621
8698
  }
8622
8699
  }
8700
+ destroy() {
8701
+ Util.releaseCanvas(this.getNativeCanvasElement(), this.getHitCanvas()._canvas);
8702
+ return super.destroy();
8703
+ }
8623
8704
  }
8624
8705
  Layer.prototype.nodeType = 'Layer';
8625
8706
  _registerNode(Layer);
@@ -11710,6 +11791,7 @@
11710
11791
  _sceneFunc(context) {
11711
11792
  const width = this.getWidth();
11712
11793
  const height = this.getHeight();
11794
+ const cornerRadius = this.cornerRadius();
11713
11795
  const image = this.attrs.image;
11714
11796
  let params;
11715
11797
  if (image) {
@@ -11732,20 +11814,31 @@
11732
11814
  params = [image, 0, 0, width, height];
11733
11815
  }
11734
11816
  }
11735
- if (this.hasFill() || this.hasStroke()) {
11817
+ if (this.hasFill() || this.hasStroke() || cornerRadius) {
11736
11818
  context.beginPath();
11737
- context.rect(0, 0, width, height);
11819
+ cornerRadius
11820
+ ? Util.drawRoundedRectPath(context, width, height, cornerRadius)
11821
+ : context.rect(0, 0, width, height);
11738
11822
  context.closePath();
11739
11823
  context.fillStrokeShape(this);
11740
11824
  }
11741
11825
  if (image) {
11826
+ if (cornerRadius) {
11827
+ context.clip();
11828
+ }
11742
11829
  context.drawImage.apply(context, params);
11743
11830
  }
11831
+ // If you need to draw later, you need to execute save/restore
11744
11832
  }
11745
11833
  _hitFunc(context) {
11746
- var width = this.width(), height = this.height();
11834
+ var width = this.width(), height = this.height(), cornerRadius = this.cornerRadius();
11747
11835
  context.beginPath();
11748
- context.rect(0, 0, width, height);
11836
+ if (!cornerRadius) {
11837
+ context.rect(0, 0, width, height);
11838
+ }
11839
+ else {
11840
+ Util.drawRoundedRectPath(context, width, height, cornerRadius);
11841
+ }
11749
11842
  context.closePath();
11750
11843
  context.fillStrokeShape(this);
11751
11844
  }
@@ -11786,6 +11879,24 @@
11786
11879
  }
11787
11880
  Image.prototype.className = 'Image';
11788
11881
  _registerNode(Image);
11882
+ /**
11883
+ * get/set corner radius
11884
+ * @method
11885
+ * @name Konva.Image#cornerRadius
11886
+ * @param {Number} cornerRadius
11887
+ * @returns {Number}
11888
+ * @example
11889
+ * // get corner radius
11890
+ * var cornerRadius = image.cornerRadius();
11891
+ *
11892
+ * // set corner radius
11893
+ * image.cornerRadius(10);
11894
+ *
11895
+ * // set different corner radius values
11896
+ * // top-left, top-right, bottom-right, bottom-left
11897
+ * image.cornerRadius([0, 10, 20, 30]);
11898
+ */
11899
+ Factory.addGetterSetter(Image, 'cornerRadius', 0, getNumberOrArrayOfNumbersValidator(4));
11789
11900
  /**
11790
11901
  * get/set image source. It can be image, canvas or video element
11791
11902
  * @name Konva.Image#image
@@ -12276,28 +12387,7 @@
12276
12387
  context.rect(0, 0, width, height);
12277
12388
  }
12278
12389
  else {
12279
- let topLeft = 0;
12280
- let topRight = 0;
12281
- let bottomLeft = 0;
12282
- let bottomRight = 0;
12283
- if (typeof cornerRadius === 'number') {
12284
- topLeft = topRight = bottomLeft = bottomRight = Math.min(cornerRadius, width / 2, height / 2);
12285
- }
12286
- else {
12287
- topLeft = Math.min(cornerRadius[0] || 0, width / 2, height / 2);
12288
- topRight = Math.min(cornerRadius[1] || 0, width / 2, height / 2);
12289
- bottomRight = Math.min(cornerRadius[2] || 0, width / 2, height / 2);
12290
- bottomLeft = Math.min(cornerRadius[3] || 0, width / 2, height / 2);
12291
- }
12292
- context.moveTo(topLeft, 0);
12293
- context.lineTo(width - topRight, 0);
12294
- context.arc(width - topRight, topRight, topRight, (Math.PI * 3) / 2, 0, false);
12295
- context.lineTo(width, height - bottomRight);
12296
- context.arc(width - bottomRight, height - bottomRight, bottomRight, 0, Math.PI / 2, false);
12297
- context.lineTo(bottomLeft, height);
12298
- context.arc(bottomLeft, height - bottomLeft, bottomLeft, Math.PI / 2, Math.PI, false);
12299
- context.lineTo(0, topLeft);
12300
- context.arc(topLeft, topLeft, topLeft, Math.PI, (Math.PI * 3) / 2, false);
12390
+ Util.drawRoundedRectPath(context, width, height, cornerRadius);
12301
12391
  }
12302
12392
  context.closePath();
12303
12393
  context.fillStrokeShape(this);
@@ -14407,6 +14497,10 @@
14407
14497
  height: maxY - minY + fontSize,
14408
14498
  };
14409
14499
  }
14500
+ destroy() {
14501
+ Util.releaseCanvas(this.dummyCanvas);
14502
+ return super.destroy();
14503
+ }
14410
14504
  }
14411
14505
  TextPath.prototype._fillFunc = _fillFunc;
14412
14506
  TextPath.prototype._strokeFunc = _strokeFunc;
@@ -15602,6 +15696,11 @@
15602
15696
  toObject() {
15603
15697
  return Node.prototype.toObject.call(this);
15604
15698
  }
15699
+ // overwrite clone to NOT use method from Container
15700
+ clone(obj) {
15701
+ var node = Node.prototype.clone.call(this, obj);
15702
+ return node;
15703
+ }
15605
15704
  getClientRect() {
15606
15705
  if (this.nodes().length > 0) {
15607
15706
  return super.getClientRect();
@@ -15663,8 +15762,8 @@
15663
15762
  * get/set resize ability. If false it will automatically hide resizing handlers
15664
15763
  * @name Konva.Transformer#resizeEnabled
15665
15764
  * @method
15666
- * @param {Array} array
15667
- * @returns {Array}
15765
+ * @param {Boolean} enabled
15766
+ * @returns {Boolean}
15668
15767
  * @example
15669
15768
  * // get
15670
15769
  * var resizeEnabled = transformer.resizeEnabled();
@@ -15675,9 +15774,9 @@
15675
15774
  Factory.addGetterSetter(Transformer, 'resizeEnabled', true);
15676
15775
  /**
15677
15776
  * get/set anchor size. Default is 10
15678
- * @name Konva.Transformer#validateAnchors
15777
+ * @name Konva.Transformer#anchorSize
15679
15778
  * @method
15680
- * @param {Number} 10
15779
+ * @param {Number} size
15681
15780
  * @returns {Number}
15682
15781
  * @example
15683
15782
  * // get
@@ -15761,8 +15860,8 @@
15761
15860
  * get/set anchor stroke color
15762
15861
  * @name Konva.Transformer#anchorStroke
15763
15862
  * @method
15764
- * @param {Boolean} enabled
15765
- * @returns {Boolean}
15863
+ * @param {String} strokeColor
15864
+ * @returns {String}
15766
15865
  * @example
15767
15866
  * // get
15768
15867
  * var anchorStroke = transformer.anchorStroke();
@@ -15775,8 +15874,8 @@
15775
15874
  * get/set anchor stroke width
15776
15875
  * @name Konva.Transformer#anchorStrokeWidth
15777
15876
  * @method
15778
- * @param {Boolean} enabled
15779
- * @returns {Boolean}
15877
+ * @param {Number} anchorStrokeWidth
15878
+ * @returns {Number}
15780
15879
  * @example
15781
15880
  * // get
15782
15881
  * var anchorStrokeWidth = transformer.anchorStrokeWidth();
@@ -15789,8 +15888,8 @@
15789
15888
  * get/set anchor fill color
15790
15889
  * @name Konva.Transformer#anchorFill
15791
15890
  * @method
15792
- * @param {Boolean} enabled
15793
- * @returns {Boolean}
15891
+ * @param {String} anchorFill
15892
+ * @returns {String}
15794
15893
  * @example
15795
15894
  * // get
15796
15895
  * var anchorFill = transformer.anchorFill();
@@ -15803,7 +15902,7 @@
15803
15902
  * get/set anchor corner radius
15804
15903
  * @name Konva.Transformer#anchorCornerRadius
15805
15904
  * @method
15806
- * @param {Number} enabled
15905
+ * @param {Number} radius
15807
15906
  * @returns {Number}
15808
15907
  * @example
15809
15908
  * // get
@@ -15831,8 +15930,8 @@
15831
15930
  * get/set border stroke width
15832
15931
  * @name Konva.Transformer#borderStrokeWidth
15833
15932
  * @method
15834
- * @param {Boolean} enabled
15835
- * @returns {Boolean}
15933
+ * @param {Number} strokeWidth
15934
+ * @returns {Number}
15836
15935
  * @example
15837
15936
  * // get
15838
15937
  * var borderStrokeWidth = transformer.borderStrokeWidth();
@@ -15845,8 +15944,8 @@
15845
15944
  * get/set border dash array
15846
15945
  * @name Konva.Transformer#borderDash
15847
15946
  * @method
15848
- * @param {Boolean} enabled
15849
- * @returns {Boolean}
15947
+ * @param {Array} dash array
15948
+ * @returns {Array}
15850
15949
  * @example
15851
15950
  * // get
15852
15951
  * var borderDash = transformer.borderDash();
@@ -17593,6 +17692,7 @@
17593
17692
  var scratchData = tempCanvas
17594
17693
  .getContext('2d')
17595
17694
  .getImageData(0, 0, xSize, ySize);
17695
+ Util.releaseCanvas(tempCanvas);
17596
17696
  // Convert thhe original to polar coordinates
17597
17697
  ToPolar(imageData, scratchData, {
17598
17698
  polarCenterX: xSize / 2,