konva 10.0.10 → 10.0.11

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 (127) hide show
  1. package/konva.js +2 -2
  2. package/konva.min.js +2 -2
  3. package/lib/Animation.d.ts +24 -0
  4. package/lib/Animation.js +142 -0
  5. package/lib/BezierFunctions.d.ts +6 -0
  6. package/lib/BezierFunctions.js +784 -0
  7. package/lib/Canvas.d.ts +33 -0
  8. package/lib/Canvas.js +107 -0
  9. package/lib/Container.d.ts +54 -0
  10. package/lib/Container.js +316 -0
  11. package/lib/Context.d.ts +95 -0
  12. package/lib/Context.js +561 -0
  13. package/lib/Core.d.ts +3 -0
  14. package/lib/Core.js +3 -0
  15. package/lib/DragAndDrop.d.ts +17 -0
  16. package/lib/DragAndDrop.js +110 -0
  17. package/lib/Factory.d.ts +22 -0
  18. package/lib/Factory.js +123 -0
  19. package/lib/FastLayer.d.ts +4 -0
  20. package/lib/FastLayer.js +12 -0
  21. package/lib/Global.d.ts +37 -0
  22. package/lib/Global.js +66 -0
  23. package/lib/Group.d.ts +9 -0
  24. package/lib/Group.js +13 -0
  25. package/lib/Layer.d.ts +62 -0
  26. package/lib/Layer.js +305 -0
  27. package/lib/Node.d.ts +323 -0
  28. package/lib/Node.js +1532 -0
  29. package/lib/PointerEvents.d.ts +11 -0
  30. package/lib/PointerEvents.js +37 -0
  31. package/lib/Shape.d.ts +196 -0
  32. package/lib/Shape.js +555 -0
  33. package/lib/Stage.d.ts +86 -0
  34. package/lib/Stage.js +715 -0
  35. package/lib/Tween.d.ts +90 -0
  36. package/lib/Tween.js +531 -0
  37. package/lib/Util.d.ts +137 -0
  38. package/lib/Util.js +909 -0
  39. package/lib/Validators.d.ts +11 -0
  40. package/lib/Validators.js +178 -0
  41. package/lib/_CoreInternals.d.ts +224 -0
  42. package/lib/_CoreInternals.js +34 -0
  43. package/lib/_FullInternals.d.ts +306 -0
  44. package/lib/_FullInternals.js +80 -0
  45. package/lib/canvas-backend.d.ts +2 -0
  46. package/lib/canvas-backend.js +27 -0
  47. package/lib/filters/Blur.d.ts +2 -0
  48. package/lib/filters/Blur.js +235 -0
  49. package/lib/filters/Brighten.d.ts +2 -0
  50. package/lib/filters/Brighten.js +12 -0
  51. package/lib/filters/Brightness.d.ts +2 -0
  52. package/lib/filters/Brightness.js +8 -0
  53. package/lib/filters/Contrast.d.ts +2 -0
  54. package/lib/filters/Contrast.js +35 -0
  55. package/lib/filters/Emboss.d.ts +2 -0
  56. package/lib/filters/Emboss.js +101 -0
  57. package/lib/filters/Enhance.d.ts +2 -0
  58. package/lib/filters/Enhance.js +84 -0
  59. package/lib/filters/Grayscale.d.ts +2 -0
  60. package/lib/filters/Grayscale.js +9 -0
  61. package/lib/filters/HSL.d.ts +2 -0
  62. package/lib/filters/HSL.js +24 -0
  63. package/lib/filters/HSV.d.ts +2 -0
  64. package/lib/filters/HSV.js +23 -0
  65. package/lib/filters/Invert.d.ts +2 -0
  66. package/lib/filters/Invert.js +8 -0
  67. package/lib/filters/Kaleidoscope.d.ts +2 -0
  68. package/lib/filters/Kaleidoscope.js +133 -0
  69. package/lib/filters/Mask.d.ts +2 -0
  70. package/lib/filters/Mask.js +151 -0
  71. package/lib/filters/Noise.d.ts +2 -0
  72. package/lib/filters/Noise.js +12 -0
  73. package/lib/filters/Pixelate.d.ts +2 -0
  74. package/lib/filters/Pixelate.js +60 -0
  75. package/lib/filters/Posterize.d.ts +2 -0
  76. package/lib/filters/Posterize.js +10 -0
  77. package/lib/filters/RGB.d.ts +2 -0
  78. package/lib/filters/RGB.js +38 -0
  79. package/lib/filters/RGBA.d.ts +2 -0
  80. package/lib/filters/RGBA.js +49 -0
  81. package/lib/filters/Sepia.d.ts +2 -0
  82. package/lib/filters/Sepia.js +11 -0
  83. package/lib/filters/Solarize.d.ts +2 -0
  84. package/lib/filters/Solarize.js +14 -0
  85. package/lib/filters/Threshold.d.ts +2 -0
  86. package/lib/filters/Threshold.js +10 -0
  87. package/lib/index.d.ts +2 -0
  88. package/lib/index.js +2 -0
  89. package/lib/shapes/Arc.d.ts +27 -0
  90. package/lib/shapes/Arc.js +60 -0
  91. package/lib/shapes/Arrow.d.ts +27 -0
  92. package/lib/shapes/Arrow.js +99 -0
  93. package/lib/shapes/Circle.d.ts +15 -0
  94. package/lib/shapes/Circle.js +33 -0
  95. package/lib/shapes/Ellipse.d.ts +18 -0
  96. package/lib/shapes/Ellipse.js +37 -0
  97. package/lib/shapes/Image.d.ts +29 -0
  98. package/lib/shapes/Image.js +131 -0
  99. package/lib/shapes/Label.d.ts +39 -0
  100. package/lib/shapes/Label.js +173 -0
  101. package/lib/shapes/Line.d.ts +29 -0
  102. package/lib/shapes/Line.js +156 -0
  103. package/lib/shapes/Path.d.ts +50 -0
  104. package/lib/shapes/Path.js +670 -0
  105. package/lib/shapes/Rect.d.ts +11 -0
  106. package/lib/shapes/Rect.js +22 -0
  107. package/lib/shapes/RegularPolygon.d.ts +26 -0
  108. package/lib/shapes/RegularPolygon.js +72 -0
  109. package/lib/shapes/Ring.d.ts +17 -0
  110. package/lib/shapes/Ring.js +33 -0
  111. package/lib/shapes/Sprite.d.ts +32 -0
  112. package/lib/shapes/Sprite.js +108 -0
  113. package/lib/shapes/Star.d.ts +19 -0
  114. package/lib/shapes/Star.js +38 -0
  115. package/lib/shapes/Text.d.ts +90 -0
  116. package/lib/shapes/Text.js +461 -0
  117. package/lib/shapes/TextPath.d.ts +72 -0
  118. package/lib/shapes/TextPath.js +268 -0
  119. package/lib/shapes/Transformer.d.ts +130 -0
  120. package/lib/shapes/Transformer.js +1009 -0
  121. package/lib/shapes/Wedge.d.ts +19 -0
  122. package/lib/shapes/Wedge.js +38 -0
  123. package/lib/skia-backend.d.ts +2 -0
  124. package/lib/skia-backend.js +24 -0
  125. package/lib/types.d.ts +56 -0
  126. package/lib/types.js +21 -0
  127. package/package.json +1 -1
