konva 10.8.0 → 10.9.1

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/Context.d.ts CHANGED
@@ -332,6 +332,7 @@ export declare class SceneContext extends Context {
332
332
  _fill(shape: Shape): void;
333
333
  _strokeLinearGradient(shape: any): void;
334
334
  _applyStrokeStyle(shape: Shape): void;
335
+ _applyDecorationStyle(shape: Shape): boolean;
335
336
  _stroke(shape: any): void;
336
337
  _applyShadow(shape: any): void;
337
338
  }
package/lib/Context.js CHANGED
@@ -710,7 +710,7 @@ export class SceneContext extends Context {
710
710
  }
711
711
  }
712
712
  _strokeLinearGradient(shape) {
713
- let start = shape.getStrokeLinearGradientStartPoint(), end = shape.getStrokeLinearGradientEndPoint();
713
+ let start = shape._toStyleSpace(shape.getStrokeLinearGradientStartPoint()), end = shape._toStyleSpace(shape.getStrokeLinearGradientEndPoint());
714
714
  if (!shape.getStrokeScaleEnabled()) {
715
715
  const { a, b, c, d, e, f } = this._context.getTransform();
716
716
  const transform = new Transform([a, b, c, d, e, f]);
@@ -747,6 +747,20 @@ export class SceneContext extends Context {
747
747
  this.setAttr('strokeStyle', shape.stroke());
748
748
  }
749
749
  }
750
+ // Text decorations are drawn like the glyphs: with the fill, or with the
751
+ // stroke when the text is only outlined. False when the glyphs paint nothing.
752
+ _applyDecorationStyle(shape) {
753
+ if (shape.hasFill()) {
754
+ this.strokeStyle = this._getFillStyle(shape);
755
+ }
756
+ else if (shape.hasStroke()) {
757
+ this._applyStrokeStyle(shape);
758
+ }
759
+ else {
760
+ return false;
761
+ }
762
+ return true;
763
+ }
750
764
  _stroke(shape) {
751
765
  const dash = shape.dash(),
752
766
  // ignore strokeScaleEnabled for Text
package/lib/Global.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Konva JavaScript Framework v10.8.0
2
+ * Konva JavaScript Framework v10.9.1
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.8.0',
32
+ version: '10.9.1',
33
33
  isBrowser: detectBrowser(),
34
34
  isUnminified: /param/.test(function (param) { }.toString()),
35
35
  dblClickWindow: 400,
package/lib/Node.d.ts CHANGED
@@ -14,6 +14,8 @@ type Filters = Array<FilterFunction | string>;
14
14
  type SubtreeObserver = {
15
15
  _onSubtreeChange(node: Node): void;
16
16
  };
17
+ type CSSFilterFallback = (imageData: ImageData, value: number, pixelRatio: number) => void;
18
+ export declare function _registerCSSFilterFallback(name: string, fallback: CSSFilterFallback): void;
17
19
  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';
18
20
  export type NodeConfig = {
19
21
  x?: number;
@@ -139,9 +141,12 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
139
141
  y: number;
140
142
  } | null;
141
143
  _clearSelfAndDescendantCache(attr?: string): void;
142
- static _absTransformCascadeDepth: number;
143
- static _pendingAfterCascade: Array<() => void>;
144
- static _runAfterAbsTransformCascade(cb: () => void): void;
144
+ static _updateBatchDepth: number;
145
+ static _pendingUpdates: Set<() => void>;
146
+ static _runAfterUpdateBatch(cb: () => void): void;
147
+ static _flushUpdateBatch(): void;
148
+ static _updateBatch<T>(fn: () => T): T;
149
+ static _beginUpdateBatch(): () => void;
145
150
  /**
146
151
  * clear cached canvas
147
152
  * @method
package/lib/Node.js CHANGED
@@ -13,14 +13,8 @@ function parseCSSFilters(cssFilter) {
13
13
  let match;
14
14
  while ((match = filterRegex.exec(cssFilter)) !== null) {
15
15
  const [, name, argument] = match;
16
- if (![
17
- 'blur',
18
- 'brightness',
19
- 'contrast',
20
- 'grayscale',
21
- 'sepia',
22
- 'invert',
23
- ].includes(name)) {
16
+ const fallback = cssFilterFallbacks[name];
17
+ if (!fallback) {
24
18
  Util.warn(`CSS filter "${name}" is not supported in fallback mode. Consider using function filters for better compatibility.`);
25
19
  continue;
26
20
  }
@@ -29,53 +23,47 @@ function parseCSSFilters(cssFilter) {
29
23
  ? 0
30
24
  : 1
31
25
  : parseFloat(argument) / (argument.includes('%') ? 100 : 1);
32
- steps.push({ name, value });
26
+ steps.push({ fallback, value });
33
27
  }
34
28
  return function (imageData, pixelRatio = 1) {
35
- var _a;
36
- for (const { name, value } of steps) {
37
- if (['grayscale', 'sepia', 'invert'].includes(name)) {
38
- const amount = Math.min(1, Math.max(0, value));
39
- const data = imageData.data;
40
- for (let i = 0; i < data.length; i += 4) {
41
- const r = data[i], g = data[i + 1], b = data[i + 2];
42
- let red, green, blue;
43
- if (name === 'grayscale') {
44
- red = green = blue = 0.2126 * r + 0.7152 * g + 0.0722 * b;
45
- }
46
- else if (name === 'sepia') {
47
- red = 0.393 * r + 0.769 * g + 0.189 * b;
48
- green = 0.349 * r + 0.686 * g + 0.168 * b;
49
- blue = 0.272 * r + 0.534 * g + 0.131 * b;
50
- }
51
- else {
52
- red = 255 - r;
53
- green = 255 - g;
54
- blue = 255 - b;
55
- }
56
- // CSS interpolates the color matrix before clamping the result.
57
- data[i] = r + (red - r) * amount;
58
- data[i + 1] = g + (green - g) * amount;
59
- data[i + 2] = b + (blue - b) * amount;
60
- }
61
- continue;
62
- }
63
- const filter = (_a = Konva.Filters) === null || _a === void 0 ? void 0 : _a[Util._capitalize(name)];
64
- if (!filter)
65
- continue;
66
- // Supply filter parameters without setters or changes to the node.
67
- const context = Object.create(this);
68
- context.attrs = { ...this.attrs };
69
- if (name === 'blur')
70
- context.attrs.blurRadius = value * 0.5;
71
- if (name === 'brightness')
72
- context.attrs.brightness = value;
73
- if (name === 'contrast')
74
- context.attrs.contrast = 100 * (Math.sqrt(value) - 1);
75
- filter.call(context, imageData, pixelRatio);
29
+ for (const { fallback, value } of steps) {
30
+ fallback(imageData, value, pixelRatio);
76
31
  }
77
32
  };
78
33
  }
34
+ // Fallbacks for CSS filter functions. Filter modules register theirs on
35
+ // import, so the core build carries none of their code.
36
+ const cssFilterFallbacks = {};
37
+ export function _registerCSSFilterFallback(name, fallback) {
38
+ cssFilterFallbacks[name] = fallback;
39
+ }
40
+ for (const name of ['grayscale', 'sepia', 'invert']) {
41
+ _registerCSSFilterFallback(name, (imageData, value) => {
42
+ const amount = Math.min(1, Math.max(0, value));
43
+ const data = imageData.data;
44
+ for (let i = 0; i < data.length; i += 4) {
45
+ const r = data[i], g = data[i + 1], b = data[i + 2];
46
+ let red, green, blue;
47
+ if (name === 'grayscale') {
48
+ red = green = blue = 0.2126 * r + 0.7152 * g + 0.0722 * b;
49
+ }
50
+ else if (name === 'sepia') {
51
+ red = 0.393 * r + 0.769 * g + 0.189 * b;
52
+ green = 0.349 * r + 0.686 * g + 0.168 * b;
53
+ blue = 0.272 * r + 0.534 * g + 0.131 * b;
54
+ }
55
+ else {
56
+ red = 255 - r;
57
+ green = 255 - g;
58
+ blue = 255 - b;
59
+ }
60
+ // CSS interpolates the color matrix before clamping the result.
61
+ data[i] = r + (red - r) * amount;
62
+ data[i + 1] = g + (green - g) * amount;
63
+ data[i + 2] = b + (blue - b) * amount;
64
+ }
65
+ });
66
+ }
79
67
  // CONSTANTS
80
68
  const ABSOLUTE_OPACITY = 'absoluteOpacity', ABSOLUTE_TRANSFORM = 'absoluteTransform', CHANGE = 'Change', CHILDREN = 'children', KONVA = 'konva', LISTENING = 'listening', MOUSEENTER = 'mouseenter', MOUSELEAVE = 'mouseleave', POINTERENTER = 'pointerenter', POINTERLEAVE = 'pointerleave', TOUCHENTER = 'touchenter', TOUCHLEAVE = 'touchleave', NON_BUBBLING_EVENTS = [
81
69
  MOUSEENTER,
@@ -186,14 +174,44 @@ export class Node {
186
174
  this.fire('absoluteTransformChange');
187
175
  }
188
176
  }
189
- static _runAfterAbsTransformCascade(cb) {
190
- if (Node._absTransformCascadeDepth > 0) {
191
- Node._pendingAfterCascade.push(cb);
177
+ static _runAfterUpdateBatch(cb) {
178
+ if (Node._updateBatchDepth > 0) {
179
+ Node._pendingUpdates.add(cb);
192
180
  }
193
181
  else {
194
182
  cb();
195
183
  }
196
184
  }
185
+ static _flushUpdateBatch() {
186
+ if (Node._updateBatchDepth > 0)
187
+ return;
188
+ const callbacks = Node._pendingUpdates;
189
+ if (!callbacks.size)
190
+ return;
191
+ Node._pendingUpdates = new Set();
192
+ callbacks.forEach((callback) => callback());
193
+ }
194
+ static _updateBatch(fn) {
195
+ Node._updateBatchDepth++;
196
+ try {
197
+ return fn();
198
+ }
199
+ finally {
200
+ Node._updateBatchDepth--;
201
+ Node._flushUpdateBatch();
202
+ }
203
+ }
204
+ static _beginUpdateBatch() {
205
+ Node._updateBatchDepth++;
206
+ let finished = false;
207
+ return () => {
208
+ if (finished)
209
+ return;
210
+ finished = true;
211
+ Node._updateBatchDepth--;
212
+ Node._flushUpdateBatch();
213
+ };
214
+ }
197
215
  /**
198
216
  * clear cached canvas
199
217
  * @method
@@ -686,12 +704,17 @@ export class Node {
686
704
  _delegate(event, selector, handler) {
687
705
  const stopNode = this;
688
706
  this.on(event, function (evt) {
707
+ const original = evt;
689
708
  const targets = evt.target.findAncestors(selector, true, stopNode);
690
709
  for (let i = 0; i < targets.length; i++) {
691
710
  evt = Util.cloneObject(evt);
692
711
  evt.currentTarget = targets[i];
693
712
  handler.call(targets[i], evt);
694
713
  }
714
+ // handlers get copies, so pass a cancelled bubble back
715
+ if (evt.cancelBubble) {
716
+ original.cancelBubble = true;
717
+ }
695
718
  });
696
719
  return this;
697
720
  }
@@ -984,18 +1007,20 @@ export class Node {
984
1007
  func();
985
1008
  return;
986
1009
  }
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);
1010
+ Node._updateBatch(() => {
1011
+ this._batchingTransformChange = true;
1012
+ try {
1013
+ func();
997
1014
  }
998
- }
1015
+ finally {
1016
+ this._batchingTransformChange = false;
1017
+ if (this._needClearTransformCache) {
1018
+ this._needClearTransformCache = false;
1019
+ this._clearCache(TRANSFORM);
1020
+ this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
1021
+ }
1022
+ }
1023
+ });
999
1024
  }
1000
1025
  setPosition(pos) {
1001
1026
  this._batchTransformChanges(() => {
@@ -2412,12 +2437,13 @@ export class Node {
2412
2437
  }
2413
2438
  // Number of registered observers. The ancestor walk is skipped while it is 0.
2414
2439
  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 = [];
2440
+ // Perf: while a batch is open, work that depends on many nodes (a Konva
2441
+ // .Transformer refresh) is queued and runs once when the outermost scope
2442
+ // ends, instead of once per node change. Scopes are the callers of
2443
+ // _updateBatch() and _beginUpdateBatch().
2444
+ Node._updateBatchDepth = 0;
2445
+ // a Set, so a callback queued once per change runs once per batch
2446
+ Node._pendingUpdates = new Set();
2421
2447
  // keyed by prototype: subclasses that share a nodeType (every shape class)
2422
2448
  // have their own listeners
2423
2449
  Node.protoListenerMap = new WeakMap();
@@ -2841,6 +2867,7 @@ addGetterSetter(Node, 'filters', undefined, function (val) {
2841
2867
  * get/set filters. Supports function filters, CSS filter strings, or mixed arrays.
2842
2868
  * CSS filters are applied using native browser performance when possible, function filters use ImageData manipulation.
2843
2869
  * CSS filters automatically fall back to function filters in unsupported browsers.
2870
+ * The fallback covers blur, brightness, contrast, grayscale, sepia and invert. A core build needs the Blur, Brightness and Contrast filters imported for the first three.
2844
2871
  * Blur radii, pixelation sizes and CSS lengths use node coordinates, independent of cache pixelRatio.
2845
2872
  * Custom filters receive (imageData, pixelRatio). ImageData contains full-resolution backing pixels.
2846
2873
  * @name Konva.Node#filters
package/lib/Shape.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Transform } from './Util.ts';
1
2
  import type { GetClientRectConfig, NodeConfig } from './Node.ts';
2
3
  import { Node } from './Node.ts';
3
4
  import type { Context } from './Context.ts';
@@ -111,6 +112,7 @@ export declare class Shape<Config extends ShapeConfig = ShapeConfig> extends Nod
111
112
  _strokeFunc: (ctx: Context) => void;
112
113
  _fillFuncHit: (ctx: Context) => void;
113
114
  _strokeFuncHit: (ctx: Context) => void;
115
+ _styleTransform?: Transform;
114
116
  constructor(config?: Config);
115
117
  /**
116
118
  * @deprecated
@@ -130,8 +132,10 @@ export declare class Shape<Config extends ShapeConfig = ShapeConfig> extends Nod
130
132
  */
131
133
  hasShadow(): any;
132
134
  _hasShadow(): boolean;
135
+ _toStyleSpace(point: Vector2d): Vector2d;
133
136
  _getFillPattern(): any;
134
- __getFillPattern(): CanvasPattern | null | undefined;
137
+ _createFillPattern(): CanvasPattern | null | undefined;
138
+ _setFillPatternTransform(pattern: CanvasPattern): CanvasPattern;
135
139
  _getLinearGradient(): any;
136
140
  __getLinearGradient(): CanvasGradient | undefined;
137
141
  _getRadialGradient(): any;
package/lib/Shape.js CHANGED
@@ -138,44 +138,54 @@ export class Shape extends Node {
138
138
  this.shadowOffsetX() ||
139
139
  this.shadowOffsetY()));
140
140
  }
