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.
@@ -15,6 +15,7 @@ export declare const DD: {
15
15
  readonly node: Node<import("./Node.ts").NodeConfig> | undefined;
16
16
  _dragElements: Map<number, DragElement>;
17
17
  _listenToWindow(win: Window): void;
18
+ _batchEvents(callback: () => void, win?: Window): void;
18
19
  _drag(evt: any, win?: Window): void;
19
20
  _endDragBefore(evt?: any, win?: Window): void;
20
21
  _endDragAfter(evt: any): void;
@@ -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) {
package/lib/Global.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Konva JavaScript Framework v10.4.0
2
+ * Konva JavaScript Framework v10.5.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.5.0',
33
33
  isBrowser: detectBrowser(),
34
34
  isUnminified: /param/.test(function (param) { }.toString()),
35
35
  dblClickWindow: 400,
package/lib/Node.d.ts CHANGED
@@ -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;
@@ -783,11 +792,11 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
783
792
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
784
793
  * @example
785
794
  * var blob = await node.toBlob({});
786
- * @returns {Promise<Blob>}
795
+ * @returns {Promise<Blob>} rejects if the canvas can not be encoded
787
796
  */
788
797
  toBlob(config?: ImageConfig & {
789
- callback?: (blob: Blob | null) => void;
790
- }): Promise<unknown>;
798
+ callback?: (blob: Blob) => void;
799
+ }): Promise<Blob>;
791
800
  setSize(size: any): this;
792
801
  getSize(): {
793
802
  width: number;
package/lib/Node.js CHANGED
@@ -104,14 +104,6 @@ const ABSOLUTE_OPACITY = 'absoluteOpacity', ABSOLUTE_TRANSFORM = 'absoluteTransf
104
104
  'transformsEnabledChange.konva',
105
105
  ].join(SPACE);
106
106
  let idCounter = 1;
107
- /**
108
- * Node constructor. Nodes are entities that can be transformed, layered,
109
- * and have bound events. The stage, layers, groups, and shapes all extend Node.
110
- * @constructor
111
- * @memberof Konva
112
- * @param {Object} config
113
- * @@nodeParams
114
- */
115
107
  // "perfect drawing" buffer for cache() and toCanvas(): it mirrors the target
116
108
  // canvas and is drawn back at (x, y) in the target's coordinate space.
117
109
  // It starts empty and Shape.drawScene sizes it on first use, so nodes
@@ -126,6 +118,14 @@ function createBufferCanvas(target, x, y) {
126
118
  bufferCanvas.y = y;
127
119
  return bufferCanvas;
128
120
  }
