konva 10.7.1 → 10.9.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.
package/lib/Animation.js CHANGED
@@ -1,5 +1,6 @@
1
1
  var _a;
2
2
  import { glob } from "./Global.js";
3
+ import { Node } from "./Node.js";
3
4
  import { Util } from "./Util.js";
4
5
  const now = ((_a = glob.performance) === null || _a === void 0 ? void 0 : _a.now)
5
6
  ? () => glob.performance.now()
@@ -128,16 +129,18 @@ export class Animation {
128
129
  const layersToDraw = new Set();
129
130
  // an animation may stop or restart itself, or another one, inside its
130
131
  // function: each one runs at most once per frame, a stopped one not at all
131
- Array.from(this.animations).forEach((anim) => {
132
- if (!this.animations.has(anim)) {
133
- return;
134
- }
135
- anim._updateFrameObject(now());
136
- // the function returns false to skip the redraw
137
- if (anim.func && anim.func.call(anim, anim.frame) === false) {
138
- return;
139
- }
140
- anim.layers.forEach((layer) => layer && layersToDraw.add(layer));
132
+ Node._updateBatch(() => {
133
+ Array.from(this.animations).forEach((anim) => {
134
+ if (!this.animations.has(anim)) {
135
+ return;
136
+ }
137
+ anim._updateFrameObject(now());
138
+ // the function returns false to skip the redraw
139
+ if (anim.func && anim.func.call(anim, anim.frame) === false) {
140
+ return;
141
+ }
142
+ anim.layers.forEach((layer) => layer && layersToDraw.add(layer));
143
+ });
141
144
  });
142
145
  layersToDraw.forEach((layer) => layer.batchDraw());
143
146
  }
@@ -61,6 +61,7 @@ export declare abstract class Container<ChildType extends Node = Node, Config ex
61
61
  * @name Konva.Container#destroyChildren
62
62
  */
63
63
  destroyChildren(): this;
64
+ _clearChildren(dispose: 'remove' | 'destroy'): this;
64
65
  abstract _validateAdd(node: Node): void;
65
66
  /**
66
67
  * add a child and children into container
package/lib/Container.js CHANGED
@@ -52,17 +52,7 @@ export class Container extends Node {
52
52
  * @name Konva.Container#removeChildren
53
53
  */
54
54
  removeChildren() {
55
- this.children.forEach((child) => {
56
- // reset parent to prevent many _setChildrenIndices calls
57
- child.parent = null;
58
- child.index = 0;
59
- child.remove();
60
- });
61
- this.children = [];
62
- // because all children were detached from parent, request draw via container
63
- this._notifySubtreeChange();
64
- this._requestDraw();
65
- return this;
55
+ return this._clearChildren('remove');
66
56
  }
67
57
  /**
68
58
  * destroy all children nodes.
@@ -70,17 +60,22 @@ export class Container extends Node {
70
60
  * @name Konva.Container#destroyChildren
71
61
  */
72
62
  destroyChildren() {
73
- this.children.forEach((child) => {
74
- // reset parent to prevent many _setChildrenIndices calls
75
- child.parent = null;
76
- child.index = 0;
77
- child.destroy();
63
+ return this._clearChildren('destroy');
64
+ }
65
+ _clearChildren(dispose) {
66
+ return Node._updateBatch(() => {
67
+ this.children.forEach((child) => {
68
+ // reset parent to prevent many _setChildrenIndices calls
69
+ child.parent = null;
70
+ child.index = 0;
71
+ child[dispose]();
72
+ });
73
+ this.children = [];
74
+ // because all children were detached from parent, request draw via container
75
+ this._notifySubtreeChange();
76
+ this._requestDraw();
77
+ return this;
78
78
  });
79
- this.children = [];
80
- // because all children were detached from parent, request draw via container
81
- this._notifySubtreeChange();
82
- this._requestDraw();
83
- return this;
84
79
  }
85
80
  /**
86
81
  * add a child and children into container
@@ -285,12 +280,12 @@ export class Container extends Node {
285
280
  }
286
281
  _clearSelfAndDescendantCache(attr) {
287
282
  var _a;
288
- // Perf: bracket absoluteTransform cascades with a depth counter so
289
- // Node._runAfterAbsTransformCascade can defer work until the outermost
290
- // walk returns. try/finally so depth/flush survive listener exceptions.
283
+ // Perf: an absoluteTransform cascade is an update batch, so a listener on
284
+ // one of N descendants defers its work until the outermost walk returns.
285
+ // try/finally so depth/flush survive listener exceptions.
291
286
  const isAbsTransform = attr === undefined || attr === 'absoluteTransform';
292
287
  if (isAbsTransform)
293
- Node._absTransformCascadeDepth++;
288
+ Node._updateBatchDepth++;
294
289
  try {
295
290
  super._clearSelfAndDescendantCache(attr);
296
291
  // a cached container is drawn from its bitmap, so its descendants keep
@@ -303,13 +298,8 @@ export class Container extends Node {
303
298
  });
304
299
  }
305
300
  finally {
306
- if (isAbsTransform && --Node._absTransformCascadeDepth === 0) {
307
- const callbacks = Node._pendingAfterCascade;
308
- if (callbacks.length) {
309
- Node._pendingAfterCascade = [];
310
- for (let i = 0; i < callbacks.length; i++)
311
- callbacks[i]();
312
- }
301
+ if (isAbsTransform && --Node._updateBatchDepth === 0) {
302
+ Node._flushUpdateBatch();
313
303
  }
314
304
  }
315
305
  }
package/lib/Global.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Konva JavaScript Framework v10.7.1
2
+ * Konva JavaScript Framework v10.9.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.7.1',
32
+ version: '10.9.0',
33
33
  isBrowser: detectBrowser(),
34
34
  isUnminified: /param/.test(function (param) { }.toString()),
35
35
  dblClickWindow: 400,
package/lib/Node.d.ts CHANGED
@@ -139,9 +139,12 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
139
139
  y: number;
140
140
  } | null;
141
141
  _clearSelfAndDescendantCache(attr?: string): void;
142
- static _absTransformCascadeDepth: number;
143
- static _pendingAfterCascade: Array<() => void>;
144
- static _runAfterAbsTransformCascade(cb: () => void): void;
142
+ static _updateBatchDepth: number;
143
+ static _pendingUpdates: Set<() => void>;
144
+ static _runAfterUpdateBatch(cb: () => void): void;
145
+ static _flushUpdateBatch(): void;
146
+ static _updateBatch<T>(fn: () => T): T;
147
+ static _beginUpdateBatch(): () => void;
145
148
  /**
146
149
  * clear cached canvas
147
150
  * @method
package/lib/Node.js CHANGED
@@ -186,14 +186,44 @@ export class Node {
186
186
  this.fire('absoluteTransformChange');
187
187
  }
188
188
  }
189
- static _runAfterAbsTransformCascade(cb) {
190
- if (Node._absTransformCascadeDepth > 0) {
191
- Node._pendingAfterCascade.push(cb);
189
+ static _runAfterUpdateBatch(cb) {
190
+ if (Node._updateBatchDepth > 0) {
191
+ Node._pendingUpdates.add(cb);
192
192
  }
193
193
  else {
194
194
  cb();
195
195
  }
196
196
  }
197
+ static _flushUpdateBatch() {
198
+ if (Node._updateBatchDepth > 0)
199
+ return;
200
+ const callbacks = Node._pendingUpdates;
201
+ if (!callbacks.size)
202
+ return;
203
+ Node._pendingUpdates = new Set();
204
+ callbacks.forEach((callback) => callback());
205
+ }
206
+ static _updateBatch(fn) {
207
+ Node._updateBatchDepth++;
208
+ try {
209
+ return fn();
210
+ }
211
+ finally {
212
+ Node._updateBatchDepth--;
213
+ Node._flushUpdateBatch();
214
+ }
215
+ }
216
+ static _beginUpdateBatch() {
217
+ Node._updateBatchDepth++;
218
+ let finished = false;
219
+ return () => {
220
+ if (finished)
221
+ return;
222
+ finished = true;
223
+ Node._updateBatchDepth--;
224
+ Node._flushUpdateBatch();
225
+ };
226
+ }
197
227
  /**
198
228
  * clear cached canvas
199
229
  * @method
@@ -984,18 +1014,20 @@ export class Node {
984
1014
  func();
985
1015
  return;
986
1016
  }
987
- this._batchingTransformChange = true;
988
- try {
989
- func();
990
- }
991
- finally {
992
- this._batchingTransformChange = false;
993
- if (this._needClearTransformCache) {
994
- this._needClearTransformCache = false;
995
- this._clearCache(TRANSFORM);
996
- this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
1017
+ Node._updateBatch(() => {
1018
+ this._batchingTransformChange = true;
1019
+ try {
1020
+ func();
997
1021
  }
998
- }
1022
+ finally {
1023
+ this._batchingTransformChange = false;
1024
+ if (this._needClearTransformCache) {
1025
+ this._needClearTransformCache = false;
1026
+ this._clearCache(TRANSFORM);
1027
+ this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
1028
+ }
1029
+ }
1030
+ });
999
1031
  }
1000
1032
  setPosition(pos) {
1001
1033
  this._batchTransformChanges(() => {
@@ -2412,12 +2444,13 @@ export class Node {
2412
2444
  }
2413
2445
  // Number of registered observers. The ancestor walk is skipped while it is 0.
2414
2446
  Node._subtreeObserverCount = 0;
2415
- // Perf: lets a listener inside an absoluteTransform cascade (e.g. Konva
2416
- // .Transformer reacting to a stage drag fan-out across N attached nodes)
2417
- // defer work until the cascade ends — collapses O(N) listener-driven calls
2418
- // into one. Counter is mutated by Container's _clearSelfAndDescendantCache.
2419
- Node._absTransformCascadeDepth = 0;
2420
- Node._pendingAfterCascade = [];
2447
+ // Perf: while a batch is open, work that depends on many nodes (a Konva
2448
+ // .Transformer refresh) is queued and runs once when the outermost scope
2449
+ // ends, instead of once per node change. Scopes are the callers of
2450
+ // _updateBatch() and _beginUpdateBatch().
2451
+ Node._updateBatchDepth = 0;
2452
+ // a Set, so a callback queued once per change runs once per batch
2453
+ Node._pendingUpdates = new Set();
2421
2454
  // keyed by prototype: subclasses that share a nodeType (every shape class)
2422
2455
  // have their own listeners
2423
2456
  Node.protoListenerMap = new WeakMap();
package/lib/Shape.js CHANGED
@@ -372,12 +372,16 @@ export class Shape extends Node {
372
372
  const forDrawing = config._forDrawing;
373
373
  // Painted bounds are measured in the canvas that draws the shape
374
374
  // (relativeTo): non-scaling strokes and shadows use that canvas's axes.
375
- // The identity transform normalizes negative sizes.
376
- const fillRect = !forDrawing
377
- ? this.getSelfRect()
378
- : this.strokeScaleEnabled()
379
- ? new Transform()._getTransformedRect(this._getSelfRectForDrawing())
380
- : this._transformedRect(this._getSelfRectForDrawing(), relativeTo);
375
+ let fillRect = forDrawing
376
+ ? this._getSelfRectForDrawing()
377
+ : this.getSelfRect();
378
+ if (forDrawing && !this.strokeScaleEnabled()) {
379
+ fillRect = this._transformedRect(fillRect, relativeTo);
380
+ }
381
+ else if (fillRect.width < 0 || fillRect.height < 0) {
382
+ // The identity transform normalizes negative sizes.
383
+ fillRect = new Transform()._getTransformedRect(fillRect);
384
+ }
381
385
  const applyStroke = !config.skipStroke && this.hasStroke();
382
386
  const strokeWidth = applyStroke
383
387
  ? forDrawing
@@ -36,6 +36,32 @@ export declare const Konva: {
36
36
  document: any;
37
37
  _injectGlobal(Konva: any): void;
38
38
  } & {
39
+ /**
40
+ * Defer automatic Konva.Transformer updates across many synchronous node changes
41
+ * and run them once when the returned function is called.
42
+ * A Transformer normally updates after every change of an attached node, so a loop
43
+ * over N selected nodes costs N updates, each measuring all N nodes. In a batch it costs one.
44
+ * A single setAttrs(), position(), destroyChildren(), removeChildren() call or animation frame is
45
+ * already batched; use this for anything else, for example a React commit or an undo.
46
+ * Attributes and change events stay synchronous. Inside the batch, the Transformer's own
47
+ * x(), y(), width(), height() and getClientRect() are computed on read; its anchors are laid out
48
+ * when the batch finishes, or earlier if something draws or measures them.
49
+ * Change events fire inside the batch, so a listener sees the new attributes and the
50
+ * Transformer's box, but not its anchors; call transformer.update() there if it needs them.
51
+ * Batches nest. Call the returned function in a finally block, and do not keep a batch
52
+ * open across an await or an animation frame.
53
+ * @method
54
+ * @name Konva.beginUpdateBatch
55
+ * @returns {Function} finishes the batch and runs the pending updates
56
+ * @example
57
+ * const finish = Konva.beginUpdateBatch();
58
+ * try {
59
+ * selectedNodes.forEach((node) => node.width(100));
60
+ * } finally {
61
+ * finish();
62
+ * }
63
+ */
64
+ beginUpdateBatch: typeof Node._beginUpdateBatch;
39
65
  Util: {
40
66
  _isElement(obj: any): obj is Element;
41
67
  _isFunction(obj: any): boolean;
@@ -14,6 +14,32 @@ import { Tween, Easings } from "./Tween.js";
14
14
  import { Context } from "./Context.js";
15
15
  import { Canvas } from "./Canvas.js";
16
16
  export const Konva = Util._assign(Global, {
17
+ /**
18
+ * Defer automatic Konva.Transformer updates across many synchronous node changes
19
+ * and run them once when the returned function is called.
20
+ * A Transformer normally updates after every change of an attached node, so a loop
21
+ * over N selected nodes costs N updates, each measuring all N nodes. In a batch it costs one.
22
+ * A single setAttrs(), position(), destroyChildren(), removeChildren() call or animation frame is
23
+ * already batched; use this for anything else, for example a React commit or an undo.
24
+ * Attributes and change events stay synchronous. Inside the batch, the Transformer's own
25
+ * x(), y(), width(), height() and getClientRect() are computed on read; its anchors are laid out
26
+ * when the batch finishes, or earlier if something draws or measures them.
27
+ * Change events fire inside the batch, so a listener sees the new attributes and the
28
+ * Transformer's box, but not its anchors; call transformer.update() there if it needs them.
29
+ * Batches nest. Call the returned function in a finally block, and do not keep a batch
30
+ * open across an await or an animation frame.
31
+ * @method
32
+ * @name Konva.beginUpdateBatch
33
+ * @returns {Function} finishes the batch and runs the pending updates
34
+ * @example
35
+ * const finish = Konva.beginUpdateBatch();
36
+ * try {
37
+ * selectedNodes.forEach((node) => node.width(100));
38
+ * } finally {
39
+ * finish();
40
+ * }
41
+ */
42
+ beginUpdateBatch: Node._beginUpdateBatch,
17
43
  Util,
18
44
  Transform,
19
45
  Node,
@@ -42,6 +42,7 @@ export declare const Konva: {
42
42
  document: any;
43
43
  _injectGlobal(Konva: any): void;
44
44
  } & {
45
+ beginUpdateBatch: typeof import("./Node.ts").Node._beginUpdateBatch;
45
46
  Util: {
46
47
  _isElement(obj: any): obj is Element;
47
48
  _isFunction(obj: any): boolean;
@@ -19,6 +19,7 @@ export interface TransformerConfig extends ContainerConfig {
19
19
  rotateAnchorOffset?: number;
20
20
  rotateAnchorAngle?: number;
21
21
  rotateAnchorCursor?: string;
22
+ rotateMode?: 'handle' | 'corners' | 'both';
22
23
  borderEnabled?: boolean;
23
24
  borderStroke?: string;
24
25
  borderStrokeWidth?: number;
@@ -55,6 +56,7 @@ export interface TransformerConfig extends ContainerConfig {
55
56
  * @param {Number} [config.rotateAnchorOffset] Default is 50
56
57
  * @param {Number} [config.rotateAnchorAngle] Angle of the rotate anchor around the box, in degrees. Default is 0 (top)
57
58
  * @param {String} [config.rotateAnchorCursor] Default is crosshair
59
+ * @param {String} [config.rotateMode] How the user rotates: 'handle' (the rotate anchor), 'corners' (invisible zones just outside the corners) or 'both'. Default is 'handle'
58
60
  * @param {Number} [config.padding] Default is 0
59
61
  * @param {Boolean} [config.borderEnabled] Should we draw border? Default is true
60
62
  * @param {String} [config.borderStroke] Border stroke color
@@ -66,7 +68,7 @@ export interface TransformerConfig extends ContainerConfig {
66
68
  * @param {Number} [config.anchorStrokeWidth] Anchor stroke size
67
69
  * @param {Number} [config.anchorSize] Default is 10
68
70
  * @param {Boolean} [config.keepRatio] Should we keep ratio when we are moving edges? Default is true
69
- * @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? Default is 'default'
71
+ * @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? 'none' also turns off the Shift rotation snap. Default is 'default'
70
72
  * @param {Boolean} [config.centeredScaling] Should we resize relative to node's center? Default is false
71
73
  * @param {Array} [config.enabledAnchors] Array of names of enabled handles
72
74
  * @param {Boolean} [config.flipEnabled] Can we flip/mirror shape on transform?. True by default
@@ -105,13 +107,16 @@ export declare class Transformer extends Group {
105
107
  _fitting: boolean;
106
108
  _transformWindow: Window | null;
107
109
  _anchorDragOffset: Vector2d;
110
+ _rotateStart?: {
111
+ pointerAngle: number;
112
+ rotation: number;
113
+ };
108
114
  sin: number;
109
115
  cos: number;
110
116
  _cursorChange: boolean;
111
117
  _elementsCreated: boolean;
112
- _updateScheduled: boolean;
113
118
  _lastNodeRect?: Readonly<Box>;
114
- _subtreeChanged: boolean;
119
+ _updatePending: boolean;
115
120
  static isTransforming: () => boolean;
116
121
  static _isLayerTransforming(layer: Node): boolean;
117
122
  constructor(config?: TransformerConfig);
@@ -129,6 +134,9 @@ export declare class Transformer extends Group {
129
134
  _getEventNamespace(): string;
130
135
  setNodes(nodes?: Array<Node>): this;
131
136
  _proxyDrag(node: Node): void;
137
+ _detachNode(node: Node): void;
138
+ _resetRotation(): void;
139
+ _removeNode(node: Node): void;
132
140
  getNodes(): Node<import("../Node.ts").NodeConfig>[];
133
141
  /**
134
142
  * return the name of current active anchor
@@ -163,7 +171,7 @@ export declare class Transformer extends Group {
163
171
  */
164
172
  _resetTransformCache(changedNode?: Node): void;
165
173
  _onSubtreeChange(node: Node): void;
166
- _syncSubtreeChange(): void;
174
+ _syncUpdate(): void;
167
175
  drawScene(can?: SceneCanvas, top?: Node): this;
168
176
  drawHit(can?: HitCanvas, top?: Node): this;
169
177
  _getNodeRect(): any;
@@ -179,7 +187,8 @@ export declare class Transformer extends Group {
179
187
  getWidth(): any;
180
188
  getHeight(): any;
181
189
  _createElements(): void;
182
- _createAnchor(name: any): void;
190
+ _createAnchor(name: string, className?: string): Rect;
191
+ _setAnchorCursor(name: string, rad?: number): void;
183
192
  _createBack(): void;
184
193
  _handleMouseDown(e: any): void;
185
194
  _handleMouseMove(e: any): void;
@@ -236,6 +245,7 @@ export declare class Transformer extends Group {
236
245
  rotateAnchorAngle: GetSet<number, this>;
237
246
  rotationSnapTolerance: GetSet<number, this>;
238
247
  rotateAnchorCursor: GetSet<string, this>;
248
+ rotateMode: GetSet<'handle' | 'corners' | 'both', this>;
239
249
  padding: GetSet<number, this>;
240
250
  borderEnabled: GetSet<boolean, this>;
241
251
  borderStroke: GetSet<string, this>;