konva 10.4.0 → 10.6.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 (63) hide show
  1. package/README.md +4 -4
  2. package/konva.js +3432 -3746
  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 +1 -1
  9. package/lib/Canvas.js +8 -5
  10. package/lib/Container.js +9 -6
  11. package/lib/Context.d.ts +4 -5
  12. package/lib/Context.js +77 -72
  13. package/lib/DragAndDrop.d.ts +5 -3
  14. package/lib/DragAndDrop.js +65 -29
  15. package/lib/Factory.d.ts +3 -2
  16. package/lib/Factory.js +10 -9
  17. package/lib/Global.d.ts +0 -6
  18. package/lib/Global.js +2 -8
  19. package/lib/Layer.js +9 -6
  20. package/lib/Node.d.ts +33 -15
  21. package/lib/Node.js +179 -135
  22. package/lib/PointerEvents.d.ts +1 -2
  23. package/lib/PointerEvents.js +20 -18
  24. package/lib/Shape.d.ts +2 -2
  25. package/lib/Shape.js +51 -7
  26. package/lib/Stage.d.ts +23 -14
  27. package/lib/Stage.js +221 -93
  28. package/lib/Tween.js +59 -66
  29. package/lib/Util.d.ts +3 -2
  30. package/lib/Util.js +43 -16
  31. package/lib/_CoreInternals.d.ts +25 -11
  32. package/lib/_FullInternals.d.ts +25 -11
  33. package/lib/filters/Blur.js +16 -12
  34. package/lib/filters/Brighten.d.ts +1 -0
  35. package/lib/filters/Brighten.js +1 -0
  36. package/lib/filters/Brightness.d.ts +2 -1
  37. package/lib/filters/Brightness.js +4 -4
  38. package/lib/filters/Emboss.js +16 -58
  39. package/lib/filters/Kaleidoscope.js +8 -15
  40. package/lib/filters/Mask.js +8 -8
  41. package/lib/filters/Pixelate.js +16 -29
  42. package/lib/filters/Threshold.d.ts +1 -1
  43. package/lib/filters/Threshold.js +1 -1
  44. package/lib/shapes/Arc.js +5 -3
  45. package/lib/shapes/Arrow.d.ts +2 -6
  46. package/lib/shapes/Arrow.js +68 -46
  47. package/lib/shapes/Image.js +16 -19
  48. package/lib/shapes/Label.d.ts +2 -3
  49. package/lib/shapes/Label.js +7 -21
  50. package/lib/shapes/Line.d.ts +1 -1
  51. package/lib/shapes/Line.js +1 -1
  52. package/lib/shapes/Path.d.ts +27 -2
  53. package/lib/shapes/Path.js +148 -142
  54. package/lib/shapes/RegularPolygon.js +1 -1
  55. package/lib/shapes/Sprite.js +2 -6
  56. package/lib/shapes/Text.d.ts +12 -4
  57. package/lib/shapes/Text.js +96 -54
  58. package/lib/shapes/TextPath.d.ts +4 -3
  59. package/lib/shapes/TextPath.js +40 -26
  60. package/lib/shapes/Transformer.d.ts +16 -1
  61. package/lib/shapes/Transformer.js +209 -130
  62. package/lib/types.d.ts +2 -2
  63. package/package.json +4 -3
@@ -1,4 +1,3 @@
1
- import { Konva } from "./Global.js";
2
1
  import { Util } from "./Util.js";
3
2
  export const DD = {
4
3
  get isDragging() {
@@ -10,13 +9,14 @@ export const DD = {
10
9
  });
11
10
  return flag;
12
11
  },
13
- justDragged: false,
14
12
  get node() {
15
- // return first dragging node
16
13
  let node;
17
- DD._dragElements.forEach((elem) => {
14
+ for (const elem of DD._dragElements.values()) {
15
+ if (elem.dragStatus === 'dragging')
16
+ return elem.node;
18
17
  node = elem.node;
19
- });
18
+ }
19
+ // isDragReady() also uses this getter before movement starts.
20
20
  return node;
21
21
  },
