konva 10.5.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 (79) hide show
  1. package/README.md +16 -12
  2. package/konva.js +3623 -3819
  3. package/konva.min.js +4 -4
  4. package/lib/Animation.d.ts +2 -2
  5. package/lib/Animation.js +2 -2
  6. package/lib/BezierFunctions.d.ts +1 -1
  7. package/lib/BezierFunctions.js +76 -116
  8. package/lib/Canvas.d.ts +10 -3
  9. package/lib/Canvas.js +91 -10
  10. package/lib/Container.d.ts +3 -2
  11. package/lib/Container.js +31 -17
  12. package/lib/Context.d.ts +9 -7
  13. package/lib/Context.js +99 -73
  14. package/lib/DragAndDrop.d.ts +4 -3
  15. package/lib/DragAndDrop.js +39 -25
  16. package/lib/Factory.d.ts +3 -2
  17. package/lib/Factory.js +10 -9
  18. package/lib/Global.d.ts +2 -7
  19. package/lib/Global.js +8 -9
  20. package/lib/Group.d.ts +7 -1
  21. package/lib/Group.js +61 -0
  22. package/lib/Layer.d.ts +1 -1
  23. package/lib/Layer.js +13 -8
  24. package/lib/Node.d.ts +25 -19
  25. package/lib/Node.js +178 -169
  26. package/lib/PointerEvents.d.ts +1 -2
  27. package/lib/PointerEvents.js +20 -18
  28. package/lib/Shape.d.ts +9 -15
  29. package/lib/Shape.js +190 -86
  30. package/lib/Stage.d.ts +19 -15
  31. package/lib/Stage.js +184 -99
  32. package/lib/Tween.js +59 -66
  33. package/lib/Util.d.ts +3 -2
  34. package/lib/Util.js +46 -16
  35. package/lib/_CoreInternals.d.ts +23 -11
  36. package/lib/_FullInternals.d.ts +23 -11
  37. package/lib/filters/Blur.js +16 -12
  38. package/lib/filters/Brighten.d.ts +1 -0
  39. package/lib/filters/Brighten.js +1 -0
  40. package/lib/filters/Brightness.d.ts +2 -1
  41. package/lib/filters/Brightness.js +4 -4
  42. package/lib/filters/Emboss.js +16 -58
  43. package/lib/filters/Kaleidoscope.js +8 -15
  44. package/lib/filters/Mask.js +8 -8
  45. package/lib/filters/Pixelate.js +16 -29
  46. package/lib/filters/Threshold.d.ts +1 -1
  47. package/lib/filters/Threshold.js +1 -1
  48. package/lib/shapes/Arc.js +8 -6
  49. package/lib/shapes/Arrow.d.ts +3 -6
  50. package/lib/shapes/Arrow.js +75 -47
  51. package/lib/shapes/Circle.d.ts +1 -0
  52. package/lib/shapes/Circle.js +4 -1
  53. package/lib/shapes/Ellipse.d.ts +1 -0
  54. package/lib/shapes/Ellipse.js +4 -1
  55. package/lib/shapes/Image.d.ts +2 -1
  56. package/lib/shapes/Image.js +22 -22
  57. package/lib/shapes/Label.d.ts +2 -3
  58. package/lib/shapes/Label.js +25 -41
  59. package/lib/shapes/Line.d.ts +2 -1
  60. package/lib/shapes/Line.js +5 -2
  61. package/lib/shapes/Path.d.ts +11 -2
  62. package/lib/shapes/Path.js +133 -143
  63. package/lib/shapes/Rect.d.ts +1 -0
  64. package/lib/shapes/Rect.js +4 -1
  65. package/lib/shapes/RegularPolygon.js +2 -2
  66. package/lib/shapes/Ring.d.ts +7 -0
  67. package/lib/shapes/Ring.js +8 -1
  68. package/lib/shapes/Sprite.d.ts +2 -1
  69. package/lib/shapes/Sprite.js +8 -9
  70. package/lib/shapes/Star.d.ts +6 -0
  71. package/lib/shapes/Star.js +5 -1
  72. package/lib/shapes/Text.d.ts +16 -5
  73. package/lib/shapes/Text.js +134 -65
  74. package/lib/shapes/TextPath.d.ts +11 -3
  75. package/lib/shapes/TextPath.js +56 -27
  76. package/lib/shapes/Transformer.js +95 -96
  77. package/lib/shapes/Wedge.js +1 -1
  78. package/lib/types.d.ts +2 -2
  79. package/package.json +5 -20
