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/Node.js ADDED
@@ -0,0 +1,1381 @@
1
+ import { Util, Transform } from './Util.js';
2
+ import { Factory } from './Factory.js';
3
+ import { SceneCanvas, HitCanvas } from './Canvas.js';
4
+ import { Konva } from './Global.js';
5
+ import { DD } from './DragAndDrop.js';
6
+ import { getNumberValidator, getStringValidator, getBooleanValidator, } from './Validators.js';
7
+ var ABSOLUTE_OPACITY = 'absoluteOpacity', ALL_LISTENERS = 'allEventListeners', ABSOLUTE_TRANSFORM = 'absoluteTransform', ABSOLUTE_SCALE = 'absoluteScale', CANVAS = 'canvas', CHANGE = 'Change', CHILDREN = 'children', KONVA = 'konva', LISTENING = 'listening', MOUSEENTER = 'mouseenter', MOUSELEAVE = 'mouseleave', NAME = 'name', SET = 'set', SHAPE = 'Shape', SPACE = ' ', STAGE = 'stage', TRANSFORM = 'transform', UPPER_STAGE = 'Stage', VISIBLE = 'visible', TRANSFORM_CHANGE_STR = [
8
+ 'xChange.konva',
9
+ 'yChange.konva',
10
+ 'scaleXChange.konva',
11
+ 'scaleYChange.konva',
12
+ 'skewXChange.konva',
13
+ 'skewYChange.konva',
14
+ 'rotationChange.konva',
15
+ 'offsetXChange.konva',
16
+ 'offsetYChange.konva',
17
+ 'transformsEnabledChange.konva',
18
+ ].join(SPACE);
19
+ let idCounter = 1;
20
+ export class Node {
21
+ constructor(config) {
22
+ this._id = idCounter++;
23
+ this.eventListeners = {};
24
+ this.attrs = {};
25
+ this.index = 0;
26
+ this._allEventListeners = null;
27
+ this.parent = null;
28
+ this._cache = new Map();
29
+ this._attachedDepsListeners = new Map();
30
+ this._lastPos = null;
31
+ this._batchingTransformChange = false;
32
+ this._needClearTransformCache = false;
33
+ this._filterUpToDate = false;
34
+ this._isUnderCache = false;
35
+ this._dragEventId = null;
36
+ this._shouldFireChangeEvents = false;
37
+ this.setAttrs(config);
38
+ this._shouldFireChangeEvents = true;
39
+ }
40
+ hasChildren() {
41
+ return false;
42
+ }
43
+ _clearCache(attr) {
44
+ if ((attr === TRANSFORM || attr === ABSOLUTE_TRANSFORM) &&
45
+ this._cache.get(attr)) {
46
+ this._cache.get(attr).dirty = true;
47
+ }
48
+ else if (attr) {
49
+ this._cache.delete(attr);
50
+ }
51
+ else {
52
+ this._cache.clear();
53
+ }
54
+ }
55
+ _getCache(attr, privateGetter) {
56
+ var cache = this._cache.get(attr);
57
+ var isTransform = attr === TRANSFORM || attr === ABSOLUTE_TRANSFORM;
58
+ var invalid = cache === undefined || (isTransform && cache.dirty === true);
59
+ if (invalid) {
60
+ cache = privateGetter.call(this);
61
+ this._cache.set(attr, cache);
62
+ }
63
+ return cache;
64
+ }
65
+ _calculate(name, deps, getter) {
66
+ if (!this._attachedDepsListeners.get(name)) {
67
+ const depsString = deps.map((dep) => dep + 'Change.konva').join(SPACE);
68
+ this.on(depsString, () => {
69
+ this._clearCache(name);
70
+ });
71
+ this._attachedDepsListeners.set(name, true);
72
+ }
73
+ return this._getCache(name, getter);
74
+ }
75
+ _getCanvasCache() {
76
+ return this._cache.get(CANVAS);
77
+ }
78
+ _clearSelfAndDescendantCache(attr) {
79
+ this._clearCache(attr);
80
+ if (attr === ABSOLUTE_TRANSFORM) {
81
+ this.fire('absoluteTransformChange');
82
+ }
83
+ }
84
+ clearCache() {
85
+ this._cache.delete(CANVAS);
86
+ this._clearSelfAndDescendantCache();
87
+ this._requestDraw();
88
+ return this;
89
+ }
90
+ cache(config) {
91
+ var conf = config || {};
92
+ var rect = {};
93
+ if (conf.x === undefined ||
94
+ conf.y === undefined ||
95
+ conf.width === undefined ||
96
+ conf.height === undefined) {
97
+ rect = this.getClientRect({
98
+ skipTransform: true,
99
+ relativeTo: this.getParent(),
100
+ });
101
+ }
102
+ var width = Math.ceil(conf.width || rect.width), height = Math.ceil(conf.height || rect.height), pixelRatio = conf.pixelRatio, x = conf.x === undefined ? rect.x : conf.x, y = conf.y === undefined ? rect.y : conf.y, offset = conf.offset || 0, drawBorder = conf.drawBorder || false, hitCanvasPixelRatio = conf.hitCanvasPixelRatio || 1;
103
+ if (!width || !height) {
104
+ Util.error('Can not cache the node. Width or height of the node equals 0. Caching is skipped.');
105
+ return;
106
+ }
107
+ width += offset * 2;
108
+ height += offset * 2;
109
+ x -= offset;
110
+ y -= offset;
111
+ var cachedSceneCanvas = new SceneCanvas({
112
+ pixelRatio: pixelRatio,
113
+ width: width,
114
+ height: height,
115
+ }), cachedFilterCanvas = new SceneCanvas({
116
+ pixelRatio: pixelRatio,
117
+ width: 0,
118
+ height: 0,
119
+ }), cachedHitCanvas = new HitCanvas({
120
+ pixelRatio: hitCanvasPixelRatio,
121
+ width: width,
122
+ height: height,
123
+ }), sceneContext = cachedSceneCanvas.getContext(), hitContext = cachedHitCanvas.getContext();
124
+ cachedHitCanvas.isCache = true;
125
+ cachedSceneCanvas.isCache = true;
126
+ this._cache.delete(CANVAS);
127
+ this._filterUpToDate = false;
128
+ if (conf.imageSmoothingEnabled === false) {
129
+ cachedSceneCanvas.getContext()._context.imageSmoothingEnabled = false;
130
+ cachedFilterCanvas.getContext()._context.imageSmoothingEnabled = false;
131
+ }
132
+ sceneContext.save();
133
+ hitContext.save();
134
+ sceneContext.translate(-x, -y);
135
+ hitContext.translate(-x, -y);
136
+ this._isUnderCache = true;
137
+ this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
138
+ this._clearSelfAndDescendantCache(ABSOLUTE_SCALE);
139
+ this.drawScene(cachedSceneCanvas, this);
140
+ this.drawHit(cachedHitCanvas, this);
141
+ this._isUnderCache = false;
142
+ sceneContext.restore();
143
+ hitContext.restore();
144
+ if (drawBorder) {
145
+ sceneContext.save();
146
+ sceneContext.beginPath();
147
+ sceneContext.rect(0, 0, width, height);
148
+ sceneContext.closePath();
149
+ sceneContext.setAttr('strokeStyle', 'red');
150
+ sceneContext.setAttr('lineWidth', 5);
151
+ sceneContext.stroke();
152
+ sceneContext.restore();
153
+ }
154
+ this._cache.set(CANVAS, {
155
+ scene: cachedSceneCanvas,
156
+ filter: cachedFilterCanvas,
157
+ hit: cachedHitCanvas,
158
+ x: x,
159
+ y: y,
160
+ });
161
+ this._requestDraw();
162
+ return this;
163
+ }
164
+ isCached() {
165
+ return this._cache.has(CANVAS);
166
+ }
167
+ getClientRect(config) {
168
+ throw new Error('abstract "getClientRect" method call');
169
+ }
170
+ _transformedRect(rect, top) {
171
+ var points = [
172
+ { x: rect.x, y: rect.y },
173
+ { x: rect.x + rect.width, y: rect.y },
174
+ { x: rect.x + rect.width, y: rect.y + rect.height },
175
+ { x: rect.x, y: rect.y + rect.height },
176
+ ];
177
+ var minX, minY, maxX, maxY;
178
+ var trans = this.getAbsoluteTransform(top);
179
+ points.forEach(function (point) {
180
+ var transformed = trans.point(point);
181
+ if (minX === undefined) {
182
+ minX = maxX = transformed.x;
183
+ minY = maxY = transformed.y;
184
+ }
185
+ minX = Math.min(minX, transformed.x);
186
+ minY = Math.min(minY, transformed.y);
187
+ maxX = Math.max(maxX, transformed.x);
188
+ maxY = Math.max(maxY, transformed.y);
189
+ });
190
+ return {
191
+ x: minX,
192
+ y: minY,
193
+ width: maxX - minX,
194
+ height: maxY - minY,
195
+ };
196
+ }
197
+ _drawCachedSceneCanvas(context) {
198
+ context.save();
199
+ context._applyOpacity(this);
200
+ context._applyGlobalCompositeOperation(this);
201
+ const canvasCache = this._getCanvasCache();
202
+ context.translate(canvasCache.x, canvasCache.y);
203
+ var cacheCanvas = this._getCachedSceneCanvas();
204
+ var ratio = cacheCanvas.pixelRatio;
205
+ context.drawImage(cacheCanvas._canvas, 0, 0, cacheCanvas.width / ratio, cacheCanvas.height / ratio);
206
+ context.restore();
207
+ }
208
+ _drawCachedHitCanvas(context) {
209
+ var canvasCache = this._getCanvasCache(), hitCanvas = canvasCache.hit;
210
+ context.save();
211
+ context.translate(canvasCache.x, canvasCache.y);
212
+ context.drawImage(hitCanvas._canvas, 0, 0, hitCanvas.width / hitCanvas.pixelRatio, hitCanvas.height / hitCanvas.pixelRatio);
213
+ context.restore();
214
+ }
215
+ _getCachedSceneCanvas() {
216
+ var filters = this.filters(), cachedCanvas = this._getCanvasCache(), sceneCanvas = cachedCanvas.scene, filterCanvas = cachedCanvas.filter, filterContext = filterCanvas.getContext(), len, imageData, n, filter;
217
+ if (filters) {
218
+ if (!this._filterUpToDate) {
219
+ var ratio = sceneCanvas.pixelRatio;
220
+ filterCanvas.setSize(sceneCanvas.width / sceneCanvas.pixelRatio, sceneCanvas.height / sceneCanvas.pixelRatio);
221
+ try {
222
+ len = filters.length;
223
+ filterContext.clear();
224
+ filterContext.drawImage(sceneCanvas._canvas, 0, 0, sceneCanvas.getWidth() / ratio, sceneCanvas.getHeight() / ratio);
225
+ imageData = filterContext.getImageData(0, 0, filterCanvas.getWidth(), filterCanvas.getHeight());
226
+ for (n = 0; n < len; n++) {
227
+ filter = filters[n];
228
+ if (typeof filter !== 'function') {
229
+ Util.error('Filter should be type of function, but got ' +
230
+ typeof filter +
231
+ ' instead. Please check correct filters');
232
+ continue;
233
+ }
234
+ filter.call(this, imageData);
235
+ filterContext.putImageData(imageData, 0, 0);
236
+ }
237
+ }
238
+ catch (e) {
239
+ Util.error('Unable to apply filter. ' +
240
+ e.message +
241
+ ' This post my help you https://konvajs.org/docs/posts/Tainted_Canvas.html.');
242
+ }
243
+ this._filterUpToDate = true;
244
+ }
245
+ return filterCanvas;
246
+ }
247
+ return sceneCanvas;
248
+ }
249
+ on(evtStr, handler) {
250
+ this._cache && this._cache.delete(ALL_LISTENERS);
251
+ if (arguments.length === 3) {
252
+ return this._delegate.apply(this, arguments);
253
+ }
254
+ var events = evtStr.split(SPACE), len = events.length, n, event, parts, baseEvent, name;
255
+ for (n = 0; n < len; n++) {
256
+ event = events[n];
257
+ parts = event.split('.');
258
+ baseEvent = parts[0];
259
+ name = parts[1] || '';
260
+ if (!this.eventListeners[baseEvent]) {
261
+ this.eventListeners[baseEvent] = [];
262
+ }
263
+ this.eventListeners[baseEvent].push({
264
+ name: name,
265
+ handler: handler,
266
+ });
267
+ }
268
+ return this;
269
+ }
270
+ off(evtStr, callback) {
271
+ var events = (evtStr || '').split(SPACE), len = events.length, n, t, event, parts, baseEvent, name;
272
+ this._cache && this._cache.delete(ALL_LISTENERS);
273
+ if (!evtStr) {
274
+ for (t in this.eventListeners) {
275
+ this._off(t);
276
+ }
277
+ }
278
+ for (n = 0; n < len; n++) {
279
+ event = events[n];
280
+ parts = event.split('.');
281
+ baseEvent = parts[0];
282
+ name = parts[1];
283
+ if (baseEvent) {
284
+ if (this.eventListeners[baseEvent]) {
285
+ this._off(baseEvent, name, callback);
286
+ }
287
+ }
288
+ else {
289
+ for (t in this.eventListeners) {
290
+ this._off(t, name, callback);
291
+ }
292
+ }
293
+ }
294
+ return this;
295
+ }
296
+ dispatchEvent(evt) {
297
+ var e = {
298
+ target: this,
299
+ type: evt.type,
300
+ evt: evt,
301
+ };
302
+ this.fire(evt.type, e);
303
+ return this;
304
+ }
305
+ addEventListener(type, handler) {
306
+ this.on(type, function (evt) {
307
+ handler.call(this, evt.evt);
308
+ });
309
+ return this;
310
+ }
311
+ removeEventListener(type) {
312
+ this.off(type);
313
+ return this;
314
+ }
315
+ _delegate(event, selector, handler) {
316
+ var stopNode = this;
317
+ this.on(event, function (evt) {
318
+ var targets = evt.target.findAncestors(selector, true, stopNode);
319
+ for (var i = 0; i < targets.length; i++) {
320
+ evt = Util.cloneObject(evt);
321
+ evt.currentTarget = targets[i];
322
+ handler.call(targets[i], evt);
323
+ }
324
+ });
325
+ }
326
+ remove() {
327
+ if (this.isDragging()) {
328
+ this.stopDrag();
329
+ }
330
+ DD._dragElements.delete(this._id);
331
+ this._remove();
332
+ return this;
333
+ }
334
+ _clearCaches() {
335
+ this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
336
+ this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
337
+ this._clearSelfAndDescendantCache(ABSOLUTE_SCALE);
338
+ this._clearSelfAndDescendantCache(STAGE);
339
+ this._clearSelfAndDescendantCache(VISIBLE);
340
+ this._clearSelfAndDescendantCache(LISTENING);
341
+ }
342
+ _remove() {
343
+ this._clearCaches();
344
+ var parent = this.getParent();
345
+ if (parent && parent.children) {
346
+ parent.children.splice(this.index, 1);
347
+ parent._setChildrenIndices();
348
+ this.parent = null;
349
+ }
350
+ }
351
+ destroy() {
352
+ this.remove();
353
+ return this;
354
+ }
355
+ getAttr(attr) {
356
+ var method = 'get' + Util._capitalize(attr);
357
+ if (Util._isFunction(this[method])) {
358
+ return this[method]();
359
+ }
360
+ return this.attrs[attr];
361
+ }
362
+ getAncestors() {
363
+ var parent = this.getParent(), ancestors = [];
364
+ while (parent) {
365
+ ancestors.push(parent);
366
+ parent = parent.getParent();
367
+ }
368
+ return ancestors;
369
+ }
370
+ getAttrs() {
371
+ return this.attrs || {};
372
+ }
373
+ setAttrs(config) {
374
+ this._batchTransformChanges(() => {
375
+ var key, method;
376
+ if (!config) {
377
+ return this;
378
+ }
379
+ for (key in config) {
380
+ if (key === CHILDREN) {
381
+ continue;
382
+ }
383
+ method = SET + Util._capitalize(key);
384
+ if (Util._isFunction(this[method])) {
385
+ this[method](config[key]);
386
+ }
387
+ else {
388
+ this._setAttr(key, config[key]);
389
+ }
390
+ }
391
+ });
392
+ return this;
393
+ }
394
+ isListening() {
395
+ return this._getCache(LISTENING, this._isListening);
396
+ }
397
+ _isListening(relativeTo) {
398
+ const listening = this.listening();
399
+ if (!listening) {
400
+ return false;
401
+ }
402
+ const parent = this.getParent();
403
+ if (parent && parent !== relativeTo && this !== relativeTo) {
404
+ return parent._isListening(relativeTo);
405
+ }
406
+ else {
407
+ return true;
408
+ }
409
+ }
410
+ isVisible() {
411
+ return this._getCache(VISIBLE, this._isVisible);
412
+ }
413
+ _isVisible(relativeTo) {
414
+ const visible = this.visible();
415
+ if (!visible) {
416
+ return false;
417
+ }
418
+ const parent = this.getParent();
419
+ if (parent && parent !== relativeTo && this !== relativeTo) {
420
+ return parent._isVisible(relativeTo);
421
+ }
422
+ else {
423
+ return true;
424
+ }
425
+ }
426
+ shouldDrawHit(top, skipDragCheck = false) {
427
+ if (top) {
428
+ return this._isVisible(top) && this._isListening(top);
429
+ }
430
+ var layer = this.getLayer();
431
+ var layerUnderDrag = false;
432
+ DD._dragElements.forEach((elem) => {
433
+ if (elem.dragStatus !== 'dragging') {
434
+ return;
435
+ }
436
+ else if (elem.node.nodeType === 'Stage') {
437
+ layerUnderDrag = true;
438
+ }
439
+ else if (elem.node.getLayer() === layer) {
440
+ layerUnderDrag = true;
441
+ }
442
+ });
443
+ var dragSkip = !skipDragCheck && !Konva.hitOnDragEnabled && layerUnderDrag;
444
+ return this.isListening() && this.isVisible() && !dragSkip;
445
+ }
446
+ show() {
447
+ this.visible(true);
448
+ return this;
449
+ }
450
+ hide() {
451
+ this.visible(false);
452
+ return this;
453
+ }
454
+ getZIndex() {
455
+ return this.index || 0;
456
+ }
457
+ getAbsoluteZIndex() {
458
+ var depth = this.getDepth(), that = this, index = 0, nodes, len, n, child;
459
+ function addChildren(children) {
460
+ nodes = [];
461
+ len = children.length;
462
+ for (n = 0; n < len; n++) {
463
+ child = children[n];
464
+ index++;
465
+ if (child.nodeType !== SHAPE) {
466
+ nodes = nodes.concat(child.getChildren().slice());
467
+ }
468
+ if (child._id === that._id) {
469
+ n = len;
470
+ }
471
+ }
472
+ if (nodes.length > 0 && nodes[0].getDepth() <= depth) {
473
+ addChildren(nodes);
474
+ }
475
+ }
476
+ if (that.nodeType !== UPPER_STAGE) {
477
+ addChildren(that.getStage().getChildren());
478
+ }
479
+ return index;
480
+ }
481
+ getDepth() {
482
+ var depth = 0, parent = this.parent;
483
+ while (parent) {
484
+ depth++;
485
+ parent = parent.parent;
486
+ }
487
+ return depth;
488
+ }
489
+ _batchTransformChanges(func) {
490
+ this._batchingTransformChange = true;
491
+ func();
492
+ this._batchingTransformChange = false;
493
+ if (this._needClearTransformCache) {
494
+ this._clearCache(TRANSFORM);
495
+ this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
496
+ }
497
+ this._needClearTransformCache = false;
498
+ }
499
+ setPosition(pos) {
500
+ this._batchTransformChanges(() => {
501
+ this.x(pos.x);
502
+ this.y(pos.y);
503
+ });
504
+ return this;
505
+ }
506
+ getPosition() {
507
+ return {
508
+ x: this.x(),
509
+ y: this.y(),
510
+ };
511
+ }
512
+ getRelativePointerPosition() {
513
+ if (!this.getStage()) {
514
+ return null;
515
+ }
516
+ var pos = this.getStage().getPointerPosition();
517
+ if (!pos) {
518
+ return null;
519
+ }
520
+ var transform = this.getAbsoluteTransform().copy();
521
+ transform.invert();
522
+ return transform.point(pos);
523
+ }
524
+ getAbsolutePosition(top) {
525
+ let haveCachedParent = false;
526
+ let parent = this.parent;
527
+ while (parent) {
528
+ if (parent.isCached()) {
529
+ haveCachedParent = true;
530
+ break;
531
+ }
532
+ parent = parent.parent;
533
+ }
534
+ if (haveCachedParent && !top) {
535
+ top = true;
536
+ }
537
+ var absoluteMatrix = this.getAbsoluteTransform(top).getMatrix(), absoluteTransform = new Transform(), offset = this.offset();
538
+ absoluteTransform.m = absoluteMatrix.slice();
539
+ absoluteTransform.translate(offset.x, offset.y);
540
+ return absoluteTransform.getTranslation();
541
+ }
542
+ setAbsolutePosition(pos) {
543
+ var origTrans = this._clearTransform();
544
+ this.attrs.x = origTrans.x;
545
+ this.attrs.y = origTrans.y;
546
+ delete origTrans.x;
547
+ delete origTrans.y;
548
+ this._clearCache(TRANSFORM);
549
+ var it = this._getAbsoluteTransform().copy();
550
+ it.invert();
551
+ it.translate(pos.x, pos.y);
552
+ pos = {
553
+ x: this.attrs.x + it.getTranslation().x,
554
+ y: this.attrs.y + it.getTranslation().y,
555
+ };
556
+ this._setTransform(origTrans);
557
+ this.setPosition({ x: pos.x, y: pos.y });
558
+ this._clearCache(TRANSFORM);
559
+ this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
560
+ return this;
561
+ }
562
+ _setTransform(trans) {
563
+ var key;
564
+ for (key in trans) {
565
+ this.attrs[key] = trans[key];
566
+ }
567
+ }
568
+ _clearTransform() {
569
+ var trans = {
570
+ x: this.x(),
571
+ y: this.y(),
572
+ rotation: this.rotation(),
573
+ scaleX: this.scaleX(),
574
+ scaleY: this.scaleY(),
575
+ offsetX: this.offsetX(),
576
+ offsetY: this.offsetY(),
577
+ skewX: this.skewX(),
578
+ skewY: this.skewY(),
579
+ };
580
+ this.attrs.x = 0;
581
+ this.attrs.y = 0;
582
+ this.attrs.rotation = 0;
583
+ this.attrs.scaleX = 1;
584
+ this.attrs.scaleY = 1;
585
+ this.attrs.offsetX = 0;
586
+ this.attrs.offsetY = 0;
587
+ this.attrs.skewX = 0;
588
+ this.attrs.skewY = 0;
589
+ return trans;
590
+ }
591
+ move(change) {
592
+ var changeX = change.x, changeY = change.y, x = this.x(), y = this.y();
593
+ if (changeX !== undefined) {
594
+ x += changeX;
595
+ }
596
+ if (changeY !== undefined) {
597
+ y += changeY;
598
+ }
599
+ this.setPosition({ x: x, y: y });
600
+ return this;
601
+ }
602
+ _eachAncestorReverse(func, top) {
603
+ var family = [], parent = this.getParent(), len, n;
604
+ if (top && top._id === this._id) {
605
+ return;
606
+ }
607
+ family.unshift(this);
608
+ while (parent && (!top || parent._id !== top._id)) {
609
+ family.unshift(parent);
610
+ parent = parent.parent;
611
+ }
612
+ len = family.length;
613
+ for (n = 0; n < len; n++) {
614
+ func(family[n]);
615
+ }
616
+ }
617
+ rotate(theta) {
618
+ this.rotation(this.rotation() + theta);
619
+ return this;
620
+ }
621
+ moveToTop() {
622
+ if (!this.parent) {
623
+ Util.warn('Node has no parent. moveToTop function is ignored.');
624
+ return false;
625
+ }
626
+ var index = this.index;
627
+ this.parent.children.splice(index, 1);
628
+ this.parent.children.push(this);
629
+ this.parent._setChildrenIndices();
630
+ return true;
631
+ }
632
+ moveUp() {
633
+ if (!this.parent) {
634
+ Util.warn('Node has no parent. moveUp function is ignored.');
635
+ return false;
636
+ }
637
+ var index = this.index, len = this.parent.getChildren().length;
638
+ if (index < len - 1) {
639
+ this.parent.children.splice(index, 1);
640
+ this.parent.children.splice(index + 1, 0, this);
641
+ this.parent._setChildrenIndices();
642
+ return true;
643
+ }
644
+ return false;
645
+ }
646
+ moveDown() {
647
+ if (!this.parent) {
648
+ Util.warn('Node has no parent. moveDown function is ignored.');
649
+ return false;
650
+ }
651
+ var index = this.index;
652
+ if (index > 0) {
653
+ this.parent.children.splice(index, 1);
654
+ this.parent.children.splice(index - 1, 0, this);
655
+ this.parent._setChildrenIndices();
656
+ return true;
657
+ }
658
+ return false;
659
+ }
660
+ moveToBottom() {
661
+ if (!this.parent) {
662
+ Util.warn('Node has no parent. moveToBottom function is ignored.');
663
+ return false;
664
+ }
665
+ var index = this.index;
666
+ if (index > 0) {
667
+ this.parent.children.splice(index, 1);
668
+ this.parent.children.unshift(this);
669
+ this.parent._setChildrenIndices();
670
+ return true;
671
+ }
672
+ return false;
673
+ }
674
+ setZIndex(zIndex) {
675
+ if (!this.parent) {
676
+ Util.warn('Node has no parent. zIndex parameter is ignored.');
677
+ return this;
678
+ }
679
+ if (zIndex < 0 || zIndex >= this.parent.children.length) {
680
+ Util.warn('Unexpected value ' +
681
+ zIndex +
682
+ ' for zIndex property. zIndex is just index of a node in children of its parent. Expected value is from 0 to ' +
683
+ (this.parent.children.length - 1) +
684
+ '.');
685
+ }
686
+ var index = this.index;
687
+ this.parent.children.splice(index, 1);
688
+ this.parent.children.splice(zIndex, 0, this);
689
+ this.parent._setChildrenIndices();
690
+ return this;
691
+ }
692
+ getAbsoluteOpacity() {
693
+ return this._getCache(ABSOLUTE_OPACITY, this._getAbsoluteOpacity);
694
+ }
695
+ _getAbsoluteOpacity() {
696
+ var absOpacity = this.opacity();
697
+ var parent = this.getParent();
698
+ if (parent && !parent._isUnderCache) {
699
+ absOpacity *= parent.getAbsoluteOpacity();
700
+ }
701
+ return absOpacity;
702
+ }
703
+ moveTo(newContainer) {
704
+ if (this.getParent() !== newContainer) {
705
+ this._remove();
706
+ newContainer.add(this);
707
+ }
708
+ return this;
709
+ }
710
+ toObject() {
711
+ var obj = {}, attrs = this.getAttrs(), key, val, getter, defaultValue, nonPlainObject;
712
+ obj.attrs = {};
713
+ for (key in attrs) {
714
+ val = attrs[key];
715
+ nonPlainObject =
716
+ Util.isObject(val) && !Util._isPlainObject(val) && !Util._isArray(val);
717
+ if (nonPlainObject) {
718
+ continue;
719
+ }
720
+ getter = typeof this[key] === 'function' && this[key];
721
+ delete attrs[key];
722
+ defaultValue = getter ? getter.call(this) : null;
723
+ attrs[key] = val;
724
+ if (defaultValue !== val) {
725
+ obj.attrs[key] = val;
726
+ }
727
+ }
728
+ obj.className = this.getClassName();
729
+ return Util._prepareToStringify(obj);
730
+ }
731
+ toJSON() {
732
+ return JSON.stringify(this.toObject());
733
+ }
734
+ getParent() {
735
+ return this.parent;
736
+ }
737
+ findAncestors(selector, includeSelf, stopNode) {
738
+ var res = [];
739
+ if (includeSelf && this._isMatch(selector)) {
740
+ res.push(this);
741
+ }
742
+ var ancestor = this.parent;
743
+ while (ancestor) {
744
+ if (ancestor === stopNode) {
745
+ return res;
746
+ }
747
+ if (ancestor._isMatch(selector)) {
748
+ res.push(ancestor);
749
+ }
750
+ ancestor = ancestor.parent;
751
+ }
752
+ return res;
753
+ }
754
+ isAncestorOf(node) {
755
+ return false;
756
+ }
757
+ findAncestor(selector, includeSelf, stopNode) {
758
+ return this.findAncestors(selector, includeSelf, stopNode)[0];
759
+ }
760
+ _isMatch(selector) {
761
+ if (!selector) {
762
+ return false;
763
+ }
764
+ if (typeof selector === 'function') {
765
+ return selector(this);
766
+ }
767
+ var selectorArr = selector.replace(/ /g, '').split(','), len = selectorArr.length, n, sel;
768
+ for (n = 0; n < len; n++) {
769
+ sel = selectorArr[n];
770
+ if (!Util.isValidSelector(sel)) {
771
+ Util.warn('Selector "' +
772
+ sel +
773
+ '" is invalid. Allowed selectors examples are "#foo", ".bar" or "Group".');
774
+ Util.warn('If you have a custom shape with such className, please change it to start with upper letter like "Triangle".');
775
+ Util.warn('Konva is awesome, right?');
776
+ }
777
+ if (sel.charAt(0) === '#') {
778
+ if (this.id() === sel.slice(1)) {
779
+ return true;
780
+ }
781
+ }
782
+ else if (sel.charAt(0) === '.') {
783
+ if (this.hasName(sel.slice(1))) {
784
+ return true;
785
+ }
786
+ }
787
+ else if (this.className === sel || this.nodeType === sel) {
788
+ return true;
789
+ }
790
+ }
791
+ return false;
792
+ }
793
+ getLayer() {
794
+ var parent = this.getParent();
795
+ return parent ? parent.getLayer() : null;
796
+ }
797
+ getStage() {
798
+ return this._getCache(STAGE, this._getStage);
799
+ }
800
+ _getStage() {
801
+ var parent = this.getParent();
802
+ if (parent) {
803
+ return parent.getStage();
804
+ }
805
+ else {
806
+ return undefined;
807
+ }
808
+ }
809
+ fire(eventType, evt = {}, bubble) {
810
+ evt.target = evt.target || this;
811
+ if (bubble) {
812
+ this._fireAndBubble(eventType, evt);
813
+ }
814
+ else {
815
+ this._fire(eventType, evt);
816
+ }
817
+ return this;
818
+ }
819
+ getAbsoluteTransform(top) {
820
+ if (top) {
821
+ return this._getAbsoluteTransform(top);
822
+ }
823
+ else {
824
+ return this._getCache(ABSOLUTE_TRANSFORM, this._getAbsoluteTransform);
825
+ }
826
+ }
827
+ _getAbsoluteTransform(top) {
828
+ var at;
829
+ if (top) {
830
+ at = new Transform();
831
+ this._eachAncestorReverse(function (node) {
832
+ var transformsEnabled = node.transformsEnabled();
833
+ if (transformsEnabled === 'all') {
834
+ at.multiply(node.getTransform());
835
+ }
836
+ else if (transformsEnabled === 'position') {
837
+ at.translate(node.x() - node.offsetX(), node.y() - node.offsetY());
838
+ }
839
+ }, top);
840
+ return at;
841
+ }
842
+ else {
843
+ at = this._cache.get(ABSOLUTE_TRANSFORM) || new Transform();
844
+ if (this.parent) {
845
+ this.parent.getAbsoluteTransform().copyInto(at);
846
+ }
847
+ else {
848
+ at.reset();
849
+ }
850
+ var transformsEnabled = this.transformsEnabled();
851
+ if (transformsEnabled === 'all') {
852
+ at.multiply(this.getTransform());
853
+ }
854
+ else if (transformsEnabled === 'position') {
855
+ const x = this.attrs.x || 0;
856
+ const y = this.attrs.y || 0;
857
+ const offsetX = this.attrs.offsetX || 0;
858
+ const offsetY = this.attrs.offsetY || 0;
859
+ at.translate(x - offsetX, y - offsetY);
860
+ }
861
+ at.dirty = false;
862
+ return at;
863
+ }
864
+ }
865
+ getAbsoluteScale(top) {
866
+ var parent = this;
867
+ while (parent) {
868
+ if (parent._isUnderCache) {
869
+ top = parent;
870
+ }
871
+ parent = parent.getParent();
872
+ }
873
+ const transform = this.getAbsoluteTransform(top);
874
+ const attrs = transform.decompose();
875
+ return {
876
+ x: attrs.scaleX,
877
+ y: attrs.scaleY,
878
+ };
879
+ }
880
+ getAbsoluteRotation() {
881
+ return this.getAbsoluteTransform().decompose().rotation;
882
+ }
883
+ getTransform() {
884
+ return this._getCache(TRANSFORM, this._getTransform);
885
+ }
886
+ _getTransform() {
887
+ var _a, _b;
888
+ var m = this._cache.get(TRANSFORM) || new Transform();
889
+ m.reset();
890
+ var x = this.x(), y = this.y(), rotation = Konva.getAngle(this.rotation()), scaleX = (_a = this.attrs.scaleX) !== null && _a !== void 0 ? _a : 1, scaleY = (_b = this.attrs.scaleY) !== null && _b !== void 0 ? _b : 1, skewX = this.attrs.skewX || 0, skewY = this.attrs.skewY || 0, offsetX = this.attrs.offsetX || 0, offsetY = this.attrs.offsetY || 0;
891
+ if (x !== 0 || y !== 0) {
892
+ m.translate(x, y);
893
+ }
894
+ if (rotation !== 0) {
895
+ m.rotate(rotation);
896
+ }
897
+ if (skewX !== 0 || skewY !== 0) {
898
+ m.skew(skewX, skewY);
899
+ }
900
+ if (scaleX !== 1 || scaleY !== 1) {
901
+ m.scale(scaleX, scaleY);
902
+ }
903
+ if (offsetX !== 0 || offsetY !== 0) {
904
+ m.translate(-1 * offsetX, -1 * offsetY);
905
+ }
906
+ m.dirty = false;
907
+ return m;
908
+ }
909
+ clone(obj) {
910
+ var attrs = Util.cloneObject(this.attrs), key, allListeners, len, n, listener;
911
+ for (key in obj) {
912
+ attrs[key] = obj[key];
913
+ }
914
+ var node = new this.constructor(attrs);
915
+ for (key in this.eventListeners) {
916
+ allListeners = this.eventListeners[key];
917
+ len = allListeners.length;
918
+ for (n = 0; n < len; n++) {
919
+ listener = allListeners[n];
920
+ if (listener.name.indexOf(KONVA) < 0) {
921
+ if (!node.eventListeners[key]) {
922
+ node.eventListeners[key] = [];
923
+ }
924
+ node.eventListeners[key].push(listener);
925
+ }
926
+ }
927
+ }
928
+ return node;
929
+ }
930
+ _toKonvaCanvas(config) {
931
+ config = config || {};
932
+ var box = this.getClientRect();
933
+ var stage = this.getStage(), x = config.x !== undefined ? config.x : box.x, y = config.y !== undefined ? config.y : box.y, pixelRatio = config.pixelRatio || 1, canvas = new SceneCanvas({
934
+ width: config.width || box.width || (stage ? stage.width() : 0),
935
+ height: config.height || box.height || (stage ? stage.height() : 0),
936
+ pixelRatio: pixelRatio,
937
+ }), context = canvas.getContext();
938
+ context.save();
939
+ if (x || y) {
940
+ context.translate(-1 * x, -1 * y);
941
+ }
942
+ this.drawScene(canvas);
943
+ context.restore();
944
+ return canvas;
945
+ }
946
+ toCanvas(config) {
947
+ return this._toKonvaCanvas(config)._canvas;
948
+ }
949
+ toDataURL(config) {
950
+ config = config || {};
951
+ var mimeType = config.mimeType || null, quality = config.quality || null;
952
+ var url = this._toKonvaCanvas(config).toDataURL(mimeType, quality);
953
+ if (config.callback) {
954
+ config.callback(url);
955
+ }
956
+ return url;
957
+ }
958
+ toImage(config) {
959
+ if (!config || !config.callback) {
960
+ throw 'callback required for toImage method config argument';
961
+ }
962
+ var callback = config.callback;
963
+ delete config.callback;
964
+ Util._urlToImage(this.toDataURL(config), function (img) {
965
+ callback(img);
966
+ });
967
+ }
968
+ setSize(size) {
969
+ this.width(size.width);
970
+ this.height(size.height);
971
+ return this;
972
+ }
973
+ getSize() {
974
+ return {
975
+ width: this.width(),
976
+ height: this.height(),
977
+ };
978
+ }
979
+ getClassName() {
980
+ return this.className || this.nodeType;
981
+ }
982
+ getType() {
983
+ return this.nodeType;
984
+ }
985
+ getDragDistance() {
986
+ if (this.attrs.dragDistance !== undefined) {
987
+ return this.attrs.dragDistance;
988
+ }
989
+ else if (this.parent) {
990
+ return this.parent.getDragDistance();
991
+ }
992
+ else {
993
+ return Konva.dragDistance;
994
+ }
995
+ }
996
+ _off(type, name, callback) {
997
+ var evtListeners = this.eventListeners[type], i, evtName, handler;
998
+ for (i = 0; i < evtListeners.length; i++) {
999
+ evtName = evtListeners[i].name;
1000
+ handler = evtListeners[i].handler;
1001
+ if ((evtName !== 'konva' || name === 'konva') &&
1002
+ (!name || evtName === name) &&
1003
+ (!callback || callback === handler)) {
1004
+ evtListeners.splice(i, 1);
1005
+ if (evtListeners.length === 0) {
1006
+ delete this.eventListeners[type];
1007
+ break;
1008
+ }
1009
+ i--;
1010
+ }
1011
+ }
1012
+ }
1013
+ _fireChangeEvent(attr, oldVal, newVal) {
1014
+ this._fire(attr + CHANGE, {
1015
+ oldVal: oldVal,
1016
+ newVal: newVal,
1017
+ });
1018
+ }
1019
+ addName(name) {
1020
+ if (!this.hasName(name)) {
1021
+ var oldName = this.name();
1022
+ var newName = oldName ? oldName + ' ' + name : name;
1023
+ this.name(newName);
1024
+ }
1025
+ return this;
1026
+ }
1027
+ hasName(name) {
1028
+ if (!name) {
1029
+ return false;
1030
+ }
1031
+ const fullName = this.name();
1032
+ if (!fullName) {
1033
+ return false;
1034
+ }
1035
+ var names = (fullName || '').split(/\s/g);
1036
+ return names.indexOf(name) !== -1;
1037
+ }
1038
+ removeName(name) {
1039
+ var names = (this.name() || '').split(/\s/g);
1040
+ var index = names.indexOf(name);
1041
+ if (index !== -1) {
1042
+ names.splice(index, 1);
1043
+ this.name(names.join(' '));
1044
+ }
1045
+ return this;
1046
+ }
1047
+ setAttr(attr, val) {
1048
+ var func = this[SET + Util._capitalize(attr)];
1049
+ if (Util._isFunction(func)) {
1050
+ func.call(this, val);
1051
+ }
1052
+ else {
1053
+ this._setAttr(attr, val);
1054
+ }
1055
+ return this;
1056
+ }
1057
+ _requestDraw() {
1058
+ if (Konva.autoDrawEnabled) {
1059
+ const drawNode = this.getLayer() || this.getStage();
1060
+ drawNode === null || drawNode === void 0 ? void 0 : drawNode.batchDraw();
1061
+ }
1062
+ }
1063
+ _setAttr(key, val) {
1064
+ var oldVal = this.attrs[key];
1065
+ if (oldVal === val && !Util.isObject(val)) {
1066
+ return;
1067
+ }
1068
+ if (val === undefined || val === null) {
1069
+ delete this.attrs[key];
1070
+ }
1071
+ else {
1072
+ this.attrs[key] = val;
1073
+ }
1074
+ if (this._shouldFireChangeEvents) {
1075
+ this._fireChangeEvent(key, oldVal, val);
1076
+ }
1077
+ this._requestDraw();
1078
+ }
1079
+ _setComponentAttr(key, component, val) {
1080
+ var oldVal;
1081
+ if (val !== undefined) {
1082
+ oldVal = this.attrs[key];
1083
+ if (!oldVal) {
1084
+ this.attrs[key] = this.getAttr(key);
1085
+ }
1086
+ this.attrs[key][component] = val;
1087
+ this._fireChangeEvent(key, oldVal, val);
1088
+ }
1089
+ }
1090
+ _fireAndBubble(eventType, evt, compareShape) {
1091
+ if (evt && this.nodeType === SHAPE) {
1092
+ evt.target = this;
1093
+ }
1094
+ var shouldStop = (eventType === MOUSEENTER || eventType === MOUSELEAVE) &&
1095
+ ((compareShape &&
1096
+ (this === compareShape ||
1097
+ (this.isAncestorOf && this.isAncestorOf(compareShape)))) ||
1098
+ (this.nodeType === 'Stage' && !compareShape));
1099
+ if (!shouldStop) {
1100
+ this._fire(eventType, evt);
1101
+ var stopBubble = (eventType === MOUSEENTER || eventType === MOUSELEAVE) &&
1102
+ compareShape &&
1103
+ compareShape.isAncestorOf &&
1104
+ compareShape.isAncestorOf(this) &&
1105
+ !compareShape.isAncestorOf(this.parent);
1106
+ if (((evt && !evt.cancelBubble) || !evt) &&
1107
+ this.parent &&
1108
+ this.parent.isListening() &&
1109
+ !stopBubble) {
1110
+ if (compareShape && compareShape.parent) {
1111
+ this._fireAndBubble.call(this.parent, eventType, evt, compareShape);
1112
+ }
1113
+ else {
1114
+ this._fireAndBubble.call(this.parent, eventType, evt);
1115
+ }
1116
+ }
1117
+ }
1118
+ }
1119
+ _getProtoListeners(eventType) {
1120
+ let listeners = this._cache.get(ALL_LISTENERS);
1121
+ if (!listeners) {
1122
+ listeners = {};
1123
+ let obj = Object.getPrototypeOf(this);
1124
+ while (obj) {
1125
+ if (!obj.eventListeners) {
1126
+ obj = Object.getPrototypeOf(obj);
1127
+ continue;
1128
+ }
1129
+ for (var event in obj.eventListeners) {
1130
+ const newEvents = obj.eventListeners[event];
1131
+ const oldEvents = listeners[event] || [];
1132
+ listeners[event] = newEvents.concat(oldEvents);
1133
+ }
1134
+ obj = Object.getPrototypeOf(obj);
1135
+ }
1136
+ this._cache.set(ALL_LISTENERS, listeners);
1137
+ }
1138
+ return listeners[eventType];
1139
+ }
1140
+ _fire(eventType, evt) {
1141
+ evt = evt || {};
1142
+ evt.currentTarget = this;
1143
+ evt.type = eventType;
1144
+ const topListeners = this._getProtoListeners(eventType);
1145
+ if (topListeners) {
1146
+ for (var i = 0; i < topListeners.length; i++) {
1147
+ topListeners[i].handler.call(this, evt);
1148
+ }
1149
+ }
1150
+ const selfListeners = this.eventListeners[eventType];
1151
+ if (selfListeners) {
1152
+ for (var i = 0; i < selfListeners.length; i++) {
1153
+ selfListeners[i].handler.call(this, evt);
1154
+ }
1155
+ }
1156
+ }
1157
+ draw() {
1158
+ this.drawScene();
1159
+ this.drawHit();
1160
+ return this;
1161
+ }
1162
+ _createDragElement(evt) {
1163
+ var pointerId = evt ? evt.pointerId : undefined;
1164
+ var stage = this.getStage();
1165
+ var ap = this.getAbsolutePosition();
1166
+ var pos = stage._getPointerById(pointerId) ||
1167
+ stage._changedPointerPositions[0] ||
1168
+ ap;
1169
+ DD._dragElements.set(this._id, {
1170
+ node: this,
1171
+ startPointerPos: pos,
1172
+ offset: {
1173
+ x: pos.x - ap.x,
1174
+ y: pos.y - ap.y,
1175
+ },
1176
+ dragStatus: 'ready',
1177
+ pointerId,
1178
+ });
1179
+ }
1180
+ startDrag(evt, bubbleEvent = true) {
1181
+ if (!DD._dragElements.has(this._id)) {
1182
+ this._createDragElement(evt);
1183
+ }
1184
+ const elem = DD._dragElements.get(this._id);
1185
+ elem.dragStatus = 'dragging';
1186
+ this.fire('dragstart', {
1187
+ type: 'dragstart',
1188
+ target: this,
1189
+ evt: evt && evt.evt,
1190
+ }, bubbleEvent);
1191
+ }
1192
+ _setDragPosition(evt, elem) {
1193
+ const pos = this.getStage()._getPointerById(elem.pointerId);
1194
+ if (!pos) {
1195
+ return;
1196
+ }
1197
+ var newNodePos = {
1198
+ x: pos.x - elem.offset.x,
1199
+ y: pos.y - elem.offset.y,
1200
+ };
1201
+ var dbf = this.dragBoundFunc();
1202
+ if (dbf !== undefined) {
1203
+ const bounded = dbf.call(this, newNodePos, evt);
1204
+ if (!bounded) {
1205
+ Util.warn('dragBoundFunc did not return any value. That is unexpected behavior. You must return new absolute position from dragBoundFunc.');
1206
+ }
1207
+ else {
1208
+ newNodePos = bounded;
1209
+ }
1210
+ }
1211
+ if (!this._lastPos ||
1212
+ this._lastPos.x !== newNodePos.x ||
1213
+ this._lastPos.y !== newNodePos.y) {
1214
+ this.setAbsolutePosition(newNodePos);
1215
+ this._requestDraw();
1216
+ }
1217
+ this._lastPos = newNodePos;
1218
+ }
1219
+ stopDrag(evt) {
1220
+ const elem = DD._dragElements.get(this._id);
1221
+ if (elem) {
1222
+ elem.dragStatus = 'stopped';
1223
+ }
1224
+ DD._endDragBefore(evt);
1225
+ DD._endDragAfter(evt);
1226
+ }
1227
+ setDraggable(draggable) {
1228
+ this._setAttr('draggable', draggable);
1229
+ this._dragChange();
1230
+ }
1231
+ isDragging() {
1232
+ const elem = DD._dragElements.get(this._id);
1233
+ return elem ? elem.dragStatus === 'dragging' : false;
1234
+ }
1235
+ _listenDrag() {
1236
+ this._dragCleanup();
1237
+ this.on('mousedown.konva touchstart.konva', function (evt) {
1238
+ var shouldCheckButton = evt.evt['button'] !== undefined;
1239
+ var canDrag = !shouldCheckButton || Konva.dragButtons.indexOf(evt.evt['button']) >= 0;
1240
+ if (!canDrag) {
1241
+ return;
1242
+ }
1243
+ if (this.isDragging()) {
1244
+ return;
1245
+ }
1246
+ var hasDraggingChild = false;
1247
+ DD._dragElements.forEach((elem) => {
1248
+ if (this.isAncestorOf(elem.node)) {
1249
+ hasDraggingChild = true;
1250
+ }
1251
+ });
1252
+ if (!hasDraggingChild) {
1253
+ this._createDragElement(evt);
1254
+ }
1255
+ });
1256
+ }
1257
+ _dragChange() {
1258
+ if (this.attrs.draggable) {
1259
+ this._listenDrag();
1260
+ }
1261
+ else {
1262
+ this._dragCleanup();
1263
+ var stage = this.getStage();
1264
+ if (!stage) {
1265
+ return;
1266
+ }
1267
+ const dragElement = DD._dragElements.get(this._id);
1268
+ const isDragging = dragElement && dragElement.dragStatus === 'dragging';
1269
+ const isReady = dragElement && dragElement.dragStatus === 'ready';
1270
+ if (isDragging) {
1271
+ this.stopDrag();
1272
+ }
1273
+ else if (isReady) {
1274
+ DD._dragElements.delete(this._id);
1275
+ }
1276
+ }
1277
+ }
1278
+ _dragCleanup() {
1279
+ this.off('mousedown.konva');
1280
+ this.off('touchstart.konva');
1281
+ }
1282
+ isClientRectOnScreen(margin = { x: 0, y: 0 }) {
1283
+ const stage = this.getStage();
1284
+ if (!stage) {
1285
+ return false;
1286
+ }
1287
+ const screenRect = {
1288
+ x: -margin.x,
1289
+ y: -margin.y,
1290
+ width: stage.width() + margin.x,
1291
+ height: stage.height() + margin.y,
1292
+ };
1293
+ return Util.haveIntersection(screenRect, this.getClientRect());
1294
+ }
1295
+ static create(data, container) {
1296
+ if (Util._isString(data)) {
1297
+ data = JSON.parse(data);
1298
+ }
1299
+ return this._createNode(data, container);
1300
+ }
1301
+ static _createNode(obj, container) {
1302
+ var className = Node.prototype.getClassName.call(obj), children = obj.children, no, len, n;
1303
+ if (container) {
1304
+ obj.attrs.container = container;
1305
+ }
1306
+ if (!Konva[className]) {
1307
+ Util.warn('Can not find a node with class name "' +
1308
+ className +
1309
+ '". Fallback to "Shape".');
1310
+ className = 'Shape';
1311
+ }
1312
+ const Class = Konva[className];
1313
+ no = new Class(obj.attrs);
1314
+ if (children) {
1315
+ len = children.length;
1316
+ for (n = 0; n < len; n++) {
1317
+ no.add(Node._createNode(children[n]));
1318
+ }
1319
+ }
1320
+ return no;
1321
+ }
1322
+ }
1323
+ Node.prototype.nodeType = 'Node';
1324
+ Node.prototype._attrsAffectingSize = [];
1325
+ Node.prototype.eventListeners = {};
1326
+ Node.prototype.on.call(Node.prototype, TRANSFORM_CHANGE_STR, function () {
1327
+ if (this._batchingTransformChange) {
1328
+ this._needClearTransformCache = true;
1329
+ return;
1330
+ }
1331
+ this._clearCache(TRANSFORM);
1332
+ this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
1333
+ });
1334
+ Node.prototype.on.call(Node.prototype, 'visibleChange.konva', function () {
1335
+ this._clearSelfAndDescendantCache(VISIBLE);
1336
+ });
1337
+ Node.prototype.on.call(Node.prototype, 'listeningChange.konva', function () {
1338
+ this._clearSelfAndDescendantCache(LISTENING);
1339
+ });
1340
+ Node.prototype.on.call(Node.prototype, 'opacityChange.konva', function () {
1341
+ this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
1342
+ });
1343
+ const addGetterSetter = Factory.addGetterSetter;
1344
+ addGetterSetter(Node, 'zIndex');
1345
+ addGetterSetter(Node, 'absolutePosition');
1346
+ addGetterSetter(Node, 'position');
1347
+ addGetterSetter(Node, 'x', 0, getNumberValidator());
1348
+ addGetterSetter(Node, 'y', 0, getNumberValidator());
1349
+ addGetterSetter(Node, 'globalCompositeOperation', 'source-over', getStringValidator());
1350
+ addGetterSetter(Node, 'opacity', 1, getNumberValidator());
1351
+ addGetterSetter(Node, 'name', '', getStringValidator());
1352
+ addGetterSetter(Node, 'id', '', getStringValidator());
1353
+ addGetterSetter(Node, 'rotation', 0, getNumberValidator());
1354
+ Factory.addComponentsGetterSetter(Node, 'scale', ['x', 'y']);
1355
+ addGetterSetter(Node, 'scaleX', 1, getNumberValidator());
1356
+ addGetterSetter(Node, 'scaleY', 1, getNumberValidator());
1357
+ Factory.addComponentsGetterSetter(Node, 'skew', ['x', 'y']);
1358
+ addGetterSetter(Node, 'skewX', 0, getNumberValidator());
1359
+ addGetterSetter(Node, 'skewY', 0, getNumberValidator());
1360
+ Factory.addComponentsGetterSetter(Node, 'offset', ['x', 'y']);
1361
+ addGetterSetter(Node, 'offsetX', 0, getNumberValidator());
1362
+ addGetterSetter(Node, 'offsetY', 0, getNumberValidator());
1363
+ addGetterSetter(Node, 'dragDistance', null, getNumberValidator());
1364
+ addGetterSetter(Node, 'width', 0, getNumberValidator());
1365
+ addGetterSetter(Node, 'height', 0, getNumberValidator());
1366
+ addGetterSetter(Node, 'listening', true, getBooleanValidator());
1367
+ addGetterSetter(Node, 'preventDefault', true, getBooleanValidator());
1368
+ addGetterSetter(Node, 'filters', null, function (val) {
1369
+ this._filterUpToDate = false;
1370
+ return val;
1371
+ });
1372
+ addGetterSetter(Node, 'visible', true, getBooleanValidator());
1373
+ addGetterSetter(Node, 'transformsEnabled', 'all', getStringValidator());
1374
+ addGetterSetter(Node, 'size');
1375
+ addGetterSetter(Node, 'dragBoundFunc');
1376
+ addGetterSetter(Node, 'draggable', false, getBooleanValidator());
1377
+ Factory.backCompat(Node, {
1378
+ rotateDeg: 'rotate',
1379
+ setRotationDeg: 'setRotation',
1380
+ getRotationDeg: 'getRotation',
1381
+ });