konva 10.0.8 → 10.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/konva.js +95 -48
  2. package/konva.min.js +4 -4
  3. package/package.json +1 -1
  4. package/lib/Animation.d.ts +0 -24
  5. package/lib/Animation.js +0 -142
  6. package/lib/BezierFunctions.d.ts +0 -6
  7. package/lib/BezierFunctions.js +0 -784
  8. package/lib/Canvas.d.ts +0 -33
  9. package/lib/Canvas.js +0 -137
  10. package/lib/Container.d.ts +0 -54
  11. package/lib/Container.js +0 -316
  12. package/lib/Context.d.ts +0 -95
  13. package/lib/Context.js +0 -561
  14. package/lib/Core.d.ts +0 -3
  15. package/lib/Core.js +0 -3
  16. package/lib/DragAndDrop.d.ts +0 -17
  17. package/lib/DragAndDrop.js +0 -110
  18. package/lib/Factory.d.ts +0 -22
  19. package/lib/Factory.js +0 -123
  20. package/lib/FastLayer.d.ts +0 -4
  21. package/lib/FastLayer.js +0 -12
  22. package/lib/Global.d.ts +0 -37
  23. package/lib/Global.js +0 -66
  24. package/lib/Group.d.ts +0 -9
  25. package/lib/Group.js +0 -13
  26. package/lib/Layer.d.ts +0 -62
  27. package/lib/Layer.js +0 -305
  28. package/lib/Node.d.ts +0 -323
  29. package/lib/Node.js +0 -1532
  30. package/lib/PointerEvents.d.ts +0 -11
  31. package/lib/PointerEvents.js +0 -37
  32. package/lib/Shape.d.ts +0 -196
  33. package/lib/Shape.js +0 -548
  34. package/lib/Stage.d.ts +0 -86
  35. package/lib/Stage.js +0 -715
  36. package/lib/Tween.d.ts +0 -90
  37. package/lib/Tween.js +0 -531
  38. package/lib/Util.d.ts +0 -133
  39. package/lib/Util.js +0 -866
  40. package/lib/Validators.d.ts +0 -11
  41. package/lib/Validators.js +0 -178
  42. package/lib/_CoreInternals.d.ts +0 -220
  43. package/lib/_CoreInternals.js +0 -34
  44. package/lib/_FullInternals.d.ts +0 -302
  45. package/lib/_FullInternals.js +0 -80
  46. package/lib/canvas-backend.d.ts +0 -2
  47. package/lib/canvas-backend.js +0 -27
  48. package/lib/filters/Blur.d.ts +0 -2
  49. package/lib/filters/Blur.js +0 -235
  50. package/lib/filters/Brighten.d.ts +0 -2
  51. package/lib/filters/Brighten.js +0 -12
  52. package/lib/filters/Brightness.d.ts +0 -2
  53. package/lib/filters/Brightness.js +0 -8
  54. package/lib/filters/Contrast.d.ts +0 -2
  55. package/lib/filters/Contrast.js +0 -35
  56. package/lib/filters/Emboss.d.ts +0 -2
  57. package/lib/filters/Emboss.js +0 -101
  58. package/lib/filters/Enhance.d.ts +0 -2
  59. package/lib/filters/Enhance.js +0 -84
  60. package/lib/filters/Grayscale.d.ts +0 -2
  61. package/lib/filters/Grayscale.js +0 -9
  62. package/lib/filters/HSL.d.ts +0 -2
  63. package/lib/filters/HSL.js +0 -24
  64. package/lib/filters/HSV.d.ts +0 -2
  65. package/lib/filters/HSV.js +0 -23
  66. package/lib/filters/Invert.d.ts +0 -2
  67. package/lib/filters/Invert.js +0 -8
  68. package/lib/filters/Kaleidoscope.d.ts +0 -2
  69. package/lib/filters/Kaleidoscope.js +0 -133
  70. package/lib/filters/Mask.d.ts +0 -2
  71. package/lib/filters/Mask.js +0 -151
  72. package/lib/filters/Noise.d.ts +0 -2
  73. package/lib/filters/Noise.js +0 -12
  74. package/lib/filters/Pixelate.d.ts +0 -2
  75. package/lib/filters/Pixelate.js +0 -60
  76. package/lib/filters/Posterize.d.ts +0 -2
  77. package/lib/filters/Posterize.js +0 -10
  78. package/lib/filters/RGB.d.ts +0 -2
  79. package/lib/filters/RGB.js +0 -38
  80. package/lib/filters/RGBA.d.ts +0 -2
  81. package/lib/filters/RGBA.js +0 -49
  82. package/lib/filters/Sepia.d.ts +0 -2
  83. package/lib/filters/Sepia.js +0 -11
  84. package/lib/filters/Solarize.d.ts +0 -2
  85. package/lib/filters/Solarize.js +0 -14
  86. package/lib/filters/Threshold.d.ts +0 -2
  87. package/lib/filters/Threshold.js +0 -10
  88. package/lib/index.d.ts +0 -2
  89. package/lib/index.js +0 -2
  90. package/lib/shapes/Arc.d.ts +0 -27
  91. package/lib/shapes/Arc.js +0 -60
  92. package/lib/shapes/Arrow.d.ts +0 -27
  93. package/lib/shapes/Arrow.js +0 -99
  94. package/lib/shapes/Circle.d.ts +0 -15
  95. package/lib/shapes/Circle.js +0 -33
  96. package/lib/shapes/Ellipse.d.ts +0 -18
  97. package/lib/shapes/Ellipse.js +0 -37
  98. package/lib/shapes/Image.d.ts +0 -29
  99. package/lib/shapes/Image.js +0 -131
  100. package/lib/shapes/Label.d.ts +0 -39
  101. package/lib/shapes/Label.js +0 -173
  102. package/lib/shapes/Line.d.ts +0 -29
  103. package/lib/shapes/Line.js +0 -156
  104. package/lib/shapes/Path.d.ts +0 -50
  105. package/lib/shapes/Path.js +0 -670
  106. package/lib/shapes/Rect.d.ts +0 -11
  107. package/lib/shapes/Rect.js +0 -22
  108. package/lib/shapes/RegularPolygon.d.ts +0 -26
  109. package/lib/shapes/RegularPolygon.js +0 -72
  110. package/lib/shapes/Ring.d.ts +0 -17
  111. package/lib/shapes/Ring.js +0 -33
  112. package/lib/shapes/Sprite.d.ts +0 -32
  113. package/lib/shapes/Sprite.js +0 -108
  114. package/lib/shapes/Star.d.ts +0 -19
  115. package/lib/shapes/Star.js +0 -38
  116. package/lib/shapes/Text.d.ts +0 -90
  117. package/lib/shapes/Text.js +0 -460
  118. package/lib/shapes/TextPath.d.ts +0 -72
  119. package/lib/shapes/TextPath.js +0 -268
  120. package/lib/shapes/Transformer.d.ts +0 -130
  121. package/lib/shapes/Transformer.js +0 -1009
  122. package/lib/shapes/Wedge.d.ts +0 -19
  123. package/lib/shapes/Wedge.js +0 -38
  124. package/lib/skia-backend.d.ts +0 -2
  125. package/lib/skia-backend.js +0 -24
  126. package/lib/types.d.ts +0 -56
  127. package/lib/types.js +0 -21