141
+ _toStyleSpace(point) {
142
+ return this._styleTransform ? this._styleTransform.point(point) : point;
143
+ }
144
+ // Unlike a gradient, a pattern can be moved after it is created, so one
145
+ // pattern serves every space it is drawn in (see _styleTransform).
141
146
  _getFillPattern() {
142
- return this._getCache(patternImage, this.__getFillPattern);
147
+ const pattern = this._getCache(patternImage, this._createFillPattern);
148
+ return pattern && this._setFillPatternTransform(pattern);
143
149
  }
144
- __getFillPattern() {
150
+ _createFillPattern() {
145
151
  if (this.fillPatternImage()) {
146
- const ctx = getDummyContext();
147
- const pattern = ctx.createPattern(this.fillPatternImage(), this.fillPatternRepeat() || 'repeat');
148
- if (pattern && pattern.setTransform) {
149
- const tr = new Transform();
150
- tr.translate(this.fillPatternX(), this.fillPatternY());
151
- tr.rotate(Konva.getAngle(this.fillPatternRotation()));
152
- tr.scale(this.fillPatternScaleX(), this.fillPatternScaleY());
153
- tr.translate(-1 * this.fillPatternOffsetX(), -1 * this.fillPatternOffsetY());
154
- const m = tr.getMatrix();
155
- const matrix = typeof DOMMatrix === 'undefined'
156
- ? {
157
- a: m[0], // Horizontal scaling. A value of 1 results in no scaling.
158
- b: m[1], // Vertical skewing.
159
- c: m[2], // Horizontal skewing.
160
- d: m[3],
161
- e: m[4], // Horizontal translation (moving).
162
- f: m[5], // Vertical translation (moving).
163
- }
164
- : new DOMMatrix(m);
165
- pattern.setTransform(matrix);
166
- }
167
- return pattern;
152
+ return getDummyContext().createPattern(this.fillPatternImage(), this.fillPatternRepeat() || 'repeat');
153
+ }
154
+ }
155
+ _setFillPatternTransform(pattern) {
156
+ var _a;
157
+ if (pattern.setTransform) {
158
+ const tr = ((_a = this._styleTransform) === null || _a === void 0 ? void 0 : _a.copy()) || new Transform();
159
+ tr.translate(this.fillPatternX(), this.fillPatternY());
160
+ tr.rotate(Konva.getAngle(this.fillPatternRotation()));
161
+ tr.scale(this.fillPatternScaleX(), this.fillPatternScaleY());
162
+ tr.translate(-1 * this.fillPatternOffsetX(), -1 * this.fillPatternOffsetY());
163
+ const m = tr.getMatrix();
164
+ const matrix = typeof DOMMatrix === 'undefined'
165
+ ? {
166
+ a: m[0], // Horizontal scaling. A value of 1 results in no scaling.
167
+ b: m[1], // Vertical skewing.
168
+ c: m[2], // Horizontal skewing.
169
+ d: m[3],
170
+ e: m[4], // Horizontal translation (moving).
171
+ f: m[5], // Vertical translation (moving).
172
+ }
173
+ : new DOMMatrix(m);
174
+ pattern.setTransform(matrix);
168
175
  }
176
+ return pattern;
169
177
  }
170
178
  _getLinearGradient() {
179
+ if (this._styleTransform)
180
+ return this.__getLinearGradient();
171
181
  return this._getCache(linearGradient, this.__getLinearGradient);
172
182
  }
173
183
  __getLinearGradient() {
174
184
  const colorStops = this.fillLinearGradientColorStops();
175
185
  if (colorStops) {
176
186
  const ctx = getDummyContext();
177
- const start = this.fillLinearGradientStartPoint();
178
- const end = this.fillLinearGradientEndPoint();
187
+ const start = this._toStyleSpace(this.fillLinearGradientStartPoint());
188
+ const end = this._toStyleSpace(this.fillLinearGradientEndPoint());
179
189
  const grd = ctx.createLinearGradient(start.x, start.y, end.x, end.y);
180
190
  // build color stops
181
191
  for (let n = 0; n < colorStops.length; n += 2) {
@@ -185,14 +195,17 @@ export class Shape extends Node {
185
195
  }
186
196
  }
187
197
  _getRadialGradient() {
198
+ if (this._styleTransform)
199
+ return this.__getRadialGradient();
188
200
  return this._getCache(radialGradient, this.__getRadialGradient);
189
201
  }
190
202
  __getRadialGradient() {
191
203
  const colorStops = this.fillRadialGradientColorStops();
192
204
  if (colorStops) {
193
205
  const ctx = getDummyContext();
194
- const start = this.fillRadialGradientStartPoint();
195
- const end = this.fillRadialGradientEndPoint();
206
+ // a rigid transform keeps the radii
207
+ const start = this._toStyleSpace(this.fillRadialGradientStartPoint());
208
+ const end = this._toStyleSpace(this.fillRadialGradientEndPoint());
196
209
  const grd = ctx.createRadialGradient(start.x, start.y, this.fillRadialGradientStartRadius(), end.x, end.y, this.fillRadialGradientEndRadius());
197
210
  // build color stops
198
211
  for (let n = 0; n < colorStops.length; n += 2) {
package/lib/Util.js CHANGED
@@ -1194,6 +1194,8 @@ export const Util = {
1194
1194
  else {
1195
1195
  currCornerRadius = i < cornerRadius.length ? cornerRadius[i] : 0;
1196
1196
  }
1197
+ // a negative corner radius would throw in context.arcTo()
1198
+ currCornerRadius = Math.max(currCornerRadius, 0);
1197
1199
  const maxCornerRadius = radius * Math.cos(Math.PI / sides);
1198
1200
  // cornerRadius creates perfect circle at 1/2 radius
1199
1201
  currCornerRadius =
@@ -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;