konva 10.6.0 → 10.7.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.
Files changed (53) hide show
  1. package/README.md +12 -8
  2. package/konva.js +617 -231
  3. package/konva.min.js +4 -4
  4. package/lib/Canvas.d.ts +9 -2
  5. package/lib/Canvas.js +83 -5
  6. package/lib/Container.d.ts +3 -2
  7. package/lib/Container.js +27 -12
  8. package/lib/Context.d.ts +5 -2
  9. package/lib/Context.js +22 -1
  10. package/lib/Global.d.ts +2 -1
  11. package/lib/Global.js +8 -3
  12. package/lib/Group.d.ts +7 -1
  13. package/lib/Group.js +61 -0
  14. package/lib/Layer.d.ts +1 -1
  15. package/lib/Layer.js +4 -2
  16. package/lib/Node.d.ts +12 -7
  17. package/lib/Node.js +53 -44
  18. package/lib/Shape.d.ts +9 -15
  19. package/lib/Shape.js +145 -85
  20. package/lib/Stage.d.ts +0 -2
  21. package/lib/Stage.js +2 -13
  22. package/lib/Util.d.ts +1 -0
  23. package/lib/Util.js +25 -0
  24. package/lib/shapes/Arc.js +3 -3
  25. package/lib/shapes/Arrow.d.ts +1 -0
  26. package/lib/shapes/Arrow.js +7 -1
  27. package/lib/shapes/Circle.d.ts +1 -0
  28. package/lib/shapes/Circle.js +4 -1
  29. package/lib/shapes/Ellipse.d.ts +1 -0
  30. package/lib/shapes/Ellipse.js +4 -1
  31. package/lib/shapes/Image.d.ts +2 -1
  32. package/lib/shapes/Image.js +6 -3
  33. package/lib/shapes/Label.js +18 -20
  34. package/lib/shapes/Line.d.ts +1 -0
  35. package/lib/shapes/Line.js +4 -1
  36. package/lib/shapes/Path.js +1 -1
  37. package/lib/shapes/Rect.d.ts +1 -0
  38. package/lib/shapes/Rect.js +4 -1
  39. package/lib/shapes/RegularPolygon.js +1 -1
  40. package/lib/shapes/Ring.d.ts +7 -0
  41. package/lib/shapes/Ring.js +8 -1
  42. package/lib/shapes/Sprite.d.ts +2 -1
  43. package/lib/shapes/Sprite.js +6 -3
  44. package/lib/shapes/Star.d.ts +6 -0
  45. package/lib/shapes/Star.js +5 -1
  46. package/lib/shapes/Text.d.ts +5 -2
  47. package/lib/shapes/Text.js +49 -22
  48. package/lib/shapes/TextPath.d.ts +7 -0
  49. package/lib/shapes/TextPath.js +16 -1
  50. package/lib/shapes/Transformer.d.ts +10 -2
  51. package/lib/shapes/Transformer.js +55 -6
  52. package/lib/shapes/Wedge.js +1 -1
  53. package/package.json +2 -18
