konva 10.3.3 → 10.5.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 (123) hide show
  1. package/README.md +1 -1
  2. package/konva.js +1576 -1636
  3. package/konva.min.js +4 -4
  4. package/lib/Animation.d.ts +63 -3
  5. package/lib/Animation.js +86 -65
  6. package/lib/BezierFunctions.d.ts +10 -0
  7. package/lib/BezierFunctions.js +32 -0
  8. package/lib/Canvas.d.ts +53 -0
  9. package/lib/Canvas.js +93 -34
  10. package/lib/Container.d.ts +156 -9
  11. package/lib/Container.js +260 -25
  12. package/lib/Context.d.ts +247 -0
  13. package/lib/Context.js +312 -74
  14. package/lib/Core.js +1 -0
  15. package/lib/DragAndDrop.d.ts +2 -0
  16. package/lib/DragAndDrop.js +92 -21
  17. package/lib/Factory.d.ts +24 -1
  18. package/lib/Factory.js +14 -20
  19. package/lib/FastLayer.d.ts +14 -1
  20. package/lib/FastLayer.js +14 -1
  21. package/lib/Global.d.ts +114 -3
  22. package/lib/Global.js +134 -5
  23. package/lib/Group.d.ts +11 -0
  24. package/lib/Group.js +11 -0
  25. package/lib/Layer.d.ts +128 -3
  26. package/lib/Layer.js +260 -49
  27. package/lib/Node.d.ts +703 -32
  28. package/lib/Node.js +1545 -202
  29. package/lib/PointerEvents.d.ts +2 -1
  30. package/lib/PointerEvents.js +19 -1
  31. package/lib/Shape.d.ts +99 -14
  32. package/lib/Shape.js +1101 -67
  33. package/lib/Stage.d.ts +85 -2
  34. package/lib/Stage.js +205 -14
  35. package/lib/Tween.d.ts +159 -4
  36. package/lib/Tween.js +243 -24
  37. package/lib/Util.d.ts +148 -4
  38. package/lib/Util.js +354 -82
  39. package/lib/Validators.d.ts +0 -2
  40. package/lib/Validators.js +3 -25
  41. package/lib/_CoreInternals.d.ts +12 -6
  42. package/lib/_CoreInternals.js +1 -0
  43. package/lib/_FullInternals.d.ts +17 -6
  44. package/lib/_FullInternals.js +7 -0
  45. package/lib/canvas-backend.js +6 -0
  46. package/lib/filters/Blur.d.ts +18 -0
  47. package/lib/filters/Blur.js +97 -24
  48. package/lib/filters/Brighten.d.ts +21 -0
  49. package/lib/filters/Brighten.js +24 -0
  50. package/lib/filters/Brightness.d.ts +11 -0
  51. package/lib/filters/Brightness.js +16 -0
  52. package/lib/filters/Contrast.d.ts +10 -0
  53. package/lib/filters/Contrast.js +21 -0
  54. package/lib/filters/Emboss.d.ts +20 -0
  55. package/lib/filters/Emboss.js +68 -8
  56. package/lib/filters/Enhance.d.ts +14 -0
  57. package/lib/filters/Enhance.js +34 -0
  58. package/lib/filters/Grayscale.d.ts +9 -0
  59. package/lib/filters/Grayscale.js +12 -0
  60. package/lib/filters/HSL.d.ts +17 -0
  61. package/lib/filters/HSL.js +48 -5
  62. package/lib/filters/HSV.d.ts +18 -0
  63. package/lib/filters/HSV.js +49 -5
  64. package/lib/filters/Invert.d.ts +9 -0
  65. package/lib/filters/Invert.js +12 -0
  66. package/lib/filters/Kaleidoscope.d.ts +12 -0
  67. package/lib/filters/Kaleidoscope.js +83 -0
  68. package/lib/filters/Mask.d.ts +11 -0
  69. package/lib/filters/Mask.js +18 -0
  70. package/lib/filters/Noise.d.ts +19 -0
  71. package/lib/filters/Noise.js +19 -0
  72. package/lib/filters/Pixelate.d.ts +20 -0
  73. package/lib/filters/Pixelate.js +28 -1
  74. package/lib/filters/Posterize.d.ts +21 -0
  75. package/lib/filters/Posterize.js +22 -0
  76. package/lib/filters/RGB.d.ts +21 -0
  77. package/lib/filters/RGB.js +43 -28
  78. package/lib/filters/RGBA.d.ts +23 -0
  79. package/lib/filters/RGBA.js +28 -41
  80. package/lib/filters/Sepia.d.ts +9 -0
  81. package/lib/filters/Sepia.js +10 -0
  82. package/lib/filters/Solarize.d.ts +10 -0
  83. package/lib/filters/Solarize.js +11 -0
  84. package/lib/filters/Threshold.d.ts +22 -0
  85. package/lib/filters/Threshold.js +22 -0
  86. package/lib/shapes/Arc.d.ts +40 -0
  87. package/lib/shapes/Arc.js +89 -6
  88. package/lib/shapes/Arrow.d.ts +24 -1
  89. package/lib/shapes/Arrow.js +83 -0
  90. package/lib/shapes/Circle.d.ts +18 -0
  91. package/lib/shapes/Circle.js +36 -9
  92. package/lib/shapes/Ellipse.d.ts +34 -2
  93. package/lib/shapes/Ellipse.js +67 -9
  94. package/lib/shapes/Image.d.ts +49 -0
  95. package/lib/shapes/Image.js +151 -22
  96. package/lib/shapes/Label.d.ts +65 -1
  97. package/lib/shapes/Label.js +113 -20
  98. package/lib/shapes/Line.d.ts +50 -8
  99. package/lib/shapes/Line.js +116 -20
  100. package/lib/shapes/Path.d.ts +56 -6
  101. package/lib/shapes/Path.js +203 -128
  102. package/lib/shapes/Rect.d.ts +18 -0
  103. package/lib/shapes/Rect.js +36 -0
  104. package/lib/shapes/RegularPolygon.d.ts +22 -0
  105. package/lib/shapes/RegularPolygon.js +70 -0
  106. package/lib/shapes/Ring.d.ts +19 -0
  107. package/lib/shapes/Ring.js +51 -5
  108. package/lib/shapes/Sprite.d.ts +78 -0
  109. package/lib/shapes/Sprite.js +238 -25
  110. package/lib/shapes/Star.d.ts +23 -0
  111. package/lib/shapes/Star.js +62 -0
  112. package/lib/shapes/Text.d.ts +59 -1
  113. package/lib/shapes/Text.js +481 -97
  114. package/lib/shapes/TextPath.d.ts +49 -3
  115. package/lib/shapes/TextPath.js +278 -34
  116. package/lib/shapes/Transformer.d.ts +124 -12
  117. package/lib/shapes/Transformer.js +864 -140
  118. package/lib/shapes/Wedge.d.ts +22 -0
  119. package/lib/shapes/Wedge.js +67 -3
  120. package/lib/skia-backend.js +9 -0
  121. package/lib/types.d.ts +2 -23
  122. package/lib/types.js +1 -21
  123. package/package.json +12 -16
