konva 10.3.3 → 10.4.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 +1327 -1572
  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 +1 -0
  16. package/lib/DragAndDrop.js +66 -17
  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 +692 -30
  28. package/lib/Node.js +1534 -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 +81 -1
  34. package/lib/Stage.js +166 -7
  35. package/lib/Tween.d.ts +159 -4
  36. package/lib/Tween.js +243 -24
  37. package/lib/Util.d.ts +147 -4
  38. package/lib/Util.js +332 -82
  39. package/lib/Validators.d.ts +0 -2
  40. package/lib/Validators.js +3 -25
  41. package/lib/_CoreInternals.d.ts +10 -6
  42. package/lib/_CoreInternals.js +1 -0
  43. package/lib/_FullInternals.d.ts +15 -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 +40 -6
  101. package/lib/shapes/Path.js +187 -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 +108 -11
  117. package/lib/shapes/Transformer.js +744 -102
  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/Stage.d.ts CHANGED
@@ -8,6 +8,21 @@ export interface StageConfig extends ContainerConfig {
8
8
  container?: HTMLDivElement | string;
9
9
  }
10
10
  export declare const stages: Stage[];
11
+ /**
12
+ * Stage constructor. A stage is used to contain multiple layers
13
+ * @constructor
14
+ * @memberof Konva
15
+ * @augments Konva.Container
16
+ * @param {Object} config
17
+ * @param {String|Element} config.container Container selector or DOM element
18
+ * @@nodeParams
19
+ * @example
20
+ * var stage = new Konva.Stage({
21
+ * width: 500,
22
+ * height: 800,
23
+ * container: 'containerId' // or "#containerId" or ".containerClass"
24
+ * });
25
+ */
11
26
  export declare class Stage extends Container<Layer, StageConfig> {
12
27
  content: HTMLDivElement;
13
28
  pointerPos: Vector2d | null;
@@ -34,12 +49,36 @@ export declare class Stage extends Container<Layer, StageConfig> {
34
49
  constructor(config: StageConfig);
35
50
  _validateAdd(child: any): void;
36
51
  _checkVisibility(): void;
52
+ /**
53
+ * The window the stage is rendered in. It is not always the window Konva was
54
+ * imported into: the container may belong to an iframe, or to a window
55
+ * opened with `window.open`.
56
+ */
37
57
  _getOwnerWindow(): Window | null;
58
+ /**
59
+ * set container dom element which contains the stage wrapper div element
60
+ * @method
61
+ * @name Konva.Stage#setContainer
62
+ * @param {DomElement} container can pass in a dom element or id string
63
+ */
38
64
  setContainer(container: any): this;
39
65
  shouldDrawHit(): boolean;
66
+ /**
67
+ * clear all layers
68
+ * @method
69
+ * @name Konva.Stage#clear
70
+ */
40
71
  clear(): this;
41
72
  clone(obj?: any): this;
42
73
  destroy(): this;
74
+ /**
75
+ * returns ABSOLUTE pointer position which can be a touch position or mouse position
76
+ * pointer position doesn't include any transforms (such as scale) of the stage
77
+ * it is just a plain position of pointer relative to top-left corner of the canvas
78
+ * @method
79
+ * @name Konva.Stage#getPointerPosition
80
+ * @returns {Vector2d|null}
81
+ */
43
82
  getPointerPosition(): Vector2d | null;
44
83
  _getPointerById(id?: number): (Vector2d & {
45
84
  id?: number;
@@ -50,6 +89,22 @@ export declare class Stage extends Container<Layer, StageConfig> {
50
89
  getStage(): this;
51
90
  getContent(): HTMLDivElement;
52
91
  _toKonvaCanvas(config: any): SceneCanvas;
92
+ /**
93
+ * get visible intersection shape. This is the preferred
94
+ * method for determining if a point intersects a shape or not.
95
+ * It reads the hit canvas, so it finds what pointer events would: nodes with
96
+ * listening set to false or invisible nodes are not detected, a shape with opacity 0
97
+ * is, and `hitStrokeWidth` counts. The position is relative to the top left corner of the
98
+ * stage container, like `stage.getPointerPosition()`, without the stage transform
99
+ * @method
100
+ * @name Konva.Stage#getIntersection
101
+ * @param {Object} pos
102
+ * @param {Number} pos.x
103
+ * @param {Number} pos.y
104
+ * @returns {Konva.Node}
105
+ * @example
106
+ * var shape = stage.getIntersection({x: 50, y: 50});
107
+ */
53
108
  getIntersection(pos: Vector2d): Shape<import("./Shape.ts").ShapeConfig> | null;
54
109
  _resizeDOM(): void;
55
110
  _syncBufferSize<T extends SceneCanvas | HitCanvas>(canvas: T): T;
@@ -59,6 +114,11 @@ export declare class Stage extends Container<Layer, StageConfig> {
59
114
  hasPointerCapture(pointerId: number): boolean;
60
115
  setPointerCapture(pointerId: number): void;
61
116
  releaseCapture(pointerId: number): void;
117
+ /**
118
+ * returns an array of layers
119
+ * @method
120
+ * @name Konva.Stage#getLayers
121
+ */
62
122
  getLayers(): Layer[];
63
123
  _bindContentEvents(): void;
64
124
  _pointerenter(evt: PointerEvent): void;
@@ -72,6 +132,20 @@ export declare class Stage extends Container<Layer, StageConfig> {
72
132
  _wheel(evt: any): void;
73
133
  _pointercancel(evt: PointerEvent): void;
74
134
  _lostpointercapture(evt: PointerEvent): void;
135
+ /**
136
+ * manually register pointers positions (mouse/touch) in the stage.
137
+ * So you can use stage.getPointerPosition(). Usually you don't need to use that method
138
+ * because all internal events are automatically registered. It may be useful if event
139
+ * is triggered outside of the stage, but you still want to use Konva methods to get pointers position.
140
+ * @method
141
+ * @name Konva.Stage#setPointersPositions
142
+ * @param {Object} evt Event object
143
+ * @example
144
+ *
145
+ * window.addEventListener('mousemove', (e) => {
146
+ * stage.setPointersPositions(e);
147
+ * });
148
+ */
75
149
  setPointersPositions(evt: any): void;
76
150
  _setPointerPosition(evt: any): void;
77
151
  _getContentPosition(): {
@@ -83,6 +157,12 @@ export declare class Stage extends Container<Layer, StageConfig> {
83
157
  _buildDOM(): void;
84
158
  cache(): this;
85
159
  clearCache(): this;
160
+ /**
161
+ * batch draw
162
+ * @method
163
+ * @name Konva.Stage#batchDraw
164
+ * @return {Konva.Stage} this
165
+ */
86
166
  batchDraw(): this;
87
- container: GetSet<HTMLDivElement, this>;
167
+ container: GetSet<HTMLDivElement, this, HTMLDivElement | string>;
88
168
  }
package/lib/Stage.js CHANGED
@@ -6,6 +6,7 @@ import { SceneCanvas, HitCanvas } from "./Canvas.js";
6
6
  import { DD } from "./DragAndDrop.js";
7
7
  import { _registerNode } from "./Global.js";
8
8
  import * as PointerEvents from "./PointerEvents.js";
9
+ // CONSTANTS
9
10
  const STAGE = 'Stage', STRING = 'string', PX = 'px', MOUSEOUT = 'mouseout', MOUSELEAVE = 'mouseleave', MOUSEOVER = 'mouseover', MOUSEENTER = 'mouseenter', MOUSEMOVE = 'mousemove', MOUSEDOWN = 'mousedown', MOUSEUP = 'mouseup', POINTERMOVE = 'pointermove', POINTERDOWN = 'pointerdown', POINTERUP = 'pointerup', POINTERCANCEL = 'pointercancel', LOSTPOINTERCAPTURE = 'lostpointercapture', POINTEROUT = 'pointerout', POINTERLEAVE = 'pointerleave', POINTEROVER = 'pointerover', POINTERENTER = 'pointerenter', CONTEXTMENU = 'contextmenu', TOUCHSTART = 'touchstart', TOUCHEND = 'touchend', TOUCHMOVE = 'touchmove', TOUCHCANCEL = 'touchcancel', WHEEL = 'wheel', MAX_LAYERS_NUMBER = 5, EVENTS = [
10
11
  [MOUSEENTER, '_pointerenter'],
11
12
  [MOUSEDOWN, '_pointerdown'],
@@ -93,6 +94,10 @@ function checkNoClip(attrs = {}) {
93
94
  }
94
95
  const NO_POINTERS_MESSAGE = `Pointer position is missing and not registered by the stage. Looks like it is outside of the stage container. You can set it manually from event: stage.setPointersPositions(event);`;
95
96
  export const stages = [];
97
+ // A stage may be rendered in another window than the one Konva was imported
98
+ // into: an iframe, or a window opened with `window.open`. Such a window sends
99
+ // its own events and has its own visibility state, so Konva listens to every
100
+ // window it renders a stage in
96
101
  const listeningWindows = new WeakSet();
97
102
  const listenToWindow = (win) => {
98
103
  if (!win || listeningWindows.has(win)) {
@@ -100,12 +105,29 @@ const listenToWindow = (win) => {
100
105
  }
101
106
  listeningWindows.add(win);
102
107
  DD._listenToWindow(win);
108
+ // chrome is clearing canvas in inactive browser window, causing layer content to be erased
109
+ // so let's redraw layers as soon as window becomes active
103
110
  win.document.addEventListener('visibilitychange', () => {
104
111
  stages.forEach((stage) => {
105
112
  stage.batchDraw();
106
113
  });
107
114
  });
108
115
  };
116
+ /**
117
+ * Stage constructor. A stage is used to contain multiple layers
118
+ * @constructor
119
+ * @memberof Konva
120
+ * @augments Konva.Container
121
+ * @param {Object} config
122
+ * @param {String|Element} config.container Container selector or DOM element
123
+ * @@nodeParams
124
+ * @example
125
+ * var stage = new Konva.Stage({
126
+ * width: 500,
127
+ * height: 800,
128
+ * container: 'containerId' // or "#containerId" or ".containerClass"
129
+ * });
130
+ */
109
131
  export class Stage extends Container {
110
132
  constructor(config) {
111
133
  super(checkNoClip(config));
@@ -136,11 +158,24 @@ export class Stage extends Container {
136
158
  const style = this.visible() ? '' : 'none';
137
159
  this.content.style.display = style;
138
160
  }
161
+ /**
162
+ * The window the stage is rendered in. It is not always the window Konva was
163
+ * imported into: the container may belong to an iframe, or to a window
164
+ * opened with `window.open`.
165
+ */
139
166
  _getOwnerWindow() {
140
167
  var _a;
141
168
  const element = this.content || this.container();
169
+ // a document with no browsing context, such as one of `DOMParser`, has no
170
+ // window of its own. The window Konva was imported into is used then
142
171
  return (((_a = element === null || element === void 0 ? void 0 : element.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView) || (Konva.isBrowser ? window : null));
143
172
  }
173
+ /**
174
+ * set container dom element which contains the stage wrapper div element
175
+ * @method
176
+ * @name Konva.Stage#setContainer
177
+ * @param {DomElement} container can pass in a dom element or id string
178
+ */
144
179
  setContainer(container) {
145
180
  if (typeof container === STRING) {
146
181
  let id;
@@ -167,6 +202,7 @@ export class Stage extends Container {
167
202
  this.content.parentElement.removeChild(this.content);
168
203
  }
169
204
  container.appendChild(this.content);
205
+ // the new container may be in another window, with its own events
170
206
  listenToWindow(this._getOwnerWindow());
171
207
  }
172
208
  return this;
@@ -174,6 +210,11 @@ export class Stage extends Container {
174
210
  shouldDrawHit() {
175
211
  return true;
176
212
  }
213
+ /**
214
+ * clear all layers
215
+ * @method
216
+ * @name Konva.Stage#clear
217
+ */
177
218
  clear() {
178
219
  const layers = this.children, len = layers.length;
179
220
  for (let n = 0; n < len; n++) {
@@ -191,6 +232,12 @@ export class Stage extends Container {
191
232
  }
192
233
  destroy() {
193
234
  super.destroy();
235
+ // a pending double-click timer is what closes the global double-click
236
+ // window, so close the window when the timer goes
237
+ ['mouse', 'touch', 'pointer'].forEach((type) => {
238
+ clearTimeout(this['_' + type + 'DblTimeout']);
239
+ Konva['_' + type + 'InDblClickWindow'] = false;
240
+ });
194
241
  const content = this.content;
195
242
  if (content && Util._isInDocument(content)) {
196
243
  this.container().removeChild(content);
@@ -202,6 +249,14 @@ export class Stage extends Container {
202
249
  Util.releaseCanvas(this.bufferCanvas._canvas, this.bufferHitCanvas._canvas);
203
250
  return this;
204
251
  }
252
+ /**
253
+ * returns ABSOLUTE pointer position which can be a touch position or mouse position
254
+ * pointer position doesn't include any transforms (such as scale) of the stage
255
+ * it is just a plain position of pointer relative to top-left corner of the canvas
256
+ * @method
257
+ * @name Konva.Stage#getPointerPosition
258
+ * @returns {Vector2d|null}
259
+ */
205
260
  getPointerPosition() {
206
261
  const pos = this._pointerPositions[0] || this._changedPointerPositions[0];
207
262
  if (!pos) {
@@ -241,15 +296,34 @@ export class Stage extends Container {
241
296
  if (config.x || config.y) {
242
297
  _context.translate(-1 * config.x, -1 * config.y);
243
298
  }
299
+ // every layer renders on its own canvas, like on screen, so a
300
+ // globalCompositeOperation inside a layer does not reach the layers below
244
301
  layers.forEach(function (layer) {
245
302
  if (!layer.isVisible()) {
246
303
  return;
247
304
  }
248
305
  const layerCanvas = layer._toKonvaCanvas(config);
249
306
  _context.drawImage(layerCanvas._canvas, config.x, config.y, layerCanvas.getWidth() / layerCanvas.getPixelRatio(), layerCanvas.getHeight() / layerCanvas.getPixelRatio());
307
+ Util.releaseCanvas(layerCanvas._canvas);
250
308
  });
251
309
  return canvas;
252
310
  }
311
+ /**
312
+ * get visible intersection shape. This is the preferred
313
+ * method for determining if a point intersects a shape or not.
314
+ * It reads the hit canvas, so it finds what pointer events would: nodes with
315
+ * listening set to false or invisible nodes are not detected, a shape with opacity 0
316
+ * is, and `hitStrokeWidth` counts. The position is relative to the top left corner of the
317
+ * stage container, like `stage.getPointerPosition()`, without the stage transform
318
+ * @method
319
+ * @name Konva.Stage#getIntersection
320
+ * @param {Object} pos
321
+ * @param {Number} pos.x
322
+ * @param {Number} pos.y
323
+ * @returns {Konva.Node}
324
+ * @example
325
+ * var shape = stage.getIntersection({x: 50, y: 50});
326
+ */
253
327
  getIntersection(pos) {
254
328
  if (!pos) {
255
329
  return null;
@@ -267,14 +341,19 @@ export class Stage extends Container {
267
341
  const width = this.width();
268
342
  const height = this.height();
269
343
  if (this.content) {
344
+ // set content dimensions
270
345
  this.content.style.width = width + PX;
271
346
  this.content.style.height = height + PX;
272
347
  }
348
+ // set layer dimensions
273
349
  this.children.forEach((layer) => {
274
- layer.setSize({ width, height });
350
+ layer._setSize({ width, height });
275
351
  layer.draw();
276
352
  });
277
353
  }
354
+ // "perfect drawing" uses bufferCanvas, shape.intersects() uses
355
+ // bufferHitCanvas; both are lazy - created at 0x0 and sized to the stage
356
+ // on first use
278
357
  _syncBufferSize(canvas) {
279
358
  canvas.setSizeIfChanged(this.width(), this.height());
280
359
  return canvas;
@@ -293,11 +372,13 @@ export class Stage extends Container {
293
372
  length +
294
373
  ' layers. Recommended maximum number of layers is 3-5. Adding more layers into the stage may drop the performance. Rethink your tree structure, you can use Konva.Group.');
295
374
  }
296
- layer.setSize({ width: this.width(), height: this.height() });
375
+ layer._setSize({ width: this.width(), height: this.height() });
376
+ // draw layer and append canvas to container
297
377
  layer.draw();
298
378
  if (Konva.isBrowser) {
299
379
  this.content.appendChild(layer.canvas._canvas);
300
380
  }
381
+ // chainable
301
382
  return this;
302
383
  }
303
384
  getParent() {
@@ -315,8 +396,13 @@ export class Stage extends Container {
315
396
  releaseCapture(pointerId) {
316
397
  PointerEvents.releaseCapture(pointerId, this);
317
398
  }
399
+ /**
400
+ * returns an array of layers
401
+ * @method
402
+ * @name Konva.Stage#getLayers
403
+ */
318
404
  getLayers() {
319
- return this.children;
405
+ return this.getChildren();
320
406
  }
321
407
  _bindContentEvents() {
322
408
  if (!Konva.isBrowser) {
@@ -402,7 +488,9 @@ export class Stage extends Container {
402
488
  this._changedPointerPositions.forEach((pos) => {
403
489
  const shape = this.getIntersection(pos);
404
490
  DD.justDragged = false;
491
+ // probably we are staring a click
405
492
  Konva['_' + eventType + 'ListenClick'] = true;
493
+ // no shape detected? do nothing
406
494
  if (!shape || !shape.isListening()) {
407
495
  this[eventType + 'ClickStartShape'] = undefined;
408
496
  return;
@@ -410,17 +498,20 @@ export class Stage extends Container {
410
498
  if (Konva.capturePointerEventsEnabled) {
411
499
  shape.setPointerCapture(pos.id);
412
500
  }
501
+ // save where we started the click
413
502
  this[eventType + 'ClickStartShape'] = shape;
414
503
  shape._fireAndBubble(events.pointerdown, {
415
504
  evt: evt,
416
505
  pointerId: pos.id,
417
506
  });
418
507
  triggeredOnShape = true;
508
+ // only call preventDefault if the shape is listening for events
419
509
  const isTouch = evt.type.indexOf('touch') >= 0;
420
510
  if (shape.preventDefault() && evt.cancelable && isTouch) {
421
511
  evt.preventDefault();
422
512
  }
423
513
  });
514
+ // trigger down on stage if not already
424
515
  if (!triggeredOnShape) {
425
516
  this._fire(events.pointerdown, {
426
517
  evt: evt,
@@ -436,6 +527,8 @@ export class Stage extends Container {
436
527
  if (!events) {
437
528
  return;
438
529
  }
530
+ // prevent default only for touch-based interactions to avoid blocking
531
+ // native mouse wheel scrolling during drag on desktop
439
532
  const isTouchPointer = evt.type.indexOf('touch') >= 0 ||
440
533
  evt.pointerType === 'touch';
441
534
  if (Konva.isDragging() &&
@@ -505,6 +598,11 @@ export class Stage extends Container {
505
598
  return;
506
599
  }
507
600
  this.setPointersPositions(evt);
601
+ // the mouse/touch families get their "listen click" flags reset by
602
+ // DD._endDragBefore, but native "pointerup" fires before the
603
+ // "mouseup"/"touchend" that ends the drag, so for the pointer family the
604
+ // flags are still set — check the live drag state as well, otherwise
605
+ // "pointerclick" fires after a drag while "click" does not (issue #1756)
508
606
  const wasDragged = DD.justDragged || (eventType === 'pointer' && Konva.isDragging());
509
607
  const listenClick = Konva['_' + eventType + 'ListenClick'] && !wasDragged;
510
608
  const clickStartShape = this[eventType + 'ClickStartShape'];
@@ -526,19 +624,21 @@ export class Stage extends Container {
526
624
  let fireDblClick = false;
527
625
  if (Konva['_' + eventType + 'InDblClickWindow']) {
528
626
  fireDblClick = true;
529
- clearTimeout(this[eventType + 'DblTimeout']);
627
+ clearTimeout(this['_' + eventType + 'DblTimeout']);
530
628
  }
531
629
  else if (!wasDragged) {
630
+ // don't set inDblClickWindow after dragging
532
631
  Konva['_' + eventType + 'InDblClickWindow'] = true;
533
- clearTimeout(this[eventType + 'DblTimeout']);
632
+ clearTimeout(this['_' + eventType + 'DblTimeout']);
534
633
  }
535
- this[eventType + 'DblTimeout'] = setTimeout(function () {
634
+ this['_' + eventType + 'DblTimeout'] = setTimeout(function () {
536
635
  Konva['_' + eventType + 'InDblClickWindow'] = false;
537
636
  }, Konva.dblClickWindow);
538
637
  if (shape && shape.isListening()) {
539
638
  skipPointerUpTrigger = true;
540
639
  this[eventType + 'ClickEndShape'] = shape;
541
640
  shape._fireAndBubble(events.pointerup, { ...event });
641
+ // detect if click or double click occurred
542
642
  if (listenClick && clickStartShape === shape) {
543
643
  shape._fireAndBubble(events.pointerclick, { ...event });
544
644
  if (fireDblClick && clickEndShape && clickEndShape === shape) {
@@ -584,6 +684,10 @@ export class Stage extends Container {
584
684
  });
585
685
  }
586
686
  Konva['_' + eventType + 'ListenClick'] = false;
687
+ // always call preventDefault for desktop events because some browsers
688
+ // try to drag and drop the canvas element
689
+ // do not call this function on mobile because it prevent "click" event on all parent containers
690
+ // but apps may listen to it.
587
691
  if (evt.cancelable && eventType !== 'touch' && eventType !== 'pointer') {
588
692
  evt.preventDefault();
589
693
  }
@@ -628,11 +732,28 @@ export class Stage extends Container {
628
732
  _lostpointercapture(evt) {
629
733
  PointerEvents.releaseCapture(evt.pointerId);
630
734
  }
735
+ /**
736
+ * manually register pointers positions (mouse/touch) in the stage.
737
+ * So you can use stage.getPointerPosition(). Usually you don't need to use that method
738
+ * because all internal events are automatically registered. It may be useful if event
739
+ * is triggered outside of the stage, but you still want to use Konva methods to get pointers position.
740
+ * @method
741
+ * @name Konva.Stage#setPointersPositions
742
+ * @param {Object} evt Event object
743
+ * @example
744
+ *
745
+ * window.addEventListener('mousemove', (e) => {
746
+ * stage.setPointersPositions(e);
747
+ * });
748
+ */
631
749
  setPointersPositions(evt) {
632
750
  const contentPosition = this._getContentPosition();
633
751
  let x = null, y = null;
634
752
  evt = evt ? evt : window.event;
753
+ // touch events
635
754
  if (evt.touches !== undefined) {
755
+ // touchlist has not support for map method
756
+ // so we have to iterate
636
757
  this._pointerPositions = [];
637
758
  this._changedPointerPositions = [];
638
759
  Array.prototype.forEach.call(evt.touches, (touch) => {
@@ -651,6 +772,7 @@ export class Stage extends Container {
651
772
  });
652
773
  }
653
774
  else {
775
+ // mouse events
654
776
  x = (evt.clientX - contentPosition.left) / contentPosition.scaleX;
655
777
  y = (evt.clientY - contentPosition.top) / contentPosition.scaleY;
656
778
  this.pointerPos = {
@@ -680,11 +802,15 @@ export class Stage extends Container {
680
802
  return {
681
803
  top: rect.top,
682
804
  left: rect.left,
805
+ // sometimes clientWidth can be equals to 0
806
+ // i saw it in react-konva test, looks like it is because of hidden testing element
683
807
  scaleX: rect.width / this.content.clientWidth || 1,
684
808
  scaleY: rect.height / this.content.clientHeight || 1,
685
809
  };
686
810
  }
687
811
  _buildDOM() {
812
+ // lazy buffer canvases, see _syncBufferSize
813
+ // https://github.com/konvajs/konva/issues/2009
688
814
  this.bufferCanvas = new SceneCanvas({
689
815
  width: 0,
690
816
  height: 0,
@@ -701,7 +827,11 @@ export class Stage extends Container {
701
827
  if (!container) {
702
828
  throw 'Stage has no container. A container is required.';
703
829
  }
830
+ // clear content inside container
704
831
  container.innerHTML = '';
832
+ // content
833
+ // the container may belong to another document - an iframe, or a popout
834
+ // window - so the content is created in the document of the container
705
835
  this.content = container.ownerDocument.createElement('div');
706
836
  this.content.style.position = 'relative';
707
837
  this.content.style.userSelect = 'none';
@@ -711,6 +841,7 @@ export class Stage extends Container {
711
841
  listenToWindow(this._getOwnerWindow());
712
842
  this._resizeDOM();
713
843
  }
844
+ // currently cache function is now working for stage, because stage has no its own canvas element
714
845
  cache() {
715
846
  Util.warn('Cache function is not allowed for stage. You may use cache only for layers, groups and shapes.');
716
847
  return this;
@@ -718,8 +849,17 @@ export class Stage extends Container {
718
849
  clearCache() {
719
850
  return this;
720
851
  }
852
+ /**
853
+ * batch draw
854
+ * @method
855
+ * @name Konva.Stage#batchDraw
856
+ * @return {Konva.Stage} this
857
+ */
721
858
  batchDraw() {
722
- this.getChildren().forEach(function (layer) {
859
+ var _a;
860
+ // the config applied by the Node constructor already requests a draw,
861
+ // before the children array exists
862
+ (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (layer) {
723
863
  layer.batchDraw();
724
864
  });
725
865
  return this;
@@ -727,4 +867,23 @@ export class Stage extends Container {
727
867
  }
728
868
  Stage.prototype.nodeType = STAGE;
729
869
  _registerNode(Stage);
870
+ /**
871
+ * get/set container DOM element. The container may belong to another window
872
+ * than the one Konva was imported into: an iframe, or a window opened with
873
+ * `window.open`. The stage then uses the events and the frames of that window.
874
+ * @method
875
+ * @name Konva.Stage#container
876
+ * @returns {DomElement} container
877
+ * @example
878
+ * // get container
879
+ * var container = stage.container();
880
+ * // set container
881
+ * var container = document.createElement('div');
882
+ * body.appendChild(container);
883
+ * stage.container(container);
884
+ *
885
+ * // a stage in a popout window
886
+ * var popout = window.open('', '', 'width=600,height=400');
887
+ * stage.container(popout.document.body);
888
+ */
730
889
  Factory.addGetterSetter(Stage, 'container');