@@ -1,1009 +0,0 @@
1
- import { Util, Transform } from "../Util.js";
2
- import { Factory } from "../Factory.js";
3
- import { Node } from "../Node.js";
4
- import { Shape } from "../Shape.js";
5
- import { Rect } from "./Rect.js";
6
- import { Group } from "../Group.js";
7
- import { Konva } from "../Global.js";
8
- import { getBooleanValidator, getNumberValidator } from "../Validators.js";
9
- import { _registerNode } from "../Global.js";
10
- const EVENTS_NAME = 'tr-konva';
11
- const ATTR_CHANGE_LIST = [
12
- 'resizeEnabledChange',
13
- 'rotateAnchorOffsetChange',
14
- 'rotateEnabledChange',
15
- 'enabledAnchorsChange',
16
- 'anchorSizeChange',
17
- 'borderEnabledChange',
18
- 'borderStrokeChange',
19
- 'borderStrokeWidthChange',
20
- 'borderDashChange',
21
- 'anchorStrokeChange',
22
- 'anchorStrokeWidthChange',
23
- 'anchorFillChange',
24
- 'anchorCornerRadiusChange',
25
- 'ignoreStrokeChange',
26
- 'anchorStyleFuncChange',
27
- ]
28
- .map((e) => e + `.${EVENTS_NAME}`)
29
- .join(' ');
30
- const NODES_RECT = 'nodesRect';
31
- const TRANSFORM_CHANGE_STR = [
32
- 'widthChange',
33
- 'heightChange',
34
- 'scaleXChange',
35
- 'scaleYChange',
36
- 'skewXChange',
37
- 'skewYChange',
38
- 'rotationChange',
39
- 'offsetXChange',
40
- 'offsetYChange',
41
- 'transformsEnabledChange',
42
- 'strokeWidthChange',
43
- 'draggableChange',
44
- ];
45
- const ANGLES = {
46
- 'top-left': -45,
47
- 'top-center': 0,
48
- 'top-right': 45,
49
- 'middle-right': -90,
50
- 'middle-left': 90,
51
- 'bottom-left': -135,
52
- 'bottom-center': 180,
53
- 'bottom-right': 135,
54
- };
55
- const TOUCH_DEVICE = 'ontouchstart' in Konva._global;
56
- function getCursor(anchorName, rad, rotateCursor) {
57
- if (anchorName === 'rotater') {
58
- return rotateCursor;
59
- }
60
- rad += Util.degToRad(ANGLES[anchorName] || 0);
61
- const angle = ((Util.radToDeg(rad) % 360) + 360) % 360;
62
- if (Util._inRange(angle, 315 + 22.5, 360) || Util._inRange(angle, 0, 22.5)) {
63
- return 'ns-resize';
64
- }
65
- else if (Util._inRange(angle, 45 - 22.5, 45 + 22.5)) {
66
- return 'nesw-resize';
67
- }
68
- else if (Util._inRange(angle, 90 - 22.5, 90 + 22.5)) {
69
- return 'ew-resize';
70
- }
71
- else if (Util._inRange(angle, 135 - 22.5, 135 + 22.5)) {
72
- return 'nwse-resize';
73
- }
74
- else if (Util._inRange(angle, 180 - 22.5, 180 + 22.5)) {
75
- return 'ns-resize';
76
- }
77
- else if (Util._inRange(angle, 225 - 22.5, 225 + 22.5)) {
78
- return 'nesw-resize';
79
- }
80
- else if (Util._inRange(angle, 270 - 22.5, 270 + 22.5)) {
81
- return 'ew-resize';
82
- }
83
- else if (Util._inRange(angle, 315 - 22.5, 315 + 22.5)) {
84
- return 'nwse-resize';
85
- }
86
- else {
87
- Util.error('Transformer has unknown angle for cursor detection: ' + angle);
88
- return 'pointer';
89
- }
90
- }
91
- const ANCHORS_NAMES = [
92
- 'top-left',
93
- 'top-center',
94
- 'top-right',
95
- 'middle-right',
96
- 'middle-left',
97
- 'bottom-left',
98
- 'bottom-center',
99
- 'bottom-right',
100
- ];
101
- const MAX_SAFE_INTEGER = 100000000;
102
- function getCenter(shape) {
103
- return {
104
- x: shape.x +
105
- (shape.width / 2) * Math.cos(shape.rotation) +
106
- (shape.height / 2) * Math.sin(-shape.rotation),
107
- y: shape.y +
108
- (shape.height / 2) * Math.cos(shape.rotation) +
109
- (shape.width / 2) * Math.sin(shape.rotation),
110
- };
111
- }
112
- function rotateAroundPoint(shape, angleRad, point) {
113
- const x = point.x +
114
- (shape.x - point.x) * Math.cos(angleRad) -
115
- (shape.y - point.y) * Math.sin(angleRad);
116
- const y = point.y +
117
- (shape.x - point.x) * Math.sin(angleRad) +
118
- (shape.y - point.y) * Math.cos(angleRad);
119
- return {
120
- ...shape,
121
- rotation: shape.rotation + angleRad,
122
- x,
123
- y,
124
- };
125
- }
126
- function rotateAroundCenter(shape, deltaRad) {
127
- const center = getCenter(shape);
128
- return rotateAroundPoint(shape, deltaRad, center);
129
- }
130
- function getSnap(snaps, newRotationRad, tol) {
131
- let snapped = newRotationRad;
132
- for (let i = 0; i < snaps.length; i++) {
133
- const angle = Konva.getAngle(snaps[i]);
134
- const absDiff = Math.abs(angle - newRotationRad) % (Math.PI * 2);
135
- const dif = Math.min(absDiff, Math.PI * 2 - absDiff);
136
- if (dif < tol) {
137
- snapped = angle;
138
- }
139
- }
140
- return snapped;
141
- }
142
- let activeTransformersCount = 0;
143
- export class Transformer extends Group {
144
- constructor(config) {
145
- super(config);
146
- this._movingAnchorName = null;
147
- this._transforming = false;
148
- this._createElements();
149
- this._handleMouseMove = this._handleMouseMove.bind(this);
150
- this._handleMouseUp = this._handleMouseUp.bind(this);
151
- this.update = this.update.bind(this);
152
- this.on(ATTR_CHANGE_LIST, this.update);
153
- if (this.getNode()) {
154
- this.update();
155
- }
156
- }
157
- attachTo(node) {
158
- this.setNode(node);
159
- return this;
160
- }
161
- setNode(node) {
162
- Util.warn('tr.setNode(shape), tr.node(shape) and tr.attachTo(shape) methods are deprecated. Please use tr.nodes(nodesArray) instead.');
163
- return this.setNodes([node]);
164
- }
165
- getNode() {
166
- return this._nodes && this._nodes[0];
167
- }
168
- _getEventNamespace() {
169
- return EVENTS_NAME + this._id;
170
- }
171
- setNodes(nodes = []) {
172
- if (this._nodes && this._nodes.length) {
173
- this.detach();
174
- }
175
- const filteredNodes = nodes.filter((node) => {
176
- if (node.isAncestorOf(this)) {
177
- Util.error('Konva.Transformer cannot be an a child of the node you are trying to attach');
178
- return false;
179
- }
180
- return true;
181
- });
182
- this._nodes = nodes = filteredNodes;
183
- if (nodes.length === 1 && this.useSingleNodeRotation()) {
184
- this.rotation(nodes[0].getAbsoluteRotation());
185
- }
186
- else {
187
- this.rotation(0);
188
- }
189
- this._nodes.forEach((node) => {
190
- const onChange = () => {
191
- if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
192
- this.rotation(this.nodes()[0].getAbsoluteRotation());
193
- }
194
- this._resetTransformCache();
195
- if (!this._transforming && !this.isDragging()) {
196
- this.update();
197
- }
198
- };
199
- if (node._attrsAffectingSize.length) {
200
- const additionalEvents = node._attrsAffectingSize
201
- .map((prop) => prop + 'Change.' + this._getEventNamespace())
202
- .join(' ');
203
- node.on(additionalEvents, onChange);
204
- }
205
- node.on(TRANSFORM_CHANGE_STR.map((e) => e + `.${this._getEventNamespace()}`).join(' '), onChange);
206
- node.on(`absoluteTransformChange.${this._getEventNamespace()}`, onChange);
207
- this._proxyDrag(node);
208
- });
209
- this._resetTransformCache();
210
- const elementsCreated = !!this.findOne('.top-left');
211
- if (elementsCreated) {
212
- this.update();
213
- }
214
- return this;
215
- }
216
- _proxyDrag(node) {
217
- let lastPos;
218
- node.on(`dragstart.${this._getEventNamespace()}`, (e) => {
219
- lastPos = node.getAbsolutePosition();
220
- if (!this.isDragging() && node !== this.findOne('.back')) {
221
- this.startDrag(e, false);
222
- }
223
- });
224
- node.on(`dragmove.${this._getEventNamespace()}`, (e) => {
225
- if (!lastPos) {
226
- return;
227
- }
228
- const abs = node.getAbsolutePosition();
229
- const dx = abs.x - lastPos.x;
230
- const dy = abs.y - lastPos.y;
231
- this.nodes().forEach((otherNode) => {
232
- if (otherNode === node) {
233
- return;
234
- }
235
- if (otherNode.isDragging()) {
236
- return;
237
- }
238
- const otherAbs = otherNode.getAbsolutePosition();
239
- otherNode.setAbsolutePosition({
240
- x: otherAbs.x + dx,
241
- y: otherAbs.y + dy,
242
- });
243
- otherNode.startDrag(e);
244
- });
245
- lastPos = null;
246
- });
247
- }
248
- getNodes() {
249
- return this._nodes || [];
250
- }
251
- getActiveAnchor() {
252
- return this._movingAnchorName;
253
- }
254
- detach() {
255
- if (this._nodes) {
256
- this._nodes.forEach((node) => {
257
- node.off('.' + this._getEventNamespace());
258
- });
259
- }
260
- this._nodes = [];
261
- this._resetTransformCache();
262
- }
263
- _resetTransformCache() {
264
- this._clearCache(NODES_RECT);
265
- this._clearCache('transform');
266
- this._clearSelfAndDescendantCache('absoluteTransform');
267
- }
268
- _getNodeRect() {
269
- return this._getCache(NODES_RECT, this.__getNodeRect);
270
- }
271
- __getNodeShape(node, rot = this.rotation(), relative) {
272
- const rect = node.getClientRect({
273
- skipTransform: true,
274
- skipShadow: true,
275
- skipStroke: this.ignoreStroke(),
276
- });
277
- const absScale = node.getAbsoluteScale(relative);
278
- const absPos = node.getAbsolutePosition(relative);
279
- const dx = rect.x * absScale.x - node.offsetX() * absScale.x;
280
- const dy = rect.y * absScale.y - node.offsetY() * absScale.y;
281
- const rotation = (Konva.getAngle(node.getAbsoluteRotation()) + Math.PI * 2) %
282
- (Math.PI * 2);
283
- const box = {
284
- x: absPos.x + dx * Math.cos(rotation) + dy * Math.sin(-rotation),
285
- y: absPos.y + dy * Math.cos(rotation) + dx * Math.sin(rotation),
286
- width: rect.width * absScale.x,
287
- height: rect.height * absScale.y,
288
- rotation: rotation,
289
- };
290
- return rotateAroundPoint(box, -Konva.getAngle(rot), {
291
- x: 0,
292
- y: 0,
293
- });
294
- }
295
- __getNodeRect() {
296
- const node = this.getNode();
297
- if (!node) {
298
- return {
299
- x: -MAX_SAFE_INTEGER,
300
- y: -MAX_SAFE_INTEGER,
301
- width: 0,
302
- height: 0,
303
- rotation: 0,
304
- };
305
- }
306
- const totalPoints = [];
307
- this.nodes().map((node) => {
308
- const box = node.getClientRect({
309
- skipTransform: true,
310
- skipShadow: true,
311
- skipStroke: this.ignoreStroke(),
312
- });
313
- const points = [
314
- { x: box.x, y: box.y },
315
- { x: box.x + box.width, y: box.y },
316
- { x: box.x + box.width, y: box.y + box.height },
317
- { x: box.x, y: box.y + box.height },
318
- ];
319
- const trans = node.getAbsoluteTransform();
320
- points.forEach(function (point) {
321
- const transformed = trans.point(point);
322
- totalPoints.push(transformed);
323
- });
324
- });
325
- const tr = new Transform();
326
- tr.rotate(-Konva.getAngle(this.rotation()));
327
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
328
- totalPoints.forEach(function (point) {
329
- const transformed = tr.point(point);
330
- if (minX === undefined) {
331
- minX = maxX = transformed.x;
332
- minY = maxY = transformed.y;
333
- }
334
- minX = Math.min(minX, transformed.x);
335
- minY = Math.min(minY, transformed.y);
336
- maxX = Math.max(maxX, transformed.x);
337
- maxY = Math.max(maxY, transformed.y);
338
- });
339
- tr.invert();
340
- const p = tr.point({ x: minX, y: minY });
341
- return {
342
- x: p.x,
343
- y: p.y,
344
- width: maxX - minX,
345
- height: maxY - minY,
346
- rotation: Konva.getAngle(this.rotation()),
347
- };
348
- }
349
- getX() {
350
- return this._getNodeRect().x;
351
- }
352
- getY() {
353
- return this._getNodeRect().y;
354
- }
355
- getWidth() {
356
- return this._getNodeRect().width;
357
- }
358
- getHeight() {
359
- return this._getNodeRect().height;
360
- }
361
- _createElements() {
362
- this._createBack();
363
- ANCHORS_NAMES.forEach((name) => {
364
- this._createAnchor(name);
365
- });
366
- this._createAnchor('rotater');
367
- }
368
- _createAnchor(name) {
369
- const anchor = new Rect({
370
- stroke: 'rgb(0, 161, 255)',
371
- fill: 'white',
372
- strokeWidth: 1,
373
- name: name + ' _anchor',
374
- dragDistance: 0,
375
- draggable: true,
376
- hitStrokeWidth: TOUCH_DEVICE ? 10 : 'auto',
377
- });
378
- const self = this;
379
- anchor.on('mousedown touchstart', function (e) {
380
- self._handleMouseDown(e);
381
- });
382
- anchor.on('dragstart', (e) => {
383
- anchor.stopDrag();
384
- e.cancelBubble = true;
385
- });
386
- anchor.on('dragend', (e) => {
387
- e.cancelBubble = true;
388
- });
389
- anchor.on('mouseenter', () => {
390
- const rad = Konva.getAngle(this.rotation());
391
- const rotateCursor = this.rotateAnchorCursor();
392
- const cursor = getCursor(name, rad, rotateCursor);
393
- anchor.getStage().content &&
394
- (anchor.getStage().content.style.cursor = cursor);
395
- this._cursorChange = true;
396
- });
397
- anchor.on('mouseout', () => {
398
- anchor.getStage().content &&
399
- (anchor.getStage().content.style.cursor = '');
400
- this._cursorChange = false;
401
- });
402
- this.add(anchor);
403
- }
404
- _createBack() {
405
- const back = new Shape({
406
- name: 'back',
407
- width: 0,
408
- height: 0,
409
- sceneFunc(ctx, shape) {
410
- const tr = shape.getParent();
411
- const padding = tr.padding();
412
- ctx.beginPath();
413
- ctx.rect(-padding, -padding, shape.width() + padding * 2, shape.height() + padding * 2);
414
- ctx.moveTo(shape.width() / 2, -padding);
415
- if (tr.rotateEnabled() && tr.rotateLineVisible()) {
416
- ctx.lineTo(shape.width() / 2, -tr.rotateAnchorOffset() * Util._sign(shape.height()) - padding);
417
- }
418
- ctx.fillStrokeShape(shape);
419
- },
420
- hitFunc: (ctx, shape) => {
421
- if (!this.shouldOverdrawWholeArea()) {
422
- return;
423
- }
424
- const padding = this.padding();
425
- ctx.beginPath();
426
- ctx.rect(-padding, -padding, shape.width() + padding * 2, shape.height() + padding * 2);
427
- ctx.fillStrokeShape(shape);
428
- },
429
- });
430
- this.add(back);
431
- this._proxyDrag(back);
432
- back.on('dragstart', (e) => {
433
- e.cancelBubble = true;
434
- });
435
- back.on('dragmove', (e) => {
436
- e.cancelBubble = true;
437
- });
438
- back.on('dragend', (e) => {
439
- e.cancelBubble = true;
440
- });
441
- this.on('dragmove', (e) => {
442
- this.update();
443
- });
444
- }
445
- _handleMouseDown(e) {
446
- if (this._transforming) {
447
- return;
448
- }
449
- this._movingAnchorName = e.target.name().split(' ')[0];
450
- const attrs = this._getNodeRect();
451
- const width = attrs.width;
452
- const height = attrs.height;
453
- const hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
454
- this.sin = Math.abs(height / hypotenuse);
455
- this.cos = Math.abs(width / hypotenuse);
456
- if (typeof window !== 'undefined') {
457
- window.addEventListener('mousemove', this._handleMouseMove);
458
- window.addEventListener('touchmove', this._handleMouseMove);
459
- window.addEventListener('mouseup', this._handleMouseUp, true);
460
- window.addEventListener('touchend', this._handleMouseUp, true);
461
- }
462
- this._transforming = true;
463
- const ap = e.target.getAbsolutePosition();
464
- const pos = e.target.getStage().getPointerPosition();
465
- this._anchorDragOffset = {
466
- x: pos.x - ap.x,
467
- y: pos.y - ap.y,
468
- };
469
- activeTransformersCount++;
470
- this._fire('transformstart', { evt: e.evt, target: this.getNode() });
471
- this._nodes.forEach((target) => {
472
- target._fire('transformstart', { evt: e.evt, target });
473
- });
474
- }
475
- _handleMouseMove(e) {
476
- let x, y, newHypotenuse;
477
- const anchorNode = this.findOne('.' + this._movingAnchorName);
478
- const stage = anchorNode.getStage();
479
- stage.setPointersPositions(e);
480
- const pp = stage.getPointerPosition();
481
- let newNodePos = {
482
- x: pp.x - this._anchorDragOffset.x,
483
- y: pp.y - this._anchorDragOffset.y,
484
- };
485
- const oldAbs = anchorNode.getAbsolutePosition();
486
- if (this.anchorDragBoundFunc()) {
487
- newNodePos = this.anchorDragBoundFunc()(oldAbs, newNodePos, e);
488
- }
489
- anchorNode.setAbsolutePosition(newNodePos);
490
- const newAbs = anchorNode.getAbsolutePosition();
491
- if (oldAbs.x === newAbs.x && oldAbs.y === newAbs.y) {
492
- return;
493
- }
494
- if (this._movingAnchorName === 'rotater') {
495
- const attrs = this._getNodeRect();
496
- x = anchorNode.x() - attrs.width / 2;
497
- y = -anchorNode.y() + attrs.height / 2;
498
- let delta = Math.atan2(-y, x) + Math.PI / 2;
499
- if (attrs.height < 0) {
500
- delta -= Math.PI;
501
- }
502
- const oldRotation = Konva.getAngle(this.rotation());
503
- const newRotation = oldRotation + delta;
504
- const tol = Konva.getAngle(this.rotationSnapTolerance());
505
- const snappedRot = getSnap(this.rotationSnaps(), newRotation, tol);
506
- const diff = snappedRot - attrs.rotation;
507
- const shape = rotateAroundCenter(attrs, diff);
508
- this._fitNodesInto(shape, e);
509
- return;
510
- }
511
- const shiftBehavior = this.shiftBehavior();
512
- let keepProportion;
513
- if (shiftBehavior === 'inverted') {
514
- keepProportion = this.keepRatio() && !e.shiftKey;
515
- }
516
- else if (shiftBehavior === 'none') {
517
- keepProportion = this.keepRatio();
518
- }
519
- else {
520
- keepProportion = this.keepRatio() || e.shiftKey;
521
- }
522
- let centeredScaling = this.centeredScaling() || e.altKey;
523
- if (this._movingAnchorName === 'top-left') {
524
- if (keepProportion) {
525
- const comparePoint = centeredScaling
526
- ? {
527
- x: this.width() / 2,
528
- y: this.height() / 2,
529
- }
530
- : {
531
- x: this.findOne('.bottom-right').x(),
532
- y: this.findOne('.bottom-right').y(),
533
- };
534
- newHypotenuse = Math.sqrt(Math.pow(comparePoint.x - anchorNode.x(), 2) +
535
- Math.pow(comparePoint.y - anchorNode.y(), 2));
536
- const reverseX = this.findOne('.top-left').x() > comparePoint.x ? -1 : 1;
537
- const reverseY = this.findOne('.top-left').y() > comparePoint.y ? -1 : 1;
538
- x = newHypotenuse * this.cos * reverseX;
539
- y = newHypotenuse * this.sin * reverseY;
540
- this.findOne('.top-left').x(comparePoint.x - x);
541
- this.findOne('.top-left').y(comparePoint.y - y);
542
- }
543
- }
544
- else if (this._movingAnchorName === 'top-center') {
545
- this.findOne('.top-left').y(anchorNode.y());
546
- }
547
- else if (this._movingAnchorName === 'top-right') {
548
- if (keepProportion) {
549
- const comparePoint = centeredScaling
550
- ? {
551
- x: this.width() / 2,
552
- y: this.height() / 2,
553
- }
554
- : {
555
- x: this.findOne('.bottom-left').x(),
556
- y: this.findOne('.bottom-left').y(),
557
- };
558
- newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - comparePoint.x, 2) +
559
- Math.pow(comparePoint.y - anchorNode.y(), 2));
560
- const reverseX = this.findOne('.top-right').x() < comparePoint.x ? -1 : 1;
561
- const reverseY = this.findOne('.top-right').y() > comparePoint.y ? -1 : 1;
562
- x = newHypotenuse * this.cos * reverseX;
563
- y = newHypotenuse * this.sin * reverseY;
564
- this.findOne('.top-right').x(comparePoint.x + x);
565
- this.findOne('.top-right').y(comparePoint.y - y);
566
- }
567
- var pos = anchorNode.position();
568
- this.findOne('.top-left').y(pos.y);
569
- this.findOne('.bottom-right').x(pos.x);
570
- }
571
- else if (this._movingAnchorName === 'middle-left') {
572
- this.findOne('.top-left').x(anchorNode.x());
573
- }
574
- else if (this._movingAnchorName === 'middle-right') {
575
- this.findOne('.bottom-right').x(anchorNode.x());
576
- }
577
- else if (this._movingAnchorName === 'bottom-left') {
578
- if (keepProportion) {
579
- const comparePoint = centeredScaling
580
- ? {
581
- x: this.width() / 2,
582
- y: this.height() / 2,
583
- }
584
- : {
585
- x: this.findOne('.top-right').x(),
586
- y: this.findOne('.top-right').y(),
587
- };
588
- newHypotenuse = Math.sqrt(Math.pow(comparePoint.x - anchorNode.x(), 2) +
589
- Math.pow(anchorNode.y() - comparePoint.y, 2));
590
- const reverseX = comparePoint.x < anchorNode.x() ? -1 : 1;
591
- const reverseY = anchorNode.y() < comparePoint.y ? -1 : 1;
592
- x = newHypotenuse * this.cos * reverseX;
593
- y = newHypotenuse * this.sin * reverseY;
594
- anchorNode.x(comparePoint.x - x);
595
- anchorNode.y(comparePoint.y + y);
596
- }
597
- pos = anchorNode.position();
598
- this.findOne('.top-left').x(pos.x);
599
- this.findOne('.bottom-right').y(pos.y);
600
- }
601
- else if (this._movingAnchorName === 'bottom-center') {
602
- this.findOne('.bottom-right').y(anchorNode.y());
603
- }
604
- else if (this._movingAnchorName === 'bottom-right') {
605
- if (keepProportion) {
606
- const comparePoint = centeredScaling
607
- ? {
608
- x: this.width() / 2,
609
- y: this.height() / 2,
610
- }
611
- : {
612
- x: this.findOne('.top-left').x(),
613
- y: this.findOne('.top-left').y(),
614
- };
615
- newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - comparePoint.x, 2) +
616
- Math.pow(anchorNode.y() - comparePoint.y, 2));
617
- const reverseX = this.findOne('.bottom-right').x() < comparePoint.x ? -1 : 1;
618
- const reverseY = this.findOne('.bottom-right').y() < comparePoint.y ? -1 : 1;
619
- x = newHypotenuse * this.cos * reverseX;
620
- y = newHypotenuse * this.sin * reverseY;
621
- this.findOne('.bottom-right').x(comparePoint.x + x);
622
- this.findOne('.bottom-right').y(comparePoint.y + y);
623
- }
624
- }
625
- else {
626
- console.error(new Error('Wrong position argument of selection resizer: ' +
627
- this._movingAnchorName));
628
- }
629
- centeredScaling = this.centeredScaling() || e.altKey;
630
- if (centeredScaling) {
631
- const topLeft = this.findOne('.top-left');
632
- const bottomRight = this.findOne('.bottom-right');
633
- const topOffsetX = topLeft.x();
634
- const topOffsetY = topLeft.y();
635
- const bottomOffsetX = this.getWidth() - bottomRight.x();
636
- const bottomOffsetY = this.getHeight() - bottomRight.y();
637
- bottomRight.move({
638
- x: -topOffsetX,
639
- y: -topOffsetY,
640
- });
641
- topLeft.move({
642
- x: bottomOffsetX,
643
- y: bottomOffsetY,
644
- });
645
- }
646
- const absPos = this.findOne('.top-left').getAbsolutePosition();
647
- x = absPos.x;
648
- y = absPos.y;
649
- const width = this.findOne('.bottom-right').x() - this.findOne('.top-left').x();
650
- const height = this.findOne('.bottom-right').y() - this.findOne('.top-left').y();
651
- this._fitNodesInto({
652
- x: x,
653
- y: y,
654
- width: width,
655
- height: height,
656
- rotation: Konva.getAngle(this.rotation()),
657
- }, e);
658
- }
659
- _handleMouseUp(e) {
660
- this._removeEvents(e);
661
- }
662
- getAbsoluteTransform() {
663
- return this.getTransform();
664
- }
665
- _removeEvents(e) {
666
- var _a;
667
- if (this._transforming) {
668
- this._transforming = false;
669
- if (typeof window !== 'undefined') {
670
- window.removeEventListener('mousemove', this._handleMouseMove);
671
- window.removeEventListener('touchmove', this._handleMouseMove);
672
- window.removeEventListener('mouseup', this._handleMouseUp, true);
673
- window.removeEventListener('touchend', this._handleMouseUp, true);
674
- }
675
- const node = this.getNode();
676
- activeTransformersCount--;
677
- this._fire('transformend', { evt: e, target: node });
678
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
679
- if (node) {
680
- this._nodes.forEach((target) => {
681
- var _a;
682
- target._fire('transformend', { evt: e, target });
683
- (_a = target.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
684
- });
685
- }
686
- this._movingAnchorName = null;
687
- }
688
- }
689
- _fitNodesInto(newAttrs, evt) {
690
- const oldAttrs = this._getNodeRect();
691
- const minSize = 1;
692
- if (Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize)) {
693
- this.update();
694
- return;
695
- }
696
- if (Util._inRange(newAttrs.height, -this.padding() * 2 - minSize, minSize)) {
697
- this.update();
698
- return;
699
- }
700
- const t = new Transform();
701
- t.rotate(Konva.getAngle(this.rotation()));
702
- if (this._movingAnchorName &&
703
- newAttrs.width < 0 &&
704
- this._movingAnchorName.indexOf('left') >= 0) {
705
- const offset = t.point({
706
- x: -this.padding() * 2,
707
- y: 0,
708
- });
709
- newAttrs.x += offset.x;
710
- newAttrs.y += offset.y;
711
- newAttrs.width += this.padding() * 2;
712
- this._movingAnchorName = this._movingAnchorName.replace('left', 'right');
713
- this._anchorDragOffset.x -= offset.x;
714
- this._anchorDragOffset.y -= offset.y;
715
- }
716
- else if (this._movingAnchorName &&
717
- newAttrs.width < 0 &&
718
- this._movingAnchorName.indexOf('right') >= 0) {
719
- const offset = t.point({
720
- x: this.padding() * 2,
721
- y: 0,
722
- });
723
- this._movingAnchorName = this._movingAnchorName.replace('right', 'left');
724
- this._anchorDragOffset.x -= offset.x;
725
- this._anchorDragOffset.y -= offset.y;
726
- newAttrs.width += this.padding() * 2;
727
- }
728
- if (this._movingAnchorName &&
729
- newAttrs.height < 0 &&
730
- this._movingAnchorName.indexOf('top') >= 0) {
731
- const offset = t.point({
732
- x: 0,
733
- y: -this.padding() * 2,
734
- });
735
- newAttrs.x += offset.x;
736
- newAttrs.y += offset.y;
737
- this._movingAnchorName = this._movingAnchorName.replace('top', 'bottom');
738
- this._anchorDragOffset.x -= offset.x;
739
- this._anchorDragOffset.y -= offset.y;
740
- newAttrs.height += this.padding() * 2;
741
- }
742
- else if (this._movingAnchorName &&
743
- newAttrs.height < 0 &&
744
- this._movingAnchorName.indexOf('bottom') >= 0) {
745
- const offset = t.point({
746
- x: 0,
747
- y: this.padding() * 2,
748
- });
749
- this._movingAnchorName = this._movingAnchorName.replace('bottom', 'top');
750
- this._anchorDragOffset.x -= offset.x;
751
- this._anchorDragOffset.y -= offset.y;
752
- newAttrs.height += this.padding() * 2;
753
- }
754
- if (this.boundBoxFunc()) {
755
- const bounded = this.boundBoxFunc()(oldAttrs, newAttrs);
756
- if (bounded) {
757
- newAttrs = bounded;
758
- }
759
- else {
760
- Util.warn('boundBoxFunc returned falsy. You should return new bound rect from it!');
761
- }
762
- }
763
- const baseSize = 10000000;
764
- const oldTr = new Transform();
765
- oldTr.translate(oldAttrs.x, oldAttrs.y);
766
- oldTr.rotate(oldAttrs.rotation);
767
- oldTr.scale(oldAttrs.width / baseSize, oldAttrs.height / baseSize);
768
- const newTr = new Transform();
769
- const newScaleX = newAttrs.width / baseSize;
770
- const newScaleY = newAttrs.height / baseSize;
771
- if (this.flipEnabled() === false) {
772
- newTr.translate(newAttrs.x, newAttrs.y);
773
- newTr.rotate(newAttrs.rotation);
774
- newTr.translate(newAttrs.width < 0 ? newAttrs.width : 0, newAttrs.height < 0 ? newAttrs.height : 0);
775
- newTr.scale(Math.abs(newScaleX), Math.abs(newScaleY));
776
- }
777
- else {
778
- newTr.translate(newAttrs.x, newAttrs.y);
779
- newTr.rotate(newAttrs.rotation);
780
- newTr.scale(newScaleX, newScaleY);
781
- }
782
- const delta = newTr.multiply(oldTr.invert());
783
- this._nodes.forEach((node) => {
784
- var _a;
785
- if (!node.getStage()) {
786
- return;
787
- }
788
- const parentTransform = node.getParent().getAbsoluteTransform();
789
- const localTransform = node.getTransform().copy();
790
- localTransform.translate(node.offsetX(), node.offsetY());
791
- const newLocalTransform = new Transform();
792
- newLocalTransform
793
- .multiply(parentTransform.copy().invert())
794
- .multiply(delta)
795
- .multiply(parentTransform)
796
- .multiply(localTransform);
797
- const attrs = newLocalTransform.decompose();
798
- node.setAttrs(attrs);
799
- (_a = node.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
800
- });
801
- this.rotation(Util._getRotation(newAttrs.rotation));
802
- this._nodes.forEach((node) => {
803
- this._fire('transform', { evt: evt, target: node });
804
- node._fire('transform', { evt: evt, target: node });
805
- });
806
- this._resetTransformCache();
807
- this.update();
808
- this.getLayer().batchDraw();
809
- }
810
- forceUpdate() {
811
- this._resetTransformCache();
812
- this.update();
813
- }
814
- _batchChangeChild(selector, attrs) {
815
- const anchor = this.findOne(selector);
816
- anchor.setAttrs(attrs);
817
- }
818
- update() {
819
- var _a;
820
- const attrs = this._getNodeRect();
821
- this.rotation(Util._getRotation(attrs.rotation));
822
- const width = attrs.width;
823
- const height = attrs.height;
824
- const enabledAnchors = this.enabledAnchors();
825
- const resizeEnabled = this.resizeEnabled();
826
- const padding = this.padding();
827
- const anchorSize = this.anchorSize();
828
- const anchors = this.find('._anchor');
829
- anchors.forEach((node) => {
830
- node.setAttrs({
831
- width: anchorSize,
832
- height: anchorSize,
833
- offsetX: anchorSize / 2,
834
- offsetY: anchorSize / 2,
835
- stroke: this.anchorStroke(),
836
- strokeWidth: this.anchorStrokeWidth(),
837
- fill: this.anchorFill(),
838
- cornerRadius: this.anchorCornerRadius(),
839
- });
840
- });
841
- this._batchChangeChild('.top-left', {
842
- x: 0,
843
- y: 0,
844
- offsetX: anchorSize / 2 + padding,
845
- offsetY: anchorSize / 2 + padding,
846
- visible: resizeEnabled && enabledAnchors.indexOf('top-left') >= 0,
847
- });
848
- this._batchChangeChild('.top-center', {
849
- x: width / 2,
850
- y: 0,
851
- offsetY: anchorSize / 2 + padding,
852
- visible: resizeEnabled && enabledAnchors.indexOf('top-center') >= 0,
853
- });
854
- this._batchChangeChild('.top-right', {
855
- x: width,
856
- y: 0,
857
- offsetX: anchorSize / 2 - padding,
858
- offsetY: anchorSize / 2 + padding,
859
- visible: resizeEnabled && enabledAnchors.indexOf('top-right') >= 0,
860
- });
861
- this._batchChangeChild('.middle-left', {
862
- x: 0,
863
- y: height / 2,
864
- offsetX: anchorSize / 2 + padding,
865
- visible: resizeEnabled && enabledAnchors.indexOf('middle-left') >= 0,
866
- });
867
- this._batchChangeChild('.middle-right', {
868
- x: width,
869
- y: height / 2,
870
- offsetX: anchorSize / 2 - padding,
871
- visible: resizeEnabled && enabledAnchors.indexOf('middle-right') >= 0,
872
- });
873
- this._batchChangeChild('.bottom-left', {
874
- x: 0,
875
- y: height,
876
- offsetX: anchorSize / 2 + padding,
877
- offsetY: anchorSize / 2 - padding,
878
- visible: resizeEnabled && enabledAnchors.indexOf('bottom-left') >= 0,
879
- });
880
- this._batchChangeChild('.bottom-center', {
881
- x: width / 2,
882
- y: height,
883
- offsetY: anchorSize / 2 - padding,
884
- visible: resizeEnabled && enabledAnchors.indexOf('bottom-center') >= 0,
885
- });
886
- this._batchChangeChild('.bottom-right', {
887
- x: width,
888
- y: height,
889
- offsetX: anchorSize / 2 - padding,
890
- offsetY: anchorSize / 2 - padding,
891
- visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
892
- });
893
- this._batchChangeChild('.rotater', {
894
- x: width / 2,
895
- y: -this.rotateAnchorOffset() * Util._sign(height) - padding,
896
- visible: this.rotateEnabled(),
897
- });
898
- this._batchChangeChild('.back', {
899
- width: width,
900
- height: height,
901
- visible: this.borderEnabled(),
902
- stroke: this.borderStroke(),
903
- strokeWidth: this.borderStrokeWidth(),
904
- dash: this.borderDash(),
905
- draggable: this.nodes().some((node) => node.draggable()),
906
- x: 0,
907
- y: 0,
908
- });
909
- const styleFunc = this.anchorStyleFunc();
910
- if (styleFunc) {
911
- anchors.forEach((node) => {
912
- styleFunc(node);
913
- });
914
- }
915
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
916
- }
917
- isTransforming() {
918
- return this._transforming;
919
- }
920
- stopTransform() {
921
- if (this._transforming) {
922
- this._removeEvents();
923
- const anchorNode = this.findOne('.' + this._movingAnchorName);
924
- if (anchorNode) {
925
- anchorNode.stopDrag();
926
- }
927
- }
928
- }
929
- destroy() {
930
- if (this.getStage() && this._cursorChange) {
931
- this.getStage().content && (this.getStage().content.style.cursor = '');
932
- }
933
- Group.prototype.destroy.call(this);
934
- this.detach();
935
- this._removeEvents();
936
- return this;
937
- }
938
- toObject() {
939
- return Node.prototype.toObject.call(this);
940
- }
941
- clone(obj) {
942
- const node = Node.prototype.clone.call(this, obj);
943
- return node;
944
- }
945
- getClientRect() {
946
- if (this.nodes().length > 0) {
947
- return super.getClientRect();
948
- }
949
- else {
950
- return { x: 0, y: 0, width: 0, height: 0 };
951
- }
952
- }
953
- }
954
- Transformer.isTransforming = () => {
955
- return activeTransformersCount > 0;
956
- };
957
- function validateAnchors(val) {
958
- if (!(val instanceof Array)) {
959
- Util.warn('enabledAnchors value should be an array');
960
- }
961
- if (val instanceof Array) {
962
- val.forEach(function (name) {
963
- if (ANCHORS_NAMES.indexOf(name) === -1) {
964
- Util.warn('Unknown anchor name: ' +
965
- name +
966
- '. Available names are: ' +
967
- ANCHORS_NAMES.join(', '));
968
- }
969
- });
970
- }
971
- return val || [];
972
- }
973
- Transformer.prototype.className = 'Transformer';
974
- _registerNode(Transformer);
975
- Factory.addGetterSetter(Transformer, 'enabledAnchors', ANCHORS_NAMES, validateAnchors);
976
- Factory.addGetterSetter(Transformer, 'flipEnabled', true, getBooleanValidator());
977
- Factory.addGetterSetter(Transformer, 'resizeEnabled', true);
978
- Factory.addGetterSetter(Transformer, 'anchorSize', 10, getNumberValidator());
979
- Factory.addGetterSetter(Transformer, 'rotateEnabled', true);
980
- Factory.addGetterSetter(Transformer, 'rotateLineVisible', true);
981
- Factory.addGetterSetter(Transformer, 'rotationSnaps', []);
982
- Factory.addGetterSetter(Transformer, 'rotateAnchorOffset', 50, getNumberValidator());
983
- Factory.addGetterSetter(Transformer, 'rotateAnchorCursor', 'crosshair');
984
- Factory.addGetterSetter(Transformer, 'rotationSnapTolerance', 5, getNumberValidator());
985
- Factory.addGetterSetter(Transformer, 'borderEnabled', true);
986
- Factory.addGetterSetter(Transformer, 'anchorStroke', 'rgb(0, 161, 255)');
987
- Factory.addGetterSetter(Transformer, 'anchorStrokeWidth', 1, getNumberValidator());
988
- Factory.addGetterSetter(Transformer, 'anchorFill', 'white');
989
- Factory.addGetterSetter(Transformer, 'anchorCornerRadius', 0, getNumberValidator());
990
- Factory.addGetterSetter(Transformer, 'borderStroke', 'rgb(0, 161, 255)');
991
- Factory.addGetterSetter(Transformer, 'borderStrokeWidth', 1, getNumberValidator());
992
- Factory.addGetterSetter(Transformer, 'borderDash');
993
- Factory.addGetterSetter(Transformer, 'keepRatio', true);
994
- Factory.addGetterSetter(Transformer, 'shiftBehavior', 'default');
995
- Factory.addGetterSetter(Transformer, 'centeredScaling', false);
996
- Factory.addGetterSetter(Transformer, 'ignoreStroke', false);
997
- Factory.addGetterSetter(Transformer, 'padding', 0, getNumberValidator());
998
- Factory.addGetterSetter(Transformer, 'nodes');
999
- Factory.addGetterSetter(Transformer, 'node');
1000
- Factory.addGetterSetter(Transformer, 'boundBoxFunc');
1001
- Factory.addGetterSetter(Transformer, 'anchorDragBoundFunc');
1002
- Factory.addGetterSetter(Transformer, 'anchorStyleFunc');
1003
- Factory.addGetterSetter(Transformer, 'shouldOverdrawWholeArea', false);
1004
- Factory.addGetterSetter(Transformer, 'useSingleNodeRotation', true);
1005
- Factory.backCompat(Transformer, {
1006
- lineEnabled: 'borderEnabled',
1007
- rotateHandlerOffset: 'rotateAnchorOffset',
1008
- enabledHandlers: 'enabledAnchors',
1009
- });