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/lib/Canvas.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { IRect } from './types.ts';
1
2
  import type { Context } from './Context.ts';
2
3
  interface ICanvasConfig {
3
4
  width?: number;
@@ -25,8 +26,6 @@ export declare class Canvas {
25
26
  _logicalWidth: number;
26
27
  _logicalHeight: number;
27
28
  isCache: boolean;
28
- x: number;
29
- y: number;
30
29
  constructor(config: ICanvasConfig);
31
30
  /**
32
31
  * get canvas context
@@ -78,6 +77,14 @@ export declare class Canvas {
78
77
  toDataURL(mimeType: any, quality: any): string;
79
78
  }
80
79
  export declare class SceneCanvas extends Canvas {
80
+ private _isolationCanvas?;
81
+ _isolationRect?: IRect;
82
+ private _isolationPeak;
83
+ private _isolationLowFrames;
84
+ _prepareIsolationCanvas(rect?: IRect): SceneCanvas;
85
+ _trimIsolationCanvas(): void;
86
+ _releaseIsolationCanvas(): void;
87
+ setSize(width: any, height: any): void;
81
88
  constructor(config?: ICanvasConfig);
82
89
  }
83
90
  export declare class HitCanvas extends Canvas {
package/lib/Canvas.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Util } from "./Util.js";
1
+ import { Transform, Util } from "./Util.js";
2
2
  import { SceneContext, HitContext } from "./Context.js";
3
3
  import { Konva } from "./Global.js";
4
4
  /**
@@ -22,10 +22,6 @@ export class Canvas {
22
22
  this._logicalWidth = 0;
23
23
  this._logicalHeight = 0;
24
24
  this.isCache = false;
25
- // origin of a buffer canvas in the coordinate space of the canvas it is
26
- // drawn back into, see Shape.drawScene
27
- this.x = 0;
28
- this.y = 0;
29
25
  const conf = config || {};
30
26
  // a Konva.pixelRatio reset to undefined or 0 means the device ratio
31
27
  const pixelRatio = conf.pixelRatio ||
@@ -156,8 +152,90 @@ export class Canvas {
156
152
  }
157
153
  }
158
154
  export class SceneCanvas extends Canvas {
155
+ // A cleared surface over `rect` (in the current drawing space), or over the
156
+ // whole canvas, in this canvas's device pixels. Siblings reuse it; nested
157
+ // groups borrow from the surface itself, so they cannot clear a parent's
158
+ // unfinished image.
159
+ _prepareIsolationCanvas(rect) {
160
+ const { a, b, c, d, e, f } = this.getContext()._context.getTransform();
161
+ const view = this._isolationRect || this;
162
+ let x = 0, y = 0, width = view.width, height = view.height;
163
+ if (rect) {
164
+ const box = new Transform([a, b, c, d, e, f])._getTransformedRect(rect);
165
+ // Round outwards, with a pixel for antialiasing.
166
+ x = Math.max(0, Math.floor(box.x) - 1);
167
+ y = Math.max(0, Math.floor(box.y) - 1);
168
+ width = Math.min(width, Math.ceil(box.x + box.width) + 1) - x;
169
+ height = Math.min(height, Math.ceil(box.y + box.height) + 1) - y;
170
+ }
171
+ if (!(width > 0 && height > 0)) {
172
+ // An empty source must still composite for copy/destination-in/etc.
173
+ x = y = 0;
174
+ width = height = 1;
175
+ }
176
+ const surface = (this._isolationCanvas || (this._isolationCanvas = new SceneCanvas({
177
+ width: 0,
178
+ height: 0,
179
+ pixelRatio: this.pixelRatio,
180
+ })));
181
+ if (surface.width < width || surface.height < height) {
182
+ // Headroom on a short side, so a growing group does not reallocate
183
+ // every frame.
184
+ const ratio = this.pixelRatio;
185
+ const grow = (need, have, max) => Math.min(max, Math.ceil((have < need ? Math.max(need, have * 1.5) : have) / ratio));
186
+ surface.setSize(grow(width, surface.width, this._logicalWidth), grow(height, surface.height, this._logicalHeight));
187
+ }
188
+ surface._isolationRect = { x, y, width, height };
189
+ const peak = surface._isolationPeak;
190
+ peak.width = Math.max(peak.width, width);
191
+ peak.height = Math.max(peak.height, height);
192
+ const context = surface.getContext();
193
+ context.setTransform(1, 0, 0, 1, 0, 0);
194
+ context.clearRect(0, 0, width, height);
195
+ context.setTransform(a, b, c, d, e - x, f - y);
196
+ const parent = this.getContext();
197
+ context.imageSmoothingEnabled = parent.imageSmoothingEnabled;
198
+ context.imageSmoothingQuality = parent.imageSmoothingQuality;
199
+ context.direction = parent.direction;
200
+ return surface;
201
+ }
202
+ // After a frame, free surfaces that ten frames in a row did not use or used
203
+ // a small part of. Content that changes size keeps its surface.
204
+ _trimIsolationCanvas() {
205
+ const surface = this._isolationCanvas;
206
+ if (!surface)
207
+ return;
208
+ const peak = surface._isolationPeak;
209
+ const low = surface.width * surface.height > 4 * peak.width * peak.height;
210
+ surface._isolationLowFrames = low ? surface._isolationLowFrames + 1 : 0;
211
+ if (surface._isolationLowFrames >= 10) {
212
+ this._releaseIsolationCanvas();
213
+ }
214
+ else {
215
+ peak.width = peak.height = 0;
216
+ surface._trimIsolationCanvas();
217
+ }
218
+ }
219
+ _releaseIsolationCanvas() {
220
+ const surface = this._isolationCanvas;
221
+ if (surface) {
222
+ surface._releaseIsolationCanvas();
223
+ Util.releaseCanvas(surface._canvas);
224
+ this._isolationCanvas = undefined;
225
+ }
226
+ }
227
+ setSize(width, height) {
228
+ // A bitmap reset also resets its origin-clean state. A surface from the
229
+ // previous lifetime may be tainted even after clearing its pixels.
230
+ this._releaseIsolationCanvas();
231
+ super.setSize(width, height);
232
+ }
159
233
  constructor(config = { width: 0, height: 0, willReadFrequently: false }) {
160
234
  super(config);
235
+ // The largest width and height requested since the last trim.
236
+ this._isolationPeak = { width: 0, height: 0 };
237
+ // Consecutive frames that used less than a quarter of the surface.
238
+ this._isolationLowFrames = 0;
161
239
  this.context = new SceneContext(this, {
162
240
  willReadFrequently: config.willReadFrequently,
163
241
  });
@@ -168,9 +168,10 @@ export declare abstract class Container<ChildType extends Node = Node, Config ex
168
168
  getAllIntersections(pos: any): Shape<import("./Shape.ts").ShapeConfig>[];
169
169
  _clearSelfAndDescendantCache(attr?: string): void;
170
170
  _setChildrenIndices(): void;
171
- drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas): this;
171
+ drawScene(can?: SceneCanvas, top?: Node): this;
172
172
  drawHit(can?: HitCanvas, top?: Node): this;
173
- _drawChildren(drawMethod: any, canvas: any, top: any, bufferCanvas?: any): void;
173
+ _drawChildren(drawMethod: any, canvas: any, top: any): void;
174
+ _drawChildNodes(drawMethod: any, canvas: any, top?: Node): void;
174
175
  getClientRect(config?: GetClientRectConfig): IRect;
175
176
  clip: GetSet<IRect, this>;
176
177
  clipX: GetSet<number, this>;
package/lib/Container.js CHANGED
@@ -60,6 +60,7 @@ export class Container extends Node {
60
60
  });
61
61
  this.children = [];
62
62
  // because all children were detached from parent, request draw via container
63
+ this._notifySubtreeChange();
63
64
  this._requestDraw();
64
65
  return this;
65
66
  }
@@ -77,6 +78,7 @@ export class Container extends Node {
77
78
  });
78
79
  this.children = [];
79
80
  // because all children were detached from parent, request draw via container
81
+ this._notifySubtreeChange();
80
82
  this._requestDraw();
81
83
  return this;
82
84
  }
@@ -115,6 +117,7 @@ export class Container extends Node {
115
117
  this._fire('add', {
116
118
  child: child,
117
119
  });
120
+ this._notifySubtreeChange();
118
121
  this._requestDraw();
119
122
  // chainable
120
123
  return this;
@@ -315,9 +318,10 @@ export class Container extends Node {
315
318
  (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child, n) {
316
319
  child.index = n;
317
320
  });
321
+ this._notifySubtreeChange();
318
322
  this._requestDraw();
319
323
  }
320
- drawScene(can, top, bufferCanvas) {
324
+ drawScene(can, top) {
321
325
  const layer = this.getLayer(), canvas = can || (layer && layer.getCanvas()), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedSceneCanvas = cachedCanvas && cachedCanvas.scene;
322
326
  const caching = top === this;
323
327
  if (!(top ? this._isVisible(top) : this.isVisible()) && !caching) {
@@ -331,7 +335,7 @@ export class Container extends Node {
331
335
  context.restore();
332
336
  }
333
337
  else {
334
- this._drawChildren('drawScene', canvas, top, bufferCanvas);
338
+ this._drawChildren('drawScene', canvas, top);
335
339
  }
336
340
  return this;
337
341
  }
@@ -352,8 +356,7 @@ export class Container extends Node {
352
356
  }
353
357
  return this;
354
358
  }
355
- _drawChildren(drawMethod, canvas, top, bufferCanvas) {
356
- var _a;
359
+ _drawChildren(drawMethod, canvas, top) {
357
360
  const context = canvas && canvas.getContext(), clipWidth = this.clipWidth(), clipHeight = this.clipHeight(), clipFunc = this.clipFunc(), hasClip = (typeof clipWidth === 'number' && typeof clipHeight === 'number') ||
358
361
  clipFunc;
359
362
  const selfCache = top === this;
@@ -383,23 +386,34 @@ export class Container extends Node {
383
386
  context.save();
384
387
  context._applyGlobalCompositeOperation(this);
385
388
  }
386
- (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
387
- child[drawMethod](canvas, top, bufferCanvas);
388
- });
389
- if (hasComposition) {
390
- context.restore();
389
+ try {
390
+ this._drawChildNodes(drawMethod, canvas, top);
391
391
  }
392
- if (hasClip) {
393
- context.restore();
392
+ finally {
393
+ if (hasComposition) {
394
+ context.restore();
395
+ }
396
+ if (hasClip) {
397
+ context.restore();
398
+ }
394
399
  }
395
400
  }
401
+ _drawChildNodes(drawMethod, canvas, top) {
402
+ var _a;
403
+ (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
404
+ child[drawMethod](canvas, top);
405
+ });
406
+ }
396
407
  getClientRect(config = {}) {
397
408
  var _a;
409
+ const cachedRect = this._getCachedSceneRect(config);
410
+ if (cachedRect)
411
+ return cachedRect;
398
412
  const skipTransform = config.skipTransform;
399
413
  const relativeTo = config.relativeTo;
400
414
  const [a, b, c, d] = this.getAbsoluteTransform().getMatrix();
401
415
  // A zero scale cannot project local padding for a non-scaling stroke.
402
- const measureTransformed = !skipTransform && a * d - b * c === 0;
416
+ const measureTransformed = !skipTransform && (config._forDrawing || a * d - b * c === 0);
403
417
  let minX, minY, maxX, maxY;
404
418
  let selfRect = {
405
419
  x: Infinity,
@@ -415,6 +429,7 @@ export class Container extends Node {
415
429
  }
416
430
  const rect = child.getClientRect({
417
431
  relativeTo: measureTransformed ? relativeTo : that,
432
+ _forDrawing: config._forDrawing,
418
433
  skipShadow: config.skipShadow,
419
434
  skipStroke: config.skipStroke,
420
435
  });
package/lib/Context.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import type { Canvas } from './Canvas.ts';
1
+ import type { Canvas, SceneCanvas } from './Canvas.ts';
2
2
  import type { Shape } from './Shape.ts';
3
3
  import type { IRect } from './types.ts';
4
4
  import type { Node } from './Node.ts';
5
- declare const CONTEXT_PROPERTIES: readonly ["fillStyle", "strokeStyle", "shadowColor", "shadowBlur", "shadowOffsetX", "shadowOffsetY", "letterSpacing", "lineCap", "lineDashOffset", "lineJoin", "lineWidth", "miterLimit", "direction", "font", "textAlign", "textBaseline", "globalAlpha", "globalCompositeOperation", "imageSmoothingEnabled", "filter"];
5
+ declare const CONTEXT_PROPERTIES: readonly ["fillStyle", "strokeStyle", "shadowColor", "shadowBlur", "shadowOffsetX", "shadowOffsetY", "letterSpacing", "lineCap", "lineDashOffset", "lineJoin", "lineWidth", "miterLimit", "direction", "font", "textAlign", "textBaseline", "globalAlpha", "globalCompositeOperation", "imageSmoothingEnabled", "imageSmoothingQuality", "filter"];
6
6
  export declare function isCSSFiltersSupported(): boolean;
7
7
  interface ExtendedCanvasRenderingContext2D extends CanvasRenderingContext2D {
8
8
  letterSpacing: string;
@@ -84,7 +84,10 @@ export declare class Context {
84
84
  * @param {Number} [bounds.height]
85
85
  */
86
86
  clear(bounds?: IRect): void;
87
+ _drawDeviceBuffer(canvas: SceneCanvas): void;
87
88
  _applyLineCap(shape: Shape): void;
89
+ _opacityRoot: Node | undefined;
90
+ _getOpacity(shape: Node): any;
88
91
  _applyOpacity(shape: Node): void;
89
92
  _applyLineJoin(shape: Shape): void;
90
93
  _applyMiterLimit(shape: Shape): void;
package/lib/Context.js CHANGED
@@ -72,6 +72,7 @@ const CONTEXT_PROPERTIES = [
72
72
  'globalAlpha',
73
73
  'globalCompositeOperation',
74
74
  'imageSmoothingEnabled',
75
+ 'imageSmoothingQuality',
75
76
  'filter',
76
77
  ];
77
78
  const traceArrMax = 100;
@@ -244,14 +245,34 @@ export class Context {
244
245
  this.clearRect(0, 0, canvas.getWidth() / canvas.pixelRatio, canvas.getHeight() / canvas.pixelRatio);
245
246
  }
246
247
  }
248
+ // Copy a buffer already rasterized in destination pixels. The caller owns
249
+ // save/restore, opacity, compositing and the destination clip.
250
+ _drawDeviceBuffer(canvas) {
251
+ const { x, y, width, height } = canvas._isolationRect;
252
+ this.setTransform(1, 0, 0, 1, 0, 0);
253
+ this.imageSmoothingEnabled = false;
254
+ this.drawImage(canvas._canvas, 0, 0, width, height, x, y, width, height);
255
+ }
247
256
  _applyLineCap(shape) {
248
257
  const lineCap = shape.attrs.lineCap;
249
258
  if (lineCap) {
250
259
  this.setAttr('lineCap', lineCap);
251
260
  }
252
261
  }
262
+ _getOpacity(shape) {
263
+ if (!this._opacityRoot) {
264
+ return shape.getAbsoluteOpacity();
265
+ }
266
+ let opacity = shape.opacity();
267
+ let parent = shape.parent;
268
+ while (parent && parent !== this._opacityRoot) {
269
+ opacity *= parent.opacity();
270
+ parent = parent.parent;
271
+ }
272
+ return opacity;
273
+ }
253
274
  _applyOpacity(shape) {
254
- const absOpacity = shape.getAbsoluteOpacity();
275
+ const absOpacity = this._getOpacity(shape);
255
276
  if (absOpacity !== 1) {
256
277
  this.setAttr('globalAlpha', absOpacity);
257
278
  }
package/lib/Global.d.ts CHANGED
@@ -139,4 +139,5 @@ export declare const Konva: {
139
139
  document: any;
140
140
  _injectGlobal(Konva: any): void;
141
141
  };
142
- export declare const _registerNode: (NodeClass: any) => void;
142
+ export declare const _boundedShapes: Set<unknown>;
143
+ export declare const _registerNode: (NodeClass: any, boundsCoverPaint?: boolean) => void;
package/lib/Global.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Konva JavaScript Framework v10.6.0
2
+ * Konva JavaScript Framework v10.7.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.6.0',
32
+ version: '10.7.1',
33
33
  isBrowser: detectBrowser(),
34
34
  isUnminified: /param/.test(function (param) { }.toString()),
35
35
  dblClickWindow: 400,
@@ -183,7 +183,12 @@ export const Konva = {
183
183
  glob.Konva = Konva;
184
184
  },
185
185
  };
186
- export const _registerNode = (NodeClass) => {
186
+ // Konva's own shapes whose bounds cover everything they paint. Plugins can
187
+ // import _registerNode, so they opt in explicitly.
188
+ export const _boundedShapes = new Set();
189
+ export const _registerNode = (NodeClass, boundsCoverPaint = false) => {
187
190
  Konva[NodeClass.prototype.getClassName()] = NodeClass;
191
+ if (boundsCoverPaint)
192
+ _boundedShapes.add(NodeClass);
188
193
  };
189
194
  Konva._injectGlobal(Konva);
package/lib/Group.d.ts CHANGED
@@ -2,7 +2,10 @@ import type { ContainerConfig } from './Container.ts';
2
2
  import { Container } from './Container.ts';
3
3
  import type { Node } from './Node.ts';
4
4
  import type { Shape } from './Shape.ts';
5
+ import type { SceneCanvas } from './Canvas.ts';
6
+ import type { GetSet } from './types.ts';
5
7
  export interface GroupConfig extends ContainerConfig {
8
+ isolated?: boolean;
6
9
  }
7
10
  /**
8
11
  * Group constructor. Groups are used to contain shapes or other groups.
@@ -10,11 +13,14 @@ export interface GroupConfig extends ContainerConfig {
10
13
  * @memberof Konva
11
14
  * @augments Konva.Container
12
15
  * @param {Object} config
16
+ * @param {Boolean} [config.isolated=false] composite children as one live image
13
17
  * @@nodeParams
14
18
  * @@containerParams
15
19
  * @example
16
20
  * var group = new Konva.Group();
17
21
  */
18
- export declare class Group extends Container<Group | Shape> {
22
+ export declare class Group extends Container<Group | Shape, GroupConfig> {
19
23
  _validateAdd(child: Node): void;
24
+ _drawChildNodes(drawMethod: any, canvas: SceneCanvas, top?: Node): void;
25
+ isolated: GetSet<boolean, this>;
20
26
  }
package/lib/Group.js CHANGED
@@ -1,12 +1,15 @@
1
1
  import { Util } from "./Util.js";
2
2
  import { Container } from "./Container.js";
3
3
  import { _registerNode } from "./Global.js";
4
+ import { Factory } from "./Factory.js";
5
+ import { getBooleanValidator } from "./Validators.js";
4
6
  /**
5
7
  * Group constructor. Groups are used to contain shapes or other groups.
6
8
  * @constructor
7
9
  * @memberof Konva
8
10
  * @augments Konva.Container
9
11
  * @param {Object} config
12
+ * @param {Boolean} [config.isolated=false] composite children as one live image
10
13
  * @@nodeParams
11
14
  * @@containerParams
12
15
  * @example
@@ -19,6 +22,64 @@ export class Group extends Container {
19
22
  Util.throw('You may only add groups and shapes to groups.');
20
23
  }
21
24
  }
25
+ _drawChildNodes(drawMethod, canvas, top) {
26
+ // cache() already provides an isolated surface and owns its snapshot.
27
+ if (drawMethod !== 'drawScene' || !this.isolated() || top === this) {
28
+ return super._drawChildNodes(drawMethod, canvas, top);
29
+ }
30
+ if (!canvas.width || !canvas.height) {
31
+ return;
32
+ }
33
+ const context = canvas.getContext();
34
+ const surface = canvas._prepareIsolationCanvas(this.getClientRect({ _forDrawing: true, relativeTo: top }));
35
+ const surfaceContext = surface.getContext();
36
+ surfaceContext._opacityRoot = this;
37
+ context.save();
38
+ try {
39
+ super._drawChildNodes(drawMethod, surface, top);
40
+ context._applyOpacity(this);
41
+ context._drawDeviceBuffer(surface);
42
+ }
43
+ catch (error) {
44
+ // A throwing sceneFunc can leave unbalanced saves or a clip on the
45
+ // scratch context. Discard it rather than contaminate the next draw.
46
+ canvas._releaseIsolationCanvas();
47
+ throw error;
48
+ }
49
+ finally {
50
+ context.restore();
51
+ surfaceContext._opacityRoot = undefined;
52
+ }
53
+ }
22
54
  }
55
+ /**
56
+ * Get or set group isolation. Default is false.
57
+ * An isolated group draws its children into a transparent canvas on each scene draw.
58
+ * The group opacity and globalCompositeOperation apply once to the completed image.
59
+ * Child blend modes and erasing affect only the content inside that canvas.
60
+ *
61
+ * Explicit cache() calls keep their snapshot behavior. clearCache() resumes live drawing.
62
+ * Hit testing keeps its normal behavior, including hits on erased content.
63
+ * For video or canvas sources, schedule draws when the source changes.
64
+ *
65
+ * Buffers use conservative content bounds at the destination pixel ratio.
66
+ * Moving the buffer origin changes native edge coverage. A few edge pixels
67
+ * can differ substantially; isolation does not promise pixel-identical antialiasing.
68
+ * Like cache(), isolation requires custom drawing (sceneFunc, charRenderFunc) to provide accurate
69
+ * bounds, including any pixels it paints outside its size: selfRectFunc or a
70
+ * getSelfRect() override.
71
+ * Text is bounded by its lines plus one font size, so a glyph with many stacked
72
+ * marks can be clipped. node-canvas shadows can change with bitmap origin.
73
+ * Siblings reuse a buffer; unused or oversized buffers are freed after each layer draw.
74
+ * @name Konva.Group#isolated
75
+ * @method
76
+ * @param {Boolean} isolated
77
+ * @returns {Boolean}
78
+ * @example
79
+ * const group = new Konva.Group({ isolated: true, opacity: 0.5 });
80
+ * group.add(shape1, shape2);
81
+ * group.isolated(false);
82
+ */
83
+ Factory.addGetterSetter(Group, 'isolated', false, getBooleanValidator());
23
84
  Group.prototype.nodeType = 'Group';
24
85
  _registerNode(Group);
package/lib/Layer.d.ts CHANGED
@@ -137,7 +137,7 @@ export declare class Layer extends Container<Group | Shape> {
137
137
  shape?: Shape;
138
138
  antialiased?: boolean;
139
139
  };
140
- drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas): this;
140
+ drawScene(can?: SceneCanvas, top?: Node): this;
141
141
  shouldDrawHit(top?: Node): boolean;
142
142
  drawHit(can?: HitCanvas, top?: Node): this;
143
143
  /**
package/lib/Layer.js CHANGED
@@ -372,7 +372,7 @@ export class Layer extends Container {
372
372
  }
373
373
  return {};
374
374
  }
375
- drawScene(can, top, bufferCanvas) {
375
+ drawScene(can, top) {
376
376
  const layer = this.getLayer(), canvas = can || (layer && layer.getCanvas());
377
377
  this._fire(BEFORE_DRAW, {
378
378
  node: this,
@@ -380,7 +380,8 @@ export class Layer extends Container {
380
380
  if (this.clearBeforeDraw()) {
381
381
  canvas.getContext().clear();
382
382
  }
383
- Container.prototype.drawScene.call(this, canvas, top, bufferCanvas);
383
+ Container.prototype.drawScene.call(this, canvas, top);
384
+ canvas._trimIsolationCanvas();
384
385
  this._fire(DRAW, {
385
386
  node: this,
386
387
  });
@@ -467,6 +468,7 @@ export class Layer extends Container {
467
468
  }
468
469
  }
469
470
  destroy() {
471
+ this.getCanvas()._releaseIsolationCanvas();
470
472
  Util.releaseCanvas(this.getNativeCanvasElement(), this.getHitCanvas()._canvas);
471
473
  return super.destroy();
472
474
  }
package/lib/Node.d.ts CHANGED
@@ -11,6 +11,9 @@ import { Transform, type AnyString } from './Util.ts';
11
11
  export type FilterFunction = (this: Node, imageData: ImageData, pixelRatio?: number) => void;
12
12
  export type Filter = FilterFunction | string;
13
13
  type Filters = Array<FilterFunction | string>;
14
+ type SubtreeObserver = {
15
+ _onSubtreeChange(node: Node): void;
16
+ };
14
17
  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';
15
18
  export type NodeConfig = {
16
19
  x?: number;
@@ -55,6 +58,7 @@ export interface KonvaEventObject<EventType, This = Node> {
55
58
  newVal?: any;
56
59
  }
57
60
  export interface GetClientRectConfig {
61
+ _forDrawing?: boolean;
58
62
  skipTransform?: boolean;
59
63
  skipShadow?: boolean;
60
64
  skipStroke?: boolean;
@@ -107,6 +111,8 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
107
111
  } | null;
108
112
  _attrsAffectingSize: string[];
109
113
  _attrsVersion?: number;
114
+ _subtreeObservers?: Set<SubtreeObserver>;
115
+ static _subtreeObserverCount: number;
110
116
  _batchingTransformChange: boolean;
111
117
  _needClearTransformCache: boolean;
112
118
  _filterUpToDate: boolean;
@@ -202,7 +208,7 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
202
208
  * @returns {Boolean}
203
209
  */
204
210
  isCached(): boolean;
205
- abstract drawScene(canvas?: Canvas, top?: Node, bufferCanvas?: Canvas): void;
211
+ abstract drawScene(canvas?: Canvas, top?: Node): void;
206
212
  abstract drawHit(canvas?: Canvas, top?: Node): void;
207
213
  /**
208
214
  * Return client rectangle {x, y, width, height} of node. This rectangle also include all styling (strokes, shadows, etc).
@@ -243,12 +249,8 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
243
249
  * // returns Object {x: -2, y: 46, width: 104, height: 208}
244
250
  */
245
251
  getClientRect(config?: GetClientRectConfig): IRect;
246
- _transformedRect(rect: IRect, top?: Node | null): {
247
- x: number;
248
- y: number;
249
- width: number;
250
- height: number;
251
- };
252
+ _getCachedSceneRect(config: GetClientRectConfig): IRect | undefined;
253
+ _transformedRect(rect: IRect, top?: Node | null): IRect;
252
254
  _drawCachedSceneCanvas(context: Context): void;
253
255
  _drawCachedHitCanvas(context: Context, hitCanvas: HitCanvas): void;
254
256
  _getCachedSceneCanvas(): Canvas;
@@ -874,6 +876,9 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
874
876
  * node.setAttr('x', 5);
875
877
  */
876
878
  setAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(attr: K, val: K extends keyof AttrConfig ? AttrConfig[K] : any): this;
879
+ _addSubtreeObserver(observer: SubtreeObserver): void;
880
+ _removeSubtreeObserver(observer: SubtreeObserver): void;
881
+ _notifySubtreeChange(): void;
877
882
  _requestDraw(): void;
878
883
  _setAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(key: K, val: K extends keyof AttrConfig ? AttrConfig[K] : any): void;
879
884
  _fireAndBubble(eventType: any, evt: any, compareShape?: any): void;