package/konva.js CHANGED
@@ -5,10 +5,10 @@
5
5
  })(this, (function () { 'use strict';
6
6
 
7
7
  /*
8
- * Konva JavaScript Framework v10.6.0
8
+ * Konva JavaScript Framework v10.7.1
9
9
  * http://konvajs.org/
10
10
  * Licensed under the MIT
11
- * Date: Sat Sep 19 2026
11
+ * Date: Mon Oct 05 2026
12
12
  *
13
13
  * Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
14
14
  * Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva)
@@ -35,7 +35,7 @@
35
35
  : {};
36
36
  const Konva$2 = {
37
37
  _global: glob,
38
- version: '10.6.0',
38
+ version: '10.7.1',
39
39
  isBrowser: detectBrowser(),
40
40
  isUnminified: /param/.test(function (param) { }.toString()),
41
41
  dblClickWindow: 400,
@@ -189,8 +189,13 @@
189
189
  glob.Konva = Konva;
190
190
  },
191
191
  };
192
- const _registerNode = (NodeClass) => {
192
+ // Konva's own shapes whose bounds cover everything they paint. Plugins can
193
+ // import _registerNode, so they opt in explicitly.
194
+ const _boundedShapes = new Set();
195
+ const _registerNode = (NodeClass, boundsCoverPaint = false) => {
193
196
  Konva$2[NodeClass.prototype.getClassName()] = NodeClass;
197
+ if (boundsCoverPaint)
198
+ _boundedShapes.add(NodeClass);
194
199
  };
195
200
  Konva$2._injectGlobal(Konva$2);
196
201
 
@@ -282,6 +287,31 @@ js: import "konva/skia-backend";
282
287
  y: m[1] * point.x + m[3] * point.y + m[5],
283
288
  };
284
289
  }
290
+ // Axis-aligned bounds of a rectangle after this transform.
291
+ _getTransformedRect(rect) {
292
+ const [a, b, c, d, e, f] = this.m;
293
+ const x1 = rect.x, y1 = rect.y, x2 = rect.x + rect.width, y2 = rect.y + rect.height;
294
+ const xs = [
295
+ a * x1 + c * y1 + e,
296
+ a * x2 + c * y1 + e,
297
+ a * x2 + c * y2 + e,
298
+ a * x1 + c * y2 + e,
299
+ ];
300
+ const ys = [
301
+ b * x1 + d * y1 + f,
302
+ b * x2 + d * y1 + f,
303
+ b * x2 + d * y2 + f,
304
+ b * x1 + d * y2 + f,
305
+ ];
306
+ const minX = Math.min(xs[0], xs[1], xs[2], xs[3]);
307
+ const minY = Math.min(ys[0], ys[1], ys[2], ys[3]);
308
+ return {
309
+ x: minX,
310
+ y: minY,
311
+ width: Math.max(xs[0], xs[1], xs[2], xs[3]) - minX,
312
+ height: Math.max(ys[0], ys[1], ys[2], ys[3]) - minY,
313
+ };
314
+ }
285
315
  /**
286
316
  * Apply translation
287
317
  * @method
@@ -1459,6 +1489,7 @@ js: import "konva/skia-backend";
1459
1489
  'globalAlpha',
1460
1490
  'globalCompositeOperation',
1461
1491
  'imageSmoothingEnabled',
1492
+ 'imageSmoothingQuality',
1462
1493
  'filter',
1463
1494
  ];
1464
1495
  const traceArrMax = 100;
@@ -1631,14 +1662,34 @@ js: import "konva/skia-backend";
1631
1662
  this.clearRect(0, 0, canvas.getWidth() / canvas.pixelRatio, canvas.getHeight() / canvas.pixelRatio);
1632
1663
  }
1633
1664
  }
1665
+ // Copy a buffer already rasterized in destination pixels. The caller owns
1666
+ // save/restore, opacity, compositing and the destination clip.
1667
+ _drawDeviceBuffer(canvas) {
1668
+ const { x, y, width, height } = canvas._isolationRect;
1669
+ this.setTransform(1, 0, 0, 1, 0, 0);
1670
+ this.imageSmoothingEnabled = false;
1671
+ this.drawImage(canvas._canvas, 0, 0, width, height, x, y, width, height);
1672
+ }
1634
1673
  _applyLineCap(shape) {
1635
1674
  const lineCap = shape.attrs.lineCap;
1636
1675
  if (lineCap) {
1637
1676
  this.setAttr('lineCap', lineCap);
1638
1677
  }
1639
1678
  }
1679
+ _getOpacity(shape) {
1680
+ if (!this._opacityRoot) {
1681
+ return shape.getAbsoluteOpacity();
1682
+ }
1683
+ let opacity = shape.opacity();
1684
+ let parent = shape.parent;
1685
+ while (parent && parent !== this._opacityRoot) {
1686
+ opacity *= parent.opacity();
1687
+ parent = parent.parent;
1688
+ }
1689
+ return opacity;
1690
+ }
1640
1691
  _applyOpacity(shape) {
1641
- const absOpacity = shape.getAbsoluteOpacity();
1692
+ const absOpacity = this._getOpacity(shape);
1642
1693
  if (absOpacity !== 1) {
1643
1694
  this.setAttr('globalAlpha', absOpacity);
1644
1695
  }
@@ -2217,10 +2268,6 @@ js: import "konva/skia-backend";
2217
2268
  this._logicalWidth = 0;
2218
2269
  this._logicalHeight = 0;
2219
2270
  this.isCache = false;
2220
- // origin of a buffer canvas in the coordinate space of the canvas it is
2221
- // drawn back into, see Shape.drawScene
2222
- this.x = 0;
2223
- this.y = 0;
2224
2271
  const conf = config || {};
2225
2272
  // a Konva.pixelRatio reset to undefined or 0 means the device ratio
2226
2273
  const pixelRatio = conf.pixelRatio ||
@@ -2351,8 +2398,90 @@ js: import "konva/skia-backend";
2351
2398
  }
2352
2399
  }
2353
2400
  class SceneCanvas extends Canvas {
2401
+ // A cleared surface over `rect` (in the current drawing space), or over the
2402
+ // whole canvas, in this canvas's device pixels. Siblings reuse it; nested
2403
+ // groups borrow from the surface itself, so they cannot clear a parent's
2404
+ // unfinished image.
2405
+ _prepareIsolationCanvas(rect) {
2406
+ const { a, b, c, d, e, f } = this.getContext()._context.getTransform();
2407
+ const view = this._isolationRect || this;
2408
+ let x = 0, y = 0, width = view.width, height = view.height;
2409
+ if (rect) {
2410
+ const box = new Transform([a, b, c, d, e, f])._getTransformedRect(rect);
2411
+ // Round outwards, with a pixel for antialiasing.
2412
+ x = Math.max(0, Math.floor(box.x) - 1);
2413
+ y = Math.max(0, Math.floor(box.y) - 1);
2414
+ width = Math.min(width, Math.ceil(box.x + box.width) + 1) - x;
2415
+ height = Math.min(height, Math.ceil(box.y + box.height) + 1) - y;
2416
+ }
2417
+ if (!(width > 0 && height > 0)) {
2418
+ // An empty source must still composite for copy/destination-in/etc.
2419
+ x = y = 0;
2420
+ width = height = 1;
2421
+ }
2422
+ const surface = (this._isolationCanvas || (this._isolationCanvas = new SceneCanvas({
2423
+ width: 0,
2424
+ height: 0,
2425
+ pixelRatio: this.pixelRatio,
2426
+ })));
2427
+ if (surface.width < width || surface.height < height) {
2428
+ // Headroom on a short side, so a growing group does not reallocate
2429
+ // every frame.
2430
+ const ratio = this.pixelRatio;
2431
+ const grow = (need, have, max) => Math.min(max, Math.ceil((have < need ? Math.max(need, have * 1.5) : have) / ratio));
2432
+ surface.setSize(grow(width, surface.width, this._logicalWidth), grow(height, surface.height, this._logicalHeight));
2433
+ }
2434
+ surface._isolationRect = { x, y, width, height };
2435
+ const peak = surface._isolationPeak;
2436
+ peak.width = Math.max(peak.width, width);
2437
+ peak.height = Math.max(peak.height, height);
2438
+ const context = surface.getContext();
2439
+ context.setTransform(1, 0, 0, 1, 0, 0);
2440
+ context.clearRect(0, 0, width, height);
2441
+ context.setTransform(a, b, c, d, e - x, f - y);
2442
+ const parent = this.getContext();
2443
+ context.imageSmoothingEnabled = parent.imageSmoothingEnabled;
2444
+ context.imageSmoothingQuality = parent.imageSmoothingQuality;
2445
+ context.direction = parent.direction;
2446
+ return surface;
2447
+ }
2448
+ // After a frame, free surfaces that ten frames in a row did not use or used
2449
+ // a small part of. Content that changes size keeps its surface.
2450
+ _trimIsolationCanvas() {
2451
+ const surface = this._isolationCanvas;
2452
+ if (!surface)
2453
+ return;
2454
+ const peak = surface._isolationPeak;
2455
+ const low = surface.width * surface.height > 4 * peak.width * peak.height;
2456
+ surface._isolationLowFrames = low ? surface._isolationLowFrames + 1 : 0;
2457
+ if (surface._isolationLowFrames >= 10) {
2458
+ this._releaseIsolationCanvas();
2459
+ }
2460
+ else {
2461
+ peak.width = peak.height = 0;
2462
+ surface._trimIsolationCanvas();
2463
+ }
2464
+ }
2465
+ _releaseIsolationCanvas() {
2466
+ const surface = this._isolationCanvas;
2467
+ if (surface) {
2468
+ surface._releaseIsolationCanvas();
2469
+ Util.releaseCanvas(surface._canvas);
2470
+ this._isolationCanvas = undefined;
2471
+ }
2472
+ }
2473
+ setSize(width, height) {
2474
+ // A bitmap reset also resets its origin-clean state. A surface from the
2475
+ // previous lifetime may be tainted even after clearing its pixels.
2476
+ this._releaseIsolationCanvas();
2477
+ super.setSize(width, height);
2478
+ }
2354
2479
  constructor(config = { width: 0, height: 0, willReadFrequently: false }) {
2355
2480
  super(config);
2481
+ // The largest width and height requested since the last trim.
2482
+ this._isolationPeak = { width: 0, height: 0 };
2483
+ // Consecutive frames that used less than a quarter of the surface.
2484
+ this._isolationLowFrames = 0;
2356
2485
  this.context = new SceneContext(this, {
2357
2486
  willReadFrequently: config.willReadFrequently,
2358
2487
  });
@@ -3002,20 +3131,6 @@ js: import "konva/skia-backend";
3002
3131
  'transformsEnabledChange.konva',
3003
3132
  ].join(SPACE$1);
3004
3133
  let idCounter$1 = 1;
3005
- // "perfect drawing" buffer for cache() and toCanvas(): it mirrors the target
3006
- // canvas and is drawn back at (x, y) in the target's coordinate space.
3007
- // It starts empty and Shape.drawScene sizes it on first use, so nodes
3008
- // without buffered shapes never allocate it.
3009
- function createBufferCanvas(target, x, y) {
3010
- const bufferCanvas = new SceneCanvas({
3011
- width: 0,
3012
- height: 0,
3013
- pixelRatio: target.pixelRatio,
3014
- });
3015
- bufferCanvas.x = x;
3016
- bufferCanvas.y = y;
3017
- return bufferCanvas;
3018
- }
3019
3134
  /**
3020
3135
  * Node constructor. Nodes are entities that can be transformed, layered,
3021
3136
  * and have bound events. The stage, layers, groups, and shapes all extend Node.
@@ -3228,7 +3343,6 @@ js: import "konva/skia-backend";
3228
3343
  height: 0,
3229
3344
  willReadFrequently: true,
3230
3345
  }), sceneContext = cachedSceneCanvas.getContext();
3231
- const bufferCanvas = createBufferCanvas(cachedSceneCanvas, x, y);
3232
3346
  cachedSceneCanvas.isCache = true;
3233
3347
  this._releaseCanvasCache();
3234
3348
  this._filterUpToDate = false;
@@ -3242,7 +3356,7 @@ js: import "konva/skia-backend";
3242
3356
  this._isUnderCache = true;
3243
3357
  this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
3244
3358
  try {
3245
- this.drawScene(cachedSceneCanvas, this, bufferCanvas);
3359
+ this.drawScene(cachedSceneCanvas, this);
3246
3360
  }
3247
3361
  catch (e) {
3248
3362
  Util.releaseCanvas(cachedSceneCanvas._canvas);
@@ -3253,8 +3367,7 @@ js: import "konva/skia-backend";
3253
3367
  // descendants cached their opacity relative to this node while drawing
3254
3368
  this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
3255
3369
  sceneContext.restore();
3256
- // the buffer is only needed while drawing
3257
- Util.releaseCanvas(bufferCanvas._canvas);
3370
+ cachedSceneCanvas._releaseIsolationCanvas();
3258
3371
  }
3259
3372
  // this will draw a red border around the cached box for
3260
3373
  // debugging purposes
@@ -3365,28 +3478,22 @@ js: import "konva/skia-backend";
3365
3478
  // redefine in Container and Shape
3366
3479
  throw new Error('abstract "getClientRect" method call');
3367
3480
  }
3368
- _transformedRect(rect, top) {
3369
- const points = [
3370
- { x: rect.x, y: rect.y },
3371
- { x: rect.x + rect.width, y: rect.y },
3372
- { x: rect.x + rect.width, y: rect.y + rect.height },
3373
- { x: rect.x, y: rect.y + rect.height },
3374
- ];
3375
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
3376
- const trans = this.getAbsoluteTransform(top);
3377
- points.forEach(function (point) {
3378
- const transformed = trans.point(point);
3379
- minX = Math.min(minX, transformed.x);
3380
- minY = Math.min(minY, transformed.y);
3381
- maxX = Math.max(maxX, transformed.x);
3382
- maxY = Math.max(maxY, transformed.y);
3383
- });
3384
- return {
3385
- x: minX,
3386
- y: minY,
3387
- width: maxX - minX,
3388
- height: maxY - minY,
3481
+ _getCachedSceneRect(config) {
3482
+ const cache = config._forDrawing && !this._isUnderCache && this._getCanvasCache();
3483
+ if (!cache)
3484
+ return;
3485
+ const rect = {
3486
+ x: cache.x,
3487
+ y: cache.y,
3488
+ width: cache.scene.width / cache.scene.pixelRatio,
3489
+ height: cache.scene.height / cache.scene.pixelRatio,
3389
3490
  };
3491
+ return config.skipTransform
3492
+ ? rect
3493
+ : this._transformedRect(rect, config.relativeTo);
3494
+ }
3495
+ _transformedRect(rect, top) {
3496
+ return this.getAbsoluteTransform(top)._getTransformedRect(rect);
3390
3497
  }
3391
3498
  _drawCachedSceneCanvas(context) {
3392
3499
  context.save();
@@ -4245,6 +4352,10 @@ js: import "konva/skia-backend";
4245
4352
  };
4246
4353
  for (key in attrs) {
4247
4354
  val = attrs[key];
4355
+ // JSON has no typed arrays, so typed line points are written as an array
4356
+ if (val instanceof TypedArray) {
4357
+ val = Array.from(val);
4358
+ }
4248
4359
  // if value is object and object is not plain
4249
4360
  // like class instance, we should skip it and to not include
4250
4361
  nonPlainObject =
@@ -4645,7 +4756,6 @@ js: import "konva/skia-backend";
4645
4756
  (stage ? stage.height() : 0),
4646
4757
  pixelRatio: pixelRatio,
4647
4758
  }), context = canvas.getContext();
4648
- const bufferCanvas = createBufferCanvas(canvas, x, y);
4649
4759
  if (config.imageSmoothingEnabled === false) {
4650
4760
  context._context.imageSmoothingEnabled = false;
4651
4761
  }
@@ -4653,10 +4763,9 @@ js: import "konva/skia-backend";
4653
4763
  if (x || y) {
4654
4764
  context.translate(-1 * x, -1 * y);
4655
4765
  }
4656
- this.drawScene(canvas, undefined, bufferCanvas);
4766
+ this.drawScene(canvas);
4657
4767
  context.restore();
4658
- // the buffer is only needed while drawing
4659
- Util.releaseCanvas(bufferCanvas._canvas);
4768
+ canvas._releaseIsolationCanvas();
4660
4769
  return canvas;
4661
4770
  }
4662
4771
  /**
@@ -4955,7 +5064,34 @@ js: import "konva/skia-backend";
4955
5064
  }
4956
5065
  return this;
4957
5066
  }
5067
+ _addSubtreeObserver(observer) {
5068
+ var _a;
5069
+ ((_a = this._subtreeObservers) !== null && _a !== void 0 ? _a : (this._subtreeObservers = new Set())).add(observer);
5070
+ Node._subtreeObserverCount++;
5071
+ }
5072
+ _removeSubtreeObserver(observer) {
5073
+ var _a;
5074
+ if ((_a = this._subtreeObservers) === null || _a === void 0 ? void 0 : _a.delete(observer)) {
5075
+ Node._subtreeObserverCount--;
5076
+ }
5077
+ }
5078
+ // Tells the observers of this node and of its ancestors that something below
5079
+ // them changed. A node's own changes reach its observers through its change
5080
+ // events, so a draw request starts at the parent. A container calls this
5081
+ // itself when its child list changes.
5082
+ _notifySubtreeChange() {
5083
+ if (!Node._subtreeObserverCount)
5084
+ return;
5085
+ for (let node = this; node; node = node.parent) {
5086
+ const observers = node._subtreeObservers;
5087
+ if (observers)
5088
+ for (const o of observers)
5089
+ o._onSubtreeChange(node);
5090
+ }
5091
+ }
4958
5092
  _requestDraw() {
5093
+ var _a;
5094
+ (_a = this.parent) === null || _a === void 0 ? void 0 : _a._notifySubtreeChange();
4959
5095
  if (Konva$2.autoDrawEnabled) {
4960
5096
  const drawNode = this.getLayer() || this.getStage();
4961
5097
  drawNode === null || drawNode === void 0 ? void 0 : drawNode.batchDraw();
@@ -5327,6 +5463,8 @@ js: import "konva/skia-backend";
5327
5463
  return no;
5328
5464
  }
5329
5465
  }
5466
+ // Number of registered observers. The ancestor walk is skipped while it is 0.
5467
+ Node._subtreeObserverCount = 0;
5330
5468
  // Perf: lets a listener inside an absoluteTransform cascade (e.g. Konva
5331
5469
  // .Transformer reacting to a stage drag fan-out across N attached nodes)
5332
5470
  // defer work until the cascade ends — collapses O(N) listener-driven calls
@@ -5969,6 +6107,7 @@ js: import "konva/skia-backend";
5969
6107
  });
5970
6108
  this.children = [];
5971
6109
  // because all children were detached from parent, request draw via container
6110
+ this._notifySubtreeChange();
5972
6111
  this._requestDraw();
5973
6112
  return this;
5974
6113
  }
@@ -5986,6 +6125,7 @@ js: import "konva/skia-backend";
5986
6125
  });
5987
6126
  this.children = [];
5988
6127
  // because all children were detached from parent, request draw via container
6128
+ this._notifySubtreeChange();
5989
6129
  this._requestDraw();
5990
6130
  return this;
5991
6131
  }
@@ -6024,6 +6164,7 @@ js: import "konva/skia-backend";
6024
6164
  this._fire('add', {
6025
6165
  child: child,
6026
6166
  });
6167
+ this._notifySubtreeChange();
6027
6168
  this._requestDraw();
6028
6169
  // chainable
6029
6170
  return this;
@@ -6224,9 +6365,10 @@ js: import "konva/skia-backend";
6224
6365
  (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child, n) {
6225
6366
  child.index = n;
6226
6367
  });
6368
+ this._notifySubtreeChange();
6227
6369
  this._requestDraw();
6228
6370
  }
6229
- drawScene(can, top, bufferCanvas) {
6371
+ drawScene(can, top) {
6230
6372
  const layer = this.getLayer(), canvas = can || (layer && layer.getCanvas()), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedSceneCanvas = cachedCanvas && cachedCanvas.scene;
6231
6373
  const caching = top === this;
6232
6374
  if (!(top ? this._isVisible(top) : this.isVisible()) && !caching) {
@@ -6240,7 +6382,7 @@ js: import "konva/skia-backend";
6240
6382
  context.restore();
6241
6383
  }
6242
6384
  else {
6243
- this._drawChildren('drawScene', canvas, top, bufferCanvas);
6385
+ this._drawChildren('drawScene', canvas, top);
6244
6386
  }
6245
6387
  return this;
6246
6388
  }
@@ -6261,8 +6403,7 @@ js: import "konva/skia-backend";
6261
6403
  }
6262
6404
  return this;
6263
6405
  }
6264
- _drawChildren(drawMethod, canvas, top, bufferCanvas) {
6265
- var _a;
6406
+ _drawChildren(drawMethod, canvas, top) {
6266
6407
  const context = canvas && canvas.getContext(), clipWidth = this.clipWidth(), clipHeight = this.clipHeight(), clipFunc = this.clipFunc(), hasClip = (typeof clipWidth === 'number' && typeof clipHeight === 'number') ||
6267
6408
  clipFunc;
6268
6409
  const selfCache = top === this;
@@ -6292,23 +6433,34 @@ js: import "konva/skia-backend";
6292
6433
  context.save();
6293
6434
  context._applyGlobalCompositeOperation(this);
6294
6435
  }
6295
- (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
6296
- child[drawMethod](canvas, top, bufferCanvas);
6297
- });
6298
- if (hasComposition) {
6299
- context.restore();
6436
+ try {
6437
+ this._drawChildNodes(drawMethod, canvas, top);
6300
6438
  }
6301
- if (hasClip) {
6302
- context.restore();
6439
+ finally {
6440
+ if (hasComposition) {
6441
+ context.restore();
6442
+ }
6443
+ if (hasClip) {
6444
+ context.restore();
6445
+ }
6303
6446
  }
6304
6447
  }
6448
+ _drawChildNodes(drawMethod, canvas, top) {
6449
+ var _a;
6450
+ (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
6451
+ child[drawMethod](canvas, top);
6452
+ });
6453
+ }
6305
6454
  getClientRect(config = {}) {
6306
6455
  var _a;
6456
+ const cachedRect = this._getCachedSceneRect(config);
6457
+ if (cachedRect)
6458
+ return cachedRect;
6307
6459
  const skipTransform = config.skipTransform;
6308
6460
  const relativeTo = config.relativeTo;
6309
6461
  const [a, b, c, d] = this.getAbsoluteTransform().getMatrix();
6310
6462
  // A zero scale cannot project local padding for a non-scaling stroke.
6311
- const measureTransformed = !skipTransform && a * d - b * c === 0;
6463
+ const measureTransformed = !skipTransform && (config._forDrawing || a * d - b * c === 0);
6312
6464
  let minX, minY, maxX, maxY;
6313
6465
  let selfRect = {
6314
6466
  x: Infinity,
@@ -6324,6 +6476,7 @@ js: import "konva/skia-backend";
6324
6476
  }
6325
6477
  const rect = child.getClientRect({
6326
6478
  relativeTo: measureTransformed ? relativeTo : that,
6479
+ _forDrawing: config._forDrawing,
6327
6480
  skipShadow: config.skipShadow,
6328
6481
  skipStroke: config.skipStroke,
6329
6482
  });
@@ -6723,7 +6876,7 @@ js: import "konva/skia-backend";
6723
6876
  if (index > -1) {
6724
6877
  stages.splice(index, 1);
6725
6878
  }
6726
- Util.releaseCanvas(this.bufferCanvas._canvas, this.bufferHitCanvas._canvas);
6879
+ Util.releaseCanvas(this.bufferHitCanvas._canvas);
6727
6880
  return this;
6728
6881
  }
6729
6882
  /**
@@ -6828,13 +6981,6 @@ js: import "konva/skia-backend";
6828
6981
  layer.draw();
6829
6982
  });
6830
6983
  }
6831
- // "perfect drawing" uses bufferCanvas, shape.intersects() uses
6832
- // bufferHitCanvas; both are lazy - created at 0x0 and sized to the stage
6833
- // on first use
6834
- _syncBufferSize(canvas) {
6835
- canvas.setSizeIfChanged(this.width(), this.height());
6836
- return canvas;
6837
- }
6838
6984
  add(layer, ...rest) {
6839
6985
  if (arguments.length > 1) {
6840
6986
  for (let i = 0; i < arguments.length; i++) {
@@ -7410,12 +7556,8 @@ js: import "konva/skia-backend";
7410
7556
  };
7411
7557
  }
7412
7558
  _buildDOM() {
7413
- // lazy buffer canvases, see _syncBufferSize
7559
+ // lazy buffer canvas, sized by Shape.intersects() on first use
7414
7560
  // https://github.com/konvajs/konva/issues/2009
7415
- this.bufferCanvas = new SceneCanvas({
7416
- width: 0,
7417
- height: 0,
7418
- });
7419
7561
  this.bufferHitCanvas = new HitCanvas({
7420
7562
  pixelRatio: 1,
7421
7563
  width: 0,
@@ -7851,7 +7993,9 @@ js: import "konva/skia-backend";
7851
7993
  if (!stage) {
7852
7994
  return false;
7853
7995
  }
7854
- const bufferHitCanvas = stage._syncBufferSize(stage.bufferHitCanvas);
7996
+ const bufferHitCanvas = stage.bufferHitCanvas;
7997
+ // lazy: allocated at stage size on first use
7998
+ bufferHitCanvas.setSizeIfChanged(stage.width(), stage.height());
7855
7999
  bufferHitCanvas.getContext().clear();
7856
8000
  this.drawHit(bufferHitCanvas);
7857
8001
  const p = bufferHitCanvas.context.getImageData(Math.floor(point.x), Math.floor(point.y), 1, 1).data;
@@ -7866,7 +8010,7 @@ js: import "konva/skia-backend";
7866
8010
  // why do we need buffer canvas?
7867
8011
  // it give better result when a shape has
7868
8012
  // stroke with fill and with some opacity
7869
- _useBufferCanvas(forceFill) {
8013
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
7870
8014
  // image and sprite still has "fill" as image
7871
8015
  // so they use that method with forced fill
7872
8016
  // it probably will be simpler, then copy/paste the code
@@ -7878,7 +8022,7 @@ js: import "konva/skia-backend";
7878
8022
  }
7879
8023
  const hasFill = forceFill || this.hasFill();
7880
8024
  const hasStroke = this.hasStroke();
7881
- const isTransparent = !this._isUnderCache && this.getAbsoluteOpacity() !== 1;
8025
+ const isTransparent = !this._isUnderCache && opacity !== 1;
7882
8026
  if (hasFill && hasStroke && isTransparent) {
7883
8027
  return true;
7884
8028
  }
@@ -7919,6 +8063,9 @@ js: import "konva/skia-backend";
7919
8063
  *
7920
8064
  */
7921
8065
  getSelfRect() {
8066
+ const selfRectFunc = this.attrs.selfRectFunc;
8067
+ if (selfRectFunc)
8068
+ return { ...selfRectFunc.call(this, this) };
7922
8069
  const size = this.size();
7923
8070
  return {
7924
8071
  x: this._centroid ? -size.width / 2 : 0,
@@ -7927,16 +8074,34 @@ js: import "konva/skia-backend";
7927
8074
  height: size.height,
7928
8075
  };
7929
8076
  }
8077
+ _getSelfRectForDrawing() {
8078
+ return this.getSelfRect();
8079
+ }
7930
8080
  getClientRect(config = {}) {
8081
+ const cachedRect = this._getCachedSceneRect(config);
8082
+ if (cachedRect)
8083
+ return cachedRect;
7931
8084
  const skipTransform = config.skipTransform;
7932
8085
  const relativeTo = config.relativeTo;
7933
- const fillRect = this.getSelfRect();
8086
+ const forDrawing = config._forDrawing;
8087
+ // Painted bounds are measured in the canvas that draws the shape
8088
+ // (relativeTo): non-scaling strokes and shadows use that canvas's axes.
8089
+ // The identity transform normalizes negative sizes.
8090
+ const fillRect = !forDrawing
8091
+ ? this.getSelfRect()
8092
+ : this.strokeScaleEnabled()
8093
+ ? new Transform()._getTransformedRect(this._getSelfRectForDrawing())
8094
+ : this._transformedRect(this._getSelfRectForDrawing(), relativeTo);
7934
8095
  const applyStroke = !config.skipStroke && this.hasStroke();
7935
- const strokeWidth = (applyStroke && this.strokeWidth()) || 0;
8096
+ const strokeWidth = applyStroke
8097
+ ? forDrawing
8098
+ ? this._getStrokePadding() * 2
8099
+ : this.strokeWidth()
8100
+ : 0;
7936
8101
  let strokeWidthX = strokeWidth;
7937
8102
  let strokeWidthY = strokeWidth;
7938
8103
  let collapsedStroke;
7939
- if (strokeWidth && !this.strokeScaleEnabled()) {
8104
+ if (!forDrawing && strokeWidth && !this.strokeScaleEnabled()) {
7940
8105
  // Strokes are constant in the canvas they are drawn into. A cached
7941
8106
  // ancestor supplies that coordinate space until its bitmap is rebuilt.
7942
8107
  let top = this;
@@ -7967,26 +8132,29 @@ js: import "konva/skia-backend";
7967
8132
  }
7968
8133
  }
7969
8134
  }
7970
- const fillAndStrokeWidth = fillRect.width + strokeWidthX;
7971
- const fillAndStrokeHeight = fillRect.height + strokeWidthY;
7972
- const applyShadow = !config.skipShadow && this.hasShadow();
7973
- const shadowOffsetX = applyShadow ? this.shadowOffsetX() : 0;
7974
- const shadowOffsetY = applyShadow ? this.shadowOffsetY() : 0;
7975
- const preWidth = fillAndStrokeWidth + Math.abs(shadowOffsetX);
7976
- const preHeight = fillAndStrokeHeight + Math.abs(shadowOffsetY);
7977
- const blurRadius = (applyShadow && this.shadowBlur()) || 0;
7978
- const width = preWidth + blurRadius * 2;
7979
- const height = preHeight + blurRadius * 2;
7980
- const rect = {
7981
- width: width,
7982
- height: height,
7983
- x: -(strokeWidthX / 2 + blurRadius) +
7984
- Math.min(shadowOffsetX, 0) +
7985
- fillRect.x,
7986
- y: -(strokeWidthY / 2 + blurRadius) +
7987
- Math.min(shadowOffsetY, 0) +
7988
- fillRect.y,
8135
+ let rect = {
8136
+ x: fillRect.x - strokeWidthX / 2,
8137
+ y: fillRect.y - strokeWidthY / 2,
8138
+ width: fillRect.width + strokeWidthX,
8139
+ height: fillRect.height + strokeWidthY,
7989
8140
  };
8141
+ if (forDrawing && this.strokeScaleEnabled()) {
8142
+ rect = this._transformedRect(rect, relativeTo);
8143
+ }
8144
+ if (!config.skipShadow && this.hasShadow()) {
8145
+ const scale = forDrawing ? this.getAbsoluteScale() : { x: 1, y: 1 };
8146
+ const dx = this.shadowOffsetX() * scale.x;
8147
+ const dy = this.shadowOffsetY() * scale.y;
8148
+ const blur = this.shadowBlur() *
8149
+ (forDrawing ? 2 * Math.min(Math.abs(scale.x), Math.abs(scale.y)) : 1);
8150
+ rect.x += Math.min(0, dx) - blur;
8151
+ rect.y += Math.min(0, dy) - blur;
8152
+ rect.width += Math.abs(dx) + blur * 2;
8153
+ rect.height += Math.abs(dy) + blur * 2;
8154
+ }
8155
+ if (forDrawing) {
8156
+ return rect;
8157
+ }
7990
8158
  if (!skipTransform) {
7991
8159
  const transformed = this._transformedRect(rect, relativeTo);
7992
8160
  if (collapsedStroke) {
@@ -8002,14 +8170,20 @@ js: import "konva/skia-backend";
8002
8170
  }
8003
8171
  return rect;
8004
8172
  }
8005
- drawScene(can, top, bufferCanvas) {
8173
+ _getStrokePadding(miterLimit = this.miterLimit() || 10) {
8174
+ // Custom paths can have sharper joins than the built-in geometry.
8175
+ if (this.attrs.sceneFunc)
8176
+ miterLimit = this.miterLimit() || 10;
8177
+ return ((Math.abs(this.strokeWidth()) / 2) *
8178
+ Math.max(this.lineCap() === 'square' ? Math.SQRT2 : 1, (this.lineJoin() || 'miter') === 'miter' ? miterLimit : 1));
8179
+ }
8180
+ drawScene(can, top) {
8006
8181
  // basically there are 3 drawing modes
8007
8182
  // 1 - simple drawing when nothing is cached.
8008
8183
  // 2 - when we are caching current
8009
8184
  // 3 - when node is cached and we need to draw it into layer
8010
8185
  const layer = this.getLayer();
8011
8186
  const canvas = can || layer.getCanvas(), context = canvas.getContext(), cachedCanvas = this._getCanvasCache(), drawFunc = this.getSceneFunc(), hasShadow = this.hasShadow();
8012
- let stage;
8013
8187
  const cachingSelf = top === this;
8014
8188
  if (!(top ? this._isVisible(top) : this.isVisible()) && !cachingSelf) {
8015
8189
  return this;
@@ -8027,66 +8201,72 @@ js: import "konva/skia-backend";
8027
8201
  return this;
8028
8202
  }
8029
8203
  context.save();
8030
- // if buffer canvas is needed
8031
- if (this._useBufferCanvas()) {
8032
- stage = this.getStage();
8033
- const bc = bufferCanvas || stage._syncBufferSize(stage.bufferCanvas);
8034
- const bufferContext = bc.getContext();
8035
- if (!bufferCanvas) {
8036
- bufferContext.clear();
8037
- }
8038
- else if (!bc.width) {
8039
- // cache() and toCanvas() hand over an empty buffer: size it to the
8040
- // destination on first use and shift it to the buffer origin
8041
- bc.setSize(canvas._logicalWidth, canvas._logicalHeight);
8042
- bufferContext.translate(-bc.x, -bc.y);
8043
- }
8044
- else {
8045
- // the buffer is translated, so reset the transform before clearing
8204
+ try {
8205
+ // if buffer canvas is needed; an empty canvas has nothing to draw into
8206
+ if (canvas.width &&
8207
+ canvas.height &&
8208
+ this._useBufferCanvas(undefined, context._getOpacity(this))) {
8209
+ // Built-in shapes, custom shapes with a selfRectFunc and isolated
8210
+ // groups use a buffer the size of the painted bounds. Other custom
8211
+ // drawing (sceneFunc, charRenderFunc, own classes) gets the whole
8212
+ // canvas, since it can paint outside its bounds. The shadow is
8213
+ // applied when the buffer is drawn, not inside it.
8214
+ const bounded = !!context._opacityRoot ||
8215
+ (!!this.attrs.selfRectFunc &&
8216
+ this.getSelfRect === Shape.prototype.getSelfRect) ||
8217
+ (!this.attrs.sceneFunc &&
8218
+ !this.attrs.charRenderFunc &&
8219
+ _boundedShapes.has(this.constructor));
8220
+ const bc = canvas._prepareIsolationCanvas(bounded
8221
+ ? this.getClientRect({
8222
+ _forDrawing: true,
8223
+ relativeTo: top,
8224
+ skipShadow: true,
8225
+ })
8226
+ : undefined);
8227
+ const bufferContext = bc.getContext();
8046
8228
  bufferContext.save();
8047
- bufferContext.setTransform(1, 0, 0, 1, 0, 0);
8048
- bufferContext.clearRect(0, 0, bc.width, bc.height);
8229
+ bufferContext._applyLineJoin(this);
8230
+ bufferContext._applyMiterLimit(this);
8231
+ // layer might be undefined if we are using cache before adding to layer
8232
+ const o = this.getAbsoluteTransform(top).getMatrix();
8233
+ bufferContext.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
8234
+ drawFunc.call(this, bufferContext, this);
8049
8235
  bufferContext.restore();
8236
+ if (hasShadow) {
8237
+ context._applyShadow(this);
8238
+ }
8239
+ // if we are caching self, we don't need to apply opacity and global composite operation
8240
+ // because it will be applied in the cache
8241
+ if (!cachingSelf) {
8242
+ context._applyOpacity(this);
8243
+ context._applyGlobalCompositeOperation(this);
8244
+ }
8245
+ context._drawDeviceBuffer(bc);
8050
8246
  }
8051
- bufferContext.save();
8052
- // the buffer canvas is a separate context that does not inherit the
8053
- // destination `imageSmoothingEnabled` flag, so propagate it to keep
8054
- // pattern/image smoothing consistent with the target context.
8055
- bufferContext.imageSmoothingEnabled = context.imageSmoothingEnabled;
8056
- bufferContext._applyLineJoin(this);
8057
- bufferContext._applyMiterLimit(this);
8058
- // layer might be undefined if we are using cache before adding to layer
8059
- const o = this.getAbsoluteTransform(top).getMatrix();
8060
- bufferContext.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
8061
- drawFunc.call(this, bufferContext, this);
8062
- bufferContext.restore();
8063
- const ratio = bc.pixelRatio;
8064
- if (hasShadow) {
8065
- context._applyShadow(this);
8066
- }
8067
- // if we are caching self, we don't need to apply opacity and global composite operation
8068
- // because it will be applied in the cache
8069
- if (!cachingSelf) {
8070
- context._applyOpacity(this);
8071
- context._applyGlobalCompositeOperation(this);
8247
+ else {
8248
+ context._applyLineJoin(this);
8249
+ context._applyMiterLimit(this);
8250
+ if (!cachingSelf) {
8251
+ const o = this.getAbsoluteTransform(top).getMatrix();
8252
+ context.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
8253
+ context._applyOpacity(this);
8254
+ context._applyGlobalCompositeOperation(this);
8255
+ }
8256
+ if (hasShadow) {
8257
+ context._applyShadow(this);
8258
+ }
8259
+ drawFunc.call(this, context, this);
8072
8260
  }
8073
- context.drawImage(bc._canvas, bc.x, bc.y, bc.width / ratio, bc.height / ratio);
8074
8261
  }
8075
- else {
8076
- context._applyLineJoin(this);
8077
- context._applyMiterLimit(this);
8078
- if (!cachingSelf) {
8079
- const o = this.getAbsoluteTransform(top).getMatrix();
8080
- context.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
8081
- context._applyOpacity(this);
8082
- context._applyGlobalCompositeOperation(this);
8083
- }
8084
- if (hasShadow) {
8085
- context._applyShadow(this);
8086
- }
8087
- drawFunc.call(this, context, this);
8262
+ catch (error) {
8263
+ // A throwing sceneFunc can leave a clip on the shared buffer.
8264
+ canvas._releaseIsolationCanvas();
8265
+ throw error;
8266
+ }
8267
+ finally {
8268
+ context.restore();
8088
8269
  }
8089
- context.restore();
8090
8270
  return this;
8091
8271
  }
8092
8272
  drawHit(can, top) {
@@ -8116,8 +8296,12 @@ js: import "konva/skia-backend";
8116
8296
  const o = this.getAbsoluteTransform(top).getMatrix();
8117
8297
  context.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
8118
8298
  }
8119
- drawFunc.call(this, context, this);
8120
- context.restore();
8299
+ try {
8300
+ drawFunc.call(this, context, this);
8301
+ }
8302
+ finally {
8303
+ context.restore();
8304
+ }
8121
8305
  return this;
8122
8306
  }
8123
8307
  /**
@@ -8394,6 +8578,24 @@ js: import "konva/skia-backend";
8394
8578
  * context.fillStrokeShape(shape);
8395
8579
  * });
8396
8580
  */
8581
+ Factory.addGetterSetter(Shape, 'selfRectFunc');
8582
+ /**
8583
+ * get/set the function that returns the self rectangle of a custom shape: the
8584
+ * local, untransformed area its sceneFunc paints, before the stroke and shadow
8585
+ * Konva adds. Use it when drawing goes outside the width and height box, so
8586
+ * bounds, cache(), isolated groups and perfect drawing include that paint.
8587
+ * Paint outside the returned rectangle can be clipped. Meant for custom
8588
+ * shapes, and for Text drawn with charRenderFunc; built-in shapes that compute
8589
+ * their own bounds may ignore it.
8590
+ * @name Konva.Shape#selfRectFunc
8591
+ * @method
8592
+ * @param {Function} selfRectFunc function returning {x, y, width, height}
8593
+ * @returns {Function}
8594
+ * @example
8595
+ * shape.selfRectFunc(() => ({ x: -10, y: -10, width: 120, height: 120 }));
8596
+ * // back to the automatic width and height box
8597
+ * shape.selfRectFunc(undefined);
8598
+ */
8397
8599
  Factory.addGetterSetter(Shape, 'dash');
8398
8600
  /**
8399
8601
  * get/set dash array for stroke.
@@ -8749,7 +8951,7 @@ js: import "konva/skia-backend";
8749
8951
  */
8750
8952
  Factory.addGetterSetter(Shape, 'strokeScaleEnabled', true);
8751
8953
  /**
8752
- * get/set strokeScale enabled flag
8954
+ * get/set strokeScale enabled flag. Text and TextPath always scale their stroke.
8753
8955
  * @name Konva.Shape#strokeScaleEnabled
8754
8956
  * @method
8755
8957
  * @param {Boolean} enabled
@@ -9604,7 +9806,7 @@ js: import "konva/skia-backend";
9604
9806
  }
9605
9807
  return {};
9606
9808
  }
9607
- drawScene(can, top, bufferCanvas) {
9809
+ drawScene(can, top) {
9608
9810
  const layer = this.getLayer(), canvas = can || (layer && layer.getCanvas());
9609
9811
  this._fire(BEFORE_DRAW, {
9610
9812
  node: this,
@@ -9612,7 +9814,8 @@ js: import "konva/skia-backend";
9612
9814
  if (this.clearBeforeDraw()) {
9613
9815
  canvas.getContext().clear();
9614
9816
  }
9615
- Container.prototype.drawScene.call(this, canvas, top, bufferCanvas);
9817
+ Container.prototype.drawScene.call(this, canvas, top);
9818
+ canvas._trimIsolationCanvas();
9616
9819
  this._fire(DRAW, {
9617
9820
  node: this,
9618
9821
  });
@@ -9699,6 +9902,7 @@ js: import "konva/skia-backend";
9699
9902
  }
9700
9903
  }
9701
9904
  destroy() {
9905
+ this.getCanvas()._releaseIsolationCanvas();
9702
9906
  Util.releaseCanvas(this.getNativeCanvasElement(), this.getHitCanvas()._canvas);
9703
9907
  return super.destroy();
9704
9908
  }
@@ -9796,6 +10000,7 @@ js: import "konva/skia-backend";
9796
10000
  * @memberof Konva
9797
10001
  * @augments Konva.Container
9798
10002
  * @param {Object} config
10003
+ * @param {Boolean} [config.isolated=false] composite children as one live image
9799
10004
  * @param {Number} [config.x]
9800
10005
  * @param {Number} [config.y]
9801
10006
  * @param {Number} [config.width]
@@ -9833,7 +10038,65 @@ js: import "konva/skia-backend";
9833
10038
  Util.throw('You may only add groups and shapes to groups.');
9834
10039
  }
9835
10040
  }
10041
+ _drawChildNodes(drawMethod, canvas, top) {
10042
+ // cache() already provides an isolated surface and owns its snapshot.
10043
+ if (drawMethod !== 'drawScene' || !this.isolated() || top === this) {
10044
+ return super._drawChildNodes(drawMethod, canvas, top);
10045
+ }
10046
+ if (!canvas.width || !canvas.height) {
10047
+ return;
10048
+ }
10049
+ const context = canvas.getContext();
10050
+ const surface = canvas._prepareIsolationCanvas(this.getClientRect({ _forDrawing: true, relativeTo: top }));
10051
+ const surfaceContext = surface.getContext();
10052
+ surfaceContext._opacityRoot = this;
10053
+ context.save();
10054
+ try {
10055
+ super._drawChildNodes(drawMethod, surface, top);
10056
+ context._applyOpacity(this);
10057
+ context._drawDeviceBuffer(surface);
10058
+ }
10059
+ catch (error) {
10060
+ // A throwing sceneFunc can leave unbalanced saves or a clip on the
10061
+ // scratch context. Discard it rather than contaminate the next draw.
10062
+ canvas._releaseIsolationCanvas();
10063
+ throw error;
10064
+ }
10065
+ finally {
10066
+ context.restore();
10067
+ surfaceContext._opacityRoot = undefined;
10068
+ }
10069
+ }
9836
10070
  }
10071
+ /**
10072
+ * Get or set group isolation. Default is false.
10073
+ * An isolated group draws its children into a transparent canvas on each scene draw.
10074
+ * The group opacity and globalCompositeOperation apply once to the completed image.
10075
+ * Child blend modes and erasing affect only the content inside that canvas.
10076
+ *
10077
+ * Explicit cache() calls keep their snapshot behavior. clearCache() resumes live drawing.
10078
+ * Hit testing keeps its normal behavior, including hits on erased content.
10079
+ * For video or canvas sources, schedule draws when the source changes.
10080
+ *
10081
+ * Buffers use conservative content bounds at the destination pixel ratio.
10082
+ * Moving the buffer origin changes native edge coverage. A few edge pixels
10083
+ * can differ substantially; isolation does not promise pixel-identical antialiasing.
10084
+ * Like cache(), isolation requires custom drawing (sceneFunc, charRenderFunc) to provide accurate
10085
+ * bounds, including any pixels it paints outside its size: selfRectFunc or a
10086
+ * getSelfRect() override.
10087
+ * Text is bounded by its lines plus one font size, so a glyph with many stacked
10088
+ * marks can be clipped. node-canvas shadows can change with bitmap origin.
10089
+ * Siblings reuse a buffer; unused or oversized buffers are freed after each layer draw.
10090
+ * @name Konva.Group#isolated
10091
+ * @method
10092
+ * @param {Boolean} isolated
10093
+ * @returns {Boolean}
10094
+ * @example
10095
+ * const group = new Konva.Group({ isolated: true, opacity: 0.5 });
10096
+ * group.add(shape1, shape2);
10097
+ * group.isolated(false);
10098
+ */
10099
+ Factory.addGetterSetter(Group, 'isolated', false, getBooleanValidator());
9837
10100
  Group.prototype.nodeType = 'Group';
9838
10101
  _registerNode(Group);
9839
10102
 
@@ -10884,8 +11147,8 @@ js: import "konva/skia-backend";
10884
11147
  this.outerRadius(height / 2);
10885
11148
  }
10886
11149
  getSelfRect() {
10887
- const innerRadius = Math.abs(this.innerRadius());
10888
- const outerRadius = Math.abs(this.outerRadius());
11150
+ const r1 = Math.abs(this.innerRadius()), r2 = Math.abs(this.outerRadius());
11151
+ const innerRadius = Math.min(r1, r2), outerRadius = Math.max(r1, r2);
10889
11152
  const clockwise = this.clockwise();
10890
11153
  // canvas draws a full circle for any non-zero full-turn end angle regardless
10891
11154
  // of direction, and nothing for angle 0. Keep exact turns distinct from
@@ -10928,7 +11191,7 @@ js: import "konva/skia-backend";
10928
11191
  'angle',
10929
11192
  'clockwise',
10930
11193
  ];
10931
- _registerNode(Arc);
11194
+ _registerNode(Arc, true);
10932
11195
  // add getters setters
10933
11196
  Factory.addGetterSetter(Arc, 'innerRadius', 0, getNumberValidator());
10934
11197
  /**
@@ -11342,6 +11605,9 @@ js: import "konva/skia-backend";
11342
11605
  onQuadratic(x0, y0, tp[len - 2], tp[len - 1], points[length - 2], points[length - 1]);
11343
11606
  }
11344
11607
  }
11608
+ _getStrokePadding(miterLimit = this.points().length === 4 && !this.closed() ? 1 : undefined) {
11609
+ return super._getStrokePadding(miterLimit);
11610
+ }
11345
11611
  _sceneFunc(context) {
11346
11612
  const points = this.points(), length = points.length, closed = this.closed(), bezier = this.bezier();
11347
11613
  if (!length) {
@@ -11441,7 +11707,7 @@ js: import "konva/skia-backend";
11441
11707
  }
11442
11708
  Line.prototype.className = 'Line';
11443
11709
  Line.prototype._attrsAffectingSize = ['points', 'bezier', 'tension', 'closed'];
11444
- _registerNode(Line);
11710
+ _registerNode(Line, true);
11445
11711
  // add getters setters
11446
11712
  Factory.addGetterSetter(Line, 'closed', false);
11447
11713
  /**
@@ -12278,7 +12544,7 @@ js: import "konva/skia-backend";
12278
12544
  }
12279
12545
  Path.prototype.className = 'Path';
12280
12546
  Path.prototype._attrsAffectingSize = ['data'];
12281
- _registerNode(Path);
12547
+ _registerNode(Path, true);
12282
12548
  /**
12283
12549
  * get/set SVG path data string. This method
12284
12550
  * also automatically parses the data string
@@ -12410,6 +12676,12 @@ js: import "konva/skia-backend";
12410
12676
  * });
12411
12677
  */
12412
12678
  class Arrow extends Line {
12679
+ _getStrokePadding() {
12680
+ // Arrowheads have joins even when the shaft is a single straight line.
12681
+ return super._getStrokePadding(this.pointerAtBeginning() || this.pointerAtEnding()
12682
+ ? this.miterLimit() || 10
12683
+ : undefined);
12684
+ }
12413
12685
  _sceneFunc(ctx) {
12414
12686
  super._sceneFunc(ctx);
12415
12687
  const points = this.points();
@@ -12526,7 +12798,7 @@ js: import "konva/skia-backend";
12526
12798
  'pointerAtBeginning',
12527
12799
  'pointerAtEnding',
12528
12800
  ];
12529
- _registerNode(Arrow);
12801
+ _registerNode(Arrow, true);
12530
12802
  /**
12531
12803
  * get/set pointerLength
12532
12804
  * @name Konva.Arrow#pointerLength
@@ -12676,6 +12948,9 @@ js: import "konva/skia-backend";
12676
12948
  * });
12677
12949
  */
12678
12950
  class Circle extends Shape {
12951
+ _getStrokePadding() {
12952
+ return super._getStrokePadding(1);
12953
+ }
12679
12954
  _sceneFunc(context) {
12680
12955
  context.beginPath();
12681
12956
  context.arc(0, 0, Math.abs(this.attrs.radius || 0), 0, Math.PI * 2, false);
@@ -12698,7 +12973,7 @@ js: import "konva/skia-backend";
12698
12973
  Circle.prototype._centroid = true;
12699
12974
  Circle.prototype.className = 'Circle';
12700
12975
  Circle.prototype._attrsAffectingSize = ['radius'];
12701
- _registerNode(Circle);
12976
+ _registerNode(Circle, true);
12702
12977
  /**
12703
12978
  * get/set radius
12704
12979
  * @name Konva.Circle#radius
@@ -12805,6 +13080,9 @@ js: import "konva/skia-backend";
12805
13080
  * });
12806
13081
  */
12807
13082
  class Ellipse extends Shape {
13083
+ _getStrokePadding() {
13084
+ return super._getStrokePadding(1);
13085
+ }
12808
13086
  _sceneFunc(context) {
12809
13087
  const rx = Math.abs(this.radiusX()), ry = Math.abs(this.radiusY());
12810
13088
  context.beginPath();
@@ -12828,7 +13106,7 @@ js: import "konva/skia-backend";
12828
13106
  Ellipse.prototype.className = 'Ellipse';
12829
13107
  Ellipse.prototype._centroid = true;
12830
13108
  Ellipse.prototype._attrsAffectingSize = ['radiusX', 'radiusY'];
12831
- _registerNode(Ellipse);
13109
+ _registerNode(Ellipse, true);
12832
13110
  // add getters setters
12833
13111
  Factory.addComponentsGetterSetter(Ellipse, 'radius', ['x', 'y']);
12834
13112
  /**
@@ -13004,7 +13282,7 @@ js: import "konva/skia-backend";
13004
13282
  super.destroy();
13005
13283
  return this;
13006
13284
  }
13007
- _useBufferCanvas() {
13285
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
13008
13286
  if (this.attrs.perfectDrawEnabled === false)
13009
13287
  return false;
13010
13288
  const hasCornerRadius = !!this.cornerRadius();
@@ -13012,7 +13290,10 @@ js: import "konva/skia-backend";
13012
13290
  if (hasCornerRadius && hasShadow) {
13013
13291
  return true;
13014
13292
  }
13015
- return super._useBufferCanvas(true);
13293
+ return super._useBufferCanvas(true, opacity);
13294
+ }
13295
+ _getStrokePadding() {
13296
+ return super._getStrokePadding(this.strokeScaleEnabled() ? 1 : undefined);
13016
13297
  }
13017
13298
  _sceneFunc(context) {
13018
13299
  const width = this.getWidth();
@@ -13090,7 +13371,7 @@ js: import "konva/skia-backend";
13090
13371
  }
13091
13372
  Image.prototype.className = 'Image';
13092
13373
  Image.prototype._attrsAffectingSize = ['image'];
13093
- _registerNode(Image);
13374
+ _registerNode(Image, true);
13094
13375
  /**
13095
13376
  * get/set corner radius
13096
13377
  * @method
@@ -13449,7 +13730,7 @@ js: import "konva/skia-backend";
13449
13730
  if (!this.text()) {
13450
13731
  return;
13451
13732
  }
13452
- let padding = this.padding(), fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, verticalAlign = this.verticalAlign(), direction = this.direction(), alignY = 0, align = this.align(), totalWidth = this.getWidth(), letterSpacing = this.letterSpacing(), charRenderFunc = this.charRenderFunc(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n;
13733
+ let padding = this.padding(), fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, direction = this.direction(), align = this.align(), totalWidth = this.getWidth(), letterSpacing = this.letterSpacing(), charRenderFunc = this.charRenderFunc(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n;
13453
13734
  let translateY = lineHeightPx / 2;
13454
13735
  let baseline = MIDDLE;
13455
13736
  if (!Konva$2.legacyTextRendering) {
@@ -13470,14 +13751,7 @@ js: import "konva/skia-backend";
13470
13751
  context.setAttr('font', this._getContextFont());
13471
13752
  context.setAttr('textBaseline', baseline);
13472
13753
  context.setAttr('textAlign', LEFT$1);
13473
- // handle vertical alignment
13474
- if (verticalAlign === MIDDLE) {
13475
- alignY = (this.getHeight() - textArrLen * lineHeightPx - padding * 2) / 2;
13476
- }
13477
- else if (verticalAlign === BOTTOM) {
13478
- alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
13479
- }
13480
- context.translate(padding, alignY + padding);
13754
+ context.translate(padding, this._getTextTop());
13481
13755
  // Start the callback with the same fill that normal drawing would use.
13482
13756
  // Assigning that color again then needs no override, including black.
13483
13757
  if (charRenderFunc) {
@@ -13606,6 +13880,16 @@ js: import "konva/skia-backend";
13606
13880
  }
13607
13881
  }
13608
13882
  }
13883
+ _getTextTop() {
13884
+ const padding = this.padding(), free = this.getHeight() -
13885
+ this.textArr.length * this.lineHeight() * this.fontSize() -
13886
+ padding * 2, verticalAlign = this.verticalAlign();
13887
+ if (verticalAlign === MIDDLE)
13888
+ return padding + free / 2;
13889
+ if (verticalAlign === BOTTOM)
13890
+ return padding + free;
13891
+ return padding;
13892
+ }
13609
13893
  _getUnderlineOffset() {
13610
13894
  var _a;
13611
13895
  return ((_a = this.underlineOffset()) !== null && _a !== void 0 ? _a : Math.round(this.fontSize() / (!Konva$2.legacyTextRendering ? 4 : 2)));
@@ -13617,21 +13901,41 @@ js: import "konva/skia-backend";
13617
13901
  return rect;
13618
13902
  }
13619
13903
  // the underline of the last line is drawn below the text block
13620
- const fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, padding = this.padding(), verticalAlign = this.verticalAlign(), blockHeight = lines * lineHeightPx + padding * 2;
13621
- let bottom = lines * lineHeightPx - lineHeightPx / 2 + padding;
13904
+ const fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize;
13905
+ let bottom = this._getTextTop() + lines * lineHeightPx - lineHeightPx / 2;
13622
13906
  if (!Konva$2.legacyTextRendering) {
13623
13907
  bottom += this._baselineShift;
13624
13908
  }
13625
- if (verticalAlign === MIDDLE) {
13626
- bottom += (rect.height - blockHeight) / 2;
13627
- }
13628
- else if (verticalAlign === BOTTOM) {
13629
- bottom += rect.height - blockHeight;
13630
- }
13631
13909
  bottom += this._getUnderlineOffset() + getDecorationLineWidth(fontSize) / 2;
13632
- rect.height = Math.max(rect.height, bottom);
13910
+ rect.height = Math.max(rect.y + rect.height, bottom) - rect.y;
13633
13911
  return rect;
13634
13912
  }
13913
+ _getSelfRectForDrawing() {
13914
+ // Lines can be wider than the box, negative spacing moves the pen back,
13915
+ // and glyph ink passes the advance box (italics, accents): up to 0.6em
13916
+ // in common fonts, so pad by 1em.
13917
+ const rect = this.getSelfRect(), fontSize = this.fontSize(), padding = this.padding(), lineHeightPx = this.lineHeight() * fontSize, available = this.getWidth() - padding * 2, align = this.align(), blockHeight = this.textArr.length * lineHeightPx, top = this._getTextTop();
13918
+ let left = rect.x, right = rect.x + rect.width;
13919
+ for (const { text, width } of this.textArr) {
13920
+ const x = padding +
13921
+ (align === RIGHT$1
13922
+ ? available - width
13923
+ : align === CENTER
13924
+ ? (available - width) / 2
13925
+ : 0);
13926
+ const back = Math.min(0, this.letterSpacing()) * text.length;
13927
+ left = Math.min(left, x + back);
13928
+ right = Math.max(right, x + width - back);
13929
+ }
13930
+ const underline = this.textDecoration().includes('underline');
13931
+ const y = Math.min(rect.y, top + (underline ? Math.min(0, this._getUnderlineOffset()) : 0));
13932
+ return {
13933
+ x: left - fontSize,
13934
+ y: y - fontSize,
13935
+ width: right - left + fontSize * 2,
13936
+ height: Math.max(rect.y + rect.height, top + blockHeight) - y + fontSize * 2,
13937
+ };
13938
+ }
13635
13939
  _hitFunc(context) {
13636
13940
  const width = this.getWidth(), height = this.getHeight();
13637
13941
  context.beginPath();
@@ -13912,7 +14216,11 @@ js: import "konva/skia-backend";
13912
14216
  getStrokeScaleEnabled() {
13913
14217
  return true;
13914
14218
  }
13915
- _useBufferCanvas() {
14219
+ _getStrokePadding() {
14220
+ // _strokeFunc uses a miter limit of two for glyph outlines.
14221
+ return super._getStrokePadding(2);
14222
+ }
14223
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
13916
14224
  if (this.attrs.perfectDrawEnabled === false)
13917
14225
  return false;
13918
14226
  const hasLine = this.textDecoration().indexOf('underline') !== -1 ||
@@ -13921,10 +14229,10 @@ js: import "konva/skia-backend";
13921
14229
  if (hasLine && hasShadow) {
13922
14230
  return true;
13923
14231
  }
13924
- if (hasShadow && this.getAbsoluteOpacity() !== 1 && hasShadowOpacityBug()) {
14232
+ if (hasShadow && opacity !== 1 && hasShadowOpacityBug()) {
13925
14233
  return true;
13926
14234
  }
13927
- return super._useBufferCanvas();
14235
+ return super._useBufferCanvas(forceFill, opacity);
13928
14236
  }
13929
14237
  }
13930
14238
  Text.prototype._fillFunc = _fillFunc$1;
@@ -13933,7 +14241,7 @@ js: import "konva/skia-backend";
13933
14241
  // the decoration attrs are not in ATTR_CHANGE_LIST: they change the bounds
13934
14242
  // through getSelfRect, but need no relayout
13935
14243
  Text.prototype._attrsAffectingSize = ATTR_CHANGE_LIST$2.filter((attr) => attr !== 'width' && attr !== 'height').concat(['textDecoration', 'underlineOffset']);
13936
- _registerNode(Text);
14244
+ _registerNode(Text, true);
13937
14245
  // update text data for certain attr changes
13938
14246
  Text.prototype.on(ATTR_CHANGE_LIST$2.map((attr) => attr + CHANGE_KONVA).join(' '), function () {
13939
14247
  this._setTextData();
@@ -14406,31 +14714,29 @@ js: import "konva/skia-backend";
14406
14714
  context.fillStrokeShape(this);
14407
14715
  }
14408
14716
  getSelfRect() {
14409
- let x = 0, y = 0, pointerWidth = this.pointerWidth(), pointerHeight = this.pointerHeight(), direction = this.pointerDirection(), width = this.width(), height = this.height();
14410
- if (direction === UP) {
14411
- y -= pointerHeight;
14412
- height += pointerHeight;
14413
- }
14414
- else if (direction === DOWN) {
14415
- height += pointerHeight;
14416
- }
14417
- else if (direction === LEFT) {
14418
- x -= pointerWidth;
14419
- width += pointerWidth;
14717
+ const pointerWidth = this.pointerWidth(), pointerHeight = this.pointerHeight(), direction = this.pointerDirection(), width = this.width(), height = this.height();
14718
+ // The pointer base is centred on its side and can be longer than it.
14719
+ if (direction === UP || direction === DOWN) {
14720
+ return {
14721
+ x: Math.min(0, (width - pointerWidth) / 2),
14722
+ y: direction === UP ? -pointerHeight : 0,
14723
+ width: Math.max(width, pointerWidth),
14724
+ height: height + pointerHeight,
14725
+ };
14420
14726
  }
14421
- else if (direction === RIGHT) {
14422
- width += pointerWidth;
14727
+ if (direction === LEFT || direction === RIGHT) {
14728
+ return {
14729
+ x: direction === LEFT ? -pointerWidth : 0,
14730
+ y: Math.min(0, (height - pointerHeight) / 2),
14731
+ width: width + pointerWidth,
14732
+ height: Math.max(height, pointerHeight),
14733
+ };
14423
14734
  }
14424
- return {
14425
- x: x,
14426
- y: y,
14427
- width: width,
14428
- height: height,
14429
- };
14735
+ return { x: 0, y: 0, width, height };
14430
14736
  }
14431
14737
  }
14432
14738
  Tag.prototype.className = 'Tag';
14433
- _registerNode(Tag);
14739
+ _registerNode(Tag, true);
14434
14740
  /**
14435
14741
  * get/set pointer direction
14436
14742
  * @name Konva.Tag#pointerDirection
@@ -14567,6 +14873,9 @@ js: import "konva/skia-backend";
14567
14873
  * });
14568
14874
  */
14569
14875
  class Rect extends Shape {
14876
+ _getStrokePadding() {
14877
+ return super._getStrokePadding(this.strokeScaleEnabled() ? 1 : undefined);
14878
+ }
14570
14879
  _sceneFunc(context) {
14571
14880
  const cornerRadius = this.cornerRadius(), width = this.width(), height = this.height();
14572
14881
  context.beginPath();
@@ -14582,7 +14891,7 @@ js: import "konva/skia-backend";
14582
14891
  }
14583
14892
  }
14584
14893
  Rect.prototype.className = 'Rect';
14585
- _registerNode(Rect);
14894
+ _registerNode(Rect, true);
14586
14895
  /**
14587
14896
  * get/set corner radius
14588
14897
  * @method
@@ -14765,7 +15074,7 @@ js: import "konva/skia-backend";
14765
15074
  RegularPolygon.prototype.className = 'RegularPolygon';
14766
15075
  RegularPolygon.prototype._centroid = true;
14767
15076
  RegularPolygon.prototype._attrsAffectingSize = ['radius', 'sides'];
14768
- _registerNode(RegularPolygon);
15077
+ _registerNode(RegularPolygon, true);
14769
15078
  /**
14770
15079
  * get/set radius
14771
15080
  * @method
@@ -14905,6 +15214,9 @@ js: import "konva/skia-backend";
14905
15214
  * });
14906
15215
  */
14907
15216
  class Ring extends Shape {
15217
+ _getStrokePadding() {
15218
+ return super._getStrokePadding(1);
15219
+ }
14908
15220
  _sceneFunc(context) {
14909
15221
  const innerRadius = Math.abs(this.innerRadius()), outerRadius = Math.abs(this.outerRadius());
14910
15222
  context.beginPath();
@@ -14914,6 +15226,10 @@ js: import "konva/skia-backend";
14914
15226
  context.closePath();
14915
15227
  context.fillStrokeShape(this);
14916
15228
  }
15229
+ getSelfRect() {
15230
+ const radius = Math.max(Math.abs(this.innerRadius()), Math.abs(this.outerRadius()));
15231
+ return { x: -radius, y: -radius, width: radius * 2, height: radius * 2 };
15232
+ }
14917
15233
  getWidth() {
14918
15234
  return Math.abs(this.outerRadius()) * 2;
14919
15235
  }
@@ -14930,7 +15246,7 @@ js: import "konva/skia-backend";
14930
15246
  Ring.prototype.className = 'Ring';
14931
15247
  Ring.prototype._centroid = true;
14932
15248
  Ring.prototype._attrsAffectingSize = ['innerRadius', 'outerRadius'];
14933
- _registerNode(Ring);
15249
+ _registerNode(Ring, true);
14934
15250
  /**
14935
15251
  * get/set innerRadius
14936
15252
  * @method
@@ -15124,6 +15440,9 @@ js: import "konva/skia-backend";
15124
15440
  offsetY: offset[index * 2 + 1] || 0,
15125
15441
  };
15126
15442
  }
15443
+ _getStrokePadding() {
15444
+ return super._getStrokePadding(this.strokeScaleEnabled() ? 1 : undefined);
15445
+ }
15127
15446
  _sceneFunc(context) {
15128
15447
  const frame = this._getFrame();
15129
15448
  if (!frame) {
@@ -15159,8 +15478,8 @@ js: import "konva/skia-backend";
15159
15478
  }
15160
15479
  : { x: 0, y: 0, width: 0, height: 0 };
15161
15480
  }
15162
- _useBufferCanvas() {
15163
- return super._useBufferCanvas(true);
15481
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
15482
+ return super._useBufferCanvas(true, opacity);
15164
15483
  }
15165
15484
  _setInterval() {
15166
15485
  const that = this;
@@ -15213,7 +15532,7 @@ js: import "konva/skia-backend";
15213
15532
  }
15214
15533
  }
15215
15534
  Sprite.prototype.className = 'Sprite';
15216
- _registerNode(Sprite);
15535
+ _registerNode(Sprite, true);
15217
15536
  // add getters setters
15218
15537
  Factory.addGetterSetter(Sprite, 'animation');
15219
15538
  /**
@@ -15454,6 +15773,10 @@ js: import "konva/skia-backend";
15454
15773
  context.closePath();
15455
15774
  context.fillStrokeShape(this);
15456
15775
  }
15776
+ getSelfRect() {
15777
+ const radius = Math.max(Math.abs(this.innerRadius()), Math.abs(this.outerRadius()));
15778
+ return { x: -radius, y: -radius, width: radius * 2, height: radius * 2 };
15779
+ }
15457
15780
  getWidth() {
15458
15781
  return this.outerRadius() * 2;
15459
15782
  }
@@ -15470,7 +15793,7 @@ js: import "konva/skia-backend";
15470
15793
  Star.prototype.className = 'Star';
15471
15794
  Star.prototype._centroid = true;
15472
15795
  Star.prototype._attrsAffectingSize = ['innerRadius', 'outerRadius'];
15473
- _registerNode(Star);
15796
+ _registerNode(Star, true);
15474
15797
  /**
15475
15798
  * get/set number of points
15476
15799
  * @name Konva.Star#numPoints
@@ -15894,6 +16217,21 @@ js: import "konva/skia-backend";
15894
16217
  height: rect.height + fontSize,
15895
16218
  };
15896
16219
  }
16220
+ // Like Text: a glyph stroke is drawn with the text, so it always scales.
16221
+ getStrokeScaleEnabled() {
16222
+ return true;
16223
+ }
16224
+ _getSelfRectForDrawing() {
16225
+ // getSelfRect() pads glyph anchors by half the font size; ascenders,
16226
+ // accents and other baselines reach further from the path.
16227
+ const rect = this.getSelfRect(), pad = this.fontSize();
16228
+ return {
16229
+ x: rect.x - pad,
16230
+ y: rect.y - pad,
16231
+ width: rect.width + pad * 2,
16232
+ height: rect.height + pad * 2,
16233
+ };
16234
+ }
15897
16235
  }
15898
16236
  TextPath.prototype._fillFunc = _fillFunc;
15899
16237
  TextPath.prototype._strokeFunc = _strokeFunc;
@@ -15912,7 +16250,7 @@ js: import "konva/skia-backend";
15912
16250
  'fontVariant',
15913
16251
  'direction',
15914
16252
  ];
15915
- _registerNode(TextPath);
16253
+ _registerNode(TextPath, true);
15916
16254
  TextPath.prototype.on('dataChange.konva', function () {
15917
16255
  this._readDataAttribute();
15918
16256
  this._setTextData();
@@ -16152,6 +16490,7 @@ js: import "konva/skia-backend";
16152
16490
  'transformsEnabledChange',
16153
16491
  'strokeWidthChange',
16154
16492
  'strokeChange',
16493
+ 'selfRectFuncChange',
16155
16494
  'strokeLinearGradientColorStopsChange',
16156
16495
  'strokeScaleEnabledChange',
16157
16496
  'strokeEnabledChange',
@@ -16366,6 +16705,8 @@ js: import "konva/skia-backend";
16366
16705
  this._transformWindow = null;
16367
16706
  this._elementsCreated = false;
16368
16707
  this._updateScheduled = false;
16708
+ // an attached container's descendants changed after the last layout
16709
+ this._subtreeChanged = false;
16369
16710
  this._createElements();
16370
16711
  // bindings
16371
16712
  this._handleMouseMove = this._handleMouseMove.bind(this);
@@ -16464,6 +16805,8 @@ js: import "konva/skia-backend";
16464
16805
  }
16465
16806
  node.on(TRANSFORM_CHANGE_STR.map((e) => e + `.${this._getEventNamespace()}`).join(' '), onChange);
16466
16807
  node.on(`absoluteTransformChange.${this._getEventNamespace()}`, onChange);
16808
+ if (node instanceof Container)
16809
+ node._addSubtreeObserver(this);
16467
16810
  node.on(`destroy.${this._getEventNamespace()}`, () => {
16468
16811
  // a destroyed node has nothing left to transform
16469
16812
  this.setNodes(this._nodes.filter((n) => n !== node));
@@ -16540,6 +16883,7 @@ js: import "konva/skia-backend";
16540
16883
  if (this._nodes) {
16541
16884
  this._nodes.forEach((node) => {
16542
16885
  node.off('.' + this._getEventNamespace());
16886
+ node._removeSubtreeObserver(this);
16543
16887
  });
16544
16888
  }
16545
16889
  this._nodes = [];
@@ -16569,6 +16913,42 @@ js: import "konva/skia-backend";
16569
16913
  this._clearCache('transform');
16570
16914
  this._clearSelfAndDescendantCache('absoluteTransform');
16571
16915
  }
16916
+ // Called for every change below an attached container. A burst of child
16917
+ // changes only drops cached values here. The new bounds are measured once,
16918
+ // before the next frame, or earlier if a draw or transform step comes first
16919
+ // (_syncSubtreeChange).
16920
+ _onSubtreeChange(node) {
16921
+ var _a, _b;
16922
+ if (this._fitting)
16923
+ return;
16924
+ // nothing was measured since the last change, so nothing cached is stale
16925
+ if (this._subtreeChanged && !this._cache[NODES_RECT])
16926
+ return;
16927
+ this._resetTransformCache(node);
16928
+ if (!this._subtreeChanged) {
16929
+ this._subtreeChanged = true;
16930
+ // queued ahead of the draw the change requests, so that draw shows the
16931
+ // new layout. Updating inside the draw would request one more frame.
16932
+ Util.requestAnimFrame(() => this._syncSubtreeChange(), (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow());
16933
+ // the transformer can be on another layer than the changed node
16934
+ (_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
16935
+ }
16936
+ }
16937
+ _syncSubtreeChange() {
16938
+ var _a;
16939
+ // while dragging, the dragmove handler updates the layout
16940
+ if (this._subtreeChanged && ((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) && !this.isDragging()) {
16941
+ this._update();
16942
+ }
16943
+ }
16944
+ drawScene(can, top) {
16945
+ this._syncSubtreeChange();
16946
+ return super.drawScene(can, top);
16947
+ }
16948
+ drawHit(can, top) {
16949
+ this._syncSubtreeChange();
16950
+ return super.drawHit(can, top);
16951
+ }
16572
16952
  _getNodeRect() {
16573
16953
  return this._getCache(NODES_RECT, this.__getNodeRect);
16574
16954
  }
@@ -16594,13 +16974,14 @@ js: import "konva/skia-backend";
16594
16974
  // Bounds may only be reused when they are a pure function of the node's
16595
16975
  // own size: then every change reaches us either through _setAttr or
16596
16976
  // through the size probe below. A shape with its own getSelfRect - Line
16597
- // mutates its points array in place, and a user subclass can measure
16598
- // anything at all - may change with no signal, so it is measured every
16599
- // time. Groups depend on their descendants, and a cached ancestor
16600
- // suppresses descendant transform-change notifications, so both are
16601
- // measured too.
16977
+ // mutates its points array in place, and a user subclass or selfRectFunc
16978
+ // can measure anything at all - may change with no signal, so it is
16979
+ // measured every time. Groups depend on their descendants, and a cached
16980
+ // ancestor suppresses descendant transform-change notifications, so
16981
+ // both are measured too.
16602
16982
  const cacheable = node instanceof Shape &&
16603
16983
  node.getSelfRect === Shape.prototype.getSelfRect &&
16984
+ !node.attrs.selfRectFunc &&
16604
16985
  !node._hasCachedAncestor();
16605
16986
  // width() and height() are the only inputs of the default getSelfRect and
16606
16987
  // they can come from outside the attrs (an Image source that loads or
@@ -16829,6 +17210,8 @@ js: import "konva/skia-backend";
16829
17210
  (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._batchEvents(() => this._moveTransform(e));
16830
17211
  }
16831
17212
  _moveTransform(e) {
17213
+ // anchors must show the current bounds before they are measured against
17214
+ this._syncSubtreeChange();
16832
17215
  let x, y, newHypotenuse;
16833
17216
  const anchorNode = this._anchors[this._movingAnchorName];
16834
17217
  const stage = anchorNode.getStage();
@@ -17266,7 +17649,9 @@ js: import "konva/skia-backend";
17266
17649
  }
17267
17650
  /**
17268
17651
  * force update of Konva.Transformer.
17269
- * Use it when you updated attached Konva.Group and now you need to reset transformer size
17652
+ * The transformer follows attribute changes of attached nodes and, for an attached group, any change of its descendants.
17653
+ * Use it after a change that sets no attribute, for example a `Line` points array mutated in place
17654
+ * or a custom `getSelfRect()` that depends on outside state.
17270
17655
  * @method
17271
17656
  * @name Konva.Transformer#forceUpdate
17272
17657
  */
@@ -17282,6 +17667,7 @@ js: import "konva/skia-backend";
17282
17667
  }
17283
17668
  _update() {
17284
17669
  var _a;
17670
+ this._subtreeChanged = false;
17285
17671
  const attrs = this._getNodeRect();
17286
17672
  this._updateElements(attrs);
17287
17673
  const draggable = this.nodes().some((node) => node.draggable());
@@ -18059,7 +18445,7 @@ js: import "konva/skia-backend";
18059
18445
  Wedge.prototype.className = 'Wedge';
18060
18446
  Wedge.prototype._centroid = true;
18061
18447
  Wedge.prototype._attrsAffectingSize = ['radius'];
18062
- _registerNode(Wedge);
18448
+ _registerNode(Wedge, true);
18063
18449
  /**
18064
18450
  * get/set radius
18065
18451
  * @name Konva.Wedge#radius