konva 10.5.0 → 10.6.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 (62) hide show
  1. package/README.md +4 -4
  2. package/konva.js +3247 -3746
  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 +1 -1
  9. package/lib/Canvas.js +8 -5
  10. package/lib/Container.js +9 -6
  11. package/lib/Context.d.ts +4 -5
  12. package/lib/Context.js +77 -72
  13. package/lib/DragAndDrop.d.ts +4 -3
  14. package/lib/DragAndDrop.js +39 -25
  15. package/lib/Factory.d.ts +3 -2
  16. package/lib/Factory.js +10 -9
  17. package/lib/Global.d.ts +0 -6
  18. package/lib/Global.js +2 -8
  19. package/lib/Layer.js +9 -6
  20. package/lib/Node.d.ts +21 -12
  21. package/lib/Node.js +159 -126
  22. package/lib/PointerEvents.d.ts +1 -2
  23. package/lib/PointerEvents.js +20 -18
  24. package/lib/Shape.d.ts +2 -2
  25. package/lib/Shape.js +51 -7
  26. package/lib/Stage.d.ts +19 -13
  27. package/lib/Stage.js +182 -86
  28. package/lib/Tween.js +59 -66
  29. package/lib/Util.d.ts +2 -2
  30. package/lib/Util.js +21 -16
  31. package/lib/_CoreInternals.d.ts +23 -11
  32. package/lib/_FullInternals.d.ts +23 -11
  33. package/lib/filters/Blur.js +16 -12
  34. package/lib/filters/Brighten.d.ts +1 -0
  35. package/lib/filters/Brighten.js +1 -0
  36. package/lib/filters/Brightness.d.ts +2 -1
  37. package/lib/filters/Brightness.js +4 -4
  38. package/lib/filters/Emboss.js +16 -58
  39. package/lib/filters/Kaleidoscope.js +8 -15
  40. package/lib/filters/Mask.js +8 -8
  41. package/lib/filters/Pixelate.js +16 -29
  42. package/lib/filters/Threshold.d.ts +1 -1
  43. package/lib/filters/Threshold.js +1 -1
  44. package/lib/shapes/Arc.js +5 -3
  45. package/lib/shapes/Arrow.d.ts +2 -6
  46. package/lib/shapes/Arrow.js +68 -46
  47. package/lib/shapes/Image.js +16 -19
  48. package/lib/shapes/Label.d.ts +2 -3
  49. package/lib/shapes/Label.js +7 -21
  50. package/lib/shapes/Line.d.ts +1 -1
  51. package/lib/shapes/Line.js +1 -1
  52. package/lib/shapes/Path.d.ts +11 -2
  53. package/lib/shapes/Path.js +132 -142
  54. package/lib/shapes/RegularPolygon.js +1 -1
  55. package/lib/shapes/Sprite.js +2 -6
  56. package/lib/shapes/Text.d.ts +12 -4
  57. package/lib/shapes/Text.js +96 -54
  58. package/lib/shapes/TextPath.d.ts +4 -3
  59. package/lib/shapes/TextPath.js +40 -26
  60. package/lib/shapes/Transformer.js +88 -91
  61. package/lib/types.d.ts +2 -2
  62. package/package.json +4 -3
package/lib/Shape.js CHANGED
@@ -306,7 +306,7 @@ export class Shape extends Node {
306
306
  }
307
307
  const hasFill = forceFill || this.hasFill();
308
308
  const hasStroke = this.hasStroke();
309
- const isTransparent = this.getAbsoluteOpacity() !== 1;
309
+ const isTransparent = !this._isUnderCache && this.getAbsoluteOpacity() !== 1;
310
310
  if (hasFill && hasStroke && isTransparent) {
311
311
  return true;
312
312
  }
