konva 10.5.0 → 10.7.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 (79) hide show
  1. package/README.md +16 -12
  2. package/konva.js +3623 -3819
  3. package/konva.min.js +4 -4
  4. package/lib/Animation.d.ts +2 -2
  5. package/lib/Animation.js +2 -2
  6. package/lib/BezierFunctions.d.ts +1 -1
  7. package/lib/BezierFunctions.js +76 -116
  8. package/lib/Canvas.d.ts +10 -3
  9. package/lib/Canvas.js +91 -10
  10. package/lib/Container.d.ts +3 -2
  11. package/lib/Container.js +31 -17
  12. package/lib/Context.d.ts +9 -7
  13. package/lib/Context.js +99 -73
  14. package/lib/DragAndDrop.d.ts +4 -3
  15. package/lib/DragAndDrop.js +39 -25
  16. package/lib/Factory.d.ts +3 -2
  17. package/lib/Factory.js +10 -9
  18. package/lib/Global.d.ts +2 -7
  19. package/lib/Global.js +8 -9
  20. package/lib/Group.d.ts +7 -1
  21. package/lib/Group.js +61 -0
  22. package/lib/Layer.d.ts +1 -1
  23. package/lib/Layer.js +13 -8
  24. package/lib/Node.d.ts +25 -19
  25. package/lib/Node.js +178 -169
  26. package/lib/PointerEvents.d.ts +1 -2
  27. package/lib/PointerEvents.js +20 -18
  28. package/lib/Shape.d.ts +9 -15
  29. package/lib/Shape.js +190 -86
  30. package/lib/Stage.d.ts +19 -15
  31. package/lib/Stage.js +184 -99
  32. package/lib/Tween.js +59 -66
  33. package/lib/Util.d.ts +3 -2
  34. package/lib/Util.js +46 -16
  35. package/lib/_CoreInternals.d.ts +23 -11
  36. package/lib/_FullInternals.d.ts +23 -11
  37. package/lib/filters/Blur.js +16 -12
  38. package/lib/filters/Brighten.d.ts +1 -0
  39. package/lib/filters/Brighten.js +1 -0
  40. package/lib/filters/Brightness.d.ts +2 -1
  41. package/lib/filters/Brightness.js +4 -4
  42. package/lib/filters/Emboss.js +16 -58
  43. package/lib/filters/Kaleidoscope.js +8 -15
  44. package/lib/filters/Mask.js +8 -8
  45. package/lib/filters/Pixelate.js +16 -29
  46. package/lib/filters/Threshold.d.ts +1 -1
  47. package/lib/filters/Threshold.js +1 -1
  48. package/lib/shapes/Arc.js +8 -6
  49. package/lib/shapes/Arrow.d.ts +3 -6
  50. package/lib/shapes/Arrow.js +75 -47
  51. package/lib/shapes/Circle.d.ts +1 -0
  52. package/lib/shapes/Circle.js +4 -1
  53. package/lib/shapes/Ellipse.d.ts +1 -0
  54. package/lib/shapes/Ellipse.js +4 -1
  55. package/lib/shapes/Image.d.ts +2 -1
  56. package/lib/shapes/Image.js +22 -22
  57. package/lib/shapes/Label.d.ts +2 -3
  58. package/lib/shapes/Label.js +25 -41
  59. package/lib/shapes/Line.d.ts +2 -1
  60. package/lib/shapes/Line.js +5 -2
  61. package/lib/shapes/Path.d.ts +11 -2
  62. package/lib/shapes/Path.js +133 -143
  63. package/lib/shapes/Rect.d.ts +1 -0
  64. package/lib/shapes/Rect.js +4 -1
  65. package/lib/shapes/RegularPolygon.js +2 -2
  66. package/lib/shapes/Ring.d.ts +7 -0
  67. package/lib/shapes/Ring.js +8 -1
  68. package/lib/shapes/Sprite.d.ts +2 -1
  69. package/lib/shapes/Sprite.js +8 -9
  70. package/lib/shapes/Star.d.ts +6 -0
  71. package/lib/shapes/Star.js +5 -1
  72. package/lib/shapes/Text.d.ts +16 -5
  73. package/lib/shapes/Text.js +134 -65
  74. package/lib/shapes/TextPath.d.ts +11 -3
  75. package/lib/shapes/TextPath.js +56 -27
  76. package/lib/shapes/Transformer.js +95 -96
  77. package/lib/shapes/Wedge.js +1 -1
  78. package/lib/types.d.ts +2 -2
  79. package/package.json +5 -20
