konva 8.1.4

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