@@ -361,8 +361,42 @@ export class Shape extends Node {
361
361
  const fillRect = this.getSelfRect();
362
362
  const applyStroke = !config.skipStroke && this.hasStroke();
363
363
  const strokeWidth = (applyStroke && this.strokeWidth()) || 0;
364
- const fillAndStrokeWidth = fillRect.width + strokeWidth;
365
- const fillAndStrokeHeight = fillRect.height + strokeWidth;
364
+ let strokeWidthX = strokeWidth;
365
+ let strokeWidthY = strokeWidth;
366
+ let collapsedStroke;
367
+ if (strokeWidth && !this.strokeScaleEnabled()) {
368
+ // Strokes are constant in the canvas they are drawn into. A cached
369
+ // ancestor supplies that coordinate space until its bitmap is rebuilt.
370
+ let top = this;
371
+ while (top && !top.isCached() && !top._isUnderCache)
372
+ top = top.parent;
373
+ const [a, b, c, d] = this.getAbsoluteTransform(top).getMatrix();
374
+ const determinant = Math.abs(a * d - b * c);
375
+ if (determinant) {
376
+ strokeWidthX *= Math.hypot(c, d) / determinant;
377
+ strokeWidthY *= Math.hypot(a, b) / determinant;
378
+ }
379
+ else if (!skipTransform) {
380
+ // A collapsed path can still draw a stroke after the context resets
381
+ // its transform. Expand in the destination space in this case.
382
+ strokeWidthX = strokeWidthY = 0;
383
+ collapsedStroke = top
384
+ ? top.getAbsoluteTransform().copy()
385
+ : new Transform();
386
+ if (relativeTo) {
387
+ const relativeTransform = relativeTo.getAbsoluteTransform();
388
+ const [a, b, c, d] = relativeTransform.getMatrix();
389
+ // A collapsed ancestor has no inverse. Its local bounds can only
390
+ // describe the uncollapsed stroke; world bounds are measured directly.
391
+ collapsedStroke =
392
+ a * d - b * c
393
+ ? relativeTransform.copy().invert().multiply(collapsedStroke)
394
+ : new Transform();
395
+ }
396
+ }
397
+ }
398
+ const fillAndStrokeWidth = fillRect.width + strokeWidthX;
399
+ const fillAndStrokeHeight = fillRect.height + strokeWidthY;
366
400
  const applyShadow = !config.skipShadow && this.hasShadow();
367
401
  const shadowOffsetX = applyShadow ? this.shadowOffsetX() : 0;
368
402
  const shadowOffsetY = applyShadow ? this.shadowOffsetY() : 0;
@@ -374,15 +408,25 @@ export class Shape extends Node {
374
408
  const rect = {
375
409
  width: width,
376
410
  height: height,
377
- x: -(strokeWidth / 2 + blurRadius) +
411
+ x: -(strokeWidthX / 2 + blurRadius) +
378
412
  Math.min(shadowOffsetX, 0) +
379
413
  fillRect.x,
380
- y: -(strokeWidth / 2 + blurRadius) +
414
+ y: -(strokeWidthY / 2 + blurRadius) +
381
415
  Math.min(shadowOffsetY, 0) +
382
416
  fillRect.y,
383
417
  };
384
418
  if (!skipTransform) {
385
- return this._transformedRect(rect, relativeTo);
419
+ const transformed = this._transformedRect(rect, relativeTo);
420
+ if (collapsedStroke) {
421
+ const [a, b, c, d] = collapsedStroke.getMatrix();
422
+ const x = (strokeWidth * Math.hypot(a, c)) / 2;
423
+ const y = (strokeWidth * Math.hypot(b, d)) / 2;
424
+ transformed.x -= x;
425
+ transformed.y -= y;
426
+ transformed.width += x * 2;
427
+ transformed.height += y * 2;
428
+ }
429
+ return transformed;
386
430
  }
387
431
  return rect;
388
432
  }
@@ -395,7 +439,7 @@ export class Shape extends Node {
395
439
  const canvas = can || layer.getCanvas(), context = canvas.getContext(), cachedCanvas = this._getCanvasCache(), drawFunc = this.getSceneFunc(), hasShadow = this.hasShadow();
396
440
  let stage;
397
441
  const cachingSelf = top === this;
398
- if (!this.isVisible() && !cachingSelf) {
442
+ if (!(top ? this._isVisible(top) : this.isVisible()) && !cachingSelf) {
399
443
  return this;
400
444
  }
401
445
  // if node is cached we just need to draw from cache
package/lib/Stage.d.ts CHANGED
@@ -8,6 +8,19 @@ export interface StageConfig extends ContainerConfig {
8
8
  container?: HTMLDivElement | string;
9
9
  eventBatchFunc?: (callback: () => void) => void;
10
10
  }
11
+ type Click = {
12
+ shape: Shape | null;
13
+ time: number;
14
+ };
15
+ type EventType = 'mouse' | 'touch' | 'pointer';
16
+ type PointerState = {
17
+ targetShape?: Shape;
18
+ downPosition?: Vector2d;
19
+ pointerType?: string;
20
+ relatedPointer?: PointerState;
21
+ clickStartShape?: Shape | null;
22
+ lastClick?: Click;
23
+ };
11
24
  export declare const stages: Stage[];
12
25
  /**
13
26
  * Stage constructor. A stage is used to contain multiple layers
@@ -33,20 +46,11 @@ export declare class Stage extends Container<Layer, StageConfig> {
33
46
  _changedPointerPositions: (Vector2d & {
34
47
  id: number;
35
48
  })[];
49
+ _pointerEventType?: EventType;
50
+ _pointerStates: Map<string, PointerState>;
51
+ _lastClicks: Partial<Record<EventType, Click>>;
36
52
  bufferCanvas: SceneCanvas;
37
53
  bufferHitCanvas: HitCanvas;
38
- _mouseTargetShape: Shape;
39
- _touchTargetShape: Shape;
40
- _pointerTargetShape: Shape;
41
- _mouseClickStartShape: Shape;
42
- _touchClickStartShape: Shape;
43
- _pointerClickStartShape: Shape;
44
- _mouseClickEndShape: Shape;
45
- _touchClickEndShape: Shape;
46
- _pointerClickEndShape: Shape;
47
- _mouseDblTimeout: any;
48
- _touchDblTimeout: any;
49
- _pointerDblTimeout: any;
50
54
  constructor(config: StageConfig);
51
55
  _validateAdd(child: any): void;
52
56
  _checkVisibility(): void;
@@ -125,7 +129,8 @@ export declare class Stage extends Container<Layer, StageConfig> {
125
129
  _batchEvents(callback: () => void): void;
126
130
  _pointerenter(evt: PointerEvent): void;
127
131
  _pointerover(evt: any): void;
128
- _getTargetShape(evenType: any): Shape<import("./Shape.ts").ShapeConfig> | null;
132
+ _pointerEventsEnabled(): boolean;
133
+ _cancelClick(pointerId: number | undefined, eventType?: EventType): void;
129
134
  _pointerleave(evt: any): void;
130
135
  _pointerdown(evt: TouchEvent | MouseEvent | PointerEvent): void;
131
136
  _pointermove(evt: TouchEvent | MouseEvent | PointerEvent): void;
@@ -169,3 +174,4 @@ export declare class Stage extends Container<Layer, StageConfig> {
169
174
  container: GetSet<HTMLDivElement, this, HTMLDivElement | string>;
170
175
  eventBatchFunc: GetSet<((callback: () => void) => void) | undefined, this>;
171
176
  }
177
+ export {};
package/lib/Stage.js CHANGED
@@ -65,17 +65,8 @@ const EVENTS_MAP = {
65
65
  pointerdblclick: 'pointerdblclick',
66
66
  },
67
67
  };
68
- const getEventType = (type) => {
69
- if (type.indexOf('pointer') >= 0) {
70
- return 'pointer';
71
- }
72
- if (type.indexOf('touch') >= 0) {
73
- return 'touch';
74
- }
75
- return 'mouse';
76
- };
77
68
  const getEventsMap = (eventType) => {
78
- const type = getEventType(eventType);
69
+ const type = Util._getEventType(eventType);
79
70
  if (type === 'pointer') {
80
71
  return Konva.pointerEventsEnabled && EVENTS_MAP.pointer;
81
72
  }
@@ -133,6 +124,8 @@ export class Stage extends Container {
133
124
  super(checkNoClip(config));
134
125
  this._pointerPositions = [];
135
126
  this._changedPointerPositions = [];
127
+ this._pointerStates = new Map();
128
+ this._lastClicks = {};
136
129
  this._buildDOM();
137
130
  this._bindContentEvents();
138
131
  stages.push(this);
@@ -231,17 +224,12 @@ export class Stage extends Container {
231
224
  return Container.prototype.clone.call(this, obj);
232
225
  }
233
226
  destroy() {
227
+ var _a;
234
228
  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
- });
229
+ this._pointerStates.clear();
230
+ this._lastClicks = {};
241
231
  const content = this.content;
242
- if (content && Util._isInDocument(content)) {
243
- this.container().removeChild(content);
244
- }
232
+ (_a = content === null || content === void 0 ? void 0 : content.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(content);
245
233
  const index = stages.indexOf(this);
246
234
  if (index > -1) {
247
235
  stages.splice(index, 1);
@@ -439,31 +427,57 @@ export class Stage extends Container {
439
427
  });
440
428
  }
441
429
  }
442
- _getTargetShape(evenType) {
443
- let shape = this[evenType + 'targetShape'];
444
- if (shape && !shape.getStage()) {
445
- shape = null;
430
+ _pointerEventsEnabled() {
431
+ if (Konva.hitOnDragEnabled)
432
+ return true;
433
+ for (const { node, dragStatus } of DD._dragElements.values()) {
434
+ if (dragStatus === 'dragging' && node.getStage() === this)
435
+ return false;
436
+ }
437
+ return !this.getLayers().some((layer) => { var _a; return (_a = Konva['Transformer']) === null || _a === void 0 ? void 0 : _a._isLayerTransforming(layer); });
438
+ }
439
+ _cancelClick(pointerId, eventType) {
440
+ const types = eventType ? [eventType] : ['mouse', 'touch', 'pointer'];
441
+ for (const type of types) {
442
+ const state = this._pointerStates.get(`${type}:${pointerId}`);
443
+ if (!state)
444
+ continue;
445
+ state.clickStartShape = undefined;
446
+ if (state.relatedPointer)
447
+ state.relatedPointer.clickStartShape = undefined;
446
448
  }
447
- return shape;
448
449
  }
449
450
  _pointerleave(evt) {
451
+ var _a, _b;
450
452
  const events = getEventsMap(evt.type);
451
- const eventType = getEventType(evt.type);
453
+ const eventType = Util._getEventType(evt.type);
452
454
  if (!events) {
453
455
  return;
454
456
  }
455
457
  this.setPointersPositions(evt);
456
- const targetShape = this._getTargetShape(eventType);
457
- const eventsEnabled = !(Konva.isDragging() || Konva.isTransforming()) || Konva.hitOnDragEnabled;
458
+ const pointerId = (_a = this._changedPointerPositions[0]) === null || _a === void 0 ? void 0 : _a.id;
459
+ const key = `${eventType}:${pointerId}`;
460
+ const state = this._pointerStates.get(key);
461
+ const targetShape = ((_b = state === null || state === void 0 ? void 0 : state.targetShape) === null || _b === void 0 ? void 0 : _b.getStage()) === this ? state.targetShape : undefined;
462
+ // A released touch will not return to deliver a deferred hover exit.
463
+ const eventsEnabled = this._pointerEventsEnabled() ||
464
+ (evt.pointerType === 'touch' && !(state === null || state === void 0 ? void 0 : state.downPosition));
465
+ if (eventsEnabled) {
466
+ if ((state === null || state === void 0 ? void 0 : state.clickStartShape) !== undefined) {
467
+ state.targetShape = undefined;
468
+ }
469
+ else {
470
+ this._pointerStates.delete(key);
471
+ }
472
+ }
458
473
  if (targetShape && eventsEnabled) {
459
- targetShape._fireAndBubble(events.pointerout, { evt: evt });
460
- targetShape._fireAndBubble(events.pointerleave, { evt: evt });
474
+ targetShape._fireAndBubble(events.pointerout, { evt, pointerId });
475
+ targetShape._fireAndBubble(events.pointerleave, { evt, pointerId });
461
476
  this._fire(events.pointerleave, {
462
477
  evt: evt,
463
478
  target: this,
464
479
  currentTarget: this,
465
480
  });
466
- this[eventType + 'targetShape'] = null;
467
481
  }
468
482
  else if (eventsEnabled) {
469
483
  this._fire(events.pointerleave, {
@@ -479,30 +493,79 @@ export class Stage extends Container {
479
493
  }
480
494
  this.pointerPos = null;
481
495
  this._pointerPositions = [];
496
+ this._changedPointerPositions = [];
497
+ this._pointerEventType = undefined;
482
498
  }
483
499
  _pointerdown(evt) {
484
500
  const events = getEventsMap(evt.type);
485
- const eventType = getEventType(evt.type);
501
+ const eventType = Util._getEventType(evt.type);
486
502
  if (!events) {
487
503
  return;
488
504
  }
489
505
  this.setPointersPositions(evt);
490
506
  let triggeredOnShape = false;
491
507
  this._changedPointerPositions.forEach((pos) => {
508
+ var _a;
492
509
  const shape = this.getIntersection(pos);
493
- DD.justDragged = false;
494
- // probably we are staring a click
495
- Konva['_' + eventType + 'ListenClick'] = true;
510
+ const key = `${eventType}:${pos.id}`;
511
+ const state = {
512
+ targetShape: (_a = this._pointerStates.get(key)) === null || _a === void 0 ? void 0 : _a.targetShape,
513
+ clickStartShape: shape && shape.isListening() ? shape : null,
514
+ lastClick: this._lastClicks[eventType],
515
+ downPosition: pos,
516
+ pointerType: evt.pointerType,
517
+ };
518
+ // A native pointerdown precedes its compatibility mouse/touch event.
519
+ // Keep that association for the press: IDs can differ, and mouse
520
+ // coordinates can be rounded by the browser.
521
+ this._pointerStates.delete(key);
522
+ if (eventType !== 'pointer') {
523
+ for (const [pointerKey, pointer] of [
524
+ ...this._pointerStates,
525
+ ].reverse()) {
526
+ if (!pointerKey.startsWith('pointer:') ||
527
+ !pointer.downPosition ||
528
+ (eventType === 'touch' && pointer.relatedPointer))
529
+ continue;
530
+ const matches = eventType === 'mouse'
531
+ ? pointer.pointerType !== 'touch'
532
+ : pointer.pointerType === 'touch' &&
533
+ pointer.downPosition.x === pos.x &&
534
+ pointer.downPosition.y === pos.y;
535
+ if (matches) {
536
+ state.relatedPointer = pointer;
537
+ pointer.relatedPointer = state;
538
+ if (pointer.clickStartShape === undefined)
539
+ state.clickStartShape = undefined;
540
+ break;
541
+ }
542
+ }
543
+ }
544
+ this._pointerStates.set(key, state);
545
+ for (const elem of DD._dragElements.values()) {
546
+ if (elem.dragStatus === 'dragging' &&
547
+ (elem.pointerId === undefined ||
548
+ (elem.pointerEventType === eventType &&
549
+ elem.pointerId === pos.id) ||
550
+ (elem.pointerEventType === 'mouse' &&
551
+ eventType === 'pointer' &&
552
+ state.pointerType !== 'touch')) &&
553
+ elem.node.getStage() === this) {
554
+ // Drag movement uses the compatibility mouse/touch pointer.
555
+ if (elem.pointerId === undefined && eventType !== 'pointer') {
556
+ elem.pointerId = pos.id;
557
+ elem.pointerEventType = eventType;
558
+ }
559
+ this._cancelClick(pos.id, eventType);
560
+ }
561
+ }
496
562
  // no shape detected? do nothing
497
563
  if (!shape || !shape.isListening()) {
498
- this[eventType + 'ClickStartShape'] = undefined;
499
564
  return;
500
565
  }
501
566
  if (Konva.capturePointerEventsEnabled) {
502
567
  shape.setPointerCapture(pos.id);
503
568
  }
504
- // save where we started the click
505
- this[eventType + 'ClickStartShape'] = shape;
506
569
  shape._fireAndBubble(events.pointerdown, {
507
570
  evt: evt,
508
571
  pointerId: pos.id,
@@ -520,13 +583,13 @@ export class Stage extends Container {
520
583
  evt: evt,
521
584
  target: this,
522
585
  currentTarget: this,
523
- pointerId: this._pointerPositions[0].id,
586
+ pointerId: this._changedPointerPositions[0].id,
524
587
  });
525
588
  }
526
589
  }
527
590
  _pointermove(evt) {
528
591
  const events = getEventsMap(evt.type);
529
- const eventType = getEventType(evt.type);
592
+ const eventType = Util._getEventType(evt.type);
530
593
  if (!events) {
531
594
  return;
532
595
  }
@@ -534,44 +597,54 @@ export class Stage extends Container {
534
597
  // native mouse wheel scrolling during drag on desktop
535
598
  const isTouchPointer = evt.type.indexOf('touch') >= 0 ||
536
599
  evt.pointerType === 'touch';
537
- if (Konva.isDragging() &&
538
- DD.node.preventDefault() &&
539
- evt.cancelable &&
540
- isTouchPointer) {
541
- evt.preventDefault();
600
+ if (evt.cancelable && isTouchPointer) {
601
+ for (const { node, dragStatus } of DD._dragElements.values()) {
602
+ if (dragStatus === 'dragging' &&
603
+ node.getStage() === this &&
604
+ node.preventDefault()) {
605
+ evt.preventDefault();
606
+ break;
607
+ }
608
+ }
542
609
  }
543
610
  this.setPointersPositions(evt);
544
- const eventsEnabled = !(Konva.isDragging() || Konva.isTransforming()) || Konva.hitOnDragEnabled;
611
+ const eventsEnabled = this._pointerEventsEnabled();
545
612
  if (!eventsEnabled) {
546
613
  return;
547
614
  }
548
615
  const processedShapesIds = {};
549
616
  let triggeredOnShape = false;
550
- const targetShape = this._getTargetShape(eventType);
551
617
  this._changedPointerPositions.forEach((pos) => {
618
+ var _a;
619
+ const key = `${eventType}:${pos.id}`;
620
+ let state = this._pointerStates.get(key);
621
+ if (!state) {
622
+ state = {};
623
+ this._pointerStates.set(key, state);
624
+ }
552
625
  const shape = (PointerEvents.getCapturedShape(pos.id) ||
553
626
  this.getIntersection(pos));
627
+ const targetShape = state.targetShape === shape || ((_a = state.targetShape) === null || _a === void 0 ? void 0 : _a.getStage()) === this
628
+ ? state.targetShape
629
+ : undefined;
554
630
  const pointerId = pos.id;
555
631
  const event = { evt: evt, pointerId };
556
632
  const differentTarget = targetShape !== shape;
633
+ state.targetShape = shape && shape.isListening() ? shape : undefined;
557
634
  if (differentTarget && targetShape) {
558
635
  targetShape._fireAndBubble(events.pointerout, { ...event }, shape);
559
636
  targetShape._fireAndBubble(events.pointerleave, { ...event }, shape);
560
637
  }
561
- if (shape) {
562
- if (processedShapesIds[shape._id]) {
563
- return;
564
- }
565
- processedShapesIds[shape._id] = true;
566
- }
567
638
  if (shape && shape.isListening()) {
568
639
  triggeredOnShape = true;
569
640
  if (differentTarget) {
570
641
  shape._fireAndBubble(events.pointerover, { ...event }, targetShape);
571
642
  shape._fireAndBubble(events.pointerenter, { ...event }, targetShape);
572
- this[eventType + 'targetShape'] = shape;
573
643
  }
574
- shape._fireAndBubble(events.pointermove, { ...event });
644
+ if (!processedShapesIds[shape._id]) {
645
+ processedShapesIds[shape._id] = true;
646
+ shape._fireAndBubble(events.pointermove, { ...event });
647
+ }
575
648
  }
576
649
  else {
577
650
  if (targetShape) {
@@ -581,7 +654,6 @@ export class Stage extends Container {
581
654
  currentTarget: this,
582
655
  pointerId,
583
656
  });
584
- this[eventType + 'targetShape'] = null;
585
657
  }
586
658
  }
587
659
  });
@@ -596,61 +668,61 @@ export class Stage extends Container {
596
668
  }
597
669
  _pointerup(evt) {
598
670
  const events = getEventsMap(evt.type);
599
- const eventType = getEventType(evt.type);
671
+ const eventType = Util._getEventType(evt.type);
600
672
  if (!events) {
601
673
  return;
602
674
  }
603
675
  this.setPointersPositions(evt);
604
- // the mouse/touch families get their "listen click" flags reset by
605
- // DD._endDragBefore, but native "pointerup" fires before the
606
- // "mouseup"/"touchend" that ends the drag, so for the pointer family the
607
- // flags are still set — check the live drag state as well, otherwise
608
- // "pointerclick" fires after a drag while "click" does not (issue #1756)
609
- const wasDragged = DD.justDragged || (eventType === 'pointer' && Konva.isDragging());
610
- const listenClick = Konva['_' + eventType + 'ListenClick'] && !wasDragged;
611
- const clickStartShape = this[eventType + 'ClickStartShape'];
612
- const clickEndShape = this[eventType + 'ClickEndShape'];
613
676
  const processedShapesIds = {};
614
677
  let skipPointerUpTrigger = false;
615
678
  this._changedPointerPositions.forEach((pos) => {
679
+ var _a;
680
+ const key = `${eventType}:${pos.id}`;
681
+ const state = this._pointerStates.get(key);
682
+ const listenClick = (state === null || state === void 0 ? void 0 : state.clickStartShape) !== undefined;
683
+ const clickStartShape = state === null || state === void 0 ? void 0 : state.clickStartShape;
684
+ if ((state === null || state === void 0 ? void 0 : state.targetShape) && eventType !== 'touch') {
685
+ this._pointerStates.set(key, { targetShape: state.targetShape });
686
+ }
687
+ else {
688
+ this._pointerStates.delete(key);
689
+ }
616
690
  const shape = (PointerEvents.getCapturedShape(pos.id) ||
617
691
  this.getIntersection(pos));
692
+ const alreadyReleased = shape && processedShapesIds[shape._id];
618
693
  if (shape) {
619
694
  shape.releaseCapture(pos.id);
620
- if (processedShapesIds[shape._id]) {
621
- return;
622
- }
623
695
  processedShapesIds[shape._id] = true;
624
696
  }
625
697
  const pointerId = pos.id;
626
698
  const event = { evt: evt, pointerId };
627
- let fireDblClick = false;
628
- if (Konva['_' + eventType + 'InDblClickWindow']) {
629
- fireDblClick = true;
630
- clearTimeout(this['_' + eventType + 'DblTimeout']);
631
- }
632
- else if (!wasDragged) {
633
- // don't set inDblClickWindow after dragging
634
- Konva['_' + eventType + 'InDblClickWindow'] = true;
635
- clearTimeout(this['_' + eventType + 'DblTimeout']);
699
+ const clickTarget = shape && shape.isListening() ? shape : null;
700
+ const lastClick = this._lastClicks[eventType];
701
+ const canClick = listenClick && (!clickTarget || clickStartShape === clickTarget);
702
+ const fireDblClick = canClick &&
703
+ lastClick &&
704
+ (state === null || state === void 0 ? void 0 : state.lastClick) === lastClick &&
705
+ lastClick.shape === clickTarget &&
706
+ Date.now() - lastClick.time < Konva.dblClickWindow;
707
+ if (canClick) {
708
+ this._lastClicks[eventType] = fireDblClick
709
+ ? undefined
710
+ : { shape: clickTarget, time: Date.now() };
636
711
  }
637
- this['_' + eventType + 'DblTimeout'] = setTimeout(function () {
638
- Konva['_' + eventType + 'InDblClickWindow'] = false;
639
- }, Konva.dblClickWindow);
640
712
  if (shape && shape.isListening()) {
641
713
  skipPointerUpTrigger = true;
642
- this[eventType + 'ClickEndShape'] = shape;
643
- shape._fireAndBubble(events.pointerup, { ...event });
714
+ if (!alreadyReleased) {
715
+ shape._fireAndBubble(events.pointerup, { ...event });
716
+ }
644
717
  // detect if click or double click occurred
645
718
  if (listenClick && clickStartShape === shape) {
646
719
  shape._fireAndBubble(events.pointerclick, { ...event });
647
- if (fireDblClick && clickEndShape && clickEndShape === shape) {
720
+ if (fireDblClick) {
648
721
  shape._fireAndBubble(events.pointerdblclick, { ...event });
649
722
  }
650
723
  }
651
724
  }
652
725
  else {
653
- this[eventType + 'ClickEndShape'] = null;
654
726
  if (!skipPointerUpTrigger) {
655
727
  this._fire(events.pointerup, {
656
728
  evt: evt,
@@ -677,6 +749,10 @@ export class Stage extends Container {
677
749
  });
678
750
  }
679
751
  }
752
+ if (eventType === 'touch' && ((_a = state === null || state === void 0 ? void 0 : state.targetShape) === null || _a === void 0 ? void 0 : _a.getStage()) === this) {
753
+ state.targetShape._fireAndBubble(events.pointerout, { ...event });
754
+ state.targetShape._fireAndBubble(events.pointerleave, { ...event });
755
+ }
680
756
  });
681
757
  if (!skipPointerUpTrigger) {
682
758
  this._fire(events.pointerup, {
@@ -686,7 +762,6 @@ export class Stage extends Container {
686
762
  pointerId: this._changedPointerPositions[0].id,
687
763
  });
688
764
  }
689
- Konva['_' + eventType + 'ListenClick'] = false;
690
765
  // always call preventDefault for desktop events because some browsers
691
766
  // try to drag and drop the canvas element
692
767
  // do not call this function on mobile because it prevent "click" event on all parent containers
@@ -727,6 +802,17 @@ export class Stage extends Container {
727
802
  this.setPointersPositions(evt);
728
803
  const events = getEventsMap(evt.type);
729
804
  this._changedPointerPositions.forEach((pos) => {
805
+ var _a;
806
+ const eventType = Util._getEventType(evt.type);
807
+ const key = `${eventType}:${pos.id}`;
808
+ this._cancelClick(pos.id, eventType);
809
+ const targetShape = (_a = this._pointerStates.get(key)) === null || _a === void 0 ? void 0 : _a.targetShape;
810
+ if (targetShape && eventType !== 'touch') {
811
+ this._pointerStates.set(key, { targetShape });
812
+ }
813
+ else {
814
+ this._pointerStates.delete(key);
815
+ }
730
816
  const shape = PointerEvents.getCapturedShape(pos.id) || this.getIntersection(pos);
731
817
  const event = { evt, pointerId: pos.id };
732
818
  if (shape) {
@@ -743,10 +829,17 @@ export class Stage extends Container {
743
829
  });
744
830
  }
745
831
  PointerEvents.releaseCapture(pos.id);
832
+ if (events && eventType === 'touch' && (targetShape === null || targetShape === void 0 ? void 0 : targetShape.getStage()) === this) {
833
+ targetShape._fireAndBubble(events.pointerout, { ...event });
834
+ targetShape._fireAndBubble(events.pointerleave, { ...event });
835
+ }
746
836
  });
747
837
  }
748
838
  _lostpointercapture(evt) {
749
- PointerEvents.releaseCapture(evt.pointerId);
839
+ const captured = PointerEvents.getCapturedShape(evt.pointerId, this);
840
+ if (captured) {
841
+ PointerEvents.releaseCapture(evt.pointerId);
842
+ }
750
843
  }
751
844
  /**
752
845
  * manually register pointers positions (mouse/touch) in the stage.
@@ -768,6 +861,7 @@ export class Stage extends Container {
768
861
  evt = evt ? evt : window.event;
769
862
  // touch events
770
863
  if (evt.touches !== undefined) {
864
+ this._pointerEventType = 'touch';
771
865
  // touchlist has not support for map method
772
866
  // so we have to iterate
773
867
  this._pointerPositions = [];
@@ -789,6 +883,8 @@ export class Stage extends Container {
789
883
  }
790
884
  else {
791
885
  // mouse events
886
+ this._pointerEventType =
887
+ evt.pointerId === undefined ? 'mouse' : 'pointer';
792
888
  x = (evt.clientX - contentPosition.left) / contentPosition.scaleX;
793
889
  y = (evt.clientY - contentPosition.top) / contentPosition.scaleY;
794
890
  this.pointerPos = {