22
22
  _dragElements: new Map(),
@@ -27,16 +27,38 @@ export const DD = {
27
27
  // window has pointer positions the stages of that window can use
28
28
  _listenToWindow(win) {
29
29
  const endDragBefore = (evt) => DD._endDragBefore(evt, win);
30
- const drag = (evt) => DD._drag(evt, win);
30
+ const drag = (evt) => DD._batchEvents(() => DD._drag(evt, win), win);
31
+ const endDragAfter = (evt) => DD._batchEvents(() => DD._endDragAfter(evt));
31
32
  win.addEventListener('mouseup', endDragBefore, true);
32
33
  win.addEventListener('touchend', endDragBefore, true);
33
34
  // add touchcancel to fix this: https://github.com/konvajs/konva/issues/1843
34
35
  win.addEventListener('touchcancel', endDragBefore, true);
35
36
  win.addEventListener('mousemove', drag);
36
37
  win.addEventListener('touchmove', drag);
37
- win.addEventListener('mouseup', DD._endDragAfter, false);
38
- win.addEventListener('touchend', DD._endDragAfter, false);
39
- win.addEventListener('touchcancel', DD._endDragAfter, false);
38
+ win.addEventListener('mouseup', endDragAfter, false);
39
+ win.addEventListener('touchend', endDragAfter, false);
40
+ win.addEventListener('touchcancel', endDragAfter, false);
41
+ },
42
+ _batchEvents(callback, win) {
43
+ if (!DD._dragElements.size)
44
+ return;
45
+ // One scope per integration, even when several stages use the same callback.
46
+ // Keep all positions and handlers in the original order across those stages.
47
+ const batches = new Set();
48
+ for (const { node } of DD._dragElements.values()) {
49
+ const stage = node.getStage();
50
+ if (!stage || (win && stage._getOwnerWindow() !== win))
51
+ continue;
52
+ const batch = stage.eventBatchFunc();
53
+ if (batch)
54
+ batches.add(batch);
55
+ }
56
+ let run = callback;
57
+ for (const batch of batches) {
58
+ const next = run;
59
+ run = () => Util._batchEvents(batch, next);
60
+ }
61
+ run();
40
62
  },
41
63
  // methods
42
64
  _drag(evt, win) {
@@ -56,10 +78,18 @@ export const DD = {
56
78
  stage.setPointersPositions(evt);
57
79
  positioned.add(stage);
58
80
  }
81
+ if (elem.pointerEventType &&
82
+ elem.pointerEventType !== stage._pointerEventType) {
83
+ return;
84
+ }
59
85
  // it is possible that user call startDrag without any event
60
86
  // it that case we need to detect first movable pointer and attach it into the node
61
87
  if (elem.pointerId === undefined) {
62
88
  elem.pointerId = Util._getFirstPointerId(evt);
89
+ elem.pointerEventType = stage._pointerEventType;
90
+ if (elem.dragStatus === 'dragging') {
91
+ stage._cancelClick(elem.pointerId, elem.pointerEventType);
92
+ }
63
93
  }
64
94
  const pos = stage._changedPointerPositions.find((pos) => pos.id === elem.pointerId);
65
95
  // not related pointer
@@ -96,11 +126,16 @@ export const DD = {
96
126
  },
97
127
  // dragBefore and dragAfter allows us to set correct order of events
98
128
  // setup all in dragbefore, and stop dragging only after pointerup triggered.
99
- _endDragBefore(evt, win) {
129
+ _endDragBefore(evt, win, only) {
100
130
  const drawNodes = [];
101
131
  const positioned = new Set();
102
132
  DD._dragElements.forEach((elem, key) => {
103
133
  const { node } = elem;
134
+ // node.stopDrag() ends only the drags of the same gesture: a Transformer
135
+ // drags its other nodes with the same pointer, another pointer is its own
136
+ if (only && elem !== only && elem.pointerId !== only.pointerId) {
137
+ return;
138
+ }
104
139
  // we need to find pointer relative to that node
105
140
  const stage = node.getStage();
106
141
  // a pointer released in another window ends the drag too - the pointer
@@ -111,31 +146,31 @@ export const DD = {
111
146
  stage.setPointersPositions(evt);
112
147
  positioned.add(stage);
113
148
  }
114
- // a drag started without an event (node.startDrag()) that has not
115
- // moved yet has no pointer: any release ends it
116
- const released = elem.pointerId === undefined ||
117
- stage._changedPointerPositions.some((pos) => pos.id === elem.pointerId);
118
- // that pointer is not related: a "ready" element of another pointer
119
- // waits for its own release
120
- if (!released) {
121
- return;
149
+ if (!only) {
150
+ if (evt &&
151
+ elem.pointerEventType &&
152
+ elem.pointerEventType !== Util._getEventType(evt.type)) {
153
+ return;
154
+ }
155
+ // a drag started without an event (node.startDrag()) that has not
156
+ // moved yet has no pointer: any release ends it
157
+ const released = elem.pointerId === undefined ||
158
+ stage._changedPointerPositions.some((pos) => pos.id === elem.pointerId);
159
+ // that pointer is not related: a "ready" element of another pointer
160
+ // waits for its own release
161
+ if (!released) {
162
+ return;
163
+ }
122
164
  }
123
165
  if (elem.dragStatus === 'ready') {
124
166
  DD._dragElements.delete(key);
125
167
  }
126
168
  else {
127
169
  // if a node is stopped manually we still need to reset events:
128
- DD.justDragged = true;
129
- Konva._mouseListenClick = false;
130
- Konva._touchListenClick = false;
131
- // for real pointer input this reset comes too late — "pointerup" fired
132
- // before this handler — so Stage._pointerup also checks the live drag
133
- // state for the pointer family (issue #1756)
134
- Konva._pointerListenClick = false;
170
+ stage._cancelClick(elem.pointerId, elem.pointerEventType);
135
171
  elem.dragStatus = 'stopped';
136
172
  // a node that has not moved needs no redraw
137
- const drawNode = elem.node.getLayer() ||
138
- (elem.node instanceof Konva['Stage'] && elem.node);
173
+ const drawNode = node.getLayer() || stage;
139
174
  if (drawNode && drawNodes.indexOf(drawNode) === -1) {
140
175
  drawNodes.push(drawNode);
141
176
  }
@@ -148,9 +183,10 @@ export const DD = {
148
183
  drawNode.draw();
149
184
  });
150
185
  },
151
- _endDragAfter(evt) {
186
+ _endDragAfter(evt, only) {
152
187
  DD._dragElements.forEach((elem, key) => {
153
- if (elem.dragStatus !== 'stopped') {
188
+ if (elem.dragStatus !== 'stopped' ||
189
+ (only && elem !== only && elem.pointerId !== only.pointerId)) {
154
190
  return;
155
191
  }
156
192
  elem.node.fire('dragend', {
package/lib/Factory.d.ts CHANGED
@@ -17,9 +17,10 @@ type Attr<T extends Constructor> = EnforceString<keyof InstanceType<T>>;
17
17
  */
18
18
  type AfterFunc<T extends Constructor> = (this: InstanceType<T>) => void;
19
19
  /**
20
- * Extracts the type of a GetSet.
20
+ * Extracts the type a GetSet returns. The type the setter accepts can be wider,
21
+ * so it is matched with `any` instead of being inferred from the getter.
21
22
  */
22
- type ExtractGetSet<T> = T extends GetSet<infer U, any> ? U : never;
23
+ type ExtractGetSet<T> = T extends GetSet<infer U, any, any> ? U : never;
23
24
  /**
24
25
  * Extracts the type of a GetSet class attribute.
25
26
  */
package/lib/Factory.js CHANGED
@@ -10,12 +10,15 @@ export const Factory = {
10
10
  },
11
11
  addGetter(constructor, attr, def) {
12
12
  const method = GET + Util._capitalize(attr);
13
- constructor.prototype[method] =
14
- constructor.prototype[method] ||
15
- function () {
16
- const val = this.attrs[attr];
17
- return val === undefined ? def : val;
18
- };
13
+ // array defaults are shared between instances, so hand out a copy
14
+ const isArr = Array.isArray(def);
15
+ const getter = function () {
16
+ const val = this.attrs[attr];
17
+ return val === undefined ? (isArr ? def.slice() : def) : val;
18
+ };
19
+ // toObject() compares an attr with the default of the generated getter
20
+ getter._def = def;
21
+ constructor.prototype[method] = constructor.prototype[method] || getter;
19
22
  },
20
23
  addSetter(constructor, attr, validator, after) {
21
24
  const method = SET + Util._capitalize(attr);
@@ -52,7 +55,7 @@ export const Factory = {
52
55
  const basicValidator = getComponentValidator(components);
53
56
  // setter
54
57
  constructor.prototype[setter] = function (val) {
55
- const oldVal = this.attrs[attr];
58
+ const oldVal = this[getter]();
56
59
  if (validator) {
57
60
  val = validator.call(this, val, attr);
58
61
  }
@@ -89,8 +92,6 @@ export const Factory = {
89
92
  // getting
90
93
  return this[getter]();
91
94
  };
92
- // marks the accessor for toObject()
93
- accessor._isAttrAccessor = true;
94
95
  constructor.prototype[attr] = accessor;
95
96
  },
96
97
  backCompat(constructor, methods) {
package/lib/Global.d.ts CHANGED
@@ -42,12 +42,6 @@ export declare const Konva: {
42
42
  * Konva.capturePointerEventsEnabled = true;
43
43
  */
44
44
  capturePointerEventsEnabled: boolean;
45
- _mouseListenClick: boolean;
46
- _touchListenClick: boolean;
47
- _pointerListenClick: boolean;
48
- _mouseInDblClickWindow: boolean;
49
- _touchInDblClickWindow: boolean;
50
- _pointerInDblClickWindow: boolean;
51
45
  _renderBackend: string;
52
46
  /**
53
47
  * Use legacy text rendering. with "middle" baseline by default.
package/lib/Global.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Konva JavaScript Framework v10.4.0
2
+ * Konva JavaScript Framework v10.6.0
3
3
  * http://konvajs.org/
4
4
  * Licensed under the MIT
5
5
  * Date: @@date
@@ -29,7 +29,7 @@ export const glob = typeof global !== 'undefined'
29
29
  : {};
30
30
  export const Konva = {
31
31
  _global: glob,
32
- version: '10.4.0',
32
+ version: '10.6.0',
33
33
  isBrowser: detectBrowser(),
34
34
  isUnminified: /param/.test(function (param) { }.toString()),
35
35
  dblClickWindow: 400,
@@ -72,12 +72,6 @@ export const Konva = {
72
72
  * Konva.capturePointerEventsEnabled = true;
73
73
  */
74
74
  capturePointerEventsEnabled: false,
75
- _mouseListenClick: false,
76
- _touchListenClick: false,
77
- _pointerListenClick: false,
78
- _mouseInDblClickWindow: false,
79
- _touchInDblClickWindow: false,
80
- _pointerInDblClickWindow: false,
81
75
  _renderBackend: 'web', // web, node-canvas, skia-canvas
82
76
  /**
83
77
  * Use legacy text rendering. with "middle" baseline by default.
package/lib/Layer.js CHANGED
@@ -177,11 +177,14 @@ export class Layer extends Container {
177
177
  return this;
178
178
  }
179
179
  remove() {
180
- const _canvas = this.getNativeCanvasElement();
181
- Node.prototype.remove.call(this);
182
- if (_canvas && _canvas.parentNode && Util._isInDocument(_canvas)) {
183
- _canvas.parentNode.removeChild(_canvas);
184
- }
180
+ var _a, _b;
181
+ super.remove();
182
+ // both canvases live in stage.content: the scene one always, the hit one
183
+ // while toggleHitCanvas() shows it
184
+ const scene = this.getNativeCanvasElement();
185
+ const hit = this.getHitCanvas()._canvas;
186
+ (_a = scene.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(scene);
187
+ (_b = hit.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(hit);
185
188
  return this;
186
189
  }
187
190
  getStage() {
@@ -398,7 +401,7 @@ export class Layer extends Container {
398
401
  let underDrag = false;
399
402
  DD._dragElements.forEach((elem) => {
400
403
  if (elem.dragStatus === 'dragging' &&
401
- (elem.node.nodeType === 'Stage' || elem.node.getLayer() === this)) {
404
+ (elem.node === this.getStage() || elem.node.getLayer() === this)) {
402
405
  underDrag = true;
403
406
  }
404
407
  });
package/lib/Node.d.ts CHANGED
@@ -8,7 +8,7 @@ import type { Stage } from './Stage.ts';
8
8
  import type { Tween, TweenConfig } from './Tween.ts';
9
9
  import type { GetSet, IRect, Vector2d } from './types.ts';
10
10
  import { Transform, type AnyString } from './Util.ts';
11
- export type FilterFunction = (this: Node, imageData: ImageData) => void;
11
+ export type FilterFunction = (this: Node, imageData: ImageData, pixelRatio?: number) => void;
12
12
  export type Filter = FilterFunction | string;
13
13
  type Filters = Array<FilterFunction | string>;
14
14
  type globalCompositeOperationType = '' | 'source-over' | 'source-in' | 'source-out' | 'source-atop' | 'destination-over' | 'destination-in' | 'destination-out' | 'destination-atop' | 'lighter' | 'copy' | 'xor' | 'multiply' | 'screen' | 'overlay' | 'darken' | 'lighten' | 'color-dodge' | 'color-burn' | 'hard-light' | 'soft-light' | 'difference' | 'exclusion' | 'hue' | 'saturation' | 'color' | 'luminosity';
@@ -73,6 +73,14 @@ export type ImageConfig = CanvasConfig & {
73
73
  mimeType?: string;
74
74
  quality?: number;
75
75
  };
76
+ /**
77
+ * Node constructor. Nodes are entities that can be transformed, layered,
78
+ * and have bound events. The stage, layers, groups, and shapes all extend Node.
79
+ * @constructor
80
+ * @memberof Konva
81
+ * @param {Object} config
82
+ * @@nodeParams
83
+ */
76
84
  export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
77
85
  _id: number;
78
86
  eventListeners: {
@@ -98,6 +106,7 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
98
106
  y: number;
99
107
  } | null;
100
108
  _attrsAffectingSize: string[];
109
+ _attrsVersion?: number;
101
110
  _batchingTransformChange: boolean;
102
111
  _needClearTransformCache: boolean;
103
112
  _filterUpToDate: boolean;
@@ -198,6 +207,8 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
198
207
  /**
199
208
  * Return client rectangle {x, y, width, height} of node. This rectangle also include all styling (strokes, shadows, etc).
200
209
  * The purpose of the method is similar to getBoundingClientRect API of the DOM.
210
+ * Non-scaling stroke padding is converted from its drawing canvas to the requested coordinates.
211
+ * Cached strokes use the cache's coordinates; their pixels scale with the cached bitmap.
201
212
  * @method
202
213
  * @name Konva.Node#getClientRect
203
214
  * @param {Object} config
@@ -696,8 +707,9 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
696
707
  * @param {Number} [config.width] width of canvas section
697
708
  * @param {Number} [config.height] height of canvas section
698
709
  * @param {Number} [config.pixelRatio] pixelRatio of output canvas. Default is 1.
699
- * You can use that property to increase quality of the image, for example for super hight quality exports
700
- * or usage on retina (or similar) displays. pixelRatio will be used to multiply the size of exported image.
710
+ * Higher pixel ratios increase export resolution. Cached nodes keep their cache resolution.
711
+ * Rebuild caches at the export pixel ratio to preserve detail.
712
+ * The pixel ratio multiplies the dimensions of the exported image.
701
713
  * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
702
714
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
703
715
  * @example
@@ -721,8 +733,9 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
721
733
  * you can specify the quality from 0 to 1, where 0 is very poor quality and 1
722
734
  * is very high quality
723
735
  * @param {Number} [config.pixelRatio] pixelRatio of output image url. Default is 1.
724
- * You can use that property to increase quality of the image, for example for super hight quality exports
725
- * or usage on retina (or similar) displays. pixelRatio will be used to multiply the size of exported image.
736
+ * Higher pixel ratios increase export resolution. Cached nodes keep their cache resolution.
737
+ * Rebuild caches at the export pixel ratio to preserve detail.
738
+ * The pixel ratio multiplies the dimensions of the exported image.
726
739
  * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
727
740
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
728
741
  * @returns {String}
@@ -749,8 +762,9 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
749
762
  * you can specify the quality from 0 to 1, where 0 is very poor quality and 1
750
763
  * is very high quality
751
764
  * @param {Number} [config.pixelRatio] pixelRatio of output image. Default is 1.
752
- * You can use that property to increase quality of the image, for example for super hight quality exports
753
- * or usage on retina (or similar) displays. pixelRatio will be used to multiply the size of exported image.
765
+ * Higher pixel ratios increase export resolution. Cached nodes keep their cache resolution.
766
+ * Rebuild caches at the export pixel ratio to preserve detail.
767
+ * The pixel ratio multiplies the dimensions of the exported image.
754
768
  * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
755
769
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
756
770
  * @return {Promise<Image>}
@@ -777,17 +791,18 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
777
791
  * @param {Number} [config.width] width of canvas section
778
792
  * @param {Number} [config.height] height of canvas section
779
793
  * @param {Number} [config.pixelRatio] pixelRatio of output canvas. Default is 1.
780
- * You can use that property to increase quality of the image, for example for super hight quality exports
781
- * or usage on retina (or similar) displays. pixelRatio will be used to multiply the size of exported image.
794
+ * Higher pixel ratios increase export resolution. Cached nodes keep their cache resolution.
795
+ * Rebuild caches at the export pixel ratio to preserve detail.
796
+ * The pixel ratio multiplies the dimensions of the exported image.
782
797
  * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
783
798
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
784
799
  * @example
785
800
  * var blob = await node.toBlob({});
786
- * @returns {Promise<Blob>}
801
+ * @returns {Promise<Blob>} rejects if the canvas can not be encoded
787
802
  */
788
803
  toBlob(config?: ImageConfig & {
789
- callback?: (blob: Blob | null) => void;
790
- }): Promise<unknown>;
804
+ callback?: (blob: Blob) => void;
805
+ }): Promise<Blob>;
791
806
  setSize(size: any): this;
792
807
  getSize(): {
793
808
  width: number;
@@ -942,12 +957,15 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
942
957
  embossWhiteLevel: GetSet<number, this>;
943
958
  enhance: GetSet<number, this>;
944
959
  filters: GetSet<Filters, this>;
945
- position: GetSet<Vector2d, this>;
946
- absolutePosition: GetSet<Vector2d, this>;
960
+ position: GetSet<Vector2d, this, Vector2d>;
961
+ absolutePosition: GetSet<Vector2d, this, Vector2d>;
947
962
  size: GetSet<{
948
963
  width: number;
949
964
  height: number;
950
- }, this>;
965
+ }, this, {
966
+ width: number;
967
+ height: number;
968
+ }>;
951
969
  id: GetSet<string, this>;
952
970
  listening: GetSet<boolean, this>;
953
971
  name: GetSet<string, this>;