package/lib/Stage.js ADDED
@@ -0,0 +1,715 @@
1
+ import { Util } from "./Util.js";
2
+ import { Factory } from "./Factory.js";
3
+ import { Container } from "./Container.js";
4
+ import { Konva } from "./Global.js";
5
+ import { SceneCanvas, HitCanvas } from "./Canvas.js";
6
+ import { DD } from "./DragAndDrop.js";
7
+ import { _registerNode } from "./Global.js";
8
+ import * as PointerEvents from "./PointerEvents.js";
9
+ 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
+ [MOUSEENTER, '_pointerenter'],
11
+ [MOUSEDOWN, '_pointerdown'],
12
+ [MOUSEMOVE, '_pointermove'],
13
+ [MOUSEUP, '_pointerup'],
14
+ [MOUSELEAVE, '_pointerleave'],
15
+ [TOUCHSTART, '_pointerdown'],
16
+ [TOUCHMOVE, '_pointermove'],
17
+ [TOUCHEND, '_pointerup'],
18
+ [TOUCHCANCEL, '_pointercancel'],
19
+ [MOUSEOVER, '_pointerover'],
20
+ [WHEEL, '_wheel'],
21
+ [CONTEXTMENU, '_contextmenu'],
22
+ [POINTERDOWN, '_pointerdown'],
23
+ [POINTERMOVE, '_pointermove'],
24
+ [POINTERUP, '_pointerup'],
25
+ [POINTERCANCEL, '_pointercancel'],
26
+ [POINTERLEAVE, '_pointerleave'],
27
+ [LOSTPOINTERCAPTURE, '_lostpointercapture'],
28
+ ];
29
+ const EVENTS_MAP = {
30
+ mouse: {
31
+ [POINTEROUT]: MOUSEOUT,
32
+ [POINTERLEAVE]: MOUSELEAVE,
33
+ [POINTEROVER]: MOUSEOVER,
34
+ [POINTERENTER]: MOUSEENTER,
35
+ [POINTERMOVE]: MOUSEMOVE,
36
+ [POINTERDOWN]: MOUSEDOWN,
37
+ [POINTERUP]: MOUSEUP,
38
+ [POINTERCANCEL]: 'mousecancel',
39
+ pointerclick: 'click',
40
+ pointerdblclick: 'dblclick',
41
+ },
42
+ touch: {
43
+ [POINTEROUT]: 'touchout',
44
+ [POINTERLEAVE]: 'touchleave',
45
+ [POINTEROVER]: 'touchover',
46
+ [POINTERENTER]: 'touchenter',
47
+ [POINTERMOVE]: TOUCHMOVE,
48
+ [POINTERDOWN]: TOUCHSTART,
49
+ [POINTERUP]: TOUCHEND,
50
+ [POINTERCANCEL]: TOUCHCANCEL,
51
+ pointerclick: 'tap',
52
+ pointerdblclick: 'dbltap',
53
+ },
54
+ pointer: {
55
+ [POINTEROUT]: POINTEROUT,
56
+ [POINTERLEAVE]: POINTERLEAVE,
57
+ [POINTEROVER]: POINTEROVER,
58
+ [POINTERENTER]: POINTERENTER,
59
+ [POINTERMOVE]: POINTERMOVE,
60
+ [POINTERDOWN]: POINTERDOWN,
61
+ [POINTERUP]: POINTERUP,
62
+ [POINTERCANCEL]: POINTERCANCEL,
63
+ pointerclick: 'pointerclick',
64
+ pointerdblclick: 'pointerdblclick',
65
+ },
66
+ };
67
+ const getEventType = (type) => {
68
+ if (type.indexOf('pointer') >= 0) {
69
+ return 'pointer';
70
+ }
71
+ if (type.indexOf('touch') >= 0) {
72
+ return 'touch';
73
+ }
74
+ return 'mouse';
75
+ };
76
+ const getEventsMap = (eventType) => {
77
+ const type = getEventType(eventType);
78
+ if (type === 'pointer') {
79
+ return Konva.pointerEventsEnabled && EVENTS_MAP.pointer;
80
+ }
81
+ if (type === 'touch') {
82
+ return EVENTS_MAP.touch;
83
+ }
84
+ if (type === 'mouse') {
85
+ return EVENTS_MAP.mouse;
86
+ }
87
+ };
88
+ function checkNoClip(attrs = {}) {
89
+ if (attrs.clipFunc || attrs.clipWidth || attrs.clipHeight) {
90
+ Util.warn('Stage does not support clipping. Please use clip for Layers or Groups.');
91
+ }
92
+ return attrs;
93
+ }
94
+ 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
+ export const stages = [];
96
+ export class Stage extends Container {
97
+ constructor(config) {
98
+ super(checkNoClip(config));
99
+ this._pointerPositions = [];
100
+ this._changedPointerPositions = [];
101
+ this._buildDOM();
102
+ this._bindContentEvents();
103
+ stages.push(this);
104
+ this.on('widthChange.konva heightChange.konva', this._resizeDOM);
105
+ this.on('visibleChange.konva', this._checkVisibility);
106
+ this.on('clipWidthChange.konva clipHeightChange.konva clipFuncChange.konva', () => {
107
+ checkNoClip(this.attrs);
108
+ });
109
+ this._checkVisibility();
110
+ }
111
+ _validateAdd(child) {
112
+ const isLayer = child.getType() === 'Layer';
113
+ const isFastLayer = child.getType() === 'FastLayer';
114
+ const valid = isLayer || isFastLayer;
115
+ if (!valid) {
116
+ Util.throw('You may only add layers to the stage.');
117
+ }
118
+ }
119
+ _checkVisibility() {
120
+ if (!this.content) {
121
+ return;
122
+ }
123
+ const style = this.visible() ? '' : 'none';
124
+ this.content.style.display = style;
125
+ }
126
+ setContainer(container) {
127
+ if (typeof container === STRING) {
128
+ let id;
129
+ if (container.charAt(0) === '.') {
130
+ const className = container.slice(1);
131
+ container = document.getElementsByClassName(className)[0];
132
+ }
133
+ else {
134
+ if (container.charAt(0) !== '#') {
135
+ id = container;
136
+ }
137
+ else {
138
+ id = container.slice(1);
139
+ }
140
+ container = document.getElementById(id);
141
+ }
142
+ if (!container) {
143
+ throw 'Can not find container in document with id ' + id;
144
+ }
145
+ }
146
+ this._setAttr('container', container);
147
+ if (this.content) {
148
+ if (this.content.parentElement) {
149
+ this.content.parentElement.removeChild(this.content);
150
+ }
151
+ container.appendChild(this.content);
152
+ }
153
+ return this;
154
+ }
155
+ shouldDrawHit() {
156
+ return true;
157
+ }
158
+ clear() {
159
+ const layers = this.children, len = layers.length;
160
+ for (let n = 0; n < len; n++) {
161
+ layers[n].clear();
162
+ }
163
+ return this;
164
+ }
165
+ clone(obj) {
166
+ if (!obj) {
167
+ obj = {};
168
+ }
169
+ obj.container =
170
+ typeof document !== 'undefined' && document.createElement('div');
171
+ return Container.prototype.clone.call(this, obj);
172
+ }
173
+ destroy() {
174
+ super.destroy();
175
+ const content = this.content;
176
+ if (content && Util._isInDocument(content)) {
177
+ this.container().removeChild(content);
178
+ }
179
+ const index = stages.indexOf(this);
180
+ if (index > -1) {
181
+ stages.splice(index, 1);
182
+ }
183
+ Util.releaseCanvas(this.bufferCanvas._canvas, this.bufferHitCanvas._canvas);
184
+ return this;
185
+ }
186
+ getPointerPosition() {
187
+ const pos = this._pointerPositions[0] || this._changedPointerPositions[0];
188
+ if (!pos) {
189
+ Util.warn(NO_POINTERS_MESSAGE);
190
+ return null;
191
+ }
192
+ return {
193
+ x: pos.x,
194
+ y: pos.y,
195
+ };
196
+ }
197
+ _getPointerById(id) {
198
+ return this._pointerPositions.find((p) => p.id === id);
199
+ }
200
+ getPointersPositions() {
201
+ return this._pointerPositions;
202
+ }
203
+ getStage() {
204
+ return this;
205
+ }
206
+ getContent() {
207
+ return this.content;
208
+ }
209
+ _toKonvaCanvas(config) {
210
+ config = { ...config };
211
+ config.x = config.x || 0;
212
+ config.y = config.y || 0;
213
+ config.width = config.width || this.width();
214
+ config.height = config.height || this.height();
215
+ const canvas = new SceneCanvas({
216
+ width: config.width,
217
+ height: config.height,
218
+ pixelRatio: config.pixelRatio || 1,
219
+ });
220
+ const _context = canvas.getContext()._context;
221
+ const layers = this.children;
222
+ if (config.x || config.y) {
223
+ _context.translate(-1 * config.x, -1 * config.y);
224
+ }
225
+ layers.forEach(function (layer) {
226
+ if (!layer.isVisible()) {
227
+ return;
228
+ }
229
+ const layerCanvas = layer._toKonvaCanvas(config);
230
+ _context.drawImage(layerCanvas._canvas, config.x, config.y, layerCanvas.getWidth() / layerCanvas.getPixelRatio(), layerCanvas.getHeight() / layerCanvas.getPixelRatio());
231
+ });
232
+ return canvas;
233
+ }
234
+ getIntersection(pos) {
235
+ if (!pos) {
236
+ return null;
237
+ }
238
+ const layers = this.children, len = layers.length, end = len - 1;
239
+ for (let n = end; n >= 0; n--) {
240
+ const shape = layers[n].getIntersection(pos);
241
+ if (shape) {
242
+ return shape;
243
+ }
244
+ }
245
+ return null;
246
+ }
247
+ _resizeDOM() {
248
+ const width = this.width();
249
+ const height = this.height();
250
+ if (this.content) {
251
+ this.content.style.width = width + PX;
252
+ this.content.style.height = height + PX;
253
+ }
254
+ this.bufferCanvas.setSize(width, height);
255
+ this.bufferHitCanvas.setSize(width, height);
256
+ this.children.forEach((layer) => {
257
+ layer.setSize({ width, height });
258
+ layer.draw();
259
+ });
260
+ }
261
+ add(layer, ...rest) {
262
+ if (arguments.length > 1) {
263
+ for (let i = 0; i < arguments.length; i++) {
264
+ this.add(arguments[i]);
265
+ }
266
+ return this;
267
+ }
268
+ super.add(layer);
269
+ const length = this.children.length;
270
+ if (length > MAX_LAYERS_NUMBER) {
271
+ Util.warn('The stage has ' +
272
+ length +
273
+ ' 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.');
274
+ }
275
+ layer.setSize({ width: this.width(), height: this.height() });
276
+ layer.draw();
277
+ if (Konva.isBrowser) {
278
+ this.content.appendChild(layer.canvas._canvas);
279
+ }
280
+ return this;
281
+ }
282
+ getParent() {
283
+ return null;
284
+ }
285
+ getLayer() {
286
+ return null;
287
+ }
288
+ hasPointerCapture(pointerId) {
289
+ return PointerEvents.hasPointerCapture(pointerId, this);
290
+ }
291
+ setPointerCapture(pointerId) {
292
+ PointerEvents.setPointerCapture(pointerId, this);
293
+ }
294
+ releaseCapture(pointerId) {
295
+ PointerEvents.releaseCapture(pointerId, this);
296
+ }
297
+ getLayers() {
298
+ return this.children;
299
+ }
300
+ _bindContentEvents() {
301
+ if (!Konva.isBrowser) {
302
+ return;
303
+ }
304
+ EVENTS.forEach(([event, methodName]) => {
305
+ this.content.addEventListener(event, (evt) => {
306
+ this[methodName](evt);
307
+ }, { passive: false });
308
+ });
309
+ }
310
+ _pointerenter(evt) {
311
+ this.setPointersPositions(evt);
312
+ const events = getEventsMap(evt.type);
313
+ if (events) {
314
+ this._fire(events.pointerenter, {
315
+ evt: evt,
316
+ target: this,
317
+ currentTarget: this,
318
+ });
319
+ }
320
+ }
321
+ _pointerover(evt) {
322
+ this.setPointersPositions(evt);
323
+ const events = getEventsMap(evt.type);
324
+ if (events) {
325
+ this._fire(events.pointerover, {
326
+ evt: evt,
327
+ target: this,
328
+ currentTarget: this,
329
+ });
330
+ }
331
+ }
332
+ _getTargetShape(evenType) {
333
+ let shape = this[evenType + 'targetShape'];
334
+ if (shape && !shape.getStage()) {
335
+ shape = null;
336
+ }
337
+ return shape;
338
+ }
339
+ _pointerleave(evt) {
340
+ const events = getEventsMap(evt.type);
341
+ const eventType = getEventType(evt.type);
342
+ if (!events) {
343
+ return;
344
+ }
345
+ this.setPointersPositions(evt);
346
+ const targetShape = this._getTargetShape(eventType);
347
+ const eventsEnabled = !(Konva.isDragging() || Konva.isTransforming()) || Konva.hitOnDragEnabled;
348
+ if (targetShape && eventsEnabled) {
349
+ targetShape._fireAndBubble(events.pointerout, { evt: evt });
350
+ targetShape._fireAndBubble(events.pointerleave, { evt: evt });
351
+ this._fire(events.pointerleave, {
352
+ evt: evt,
353
+ target: this,
354
+ currentTarget: this,
355
+ });
356
+ this[eventType + 'targetShape'] = null;
357
+ }
358
+ else if (eventsEnabled) {
359
+ this._fire(events.pointerleave, {
360
+ evt: evt,
361
+ target: this,
362
+ currentTarget: this,
363
+ });
364
+ this._fire(events.pointerout, {
365
+ evt: evt,
366
+ target: this,
367
+ currentTarget: this,
368
+ });
369
+ }
370
+ this.pointerPos = null;
371
+ this._pointerPositions = [];
372
+ }
373
+ _pointerdown(evt) {
374
+ const events = getEventsMap(evt.type);
375
+ const eventType = getEventType(evt.type);
376
+ if (!events) {
377
+ return;
378
+ }
379
+ this.setPointersPositions(evt);
380
+ let triggeredOnShape = false;
381
+ this._changedPointerPositions.forEach((pos) => {
382
+ const shape = this.getIntersection(pos);
383
+ DD.justDragged = false;
384
+ Konva['_' + eventType + 'ListenClick'] = true;
385
+ if (!shape || !shape.isListening()) {
386
+ this[eventType + 'ClickStartShape'] = undefined;
387
+ return;
388
+ }
389
+ if (Konva.capturePointerEventsEnabled) {
390
+ shape.setPointerCapture(pos.id);
391
+ }
392
+ this[eventType + 'ClickStartShape'] = shape;
393
+ shape._fireAndBubble(events.pointerdown, {
394
+ evt: evt,
395
+ pointerId: pos.id,
396
+ });
397
+ triggeredOnShape = true;
398
+ const isTouch = evt.type.indexOf('touch') >= 0;
399
+ if (shape.preventDefault() && evt.cancelable && isTouch) {
400
+ evt.preventDefault();
401
+ }
402
+ });
403
+ if (!triggeredOnShape) {
404
+ this._fire(events.pointerdown, {
405
+ evt: evt,
406
+ target: this,
407
+ currentTarget: this,
408
+ pointerId: this._pointerPositions[0].id,
409
+ });
410
+ }
411
+ }
412
+ _pointermove(evt) {
413
+ const events = getEventsMap(evt.type);
414
+ const eventType = getEventType(evt.type);
415
+ if (!events) {
416
+ return;
417
+ }
418
+ const isTouchPointer = evt.type.indexOf('touch') >= 0 ||
419
+ evt.pointerType === 'touch';
420
+ if (Konva.isDragging() &&
421
+ DD.node.preventDefault() &&
422
+ evt.cancelable &&
423
+ isTouchPointer) {
424
+ evt.preventDefault();
425
+ }
426
+ this.setPointersPositions(evt);
427
+ const eventsEnabled = !(Konva.isDragging() || Konva.isTransforming()) || Konva.hitOnDragEnabled;
428
+ if (!eventsEnabled) {
429
+ return;
430
+ }
431
+ const processedShapesIds = {};
432
+ let triggeredOnShape = false;
433
+ const targetShape = this._getTargetShape(eventType);
434
+ this._changedPointerPositions.forEach((pos) => {
435
+ const shape = (PointerEvents.getCapturedShape(pos.id) ||
436
+ this.getIntersection(pos));
437
+ const pointerId = pos.id;
438
+ const event = { evt: evt, pointerId };
439
+ const differentTarget = targetShape !== shape;
440
+ if (differentTarget && targetShape) {
441
+ targetShape._fireAndBubble(events.pointerout, { ...event }, shape);
442
+ targetShape._fireAndBubble(events.pointerleave, { ...event }, shape);
443
+ }
444
+ if (shape) {
445
+ if (processedShapesIds[shape._id]) {
446
+ return;
447
+ }
448
+ processedShapesIds[shape._id] = true;
449
+ }
450
+ if (shape && shape.isListening()) {
451
+ triggeredOnShape = true;
452
+ if (differentTarget) {
453
+ shape._fireAndBubble(events.pointerover, { ...event }, targetShape);
454
+ shape._fireAndBubble(events.pointerenter, { ...event }, targetShape);
455
+ this[eventType + 'targetShape'] = shape;
456
+ }
457
+ shape._fireAndBubble(events.pointermove, { ...event });
458
+ }
459
+ else {
460
+ if (targetShape) {
461
+ this._fire(events.pointerover, {
462
+ evt: evt,
463
+ target: this,
464
+ currentTarget: this,
465
+ pointerId,
466
+ });
467
+ this[eventType + 'targetShape'] = null;
468
+ }
469
+ }
470
+ });
471
+ if (!triggeredOnShape) {
472
+ this._fire(events.pointermove, {
473
+ evt: evt,
474
+ target: this,
475
+ currentTarget: this,
476
+ pointerId: this._changedPointerPositions[0].id,
477
+ });
478
+ }
479
+ }
480
+ _pointerup(evt) {
481
+ const events = getEventsMap(evt.type);
482
+ const eventType = getEventType(evt.type);
483
+ if (!events) {
484
+ return;
485
+ }
486
+ this.setPointersPositions(evt);
487
+ const clickStartShape = this[eventType + 'ClickStartShape'];
488
+ const clickEndShape = this[eventType + 'ClickEndShape'];
489
+ const processedShapesIds = {};
490
+ let skipPointerUpTrigger = false;
491
+ this._changedPointerPositions.forEach((pos) => {
492
+ const shape = (PointerEvents.getCapturedShape(pos.id) ||
493
+ this.getIntersection(pos));
494
+ if (shape) {
495
+ shape.releaseCapture(pos.id);
496
+ if (processedShapesIds[shape._id]) {
497
+ return;
498
+ }
499
+ processedShapesIds[shape._id] = true;
500
+ }
501
+ const pointerId = pos.id;
502
+ const event = { evt: evt, pointerId };
503
+ let fireDblClick = false;
504
+ if (Konva['_' + eventType + 'InDblClickWindow']) {
505
+ fireDblClick = true;
506
+ clearTimeout(this[eventType + 'DblTimeout']);
507
+ }
508
+ else if (!DD.justDragged) {
509
+ Konva['_' + eventType + 'InDblClickWindow'] = true;
510
+ clearTimeout(this[eventType + 'DblTimeout']);
511
+ }
512
+ this[eventType + 'DblTimeout'] = setTimeout(function () {
513
+ Konva['_' + eventType + 'InDblClickWindow'] = false;
514
+ }, Konva.dblClickWindow);
515
+ if (shape && shape.isListening()) {
516
+ skipPointerUpTrigger = true;
517
+ this[eventType + 'ClickEndShape'] = shape;
518
+ shape._fireAndBubble(events.pointerup, { ...event });
519
+ if (Konva['_' + eventType + 'ListenClick'] &&
520
+ clickStartShape &&
521
+ clickStartShape === shape) {
522
+ shape._fireAndBubble(events.pointerclick, { ...event });
523
+ if (fireDblClick && clickEndShape && clickEndShape === shape) {
524
+ shape._fireAndBubble(events.pointerdblclick, { ...event });
525
+ }
526
+ }
527
+ }
528
+ else {
529
+ this[eventType + 'ClickEndShape'] = null;
530
+ if (!skipPointerUpTrigger) {
531
+ this._fire(events.pointerup, {
532
+ evt: evt,
533
+ target: this,
534
+ currentTarget: this,
535
+ pointerId: this._changedPointerPositions[0].id,
536
+ });
537
+ skipPointerUpTrigger = true;
538
+ }
539
+ if (Konva['_' + eventType + 'ListenClick']) {
540
+ this._fire(events.pointerclick, {
541
+ evt: evt,
542
+ target: this,
543
+ currentTarget: this,
544
+ pointerId,
545
+ });
546
+ }
547
+ if (fireDblClick) {
548
+ this._fire(events.pointerdblclick, {
549
+ evt: evt,
550
+ target: this,
551
+ currentTarget: this,
552
+ pointerId,
553
+ });
554
+ }
555
+ }
556
+ });
557
+ if (!skipPointerUpTrigger) {
558
+ this._fire(events.pointerup, {
559
+ evt: evt,
560
+ target: this,
561
+ currentTarget: this,
562
+ pointerId: this._changedPointerPositions[0].id,
563
+ });
564
+ }
565
+ Konva['_' + eventType + 'ListenClick'] = false;
566
+ if (evt.cancelable && eventType !== 'touch' && eventType !== 'pointer') {
567
+ evt.preventDefault();
568
+ }
569
+ }
570
+ _contextmenu(evt) {
571
+ this.setPointersPositions(evt);
572
+ const shape = this.getIntersection(this.getPointerPosition());
573
+ if (shape && shape.isListening()) {
574
+ shape._fireAndBubble(CONTEXTMENU, { evt: evt });
575
+ }
576
+ else {
577
+ this._fire(CONTEXTMENU, {
578
+ evt: evt,
579
+ target: this,
580
+ currentTarget: this,
581
+ });
582
+ }
583
+ }
584
+ _wheel(evt) {
585
+ this.setPointersPositions(evt);
586
+ const shape = this.getIntersection(this.getPointerPosition());
587
+ if (shape && shape.isListening()) {
588
+ shape._fireAndBubble(WHEEL, { evt: evt });
589
+ }
590
+ else {
591
+ this._fire(WHEEL, {
592
+ evt: evt,
593
+ target: this,
594
+ currentTarget: this,
595
+ });
596
+ }
597
+ }
598
+ _pointercancel(evt) {
599
+ this.setPointersPositions(evt);
600
+ const shape = PointerEvents.getCapturedShape(evt.pointerId) ||
601
+ this.getIntersection(this.getPointerPosition());
602
+ if (shape) {
603
+ shape._fireAndBubble(POINTERUP, PointerEvents.createEvent(evt));
604
+ }
605
+ PointerEvents.releaseCapture(evt.pointerId);
606
+ }
607
+ _lostpointercapture(evt) {
608
+ PointerEvents.releaseCapture(evt.pointerId);
609
+ }
610
+ setPointersPositions(evt) {
611
+ const contentPosition = this._getContentPosition();
612
+ let x = null, y = null;
613
+ evt = evt ? evt : window.event;
614
+ if (evt.touches !== undefined) {
615
+ this._pointerPositions = [];
616
+ this._changedPointerPositions = [];
617
+ Array.prototype.forEach.call(evt.touches, (touch) => {
618
+ this._pointerPositions.push({
619
+ id: touch.identifier,
620
+ x: (touch.clientX - contentPosition.left) / contentPosition.scaleX,
621
+ y: (touch.clientY - contentPosition.top) / contentPosition.scaleY,
622
+ });
623
+ });
624
+ Array.prototype.forEach.call(evt.changedTouches || evt.touches, (touch) => {
625
+ this._changedPointerPositions.push({
626
+ id: touch.identifier,
627
+ x: (touch.clientX - contentPosition.left) / contentPosition.scaleX,
628
+ y: (touch.clientY - contentPosition.top) / contentPosition.scaleY,
629
+ });
630
+ });
631
+ }
632
+ else {
633
+ x = (evt.clientX - contentPosition.left) / contentPosition.scaleX;
634
+ y = (evt.clientY - contentPosition.top) / contentPosition.scaleY;
635
+ this.pointerPos = {
636
+ x: x,
637
+ y: y,
638
+ };
639
+ this._pointerPositions = [{ x, y, id: Util._getFirstPointerId(evt) }];
640
+ this._changedPointerPositions = [
641
+ { x, y, id: Util._getFirstPointerId(evt) },
642
+ ];
643
+ }
644
+ }
645
+ _setPointerPosition(evt) {
646
+ Util.warn('Method _setPointerPosition is deprecated. Use "stage.setPointersPositions(event)" instead.');
647
+ this.setPointersPositions(evt);
648
+ }
649
+ _getContentPosition() {
650
+ if (!this.content || !this.content.getBoundingClientRect) {
651
+ return {
652
+ top: 0,
653
+ left: 0,
654
+ scaleX: 1,
655
+ scaleY: 1,
656
+ };
657
+ }
658
+ const rect = this.content.getBoundingClientRect();
659
+ return {
660
+ top: rect.top,
661
+ left: rect.left,
662
+ scaleX: rect.width / this.content.clientWidth || 1,
663
+ scaleY: rect.height / this.content.clientHeight || 1,
664
+ };
665
+ }
666
+ _buildDOM() {
667
+ this.bufferCanvas = new SceneCanvas({
668
+ width: this.width(),
669
+ height: this.height(),
670
+ });
671
+ this.bufferHitCanvas = new HitCanvas({
672
+ pixelRatio: 1,
673
+ width: this.width(),
674
+ height: this.height(),
675
+ });
676
+ if (!Konva.isBrowser) {
677
+ return;
678
+ }
679
+ const container = this.container();
680
+ if (!container) {
681
+ throw 'Stage has no container. A container is required.';
682
+ }
683
+ container.innerHTML = '';
684
+ this.content = document.createElement('div');
685
+ this.content.style.position = 'relative';
686
+ this.content.style.userSelect = 'none';
687
+ this.content.className = 'konvajs-content';
688
+ this.content.setAttribute('role', 'presentation');
689
+ container.appendChild(this.content);
690
+ this._resizeDOM();
691
+ }
692
+ cache() {
693
+ Util.warn('Cache function is not allowed for stage. You may use cache only for layers, groups and shapes.');
694
+ return this;
695
+ }
696
+ clearCache() {
697
+ return this;
698
+ }
699
+ batchDraw() {
700
+ this.getChildren().forEach(function (layer) {
701
+ layer.batchDraw();
702
+ });
703
+ return this;
704
+ }
705
+ }
706
+ Stage.prototype.nodeType = STAGE;
707
+ _registerNode(Stage);
708
+ Factory.addGetterSetter(Stage, 'container');
709
+ if (Konva.isBrowser) {
710
+ document.addEventListener('visibilitychange', () => {
711
+ stages.forEach((stage) => {
712
+ stage.batchDraw();
713
+ });
714
+ });
715
+ }