package/lib/Group.d.ts CHANGED
@@ -4,6 +4,17 @@ import type { Node } from './Node.ts';
4
4
  import type { Shape } from './Shape.ts';
5
5
  export interface GroupConfig extends ContainerConfig {
6
6
  }
7
+ /**
8
+ * Group constructor. Groups are used to contain shapes or other groups.
9
+ * @constructor
10
+ * @memberof Konva
11
+ * @augments Konva.Container
12
+ * @param {Object} config
13
+ * @@nodeParams
14
+ * @@containerParams
15
+ * @example
16
+ * var group = new Konva.Group();
17
+ */
7
18
  export declare class Group extends Container<Group | Shape> {
8
19
  _validateAdd(child: Node): void;
9
20
  }
package/lib/Group.js CHANGED
@@ -1,6 +1,17 @@
1
1
  import { Util } from "./Util.js";
2
2
  import { Container } from "./Container.js";
3
3
  import { _registerNode } from "./Global.js";
4
+ /**
5
+ * Group constructor. Groups are used to contain shapes or other groups.
6
+ * @constructor
7
+ * @memberof Konva
8
+ * @augments Konva.Container
9
+ * @param {Object} config
10
+ * @@nodeParams
11
+ * @@containerParams
12
+ * @example
13
+ * var group = new Konva.Group();
14
+ */
4
15
  export class Group extends Container {
5
16
  _validateAdd(child) {
6
17
  const type = child.getType();
package/lib/Layer.d.ts CHANGED
@@ -3,7 +3,7 @@ import { Container } from './Container.ts';
3
3
  import { Node } from './Node.ts';
4
4
  import { SceneCanvas, HitCanvas } from './Canvas.ts';
5
5
  import type { Stage } from './Stage.ts';
6
- import type { GetSet, Vector2d } from './types.ts';
6
+ import type { GetSet, IRect, Vector2d } from './types.ts';
7
7
  import type { Group } from './Group.ts';
8
8
  import type { Shape } from './Shape.ts';
9
9
  export interface LayerConfig extends ContainerConfig {
@@ -11,17 +11,64 @@ export interface LayerConfig extends ContainerConfig {
11
11
  hitGraphEnabled?: boolean;
12
12
  imageSmoothingEnabled?: boolean;
13
13
  }
14
+ /**
15
+ * Layer constructor. Layers are tied to their own canvas element and are used
16
+ * to contain groups or shapes.
17
+ * @constructor
18
+ * @memberof Konva
19
+ * @augments Konva.Container
20
+ * @param {Object} config
21
+ * @param {Boolean} [config.clearBeforeDraw] set this property to false if you don't want
22
+ * to clear the canvas before each layer draw. The default value is true.
23
+ * @@nodeParams
24
+ * @@containerParams
25
+ * @example
26
+ * var layer = new Konva.Layer();
27
+ * stage.add(layer);
28
+ * // now you can add shapes, groups into the layer
29
+ */
14
30
  export declare class Layer extends Container<Group | Shape> {
15
31
  canvas: SceneCanvas;
16
32
  hitCanvas: HitCanvas;
17
33
  _waitingForDraw: boolean;
18
34
  constructor(config?: LayerConfig);
19
35
  createPNGStream(): any;
36
+ /**
37
+ * get layer canvas wrapper
38
+ * @method
39
+ * @name Konva.Layer#getCanvas
40
+ */
20
41
  getCanvas(): SceneCanvas;
42
+ /**
43
+ * get native canvas element
44
+ * @method
45
+ * @name Konva.Layer#getNativeCanvasElement
46
+ */
21
47
  getNativeCanvasElement(): HTMLCanvasElement;
48
+ /**
49
+ * get layer hit canvas
50
+ * @method
51
+ * @name Konva.Layer#getHitCanvas
52
+ */
22
53
  getHitCanvas(): HitCanvas;
54
+ /**
55
+ * get layer canvas context
56
+ * @method
57
+ * @name Konva.Layer#getContext
58
+ */
23
59
  getContext(): import("./Context.ts").Context;
24
- clear(bounds?: any): this;
60
+ /**
61
+ * clear the scene and hit canvas of the layer. The nodes stay and are
62
+ * drawn again by the next `draw()`
63
+ * @method
64
+ * @name Konva.Layer#clear
65
+ * @param {Object} [bounds] clear only this rectangle: `{ x, y, width, height }`
66
+ * @returns {Konva.Layer}
67
+ * @example
68
+ * layer.clear();
69
+ * layer.clear({ x: 0, y: 0, width: 100, height: 100 });
70
+ */
71
+ clear(bounds?: IRect): this;
25
72
  setZIndex(index: number): this;
26
73
  moveToTop(): boolean;
27
74
  moveUp(): boolean;
@@ -30,7 +77,7 @@ export declare class Layer extends Container<Group | Shape> {
30
77
  getLayer(): this;
31
78
  remove(): this;
32
79
  getStage(): Stage;
33
- setSize({ width, height }: {
80
+ _setSize({ width, height }: {
34
81
  width: any;
35
82
  height: any;
36
83
  }): this;
@@ -39,25 +86,103 @@ export declare class Layer extends Container<Group | Shape> {
39
86
  _toKonvaCanvas(config: any): SceneCanvas;
40
87
  _checkVisibility(): void;
41
88
  _setSmoothEnabled(): void;
89
+ /**
90
+ * get/set width of layer. getter return width of stage. setter doing nothing.
91
+ * if you want change width use `stage.width(value);`
92
+ * @name Konva.Layer#width
93
+ * @method
94
+ * @returns {Number}
95
+ * @example
96
+ * var width = layer.width();
97
+ */
42
98
  getWidth(): number | undefined;
43
99
  setWidth(): void;
100
+ /**
101
+ * get/set height of layer.getter return height of stage. setter doing nothing.
102
+ * if you want change height use `stage.height(value);`
103
+ * @name Konva.Layer#height
104
+ * @method
105
+ * @returns {Number}
106
+ * @example
107
+ * var height = layer.height();
108
+ */
44
109
  getHeight(): number | undefined;
45
110
  setHeight(): void;
111
+ /**
112
+ * batch draw. this function will not do immediate draw
113
+ * but it will schedule drawing to next tick (requestAnimFrame)
114
+ * @method
115
+ * @name Konva.Layer#batchDraw
116
+ * @return {Konva.Layer} this
117
+ */
46
118
  batchDraw(): this;
119
+ /**
120
+ * get visible intersection shape. This is the preferred
121
+ * method for determining if a point intersects a shape or not.
122
+ * It reads the hit canvas, so it finds what pointer events would: nodes with
123
+ * listening set to false or invisible nodes are not detected, a shape with opacity 0
124
+ * is, and `hitStrokeWidth` counts. The position is relative to the top left corner of the
125
+ * stage container, like `stage.getPointerPosition()`, without the stage transform
126
+ * @method
127
+ * @name Konva.Layer#getIntersection
128
+ * @param {Object} pos
129
+ * @param {Number} pos.x
130
+ * @param {Number} pos.y
131
+ * @returns {Konva.Node}
132
+ * @example
133
+ * var shape = layer.getIntersection({x: 50, y: 50});
134
+ */
47
135
  getIntersection(pos: Vector2d): Shape<import("./Shape.ts").ShapeConfig> | null;
48
136
  _getIntersection(pos: Vector2d): {
49
137
  shape?: Shape;
50
138
  antialiased?: boolean;
51
139
  };
52
140
  drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas): this;
141
+ shouldDrawHit(top?: Node): boolean;
53
142
  drawHit(can?: HitCanvas, top?: Node): this;
143
+ /**
144
+ * enable hit graph. **DEPRECATED!** Use `layer.listening(true)` instead.
145
+ * @name Konva.Layer#enableHitGraph
146
+ * @method
147
+ * @returns {Layer}
148
+ */
54
149
  enableHitGraph(): this;
150
+ /**
151
+ * disable hit graph. **DEPRECATED!** Use `layer.listening(false)` instead.
152
+ * @name Konva.Layer#disableHitGraph
153
+ * @method
154
+ * @returns {Layer}
155
+ */
55
156
  disableHitGraph(): this;
56
157
  setHitGraphEnabled(val: any): void;
57
158
  getHitGraphEnabled(val: any): boolean;
159
+ /**
160
+ * Show or hide hit canvas over the stage. May be useful for debugging custom hitFunc
161
+ * @name Konva.Layer#toggleHitCanvas
162
+ * @method
163
+ */
58
164
  toggleHitCanvas(): void;
59
165
  destroy(): this;
60
166
  hitGraphEnabled: GetSet<boolean, this>;
61
167
  clearBeforeDraw: GetSet<boolean, this>;
62
168
  imageSmoothingEnabled: GetSet<boolean, this>;
63
169
  }
170
+ /**
171
+ * get/set hitGraphEnabled flag. **DEPRECATED!** Use `layer.listening(false)` instead.
172
+ * Disabling the hit graph will greatly increase
173
+ * draw performance because the hit graph will not be redrawn each time the layer is
174
+ * drawn. This, however, also disables mouse/touch event detection
175
+ * @name Konva.Layer#hitGraphEnabled
176
+ * @method
177
+ * @param {Boolean} enabled
178
+ * @returns {Boolean}
179
+ * @example
180
+ * // get hitGraphEnabled flag
181
+ * var hitGraphEnabled = layer.hitGraphEnabled();
182
+ *
183
+ * // disable hit graph
184
+ * layer.hitGraphEnabled(false);
185
+ *
186
+ * // enable hit graph
187
+ * layer.hitGraphEnabled(true);
188
+ */
package/lib/Layer.js CHANGED
@@ -3,16 +3,38 @@ import { Container } from "./Container.js";
3
3
  import { Node } from "./Node.js";
4
4
  import { Factory } from "./Factory.js";
5
5
  import { SceneCanvas, HitCanvas } from "./Canvas.js";
6
- import { getBooleanValidator } from "./Validators.js";
7
6
  import { shapes } from "./Shape.js";
8
- import { _registerNode } from "./Global.js";
9
- const BEFORE_DRAW = 'beforeDraw', DRAW = 'draw', INTERSECTION_OFFSETS = [
10
- { x: 0, y: 0 },
11
- { x: -1, y: -1 },
12
- { x: 1, y: -1 },
13
- { x: 1, y: 1 },
14
- { x: -1, y: 1 },
15
- ], INTERSECTION_OFFSETS_LEN = INTERSECTION_OFFSETS.length;
7
+ import { _registerNode, Konva } from "./Global.js";
8
+ import { DD } from "./DragAndDrop.js";
9
+ // constants
10
+ const BEFORE_DRAW = 'beforeDraw', DRAW = 'draw',
11
+ // getIntersection() reads the hit canvas around an edge pixel in blocks
12
+ // of this radius, doubling it as long as the search goes on
13
+ HIT_SEARCH_RADIUS = 10, HIT_SEARCH_MAX_DISTANCE = 256;
14
+ // the shape drawn at a pixel of the hit graph: an opaque pixel, or an edge
15
+ // pixel that the shape covers by at least half, so that its colour read back
16
+ // premultiplied still rounds to the key of the shape (see getHitColorKey)
17
+ function getHitShape(data, i) {
18
+ return data[i + 3] >= 128
19
+ ? shapes[Util.getHitColorKey(data[i], data[i + 1], data[i + 2])]
20
+ : undefined;
21
+ }
22
+ /**
23
+ * Layer constructor. Layers are tied to their own canvas element and are used
24
+ * to contain groups or shapes.
25
+ * @constructor
26
+ * @memberof Konva
27
+ * @augments Konva.Container
28
+ * @param {Object} config
29
+ * @param {Boolean} [config.clearBeforeDraw] set this property to false if you don't want
30
+ * to clear the canvas before each layer draw. The default value is true.
31
+ * @@nodeParams
32
+ * @@containerParams
33
+ * @example
34
+ * var layer = new Konva.Layer();
35
+ * stage.add(layer);
36
+ * // now you can add shapes, groups into the layer
37
+ */
16
38
  export class Layer extends Container {
17
39
  constructor(config) {
18
40
  super(config);
@@ -26,27 +48,60 @@ export class Layer extends Container {
26
48
  this.on('imageSmoothingEnabledChange.konva', this._setSmoothEnabled);
27
49
  this._setSmoothEnabled();
28
50
  }
51
+ // for nodejs?
29
52
  createPNGStream() {
30
53
  const c = this.canvas._canvas;
31
54
  return c.createPNGStream();
32
55
  }
56
+ /**
57
+ * get layer canvas wrapper
58
+ * @method
59
+ * @name Konva.Layer#getCanvas
60
+ */
33
61
  getCanvas() {
34
62
  return this.canvas;
35
63
  }
64
+ /**
65
+ * get native canvas element
66
+ * @method
67
+ * @name Konva.Layer#getNativeCanvasElement
68
+ */
36
69
  getNativeCanvasElement() {
37
70
  return this.canvas._canvas;
38
71
  }
72
+ /**
73
+ * get layer hit canvas
74
+ * @method
75
+ * @name Konva.Layer#getHitCanvas
76
+ */
39
77
  getHitCanvas() {
40
78
  return this.hitCanvas;
41
79
  }
80
+ /**
81
+ * get layer canvas context
82
+ * @method
83
+ * @name Konva.Layer#getContext
84
+ */
42
85
  getContext() {
43
86
  return this.getCanvas().getContext();
44
87
  }
88
+ /**
89
+ * clear the scene and hit canvas of the layer. The nodes stay and are
90
+ * drawn again by the next `draw()`
91
+ * @method
92
+ * @name Konva.Layer#clear
93
+ * @param {Object} [bounds] clear only this rectangle: `{ x, y, width, height }`
94
+ * @returns {Konva.Layer}
95
+ * @example
96
+ * layer.clear();
97
+ * layer.clear({ x: 0, y: 0, width: 100, height: 100 });
98
+ */
45
99
  clear(bounds) {
46
100
  this.getContext().clear(bounds);
47
101
  this.getHitCanvas().getContext().clear(bounds);
48
102
  return this;
49
103
  }
104
+ // extend Node.prototype.setZIndex
50
105
  setZIndex(index) {
51
106
  super.setZIndex(index);
52
107
  const stage = this.getStage();
@@ -88,6 +143,7 @@ export class Layer extends Container {
88
143
  }
89
144
  return true;
90
145
  }
146
+ // extend Node.prototype.moveDown
91
147
  moveDown() {
92
148
  if (Node.prototype.moveDown.call(this)) {
93
149
  const stage = this.getStage();
@@ -102,6 +158,7 @@ export class Layer extends Container {
102
158
  }
103
159
  return false;
104
160
  }
161
+ // extend Node.prototype.moveToBottom
105
162
  moveToBottom() {
106
163
  if (Node.prototype.moveToBottom.call(this)) {
107
164
  const stage = this.getStage();
@@ -130,12 +187,18 @@ export class Layer extends Container {
130
187
  getStage() {
131
188
  return this.parent;
132
189
  }
133
- setSize({ width, height }) {
190
+ // the canvases follow the stage size. The public size() of a layer, like
191
+ // width() and height(), warns and does nothing
192
+ _setSize({ width, height }) {
134
193
  this.canvas.setSize(width, height);
135
194
  this._syncHitCanvasSize();
136
195
  this._setSmoothEnabled();
137
196
  return this;
138
197
  }
198
+ // the hit canvas is only allocated while the layer is listening;
199
+ // for a non-listening layer it is kept released (0x0), so a purely
200
+ // presentational layer does not pay for a stage-sized hit bitmap
201
+ // https://github.com/konvajs/konva/issues/2009
139
202
  _syncHitCanvasSize() {
140
203
  const listening = this.isListening();
141
204
  this.hitCanvas.setSizeIfChanged((listening ? this.getWidth() : 0) || 0, (listening ? this.getHeight() : 0) || 0);
@@ -167,6 +230,15 @@ export class Layer extends Container {
167
230
  this.getContext()._context.imageSmoothingEnabled =
168
231
  this.imageSmoothingEnabled();
169
232
  }
233
+ /**
234
+ * get/set width of layer. getter return width of stage. setter doing nothing.
235
+ * if you want change width use `stage.width(value);`
236
+ * @name Konva.Layer#width
237
+ * @method
238
+ * @returns {Number}
239
+ * @example
240
+ * var width = layer.width();
241
+ */
170
242
  getWidth() {
171
243
  if (this.parent) {
172
244
  return this.parent.width();
@@ -175,6 +247,15 @@ export class Layer extends Container {
175
247
  setWidth() {
176
248
  Util.warn('Can not change width of layer. Use "stage.width(value)" function instead.');
177
249
  }
250
+ /**
251
+ * get/set height of layer.getter return height of stage. setter doing nothing.
252
+ * if you want change height use `stage.height(value);`
253
+ * @name Konva.Layer#height
254
+ * @method
255
+ * @returns {Number}
256
+ * @example
257
+ * var height = layer.height();
258
+ */
178
259
  getHeight() {
179
260
  if (this.parent) {
180
261
  return this.parent.height();
@@ -183,70 +264,108 @@ export class Layer extends Container {
183
264
  setHeight() {
184
265
  Util.warn('Can not change height of layer. Use "stage.height(value)" function instead.');
185
266
  }
267
+ /**
268
+ * batch draw. this function will not do immediate draw
269
+ * but it will schedule drawing to next tick (requestAnimFrame)
270
+ * @method
271
+ * @name Konva.Layer#batchDraw
272
+ * @return {Konva.Layer} this
273
+ */
186
274
  batchDraw() {
187
275
  var _a;
188
276
  if (!this._waitingForDraw) {
189
277
  this._waitingForDraw = true;
278
+ // ask the window the stage is rendered in for the frame: a stage in a
279
+ // popout window keeps drawing while the opener window is not visible
190
280
  Util.requestAnimFrame(() => {
191
- this.draw();
281
+ // reset before drawing, so a throwing draw does not freeze the layer
282
+ // and a draw handler can request the next frame
192
283
  this._waitingForDraw = false;
284
+ this.draw();
193
285
  }, (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow());
194
286
  }
195
287
  return this;
196
288
  }
289
+ /**
290
+ * get visible intersection shape. This is the preferred
291
+ * method for determining if a point intersects a shape or not.
292
+ * It reads the hit canvas, so it finds what pointer events would: nodes with
293
+ * listening set to false or invisible nodes are not detected, a shape with opacity 0
294
+ * is, and `hitStrokeWidth` counts. The position is relative to the top left corner of the
295
+ * stage container, like `stage.getPointerPosition()`, without the stage transform
296
+ * @method
297
+ * @name Konva.Layer#getIntersection
298
+ * @param {Object} pos
299
+ * @param {Number} pos.x
300
+ * @param {Number} pos.y
301
+ * @returns {Konva.Node}
302
+ * @example
303
+ * var shape = layer.getIntersection({x: 50, y: 50});
304
+ */
197
305
  getIntersection(pos) {
198
306
  if (!this.isListening() || !this.isVisible()) {
199
307
  return null;
200
308
  }
201
- let spiralSearchDistance = 1;
202
- let continueSearch = false;
203
- while (true) {
204
- for (let i = 0; i < INTERSECTION_OFFSETS_LEN; i++) {
205
- const intersectionOffset = INTERSECTION_OFFSETS[i];
206
- const obj = this._getIntersection({
207
- x: pos.x + intersectionOffset.x * spiralSearchDistance,
208
- y: pos.y + intersectionOffset.y * spiralSearchDistance,
209
- });
210
- const shape = obj.shape;
211
- if (shape) {
212
- return shape;
213
- }
214
- continueSearch = !!obj.antialiased;
215
- if (!obj.antialiased) {
216
- break;
217
- }
309
+ const hit = this._getIntersection(pos);
310
+ if (hit.shape) {
311
+ return hit.shape;
312
+ }
313
+ if (!hit.antialiased) {
314
+ return null;
315
+ }
316
+ // the pointer is on the outer part of an edge, or on an opaque pixel with
317
+ // the colours of two shapes mixed. Walk outward ring by ring to the
318
+ // nearest pixel that resolves to a shape. The smoothed edge of a cached
319
+ // node that is scaled up can be many pixels wide, so keep going while the
320
+ // alpha still rises (the ramp leads into the shape) or the ring holds
321
+ // mixed pixels. A thin line has a flat alpha, so the search stops at once
322
+ // instead of walking along it to an unrelated shape nearby
323
+ const ratio = this.hitCanvas.pixelRatio, cx = Math.floor(pos.x * ratio), cy = Math.floor(pos.y * ratio);
324
+ const read = (r) => this.hitCanvas.context.getImageData(cx - r, cy - r, r * 2 + 1, r * 2 + 1)
325
+ .data;
326
+ let r = HIT_SEARCH_RADIUS, size = r * 2 + 1, data = read(r), previousMax = data[(r * size + r) * 4 + 3];
327
+ for (let d = 1; d <= HIT_SEARCH_MAX_DISTANCE; d++) {
328
+ if (d > r) {
329
+ r = Math.min(HIT_SEARCH_MAX_DISTANCE, r * 2);
330
+ size = r * 2 + 1;
331
+ data = read(r);
218
332
  }
219
- if (continueSearch) {
220
- spiralSearchDistance += 1;
333
+ let max = 0;
334
+ for (let y = -d; y <= d; y++) {
335
+ // whole top and bottom rows, only the two side pixels of the others
336
+ const step = Math.abs(y) === d ? 1 : d * 2;
337
+ for (let x = -d; x <= d; x += step) {
338
+ const i = ((y + r) * size + x + r) * 4;
339
+ const shape = getHitShape(data, i);
340
+ if (shape) {
341
+ return shape;
342
+ }
343
+ max = Math.max(max, data[i + 3]);
344
+ }
221
345
  }
222
- else {
346
+ // 255 here is a mixed pixel, as an opaque one resolved above
347
+ if (max <= previousMax && max !== 255) {
223
348
  return null;
224
349
  }
350
+ previousMax = max;
225
351
  }
352
+ return null;
226
353
  }
227
354
  _getIntersection(pos) {
355
+ // a non-listening layer keeps its hit canvas released (0x0),
356
+ // and a just-enabled layer may not have drawn its hit graph yet
228
357
  if (!this.hitCanvas.width || !this.hitCanvas.height) {
229
358
  return {};
230
359
  }
231
360
  const ratio = this.hitCanvas.pixelRatio;
232
- const p = this.hitCanvas.context.getImageData(Math.round(pos.x * ratio), Math.round(pos.y * ratio), 1, 1).data;
233
- const p3 = p[3];
234
- if (p3 === 255) {
235
- const colorKey = Util.getHitColorKey(p[0], p[1], p[2]);
236
- const shape = shapes[colorKey];
237
- if (shape) {
238
- return {
239
- shape: shape,
240
- };
241
- }
242
- return {
243
- antialiased: true,
244
- };
361
+ const p = this.hitCanvas.context.getImageData(Math.floor(pos.x * ratio), Math.floor(pos.y * ratio), 1, 1).data;
362
+ const shape = getHitShape(p, 0);
363
+ if (shape) {
364
+ return { shape };
245
365
  }
246
- else if (p3 > 0) {
247
- return {
248
- antialiased: true,
249
- };
366
+ if (p[3] > 0) {
367
+ // an edge pixel, or an opaque one with the colours of two shapes mixed
368
+ return { antialiased: true };
250
369
  }
251
370
  return {};
252
371
  }
@@ -264,9 +383,32 @@ export class Layer extends Container {
264
383
  });
265
384
  return this;
266
385
  }
386
+ // the hit graph is skipped while a node of the layer (or the stage) is
387
+ // dragged or a transformer on the layer, or of a node on it, is
388
+ // transforming, as the layer is redrawn every frame; the draw that follows
389
+ // restores it
390
+ shouldDrawHit(top) {
391
+ var _a;
392
+ if (!super.shouldDrawHit(top)) {
393
+ return false;
394
+ }
395
+ if (top || Konva.hitOnDragEnabled) {
396
+ return true;
397
+ }
398
+ let underDrag = false;
399
+ DD._dragElements.forEach((elem) => {
400
+ if (elem.dragStatus === 'dragging' &&
401
+ (elem.node.nodeType === 'Stage' || elem.node.getLayer() === this)) {
402
+ underDrag = true;
403
+ }
404
+ });
405
+ return !underDrag && !((_a = Konva['Transformer']) === null || _a === void 0 ? void 0 : _a._isLayerTransforming(this));
406
+ }
267
407
  drawHit(can, top) {
268
408
  const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas);
269
409
  if (!can && layer) {
410
+ // allocate or release the hit canvas to match the current listening
411
+ // state - this also picks up a listening change made after the last draw
270
412
  layer._syncHitCanvasSize();
271
413
  if (layer.clearBeforeDraw()) {
272
414
  layer.getHitCanvas().getContext().clear();
@@ -275,10 +417,22 @@ export class Layer extends Container {
275
417
  Container.prototype.drawHit.call(this, canvas, top);
276
418
  return this;
277
419
  }
420
+ /**
421
+ * enable hit graph. **DEPRECATED!** Use `layer.listening(true)` instead.
422
+ * @name Konva.Layer#enableHitGraph
423
+ * @method
424
+ * @returns {Layer}
425
+ */
278
426
  enableHitGraph() {
279
427
  this.hitGraphEnabled(true);
280
428
  return this;
281
429
  }
430
+ /**
431
+ * disable hit graph. **DEPRECATED!** Use `layer.listening(false)` instead.
432
+ * @name Konva.Layer#disableHitGraph
433
+ * @method
434
+ * @returns {Layer}
435
+ */
282
436
  disableHitGraph() {
283
437
  this.hitGraphEnabled(false);
284
438
  return this;
@@ -291,6 +445,11 @@ export class Layer extends Container {
291
445
  Util.warn('hitGraphEnabled method is deprecated. Please use layer.listening() instead.');
292
446
  return this.listening();
293
447
  }
448
+ /**
449
+ * Show or hide hit canvas over the stage. May be useful for debugging custom hitFunc
450
+ * @name Konva.Layer#toggleHitCanvas
451
+ * @method
452
+ */
294
453
  toggleHitCanvas() {
295
454
  if (!this.parent || !this.parent['content']) {
296
455
  return;
@@ -311,6 +470,58 @@ export class Layer extends Container {
311
470
  }
312
471
  Layer.prototype.nodeType = 'Layer';
313
472
  _registerNode(Layer);
473
+ /**
474
+ * get/set imageSmoothingEnabled flag
475
+ * For more info see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/imageSmoothingEnabled
476
+ * @name Konva.Layer#imageSmoothingEnabled
477
+ * @method
478
+ * @param {Boolean} imageSmoothingEnabled
479
+ * @returns {Boolean}
480
+ * @example
481
+ * // get imageSmoothingEnabled flag
482
+ * var imageSmoothingEnabled = layer.imageSmoothingEnabled();
483
+ *
484
+ * layer.imageSmoothingEnabled(false);
485
+ *
486
+ * layer.imageSmoothingEnabled(true);
487
+ */
314
488
  Factory.addGetterSetter(Layer, 'imageSmoothingEnabled', true);
489
+ /**
490
+ * get/set clearBeforeDraw flag which determines if the layer is cleared or not
491
+ * before drawing
492
+ * @name Konva.Layer#clearBeforeDraw
493
+ * @method
494
+ * @param {Boolean} clearBeforeDraw
495
+ * @returns {Boolean}
496
+ * @example
497
+ * // get clearBeforeDraw flag
498
+ * var clearBeforeDraw = layer.clearBeforeDraw();
499
+ *
500
+ * // disable clear before draw
501
+ * layer.clearBeforeDraw(false);
502
+ *
503
+ * // enable clear before draw
504
+ * layer.clearBeforeDraw(true);
505
+ */
315
506
  Factory.addGetterSetter(Layer, 'clearBeforeDraw', true);
316
- Factory.addGetterSetter(Layer, 'hitGraphEnabled', true, getBooleanValidator());
507
+ // the getter and setter are the deprecated ones above
508
+ Factory.addOverloadedGetterSetter(Layer, 'hitGraphEnabled');
509
+ /**
510
+ * get/set hitGraphEnabled flag. **DEPRECATED!** Use `layer.listening(false)` instead.
511
+ * Disabling the hit graph will greatly increase
512
+ * draw performance because the hit graph will not be redrawn each time the layer is
513
+ * drawn. This, however, also disables mouse/touch event detection
514
+ * @name Konva.Layer#hitGraphEnabled
515
+ * @method
516
+ * @param {Boolean} enabled
517
+ * @returns {Boolean}
518
+ * @example
519
+ * // get hitGraphEnabled flag
520
+ * var hitGraphEnabled = layer.hitGraphEnabled();
521
+ *
522
+ * // disable hit graph
523
+ * layer.hitGraphEnabled(false);
524
+ *
525
+ * // enable hit graph
526
+ * layer.hitGraphEnabled(true);
527
+ */