@@ -39,36 +39,36 @@ export class Image extends Shape {
39
39
  this._setImageLoad();
40
40
  }
41
41
  _setImageLoad() {
42
+ var _a;
42
43
  const image = this.image();
43
- // check is image is already loaded
44
- if (image && image.complete) {
44
+ // A fresh `new Image()` with no src already reports complete === true,
45
+ // so it still needs the listener.
46
+ if (((image === null || image === void 0 ? void 0 : image.complete) && image.src) || (image === null || image === void 0 ? void 0 : image.readyState) >= 2) {
45
47
  return;
46
48
  }
47
- // check is video is already loaded
48
- if (image && image.readyState === 4) {
49
- return;
50
- }
51
- if (image && image['addEventListener']) {
52
- image['addEventListener']('load', this._loadListener);
53
- }
49
+ (_a = image === null || image === void 0 ? void 0 : image.addEventListener) === null || _a === void 0 ? void 0 : _a.call(image, 'videoWidth' in image ? 'loadeddata' : 'load', this._loadListener);
54
50
  }
55
51
  _removeImageLoad(image) {
56
- if (image && image['removeEventListener']) {
57
- image['removeEventListener']('load', this._loadListener);
58
- }
52
+ var _a;
53
+ (_a = image === null || image === void 0 ? void 0 : image.removeEventListener) === null || _a === void 0 ? void 0 : _a.call(image, 'videoWidth' in image ? 'loadeddata' : 'load', this._loadListener);
59
54
  }
60
55
  destroy() {
61
56
  this._removeImageLoad(this.image());
62
57
  super.destroy();
63
58
  return this;
64
59
  }
65
- _useBufferCanvas() {
60
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
61
+ if (this.attrs.perfectDrawEnabled === false)
62
+ return false;
66
63
  const hasCornerRadius = !!this.cornerRadius();
67
64
  const hasShadow = this.hasShadow();
68
65
  if (hasCornerRadius && hasShadow) {
69
66
  return true;
70
67
  }
71
- return super._useBufferCanvas(true);
68
+ return super._useBufferCanvas(true, opacity);
69
+ }
70
+ _getStrokePadding() {
71
+ return super._getStrokePadding(this.strokeScaleEnabled() ? 1 : undefined);
72
72
  }
73
73
  _sceneFunc(context) {
74
74
  const width = this.getWidth();
@@ -112,11 +112,13 @@ export class Image extends Shape {
112
112
  }
113
113
  getWidth() {
114
114
  var _a, _b, _c;
115
- return (_c = (_a = this.attrs.width) !== null && _a !== void 0 ? _a : (_b = this.image()) === null || _b === void 0 ? void 0 : _b.width) !== null && _c !== void 0 ? _c : 0;
115
+ const image = this.image();
116
+ return (_c = (_b = (_a = this.attrs.width) !== null && _a !== void 0 ? _a : image === null || image === void 0 ? void 0 : image.videoWidth) !== null && _b !== void 0 ? _b : image === null || image === void 0 ? void 0 : image.width) !== null && _c !== void 0 ? _c : 0;
116
117
  }
117
118
  getHeight() {
118
119
  var _a, _b, _c;
119
- return (_c = (_a = this.attrs.height) !== null && _a !== void 0 ? _a : (_b = this.image()) === null || _b === void 0 ? void 0 : _b.height) !== null && _c !== void 0 ? _c : 0;
120
+ const image = this.image();
121
+ return (_c = (_b = (_a = this.attrs.height) !== null && _a !== void 0 ? _a : image === null || image === void 0 ? void 0 : image.videoHeight) !== null && _b !== void 0 ? _b : image === null || image === void 0 ? void 0 : image.height) !== null && _c !== void 0 ? _c : 0;
120
122
  }
121
123
  /**
122
124
  * load image from given url and create `Konva.Image` instance
@@ -133,11 +135,9 @@ export class Image extends Shape {
133
135
  */
134
136
  static fromURL(url, callback, onError = null) {
135
137
  const img = Util.createImageElement();
136
- img.onload = function () {
137
- const image = new Image({
138
- image: img,
139
- });
140
- callback(image);
138
+ img.onload = () => {
139
+ img.onload = img.onerror = null;
140
+ callback(new Image({ image: img }));
141
141
  };
142
142
  img.onerror = onError;
143
143
  img.crossOrigin = 'Anonymous';
@@ -146,7 +146,7 @@ export class Image extends Shape {
146
146
  }
147
147
  Image.prototype.className = 'Image';
148
148
  Image.prototype._attrsAffectingSize = ['image'];
149
- _registerNode(Image);
149
+ _registerNode(Image, true);
150
150
  /**
151
151
  * get/set corner radius
152
152
  * @method
@@ -4,7 +4,7 @@ import { Group } from '../Group.ts';
4
4
  import type { Context } from '../Context.ts';
5
5
  import type { ContainerConfig } from '../Container.ts';
6
6
  import type { GetSet } from '../types.ts';
7
- import type { Text } from './Text.ts';
7
+ import { Text } from './Text.ts';
8
8
  export interface LabelConfig extends ContainerConfig {
9
9
  }
10
10
  declare const NONE = "none";
@@ -28,7 +28,7 @@ declare const NONE = "none";
28
28
  * stroke: '#333',
29
29
  * shadowColor: 'black',
30
30
  * shadowBlur: 10,
31
- * shadowOffset: [10, 10],
31
+ * shadowOffset: { x: 10, y: 10 },
32
32
  * shadowOpacity: 0.2,
33
33
  * lineJoin: 'round',
34
34
  * pointerDirection: 'up',
@@ -64,7 +64,6 @@ export declare class Label extends Group {
64
64
  * @method
65
65
  */
66
66
  getTag(): Tag;
67
- _addListeners(text: any): void;
68
67
  getWidth(): number;
69
68
  getHeight(): number;
70
69
  _sync(): void;
@@ -4,22 +4,16 @@ import { Shape } from "../Shape.js";
4
4
  import { Group } from "../Group.js";
5
5
  import { getNumberOrArrayOfNumbersValidator, getNumberValidator, } from "../Validators.js";
6
6
  import { _registerNode } from "../Global.js";
7
+ import { Text } from "./Text.js";
7
8
  // constants
8
9
  const ATTR_CHANGE_LIST = [
9
- 'fontFamily',
10
- 'fontSize',
11
- 'fontStyle',
12
- 'padding',
13
- 'lineHeight',
14
- 'text',
10
+ ...Text.prototype._attrsAffectingSize,
15
11
  'width',
16
12
  'height',
17
13
  'pointerDirection',
18
14
  'pointerWidth',
19
15
  'pointerHeight',
20
- ], CHANGE_KONVA = 'Change.konva', NONE = 'none', UP = 'up', RIGHT = 'right', DOWN = 'down', LEFT = 'left',
21
- // cached variables
22
- attrChangeListLen = ATTR_CHANGE_LIST.length;
16
+ ], CHANGE_EVENTS = ATTR_CHANGE_LIST.map((attr) => attr + 'Change.konva').join(' '), NONE = 'none', UP = 'up', RIGHT = 'right', DOWN = 'down', LEFT = 'left';
23
17
  /**
24
18
  * Label constructor.  Labels are groups that contain a Text and Tag shape
25
19
  * @constructor
@@ -40,7 +34,7 @@ attrChangeListLen = ATTR_CHANGE_LIST.length;
40
34
  * stroke: '#333',
41
35
  * shadowColor: 'black',
42
36
  * shadowBlur: 10,
43
- * shadowOffset: [10, 10],
37
+ * shadowOffset: { x: 10, y: 10 },
44
38
  * shadowOpacity: 0.2,
45
39
  * lineJoin: 'round',
46
40
  * pointerDirection: 'up',
@@ -61,8 +55,10 @@ attrChangeListLen = ATTR_CHANGE_LIST.length;
61
55
  export class Label extends Group {
62
56
  constructor(config) {
63
57
  super(config);
58
+ this._sync = this._sync.bind(this);
64
59
  this.on('add.konva', function (evt) {
65
- this._addListeners(evt.child);
60
+ // re-adding the same child must not stack another listener
61
+ evt.child.off(CHANGE_EVENTS, this._sync).on(CHANGE_EVENTS, this._sync);
66
62
  this._sync();
67
63
  });
68
64
  }
@@ -86,16 +82,6 @@ export class Label extends Group {
86
82
  getTag() {
87
83
  return this.find('Tag')[0];
88
84
  }
89
- _addListeners(text) {
90
- let that = this, n;
91
- const func = function () {
92
- that._sync();
93
- };
94
- // update text data for certain attr changes
95
- for (n = 0; n < attrChangeListLen; n++) {
96
- text.on(ATTR_CHANGE_LIST[n] + CHANGE_KONVA, func);
97
- }
98
- }
99
85
  getWidth() {
100
86
  return this.getText().width();
101
87
  }
@@ -195,31 +181,29 @@ export class Tag extends Shape {
195
181
  context.fillStrokeShape(this);
196
182
  }
197
183
  getSelfRect() {
198
- let x = 0, y = 0, pointerWidth = this.pointerWidth(), pointerHeight = this.pointerHeight(), direction = this.pointerDirection(), width = this.width(), height = this.height();
199
- if (direction === UP) {
200
- y -= pointerHeight;
201
- height += pointerHeight;
202
- }
203
- else if (direction === DOWN) {
204
- height += pointerHeight;
205
- }
206
- else if (direction === LEFT) {
207
- x -= pointerWidth;
208
- width += pointerWidth;
184
+ const pointerWidth = this.pointerWidth(), pointerHeight = this.pointerHeight(), direction = this.pointerDirection(), width = this.width(), height = this.height();
185
+ // The pointer base is centred on its side and can be longer than it.
186
+ if (direction === UP || direction === DOWN) {
187
+ return {
188
+ x: Math.min(0, (width - pointerWidth) / 2),
189
+ y: direction === UP ? -pointerHeight : 0,
190
+ width: Math.max(width, pointerWidth),
191
+ height: height + pointerHeight,
192
+ };
209
193
  }
210
- else if (direction === RIGHT) {
211
- width += pointerWidth;
194
+ if (direction === LEFT || direction === RIGHT) {
195
+ return {
196
+ x: direction === LEFT ? -pointerWidth : 0,
197
+ y: Math.min(0, (height - pointerHeight) / 2),
198
+ width: width + pointerWidth,
199
+ height: Math.max(height, pointerHeight),
200
+ };
212
201
  }
213
- return {
214
- x: x,
215
- y: y,
216
- width: width,
217
- height: height,
218
- };
202
+ return { x: 0, y: 0, width, height };
219
203
  }
220
204
  }
221
205
  Tag.prototype.className = 'Tag';
222
- _registerNode(Tag);
206
+ _registerNode(Tag, true);
223
207
  /**
224
208
  * get/set pointer direction
225
209
  * @name Konva.Tag#pointerDirection
@@ -43,6 +43,7 @@ export declare class Line<Config extends LineConfig = LineConfig> extends Shape<
43
43
  * The handlers take plain numbers, because this runs on every frame.
44
44
  */
45
45
  _eachTensionSegment(onQuadratic: (x0: number, y0: number, cpx: number, cpy: number, x: number, y: number) => void, onCubic: (x0: number, y0: number, cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number) => void): void;
46
+ _getStrokePadding(miterLimit?: number | undefined): number;
46
47
  _sceneFunc(context: Context): void;
47
48
  getTensionPoints(): any;
48
49
  _getTensionPoints(): number[];
@@ -53,7 +54,7 @@ export declare class Line<Config extends LineConfig = LineConfig> extends Shape<
53
54
  closed: GetSet<boolean, this>;
54
55
  bezier: GetSet<boolean, this>;
55
56
  tension: GetSet<number, this>;
56
- points: GetSet<number[], this, LinePoints>;
57
+ points: GetSet<number[], this, LinePoints | null | undefined>;
57
58
  }
58
59
  /**
59
60
  * get/set points array. Points is a flat array [x1, y1, x2, y2]. It is flat for performance reasons.
@@ -105,6 +105,9 @@ export class Line extends Shape {
105
105
  onQuadratic(x0, y0, tp[len - 2], tp[len - 1], points[length - 2], points[length - 1]);
106
106
  }
107
107
  }
108
+ _getStrokePadding(miterLimit = this.points().length === 4 && !this.closed() ? 1 : undefined) {
109
+ return super._getStrokePadding(miterLimit);
110
+ }
108
111
  _sceneFunc(context) {
109
112
  const points = this.points(), length = points.length, closed = this.closed(), bezier = this.bezier();
110
113
  if (!length) {
@@ -203,8 +206,8 @@ export class Line extends Shape {
203
206
  }
204
207
  }
205
208
  Line.prototype.className = 'Line';
206
- Line.prototype._attrsAffectingSize = ['points', 'bezier', 'tension'];
207
- _registerNode(Line);
209
+ Line.prototype._attrsAffectingSize = ['points', 'bezier', 'tension', 'closed'];
210
+ _registerNode(Line, true);
208
211
  // add getters setters
209
212
  Factory.addGetterSetter(Line, 'closed', false);
210
213
  /**
@@ -58,7 +58,10 @@ export declare class Path extends Shape<PathConfig> {
58
58
  data: GetSet<string, this>;
59
59
  static getLineLength(x1: any, y1: any, x2: any, y2: any): number;
60
60
  static getPathLength(dataArray: PathSegment[]): number;
61
- static getPointAtLengthOfDataArray(length: number, dataArray: PathSegment[]): {
61
+ static getPointAtLengthOfDataArray(length: number, dataArray: PathSegment[], cursor?: {
62
+ index: number;
63
+ offset: number;
64
+ }): {
62
65
  x: number;
63
66
  y: number;
64
67
  } | null;
@@ -79,7 +82,13 @@ export declare class Path extends Shape<PathConfig> {
79
82
  y: number;
80
83
  };
81
84
  static parsePathData(data: any): PathSegment[];
82
- static calcLength(x: any, y: any, cmd: any, points: any): any;
85
+ /**
86
+ * Walks an arc in one degree steps, accumulating its length. Returns the
87
+ * angle `length` along the arc, or its end angle and total length when
88
+ * `length` is past the end.
89
+ */
90
+ private static _walkArc;
91
+ static calcLength(x: any, y: any, cmd: any, points: any): number;
83
92
  static convertEndpointToCenterParameterization(x1: number, y1: number, x2: number, y2: number, fa: number, fs: number, rx: number, ry: number, psiDeg: number): number[];
84
93
  }
85
94
  /**