konva 10.6.0 → 10.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +12 -8
  2. package/konva.js +533 -230
  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 +23 -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 +4 -7
  17. package/lib/Node.js +19 -43
  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.js +7 -5
  51. package/lib/shapes/Wedge.js +1 -1
  52. 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
@@ -317,7 +317,7 @@ export class Container extends Node {
317
317
  });
318
318
  this._requestDraw();
319
319
  }
320
- drawScene(can, top, bufferCanvas) {
320
+ drawScene(can, top) {
321
321
  const layer = this.getLayer(), canvas = can || (layer && layer.getCanvas()), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedSceneCanvas = cachedCanvas && cachedCanvas.scene;
322
322
  const caching = top === this;
323
323
  if (!(top ? this._isVisible(top) : this.isVisible()) && !caching) {
@@ -331,7 +331,7 @@ export class Container extends Node {
331
331
  context.restore();
332
332
  }
333
333
  else {
334
- this._drawChildren('drawScene', canvas, top, bufferCanvas);
334
+ this._drawChildren('drawScene', canvas, top);
335
335
  }
336
336
  return this;
337
337
  }
@@ -352,8 +352,7 @@ export class Container extends Node {
352
352
  }
353
353
  return this;
354
354
  }
355
- _drawChildren(drawMethod, canvas, top, bufferCanvas) {
356
- var _a;
355
+ _drawChildren(drawMethod, canvas, top) {
357
356
  const context = canvas && canvas.getContext(), clipWidth = this.clipWidth(), clipHeight = this.clipHeight(), clipFunc = this.clipFunc(), hasClip = (typeof clipWidth === 'number' && typeof clipHeight === 'number') ||
358
357
  clipFunc;
359
358
  const selfCache = top === this;
@@ -383,23 +382,34 @@ export class Container extends Node {
383
382
  context.save();
384
383
  context._applyGlobalCompositeOperation(this);
385
384
  }
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();
385
+ try {
386
+ this._drawChildNodes(drawMethod, canvas, top);
391
387
  }
392
- if (hasClip) {
393
- context.restore();
388
+ finally {
389
+ if (hasComposition) {
390
+ context.restore();
391
+ }
392
+ if (hasClip) {
393
+ context.restore();
394
+ }
394
395
  }
395
396
  }
397
+ _drawChildNodes(drawMethod, canvas, top) {
398
+ var _a;
399
+ (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
400
+ child[drawMethod](canvas, top);
401
+ });
402
+ }
396
403
  getClientRect(config = {}) {
397
404
  var _a;
405
+ const cachedRect = this._getCachedSceneRect(config);
406
+ if (cachedRect)
407
+ return cachedRect;
398
408
  const skipTransform = config.skipTransform;
399
409
  const relativeTo = config.relativeTo;
400
410
  const [a, b, c, d] = this.getAbsoluteTransform().getMatrix();
401
411
  // A zero scale cannot project local padding for a non-scaling stroke.
402
- const measureTransformed = !skipTransform && a * d - b * c === 0;
412
+ const measureTransformed = !skipTransform && (config._forDrawing || a * d - b * c === 0);
403
413
  let minX, minY, maxX, maxY;
404
414
  let selfRect = {
405
415
  x: Infinity,
@@ -415,6 +425,7 @@ export class Container extends Node {
415
425
  }
416
426
  const rect = child.getClientRect({
417
427
  relativeTo: measureTransformed ? relativeTo : that,
428
+ _forDrawing: config._forDrawing,
418
429
  skipShadow: config.skipShadow,
419
430
  skipStroke: config.skipStroke,
420
431
  });
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.0
3
3
  * http://konvajs.org/
4
4
  * Licensed under the MIT
5
5
  * Date: @@date
@@ -29,7 +29,7 @@ export const glob = typeof global !== 'undefined'
29
29
  : {};
30
30
  export const Konva = {
31
31
  _global: glob,
32
- version: '10.6.0',
32
+ version: '10.7.0',
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
@@ -55,6 +55,7 @@ export interface KonvaEventObject<EventType, This = Node> {
55
55
  newVal?: any;
56
56
  }
57
57
  export interface GetClientRectConfig {
58
+ _forDrawing?: boolean;
58
59
  skipTransform?: boolean;
59
60
  skipShadow?: boolean;
60
61
  skipStroke?: boolean;
@@ -202,7 +203,7 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
202
203
  * @returns {Boolean}
203
204
  */
204
205
  isCached(): boolean;
205
- abstract drawScene(canvas?: Canvas, top?: Node, bufferCanvas?: Canvas): void;
206
+ abstract drawScene(canvas?: Canvas, top?: Node): void;
206
207
  abstract drawHit(canvas?: Canvas, top?: Node): void;
207
208
  /**
208
209
  * Return client rectangle {x, y, width, height} of node. This rectangle also include all styling (strokes, shadows, etc).
@@ -243,12 +244,8 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
243
244
  * // returns Object {x: -2, y: 46, width: 104, height: 208}
244
245
  */
245
246
  getClientRect(config?: GetClientRectConfig): IRect;
246
- _transformedRect(rect: IRect, top?: Node | null): {
247
- x: number;
248
- y: number;
249
- width: number;
250
- height: number;
251
- };
247
+ _getCachedSceneRect(config: GetClientRectConfig): IRect | undefined;
248
+ _transformedRect(rect: IRect, top?: Node | null): IRect;
252
249
  _drawCachedSceneCanvas(context: Context): void;
253
250
  _drawCachedHitCanvas(context: Context, hitCanvas: HitCanvas): void;
254
251
  _getCachedSceneCanvas(): Canvas;
package/lib/Node.js CHANGED
@@ -97,20 +97,6 @@ const ABSOLUTE_OPACITY = 'absoluteOpacity', ABSOLUTE_TRANSFORM = 'absoluteTransf
97
97
  'transformsEnabledChange.konva',
98
98
  ].join(SPACE);
99
99
  let idCounter = 1;
100
- // "perfect drawing" buffer for cache() and toCanvas(): it mirrors the target
101
- // canvas and is drawn back at (x, y) in the target's coordinate space.
102
- // It starts empty and Shape.drawScene sizes it on first use, so nodes
103
- // without buffered shapes never allocate it.
104
- function createBufferCanvas(target, x, y) {
105
- const bufferCanvas = new SceneCanvas({
106
- width: 0,
107
- height: 0,
108
- pixelRatio: target.pixelRatio,
109
- });
110
- bufferCanvas.x = x;
111
- bufferCanvas.y = y;
112
- return bufferCanvas;
113
- }
114
100
  /**
115
101
  * Node constructor. Nodes are entities that can be transformed, layered,
116
102
  * and have bound events. The stage, layers, groups, and shapes all extend Node.
@@ -304,7 +290,6 @@ export class Node {
304
290
  height: 0,
305
291
  willReadFrequently: true,
306
292
  }), sceneContext = cachedSceneCanvas.getContext();
307
- const bufferCanvas = createBufferCanvas(cachedSceneCanvas, x, y);
308
293
  cachedSceneCanvas.isCache = true;
309
294
  this._releaseCanvasCache();
310
295
  this._filterUpToDate = false;
@@ -318,7 +303,7 @@ export class Node {
318
303
  this._isUnderCache = true;
319
304
  this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
320
305
  try {
321
- this.drawScene(cachedSceneCanvas, this, bufferCanvas);
306
+ this.drawScene(cachedSceneCanvas, this);
322
307
  }
323
308
  catch (e) {
324
309
  Util.releaseCanvas(cachedSceneCanvas._canvas);
@@ -329,8 +314,7 @@ export class Node {
329
314
  // descendants cached their opacity relative to this node while drawing
330
315
  this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
331
316
  sceneContext.restore();
332
- // the buffer is only needed while drawing
333
- Util.releaseCanvas(bufferCanvas._canvas);
317
+ cachedSceneCanvas._releaseIsolationCanvas();
334
318
  }
335
319
  // this will draw a red border around the cached box for
336
320
  // debugging purposes
@@ -441,28 +425,22 @@ export class Node {
441
425
  // redefine in Container and Shape
442
426
  throw new Error('abstract "getClientRect" method call');
443
427
  }
444
- _transformedRect(rect, top) {
445
- const points = [
446
- { x: rect.x, y: rect.y },
447
- { x: rect.x + rect.width, y: rect.y },
448
- { x: rect.x + rect.width, y: rect.y + rect.height },
449
- { x: rect.x, y: rect.y + rect.height },
450
- ];
451
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
452
- const trans = this.getAbsoluteTransform(top);
453
- points.forEach(function (point) {
454
- const transformed = trans.point(point);
455
- minX = Math.min(minX, transformed.x);
456
- minY = Math.min(minY, transformed.y);
457
- maxX = Math.max(maxX, transformed.x);
458
- maxY = Math.max(maxY, transformed.y);
459
- });
460
- return {
461
- x: minX,
462
- y: minY,
463
- width: maxX - minX,
464
- height: maxY - minY,
428
+ _getCachedSceneRect(config) {
429
+ const cache = config._forDrawing && !this._isUnderCache && this._getCanvasCache();
430
+ if (!cache)
431
+ return;
432
+ const rect = {
433
+ x: cache.x,
434
+ y: cache.y,
435
+ width: cache.scene.width / cache.scene.pixelRatio,
436
+ height: cache.scene.height / cache.scene.pixelRatio,
465
437
  };
438
+ return config.skipTransform
439
+ ? rect
440
+ : this._transformedRect(rect, config.relativeTo);
441
+ }
442
+ _transformedRect(rect, top) {
443
+ return this.getAbsoluteTransform(top)._getTransformedRect(rect);
466
444
  }
467
445
  _drawCachedSceneCanvas(context) {
468
446
  context.save();
@@ -1721,7 +1699,6 @@ export class Node {
1721
1699
  (stage ? stage.height() : 0),
1722
1700
  pixelRatio: pixelRatio,
1723
1701
  }), context = canvas.getContext();
1724
- const bufferCanvas = createBufferCanvas(canvas, x, y);
1725
1702
  if (config.imageSmoothingEnabled === false) {
1726
1703
  context._context.imageSmoothingEnabled = false;
1727
1704
  }
@@ -1729,10 +1706,9 @@ export class Node {
1729
1706
  if (x || y) {
1730
1707
  context.translate(-1 * x, -1 * y);
1731
1708
  }
1732
- this.drawScene(canvas, undefined, bufferCanvas);
1709
+ this.drawScene(canvas);
1733
1710
  context.restore();
1734
- // the buffer is only needed while drawing
1735
- Util.releaseCanvas(bufferCanvas._canvas);
1711
+ canvas._releaseIsolationCanvas();
1736
1712
  return canvas;
1737
1713
  }
1738
1714
  /**