package/lib/Context.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Util } from "./Util.js";
1
+ import { Transform, Util } from "./Util.js";
2
2
  import { Konva } from "./Global.js";
3
3
  function simplifyArray(arr) {
4
4
  const retArr = [], len = arr.length, util = Util;
@@ -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
  }
@@ -648,77 +669,68 @@ export class SceneContext extends Context {
648
669
  willReadFrequently,
649
670
  });
650
671
  }
651
- _fillColor(shape) {
652
- const fill = shape.fill();
653
- this.setAttr('fillStyle', fill);
654
- shape._fillFunc(this);
655
- }
656
- _fillPattern(shape) {
657
- // `imageSmoothingEnabled` (set on the layer/context) controls pattern
658
- // smoothing in the browser. node-canvas ignores it for pattern fills and
659
- // instead exposes a non-standard `patternQuality` property, so mirror the
660
- // current smoothing setting onto it to keep both backends consistent.
672
+ _getFillPattern(shape) {
673
+ // node-canvas exposes pattern smoothing through patternQuality.
661
674
  const context = this._context;
662
675
  if ('patternQuality' in context) {
663
676
  context.patternQuality = context.imageSmoothingEnabled
664
677
  ? 'good'
665
678
  : 'nearest';
666
679
  }
667
- this.setAttr('fillStyle', shape._getFillPattern());
668
- shape._fillFunc(this);
669
- }
670
- _fillLinearGradient(shape) {
671
- const grd = shape._getLinearGradient();
672
- if (grd) {
673
- this.setAttr('fillStyle', grd);
674
- shape._fillFunc(this);
675
- }
676
- }
677
- _fillRadialGradient(shape) {
678
- const grd = shape._getRadialGradient();
679
- if (grd) {
680
- this.setAttr('fillStyle', grd);
681
- shape._fillFunc(this);
682
- }
680
+ return shape._getFillPattern();
681
+ }
682
+ _getFillStyle(shape) {
683
+ const color = shape.fill();
684
+ const priority = shape.fillPriority();
685
+ if (color && priority === 'color')
686
+ return color;
687
+ const pattern = shape.fillPatternImage();
688
+ if (pattern && priority === 'pattern')
689
+ return this._getFillPattern(shape);
690
+ const linear = shape.fillLinearGradientColorStops();
691
+ if (linear && priority === 'linear-gradient')
692
+ return shape._getLinearGradient();
693
+ const radial = shape.fillRadialGradientColorStops();
694
+ if (radial && priority === 'radial-gradient')
695
+ return shape._getRadialGradient();
696
+ if (color)
697
+ return color;
698
+ if (pattern)
699
+ return this._getFillPattern(shape);
700
+ if (linear)
701
+ return shape._getLinearGradient();
702
+ if (radial)
703
+ return shape._getRadialGradient();
683
704
  }
684
705
  _fill(shape) {
685
- const hasColor = shape.fill(), fillPriority = shape.getFillPriority();
686
- // priority fills
687
- if (hasColor && fillPriority === 'color') {
688
- this._fillColor(shape);
689
- return;
690
- }
691
- const hasPattern = shape.getFillPatternImage();
692
- if (hasPattern && fillPriority === 'pattern') {
693
- this._fillPattern(shape);
694
- return;
695
- }
696
- const hasLinearGradient = shape.getFillLinearGradientColorStops();
697
- if (hasLinearGradient && fillPriority === 'linear-gradient') {
698
- this._fillLinearGradient(shape);
699
- return;
700
- }
701
- const hasRadialGradient = shape.getFillRadialGradientColorStops();
702
- if (hasRadialGradient && fillPriority === 'radial-gradient') {
703
- this._fillRadialGradient(shape);
704
- return;
705
- }
706
- // now just try and fill with whatever is available
707
- if (hasColor) {
708
- this._fillColor(shape);
709
- }
710
- else if (hasPattern) {
711
- this._fillPattern(shape);
712
- }
713
- else if (hasLinearGradient) {
714
- this._fillLinearGradient(shape);
715
- }
716
- else if (hasRadialGradient) {
717
- this._fillRadialGradient(shape);
706
+ const style = this._getFillStyle(shape);
707
+ if (style !== undefined) {
708
+ this.setAttr('fillStyle', style);
709
+ shape._fillFunc(this);
718
710
  }
719
711
  }
720
712
  _strokeLinearGradient(shape) {
721
- const start = shape.getStrokeLinearGradientStartPoint(), end = shape.getStrokeLinearGradientEndPoint(), colorStops = shape.getStrokeLinearGradientColorStops(), grd = this.createLinearGradient(start.x, start.y, end.x, end.y);
713
+ let start = shape.getStrokeLinearGradientStartPoint(), end = shape.getStrokeLinearGradientEndPoint();
714
+ if (!shape.getStrokeScaleEnabled()) {
715
+ const { a, b, c, d, e, f } = this._context.getTransform();
716
+ const transform = new Transform([a, b, c, d, e, f]);
717
+ const ratio = this.canvas.getPixelRatio();
718
+ const dx = end.x - start.x;
719
+ const dy = end.y - start.y;
720
+ // Gradient colors follow the inverse-transposed direction. Transforming
721
+ // the endpoints alone distorts them under skew or nonuniform scaling.
722
+ const nx = d * dx - b * dy;
723
+ const ny = a * dy - c * dx;
724
+ const lengthSquared = nx * nx + ny * ny;
725
+ const scale = lengthSquared
726
+ ? ((dx * dx + dy * dy) * (a * d - b * c)) / lengthSquared
727
+ : 0;
728
+ start = transform.point(start);
729
+ end = { x: start.x + nx * scale, y: start.y + ny * scale };
730
+ start = { x: start.x / ratio, y: start.y / ratio };
731
+ end = { x: end.x / ratio, y: end.y / ratio };
732
+ }
733
+ const colorStops = shape.getStrokeLinearGradientColorStops(), grd = this.createLinearGradient(start.x, start.y, end.x, end.y);
722
734
  if (colorStops) {
723
735
  // build color stops
724
736
  for (let n = 0; n < colorStops.length; n += 2) {
@@ -727,14 +739,20 @@ export class SceneContext extends Context {
727
739
  this.setAttr('strokeStyle', grd);
728
740
  }
729
741
  }
742
+ _applyStrokeStyle(shape) {
743
+ if (shape.strokeLinearGradientColorStops()) {
744
+ this._strokeLinearGradient(shape);
745
+ }
746
+ else {
747
+ this.setAttr('strokeStyle', shape.stroke());
748
+ }
749
+ }
730
750
  _stroke(shape) {
731
751
  const dash = shape.dash(),
732
752
  // ignore strokeScaleEnabled for Text
733
753
  strokeScaleEnabled = shape.getStrokeScaleEnabled();
734
754
  if (!strokeScaleEnabled) {
735
755
  this.save();
736
- const pixelRatio = this.getCanvas().getPixelRatio();
737
- this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
738
756
  }
739
757
  this._applyLineCap(shape);
740
758
  if (dash && shape.dashEnabled()) {
@@ -742,19 +760,27 @@ export class SceneContext extends Context {
742
760
  this.setAttr('lineDashOffset', shape.dashOffset());
743
761
  }
744
762
  this.setAttr('lineWidth', shape.strokeWidth());
745
- if (!shape.getShadowForStrokeEnabled()) {
763
+ const shadowColor = this.shadowColor;
764
+ const shadowForStrokeEnabled = shape.getShadowForStrokeEnabled();
765
+ if (!shadowForStrokeEnabled) {
746
766
  this.setAttr('shadowColor', 'rgba(0,0,0,0)');
747
767
  }
748
- const hasLinearGradient = shape.getStrokeLinearGradientColorStops();
749
- if (hasLinearGradient) {
750
- this._strokeLinearGradient(shape);
768
+ this._applyStrokeStyle(shape);
769
+ // Resolve the gradient in local coordinates before drawing an unscaled stroke.
770
+ if (!strokeScaleEnabled) {
771
+ const pixelRatio = this.getCanvas().getPixelRatio();
772
+ this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
751
773
  }
752
- else {
753
- this.setAttr('strokeStyle', shape.stroke());
774
+ try {
775
+ shape._strokeFunc(this);
754
776
  }
755
- shape._strokeFunc(this);
756
- if (!strokeScaleEnabled) {
757
- this.restore();
777
+ finally {
778
+ if (!shadowForStrokeEnabled) {
779
+ this.setAttr('shadowColor', shadowColor);
780
+ }
781
+ if (!strokeScaleEnabled) {
782
+ this.restore();
783
+ }
758
784
  }
759
785
  }
760
786
  _applyShadow(shape) {
@@ -1,23 +1,24 @@
1
1
  import type { Node } from './Node.ts';
2
+ import type { Stage } from './Stage.ts';
2
3
  import type { Vector2d } from './types.ts';
3
4
  type DragElement = {
4
5
  node: Node;
5
6
  startPointerPos: Vector2d;
6
7
  offset: Vector2d;
7
8
  pointerId?: number;
9
+ pointerEventType?: Stage['_pointerEventType'];
8
10
  lastPos?: Vector2d;
9
11
  startEvent?: any;
10
12
  dragStatus: 'ready' | 'dragging' | 'stopped';
11
13
  };
12
14
  export declare const DD: {
13
15
  readonly isDragging: boolean;
14
- justDragged: boolean;
15
16
  readonly node: Node<import("./Node.ts").NodeConfig> | undefined;
16
17
  _dragElements: Map<number, DragElement>;
17
18
  _listenToWindow(win: Window): void;
18
19
  _batchEvents(callback: () => void, win?: Window): void;
19
20
  _drag(evt: any, win?: Window): void;
20
- _endDragBefore(evt?: any, win?: Window): void;
21
- _endDragAfter(evt: any): void;
21
+ _endDragBefore(evt?: any, win?: Window, only?: DragElement): void;
22
+ _endDragAfter(evt: any, only?: DragElement): void;
22
23
  };
23
24
  export {};
@@ -1,4 +1,3 @@
1
- import { Konva } from "./Global.js";
2
1
  import { Util } from "./Util.js";
3
2
  export const DD = {
4
3
  get isDragging() {
@@ -10,13 +9,14 @@ export const DD = {
10
9
  });
11
10
  return flag;
12
11
  },
13
- justDragged: false,
14
12
  get node() {
15
- // return first dragging node
16
13
  let node;
17
- DD._dragElements.forEach((elem) => {
14
+ for (const elem of DD._dragElements.values()) {
15
+ if (elem.dragStatus === 'dragging')
16
+ return elem.node;
18
17
  node = elem.node;
19
- });
18
+ }
19
+ // isDragReady() also uses this getter before movement starts.
20
20
  return node;
21
21
  },
22
22
  _dragElements: new Map(),
@@ -78,10 +78,18 @@ export const DD = {
78
78
  stage.setPointersPositions(evt);
79
79
  positioned.add(stage);
80
80
  }
81
+ if (elem.pointerEventType &&
82
+ elem.pointerEventType !== stage._pointerEventType) {
83
+ return;
84
+ }
81
85
  // it is possible that user call startDrag without any event
82
86
  // it that case we need to detect first movable pointer and attach it into the node
83
87
  if (elem.pointerId === undefined) {
84
88
  elem.pointerId = Util._getFirstPointerId(evt);
89
+ elem.pointerEventType = stage._pointerEventType;
90
+ if (elem.dragStatus === 'dragging') {
91
+ stage._cancelClick(elem.pointerId, elem.pointerEventType);
92
+ }
85
93
  }
86
94
  const pos = stage._changedPointerPositions.find((pos) => pos.id === elem.pointerId);
87
95
  // not related pointer
@@ -118,11 +126,16 @@ export const DD = {
118
126
  },
119
127
  // dragBefore and dragAfter allows us to set correct order of events
120
128
  // setup all in dragbefore, and stop dragging only after pointerup triggered.
121
- _endDragBefore(evt, win) {
129
+ _endDragBefore(evt, win, only) {
122
130
  const drawNodes = [];
123
131
  const positioned = new Set();
124
132
  DD._dragElements.forEach((elem, key) => {
125
133
  const { node } = elem;
134
+ // node.stopDrag() ends only the drags of the same gesture: a Transformer
135
+ // drags its other nodes with the same pointer, another pointer is its own
136
+ if (only && elem !== only && elem.pointerId !== only.pointerId) {
137
+ return;
138
+ }
126
139
  // we need to find pointer relative to that node
127
140
  const stage = node.getStage();
128
141
  // a pointer released in another window ends the drag too - the pointer
@@ -133,31 +146,31 @@ export const DD = {
133
146
  stage.setPointersPositions(evt);
134
147
  positioned.add(stage);
135
148
  }
136
- // a drag started without an event (node.startDrag()) that has not
137
- // moved yet has no pointer: any release ends it
138
- const released = elem.pointerId === undefined ||
139
- stage._changedPointerPositions.some((pos) => pos.id === elem.pointerId);
140
- // that pointer is not related: a "ready" element of another pointer
141
- // waits for its own release
142
- if (!released) {
143
- return;
149
+ if (!only) {
150
+ if (evt &&
151
+ elem.pointerEventType &&
152
+ elem.pointerEventType !== Util._getEventType(evt.type)) {
153
+ return;
154
+ }
155
+ // a drag started without an event (node.startDrag()) that has not
156
+ // moved yet has no pointer: any release ends it
157
+ const released = elem.pointerId === undefined ||
158
+ stage._changedPointerPositions.some((pos) => pos.id === elem.pointerId);
159
+ // that pointer is not related: a "ready" element of another pointer
160
+ // waits for its own release
161
+ if (!released) {
162
+ return;
163
+ }
144
164
  }
145
165
  if (elem.dragStatus === 'ready') {
146
166
  DD._dragElements.delete(key);
147
167
  }
148
168
  else {
149
169
  // if a node is stopped manually we still need to reset events:
150
- DD.justDragged = true;
151
- Konva._mouseListenClick = false;
152
- Konva._touchListenClick = false;
153
- // for real pointer input this reset comes too late — "pointerup" fired
154
- // before this handler — so Stage._pointerup also checks the live drag
155
- // state for the pointer family (issue #1756)
156
- Konva._pointerListenClick = false;
170
+ stage._cancelClick(elem.pointerId, elem.pointerEventType);
157
171
  elem.dragStatus = 'stopped';
158
172
  // a node that has not moved needs no redraw
159
- const drawNode = elem.node.getLayer() ||
160
- (elem.node instanceof Konva['Stage'] && elem.node);
173
+ const drawNode = node.getLayer() || stage;
161
174
  if (drawNode && drawNodes.indexOf(drawNode) === -1) {
162
175
  drawNodes.push(drawNode);
163
176
  }
@@ -170,9 +183,10 @@ export const DD = {
170
183
  drawNode.draw();
171
184
  });
172
185
  },
173
- _endDragAfter(evt) {
186
+ _endDragAfter(evt, only) {
174
187
  DD._dragElements.forEach((elem, key) => {
175
- if (elem.dragStatus !== 'stopped') {
188
+ if (elem.dragStatus !== 'stopped' ||
189
+ (only && elem !== only && elem.pointerId !== only.pointerId)) {
176
190
  return;
177
191
  }
178
192
  elem.node.fire('dragend', {
package/lib/Factory.d.ts CHANGED
@@ -17,9 +17,10 @@ type Attr<T extends Constructor> = EnforceString<keyof InstanceType<T>>;
17
17
  */
18
18
  type AfterFunc<T extends Constructor> = (this: InstanceType<T>) => void;
19
19
  /**
20
- * Extracts the type of a GetSet.
20
+ * Extracts the type a GetSet returns. The type the setter accepts can be wider,
21
+ * so it is matched with `any` instead of being inferred from the getter.
21
22
  */
22
- type ExtractGetSet<T> = T extends GetSet<infer U, any> ? U : never;
23
+ type ExtractGetSet<T> = T extends GetSet<infer U, any, any> ? U : never;
23
24
  /**
24
25
  * Extracts the type of a GetSet class attribute.
25
26
  */
package/lib/Factory.js CHANGED
@@ -10,12 +10,15 @@ export const Factory = {
10
10
  },
11
11
  addGetter(constructor, attr, def) {
12
12
  const method = GET + Util._capitalize(attr);
13
- constructor.prototype[method] =
14
- constructor.prototype[method] ||
15
- function () {
16
- const val = this.attrs[attr];
17
- return val === undefined ? def : val;
18
- };
13
+ // array defaults are shared between instances, so hand out a copy
14
+ const isArr = Array.isArray(def);
15
+ const getter = function () {
16
+ const val = this.attrs[attr];
17
+ return val === undefined ? (isArr ? def.slice() : def) : val;
18
+ };
19
+ // toObject() compares an attr with the default of the generated getter
20
+ getter._def = def;
21
+ constructor.prototype[method] = constructor.prototype[method] || getter;
19
22
  },
20
23
  addSetter(constructor, attr, validator, after) {
21
24
  const method = SET + Util._capitalize(attr);
@@ -52,7 +55,7 @@ export const Factory = {
52
55
  const basicValidator = getComponentValidator(components);
53
56
  // setter
54
57
  constructor.prototype[setter] = function (val) {
55
- const oldVal = this.attrs[attr];
58
+ const oldVal = this[getter]();
56
59
  if (validator) {
57
60
  val = validator.call(this, val, attr);
58
61
  }
@@ -89,8 +92,6 @@ export const Factory = {
89
92
  // getting
90
93
  return this[getter]();
91
94
  };
92
- // marks the accessor for toObject()
93
- accessor._isAttrAccessor = true;
94
95
  constructor.prototype[attr] = accessor;
95
96
  },
96
97
  backCompat(constructor, methods) {
package/lib/Global.d.ts CHANGED
@@ -42,12 +42,6 @@ export declare const Konva: {
42
42
  * Konva.capturePointerEventsEnabled = true;
43
43
  */
44
44
  capturePointerEventsEnabled: boolean;
45
- _mouseListenClick: boolean;
46
- _touchListenClick: boolean;
47
- _pointerListenClick: boolean;
48
- _mouseInDblClickWindow: boolean;
49
- _touchInDblClickWindow: boolean;
50
- _pointerInDblClickWindow: boolean;
51
45
  _renderBackend: string;
52
46
  /**
53
47
  * Use legacy text rendering. with "middle" baseline by default.
@@ -145,4 +139,5 @@ export declare const Konva: {
145
139
  document: any;
146
140
  _injectGlobal(Konva: any): void;
147
141
  };
148
- 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.5.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.5.0',
32
+ version: '10.7.0',
33
33
  isBrowser: detectBrowser(),
34
34
  isUnminified: /param/.test(function (param) { }.toString()),
35
35
  dblClickWindow: 400,
@@ -72,12 +72,6 @@ export const Konva = {
72
72
  * Konva.capturePointerEventsEnabled = true;
73
73
  */
74
74
  capturePointerEventsEnabled: false,
75
- _mouseListenClick: false,
76
- _touchListenClick: false,
77
- _pointerListenClick: false,
78
- _mouseInDblClickWindow: false,
79
- _touchInDblClickWindow: false,
80
- _pointerInDblClickWindow: false,
81
75
  _renderBackend: 'web', // web, node-canvas, skia-canvas
82
76
  /**
83
77
  * Use legacy text rendering. with "middle" baseline by default.
@@ -189,7 +183,12 @@ export const Konva = {
189
183
  glob.Konva = Konva;
190
184
  },
191
185
  };
192
- 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) => {
193
190
  Konva[NodeClass.prototype.getClassName()] = NodeClass;
191
+ if (boundsCoverPaint)
192
+ _boundedShapes.add(NodeClass);
194
193
  };
195
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
  /**