121
+ /**
122
+ * Node constructor. Nodes are entities that can be transformed, layered,
123
+ * and have bound events. The stage, layers, groups, and shapes all extend Node.
124
+ * @constructor
125
+ * @memberof Konva
126
+ * @param {Object} config
127
+ * @@nodeParams
128
+ */
129
129
  export class Node {
130
130
  constructor(config) {
131
131
  this._id = idCounter++;
@@ -166,6 +166,10 @@ export class Node {
166
166
  }
167
167
  else {
168
168
  this._cache = {};
169
+ // clearCache() also reaches descendants whose ancestor transform changes
170
+ // were suppressed while cached. Invalidate their saved Transformer bounds.
171
+ if (this._attrsVersion !== undefined)
172
+ this._attrsVersion++;
169
173
  }
170
174
  }
171
175
  // drop the cached canvases and give their memory back
@@ -1842,7 +1846,7 @@ export class Node {
1842
1846
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
1843
1847
  * @example
1844
1848
  * var blob = await node.toBlob({});
1845
- * @returns {Promise<Blob>}
1849
+ * @returns {Promise<Blob>} rejects if the canvas can not be encoded
1846
1850
  */
1847
1851
  toBlob(config) {
1848
1852
  return new Promise((resolve, reject) => {
@@ -1851,6 +1855,11 @@ export class Node {
1851
1855
  if (callback)
1852
1856
  delete config.callback;
1853
1857
  this.toCanvas(config).toBlob((blob) => {
1858
+ // the canvas reports a failed encode by passing null
1859
+ if (!blob) {
1860
+ reject(new Error('Konva: toBlob() failed, the canvas was not encoded'));
1861
+ return;
1862
+ }
1854
1863
  resolve(blob);
1855
1864
  callback === null || callback === void 0 ? void 0 : callback(blob);
1856
1865
  }, config === null || config === void 0 ? void 0 : config.mimeType, config === null || config === void 0 ? void 0 : config.quality);
@@ -2030,6 +2039,8 @@ export class Node {
2030
2039
  else {
2031
2040
  this.attrs[key] = val;
2032
2041
  }
2042
+ if (this._attrsVersion !== undefined)
2043
+ this._attrsVersion++;
2033
2044
  if (this._shouldFireChangeEvents) {
2034
2045
  this._fireChangeEvent(key, oldVal, val);
2035
2046
  }
package/lib/Stage.d.ts CHANGED
@@ -6,6 +6,7 @@ import type { Shape } from './Shape.ts';
6
6
  import type { Layer } from './Layer.ts';
7
7
  export interface StageConfig extends ContainerConfig {
8
8
  container?: HTMLDivElement | string;
9
+ eventBatchFunc?: (callback: () => void) => void;
9
10
  }
10
11
  export declare const stages: Stage[];
11
12
  /**
@@ -121,6 +122,7 @@ export declare class Stage extends Container<Layer, StageConfig> {
121
122
  */
122
123
  getLayers(): Layer[];
123
124
  _bindContentEvents(): void;
125
+ _batchEvents(callback: () => void): void;
124
126
  _pointerenter(evt: PointerEvent): void;
125
127
  _pointerover(evt: any): void;
126
128
  _getTargetShape(evenType: any): Shape<import("./Shape.ts").ShapeConfig> | null;
@@ -130,7 +132,7 @@ export declare class Stage extends Container<Layer, StageConfig> {
130
132
  _pointerup(evt: any): void;
131
133
  _contextmenu(evt: any): void;
132
134
  _wheel(evt: any): void;
133
- _pointercancel(evt: PointerEvent): void;
135
+ _pointercancel(evt: PointerEvent | TouchEvent): void;
134
136
  _lostpointercapture(evt: PointerEvent): void;
135
137
  /**
136
138
  * manually register pointers positions (mouse/touch) in the stage.
@@ -165,4 +167,5 @@ export declare class Stage extends Container<Layer, StageConfig> {
165
167
  */
166
168
  batchDraw(): this;
167
169
  container: GetSet<HTMLDivElement, this, HTMLDivElement | string>;
170
+ eventBatchFunc: GetSet<((callback: () => void) => void) | undefined, this>;
168
171
  }
package/lib/Stage.js CHANGED
@@ -410,10 +410,13 @@ export class Stage extends Container {
410
410
  }
411
411
  EVENTS.forEach(([event, methodName]) => {
412
412
  this.content.addEventListener(event, (evt) => {
413
- this[methodName](evt);
413
+ this._batchEvents(() => this[methodName](evt));
414
414
  }, { passive: false });
415
415
  });
416
416
  }
417
+ _batchEvents(callback) {
418
+ Util._batchEvents(this.eventBatchFunc(), callback);
419
+ }
417
420
  _pointerenter(evt) {
418
421
  this.setPointersPositions(evt);
419
422
  const events = getEventsMap(evt.type);
@@ -722,12 +725,25 @@ export class Stage extends Container {
722
725
  }
723
726
  _pointercancel(evt) {
724
727
  this.setPointersPositions(evt);
725
- const shape = PointerEvents.getCapturedShape(evt.pointerId) ||
726
- this.getIntersection(this.getPointerPosition());
727
- if (shape) {
728
- shape._fireAndBubble(POINTERUP, PointerEvents.createEvent(evt));
729
- }
730
- PointerEvents.releaseCapture(evt.pointerId);
728
+ const events = getEventsMap(evt.type);
729
+ this._changedPointerPositions.forEach((pos) => {
730
+ const shape = PointerEvents.getCapturedShape(pos.id) || this.getIntersection(pos);
731
+ const event = { evt, pointerId: pos.id };
732
+ if (shape) {
733
+ if (events)
734
+ shape._fireAndBubble(events.pointercancel, { ...event });
735
+ // Keep the historical pointerup notification for cancellation listeners.
736
+ shape._fireAndBubble(POINTERUP, { ...event });
737
+ }
738
+ else if (events) {
739
+ this._fire(events.pointercancel, {
740
+ ...event,
741
+ target: this,
742
+ currentTarget: this,
743
+ });
744
+ }
745
+ PointerEvents.releaseCapture(pos.id);
746
+ });
731
747
  }
732
748
  _lostpointercapture(evt) {
733
749
  PointerEvents.releaseCapture(evt.pointerId);
@@ -887,3 +903,19 @@ _registerNode(Stage);
887
903
  * stage.container(popout.document.body);
888
904
  */
889
905
  Factory.addGetterSetter(Stage, 'container');
906
+ /**
907
+ * Get/set the callback used by framework integrations to batch native input.
908
+ * The callback must invoke its argument exactly once, synchronously. Existing
909
+ * node events keep their order and cancellation behavior. Direct node.fire()
910
+ * and programmatic mutations do not create a batch.
911
+ * Missing calls warn; duplicate or late calls warn and are ignored. Exceptions
912
+ * propagate and skipped input is not replayed, so an invalid callback can
913
+ * prevent input processing and drag cleanup.
914
+ * Each native Konva listener creates its own scope; a DOM event may invoke
915
+ * several listeners. Batching does not reorder or merge those listeners.
916
+ * @method
917
+ * @name Konva.Stage#eventBatchFunc
918
+ * @example
919
+ * stage.eventBatchFunc((run) => frameworkBatch(run));
920
+ */
921
+ Factory.addGetterSetter(Stage, 'eventBatchFunc');
package/lib/Util.d.ts CHANGED
@@ -258,6 +258,7 @@ export declare const Util: {
258
258
  throw(str: string): never;
259
259
  error(str: string): void;
260
260
  warn(str: string): void;
261
+ _batchEvents(batch: ((run: () => void) => void) | undefined, run: () => void): void;
261
262
  each(obj: object, func: Function): void;
262
263
  _inRange(val: number, left: number, right: number): boolean;
263
264
  _getProjectionToSegment(x1: any, y1: any, x2: any, y2: any, x3: any, y3: any): any[];
package/lib/Util.js CHANGED
@@ -968,6 +968,28 @@ export const Util = {
968
968
  }
969
969
  console.warn(KONVA_WARNING + str);
970
970
  },
971
+ _batchEvents(batch, run) {
972
+ if (!batch)
973
+ return run();
974
+ let called = false;
975
+ let active = true;
976
+ try {
977
+ batch(() => {
978
+ if (called || !active) {
979
+ Util.warn('eventBatchFunc must call its argument exactly once, synchronously.');
980
+ return;
981
+ }
982
+ called = true;
983
+ run();
984
+ });
985
+ }
986
+ finally {
987
+ active = false;
988
+ if (!called) {
989
+ Util.warn('eventBatchFunc must call its argument synchronously.');
990
+ }
991
+ }
992
+ },
971
993
  each(obj, func) {
972
994
  for (const key in obj) {
973
995
  func(key, obj[key]);
@@ -126,6 +126,7 @@ export declare const Konva: {
126
126
  throw(str: string): never;
127
127
  error(str: string): void;
128
128
  warn(str: string): void;
129
+ _batchEvents(batch: ((run: () => void) => void) | undefined, run: () => void): void;
129
130
  each(obj: object, func: Function): void;
130
131
  _inRange(val: number, left: number, right: number): boolean;
131
132
  _getProjectionToSegment(x1: any, y1: any, x2: any, y2: any, x3: any, y3: any): any[];
@@ -161,6 +162,7 @@ export declare const Konva: {
161
162
  dragStatus: "ready" | "dragging" | "stopped";
162
163
  }>;
163
164
  _listenToWindow(win: Window): void;
165
+ _batchEvents(callback: () => void, win?: Window): void;
164
166
  _drag(evt: any, win?: Window): void;
165
167
  _endDragBefore(evt?: any, win?: Window): void;
166
168
  _endDragAfter(evt: any): void;
@@ -132,6 +132,7 @@ export declare const Konva: {
132
132
  throw(str: string): never;
133
133
  error(str: string): void;
134
134
  warn(str: string): void;
135
+ _batchEvents(batch: ((run: () => void) => void) | undefined, run: () => void): void;
135
136
  each(obj: object, func: Function): void;
136
137
  _inRange(val: number, left: number, right: number): boolean;
137
138
  _getProjectionToSegment(x1: any, y1: any, x2: any, y2: any, x3: any, y3: any): any[];
@@ -167,6 +168,7 @@ export declare const Konva: {
167
168
  dragStatus: "ready" | "dragging" | "stopped";
168
169
  }>;
169
170
  _listenToWindow(win: Window): void;
171
+ _batchEvents(callback: () => void, win?: Window): void;
170
172
  _drag(evt: any, win?: Window): void;
171
173
  _endDragBefore(evt?: any, win?: Window): void;
172
174
  _endDragAfter(evt: any): void;
@@ -82,3 +82,19 @@ export declare class Path extends Shape<PathConfig> {
82
82
  static calcLength(x: any, y: any, cmd: any, points: any): any;
83
83
  static convertEndpointToCenterParameterization(x1: number, y1: number, x2: number, y2: number, fa: number, fs: number, rx: number, ry: number, psiDeg: number): number[];
84
84
  }
85
+ /**
86
+ * get width of the path. It is computed from the path data and cannot be set
87
+ * @name Konva.Path#width
88
+ * @method
89
+ * @returns {Number}
90
+ * @example
91
+ * var width = path.width();
92
+ */
93
+ /**
94
+ * get height of the path. It is computed from the path data and cannot be set
95
+ * @name Konva.Path#height
96
+ * @method
97
+ * @returns {Number}
98
+ * @example
99
+ * var height = path.height();
100
+ */
@@ -731,3 +731,19 @@ _registerNode(Path);
731
731
  * path.data('M200,100h100v50z');
732
732
  */
733
733
  Factory.addGetterSetter(Path, 'data');
734
+ /**
735
+ * get width of the path. It is computed from the path data and cannot be set
736
+ * @name Konva.Path#width
737
+ * @method
738
+ * @returns {Number}
739
+ * @example
740
+ * var width = path.width();
741
+ */
742
+ /**
743
+ * get height of the path. It is computed from the path data and cannot be set
744
+ * @name Konva.Path#height
745
+ * @method
746
+ * @returns {Number}
747
+ * @example
748
+ * var height = path.height();
749
+ */
@@ -85,6 +85,17 @@ export interface TransformerConfig extends ContainerConfig {
85
85
  */
86
86
  export declare class Transformer extends Group {
87
87
  _nodes: Array<Node>;
88
+ _nodeRectCache?: Map<Node, {
89
+ rotation: number;
90
+ ignoreStroke: boolean;
91
+ version: number;
92
+ width: number;
93
+ height: number;
94
+ minX: number;
95
+ minY: number;
96
+ maxX: number;
97
+ maxY: number;
98
+ }>;
88
99
  _movingAnchorName: string | null;
89
100
  _pointerId?: number;
90
101
  _anchors: Record<string, Rect>;
@@ -98,6 +109,7 @@ export declare class Transformer extends Group {
98
109
  _cursorChange: boolean;
99
110
  _elementsCreated: boolean;
100
111
  _updateScheduled: boolean;
112
+ _lastNodeRect?: Readonly<Box>;
101
113
  static isTransforming: () => boolean;
102
114
  static _isLayerTransforming(layer: Node): boolean;
103
115
  constructor(config?: TransformerConfig);
@@ -147,7 +159,7 @@ export declare class Transformer extends Group {
147
159
  * console.log('transform started');
148
160
  * });
149
161
  */
150
- _resetTransformCache(): void;
162
+ _resetTransformCache(changedNode?: Node): void;
151
163
  _getNodeRect(): any;
152
164
  __getNodeRect(): {
153
165
  x: number;
@@ -165,6 +177,7 @@ export declare class Transformer extends Group {
165
177
  _createBack(): void;
166
178
  _handleMouseDown(e: any): void;
167
179
  _handleMouseMove(e: any): void;
180
+ _moveTransform(e: any): void;
168
181
  _handleMouseUp(e: any): void;
169
182
  getAbsoluteTransform(top?: Node | null): Transform;
170
183
  _removeEvents(e?: any): void;
@@ -179,6 +192,8 @@ export declare class Transformer extends Group {
179
192
  */
180
193
  forceUpdate(): void;
181
194
  update(): void;
195
+ _update(): void;
196
+ _updateElements(attrs: Box): void;
182
197
  /**
183
198
  * determine if transformer is in active transform
184
199
  * @method