konva 4.2.0 → 5.0.1

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.
@@ -12,6 +12,17 @@ var __extends = (this && this.__extends) || (function () {
12
12
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
13
  };
14
14
  })();
15
+ var __assign = (this && this.__assign) || function () {
16
+ __assign = Object.assign || function(t) {
17
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
18
+ s = arguments[i];
19
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
+ t[p] = s[p];
21
+ }
22
+ return t;
23
+ };
24
+ return __assign.apply(this, arguments);
25
+ };
15
26
  Object.defineProperty(exports, "__esModule", { value: true });
16
27
  var Util_1 = require("../Util");
17
28
  var Factory_1 = require("../Factory");
@@ -41,7 +52,7 @@ var ATTR_CHANGE_LIST = [
41
52
  ]
42
53
  .map(function (e) { return e + ("." + EVENTS_NAME); })
43
54
  .join(' ');
44
- var NODE_RECT = 'nodeRect';
55
+ var NODES_RECT = 'nodesRect';
45
56
  var TRANSFORM_CHANGE_STR = [
46
57
  'widthChange',
47
58
  'heightChange',
@@ -68,14 +79,11 @@ var ANGLES = {
68
79
  'bottom-right': 135
69
80
  };
70
81
  var TOUCH_DEVICE = 'ontouchstart' in Global_1.Konva._global;
71
- function getCursor(anchorName, rad, isMirrored) {
82
+ function getCursor(anchorName, rad) {
72
83
  if (anchorName === 'rotater') {
73
84
  return 'crosshair';
74
85
  }
75
86
  rad += Util_1.Util._degToRad(ANGLES[anchorName] || 0);
76
- if (isMirrored) {
77
- rad *= -1;
78
- }
79
87
  var angle = ((Util_1.Util._radToDeg(rad) % 360) + 360) % 360;
80
88
  if (Util_1.Util._inRange(angle, 315 + 22.5, 360) || Util_1.Util._inRange(angle, 0, 22.5)) {
81
89
  return 'ns-resize';
@@ -117,6 +125,114 @@ var ANCHORS_NAMES = [
117
125
  'bottom-right'
118
126
  ];
119
127
  var MAX_SAFE_INTEGER = 100000000;
128
+ function getCenter(shape) {
129
+ return {
130
+ x: shape.x +
131
+ (shape.width / 2) * Math.cos(shape.rotation) +
132
+ (shape.height / 2) * Math.sin(-shape.rotation),
133
+ y: shape.y +
134
+ (shape.height / 2) * Math.cos(shape.rotation) +
135
+ (shape.width / 2) * Math.sin(shape.rotation)
136
+ };
137
+ }
138
+ function rotateAroundPoint(shape, angleRad, point) {
139
+ var x = point.x +
140
+ (shape.x - point.x) * Math.cos(angleRad) -
141
+ (shape.y - point.y) * Math.sin(angleRad);
142
+ var y = point.y +
143
+ (shape.x - point.x) * Math.sin(angleRad) +
144
+ (shape.y - point.y) * Math.cos(angleRad);
145
+ return __assign(__assign({}, shape), { rotation: shape.rotation + angleRad, x: x,
146
+ y: y });
147
+ }
148
+ function rotateAroundCenter(shape, deltaRad) {
149
+ var center = getCenter(shape);
150
+ return rotateAroundPoint(shape, deltaRad, center);
151
+ }
152
+ function getShapeRect(shape) {
153
+ var angleRad = shape.rotation;
154
+ var x1 = shape.x;
155
+ var y1 = shape.y;
156
+ var x2 = x1 + shape.width * Math.cos(angleRad);
157
+ var y2 = y1 + shape.width * Math.sin(angleRad);
158
+ var x3 = shape.x +
159
+ shape.width * Math.cos(angleRad) +
160
+ shape.height * Math.sin(-angleRad);
161
+ var y3 = shape.y +
162
+ shape.height * Math.cos(angleRad) +
163
+ shape.width * Math.sin(angleRad);
164
+ var x4 = shape.x + shape.height * Math.sin(-angleRad);
165
+ var y4 = shape.y + shape.height * Math.cos(angleRad);
166
+ var leftX = Math.min(x1, x2, x3, x4);
167
+ var rightX = Math.max(x1, x2, x3, x4);
168
+ var topY = Math.min(y1, y2, y3, y4);
169
+ var bottomY = Math.max(y1, y2, y3, y4);
170
+ return {
171
+ x: leftX,
172
+ y: topY,
173
+ width: rightX - leftX,
174
+ height: bottomY - topY
175
+ };
176
+ }
177
+ function getShapesRect(shapes) {
178
+ var x1 = 9999999999;
179
+ var y1 = 9999999999;
180
+ var x2 = -999999999;
181
+ var y2 = -999999999;
182
+ shapes.forEach(function (shape) {
183
+ var rect = getShapeRect(shape);
184
+ x1 = Math.min(x1, rect.x);
185
+ y1 = Math.min(y1, rect.y);
186
+ x2 = Math.max(x2, rect.x + rect.width);
187
+ y2 = Math.max(y2, rect.y + rect.height);
188
+ });
189
+ return {
190
+ x: x1,
191
+ y: y1,
192
+ width: x2 - x1,
193
+ height: y2 - y1,
194
+ rotation: 0
195
+ };
196
+ }
197
+ function transformShape(shape, oldSelection, newSelection, keepOffset) {
198
+ if (keepOffset === void 0) { keepOffset = 1; }
199
+ var offset = rotateAroundPoint(shape, -oldSelection.rotation, {
200
+ x: oldSelection.x,
201
+ y: oldSelection.y
202
+ });
203
+ var offsetX = offset.x - oldSelection.x;
204
+ var offsetY = offset.y - oldSelection.y;
205
+ var angle = oldSelection.rotation;
206
+ var scaleX = shape.width ? newSelection.width / oldSelection.width : 1;
207
+ var scaleY = shape.height ? newSelection.height / oldSelection.height : 1;
208
+ return {
209
+ x: keepOffset * newSelection.x +
210
+ offsetX * scaleX * Math.cos(angle) +
211
+ offsetY * scaleY * Math.sin(-angle),
212
+ y: keepOffset * newSelection.y +
213
+ offsetX * scaleX * Math.sin(angle) +
214
+ offsetY * scaleY * Math.cos(angle),
215
+ width: shape.width * scaleX,
216
+ height: shape.height * scaleY,
217
+ rotation: shape.rotation
218
+ };
219
+ }
220
+ function transformAndRotateShape(shape, oldSelection, newSelection) {
221
+ var updated = transformShape(shape, oldSelection, newSelection);
222
+ return rotateAroundPoint(updated, newSelection.rotation - oldSelection.rotation, newSelection);
223
+ }
224
+ function getSnap(snaps, newRotationRad, tol) {
225
+ var snapped = newRotationRad;
226
+ for (var i = 0; i < snaps.length; i++) {
227
+ var angle = Global_1.Konva.getAngle(snaps[i]);
228
+ var absDiff = Math.abs(angle - newRotationRad) % (Math.PI * 2);
229
+ var dif = Math.min(absDiff, Math.PI * 2 - absDiff);
230
+ if (dif < tol) {
231
+ snapped = angle;
232
+ }
233
+ }
234
+ return snapped;
235
+ }
120
236
  var Transformer = (function (_super) {
121
237
  __extends(Transformer, _super);
122
238
  function Transformer(config) {
@@ -137,34 +253,80 @@ var Transformer = (function (_super) {
137
253
  return this;
138
254
  };
139
255
  Transformer.prototype.setNode = function (node) {
256
+ Util_1.Util.warn('tr.setNode(shape), tr.node(shape) and tr.attachTo(shape) methods are deprecated. Please use tr.nodes(nodesArray) instead.');
257
+ return this.setNodes([node]);
258
+ };
259
+ Transformer.prototype.getNode = function () {
260
+ return this._nodes && this._nodes[0];
261
+ };
262
+ Transformer.prototype.setNodes = function (nodes) {
140
263
  var _this = this;
141
- if (this._node) {
264
+ if (nodes === void 0) { nodes = []; }
265
+ if (this._nodes && this._nodes.length) {
142
266
  this.detach();
143
267
  }
144
- this._node = node;
145
- this._resetTransformCache();
146
- var additionalEvents = node._attrsAffectingSize
147
- .map(function (prop) { return prop + 'Change.' + EVENTS_NAME; })
148
- .join(' ');
149
- var onChange = function () {
150
- _this._resetTransformCache();
151
- if (!_this._transforming) {
152
- _this.update();
153
- }
154
- };
155
- node.on(additionalEvents, onChange);
156
- node.on(TRANSFORM_CHANGE_STR, onChange);
157
- node.on("xChange." + EVENTS_NAME + " yChange." + EVENTS_NAME, function () {
158
- return _this._resetTransformCache();
268
+ this._nodes = nodes;
269
+ if (nodes.length === 1) {
270
+ this.rotation(nodes[0].rotation());
271
+ }
272
+ else {
273
+ this.rotation(0);
274
+ }
275
+ this._nodes.forEach(function (node) {
276
+ var additionalEvents = node._attrsAffectingSize
277
+ .map(function (prop) { return prop + 'Change.' + EVENTS_NAME; })
278
+ .join(' ');
279
+ var onChange = function () {
280
+ _this._resetTransformCache();
281
+ if (!_this._transforming) {
282
+ _this.update();
283
+ }
284
+ };
285
+ node.on(additionalEvents, onChange);
286
+ node.on(TRANSFORM_CHANGE_STR, onChange);
287
+ node.on("_clearTransformCache." + EVENTS_NAME, onChange);
288
+ node.on("xChange." + EVENTS_NAME + " yChange." + EVENTS_NAME, onChange);
289
+ _this._proxyDrag(node);
159
290
  });
291
+ this._resetTransformCache();
160
292
  var elementsCreated = !!this.findOne('.top-left');
161
293
  if (elementsCreated) {
162
294
  this.update();
163
295
  }
164
296
  return this;
165
297
  };
166
- Transformer.prototype.getNode = function () {
167
- return this._node;
298
+ Transformer.prototype._proxyDrag = function (node) {
299
+ var _this = this;
300
+ var lastPos;
301
+ node.on("dragstart." + EVENTS_NAME, function () {
302
+ lastPos = node.getAbsolutePosition();
303
+ });
304
+ node.on("dragmove." + EVENTS_NAME, function () {
305
+ if (!lastPos) {
306
+ return;
307
+ }
308
+ var abs = node.getAbsolutePosition();
309
+ var dx = abs.x - lastPos.x;
310
+ var dy = abs.y - lastPos.y;
311
+ _this.nodes().forEach(function (otherNode) {
312
+ if (otherNode === node) {
313
+ return;
314
+ }
315
+ if (otherNode.isDragging()) {
316
+ return;
317
+ }
318
+ var otherAbs = otherNode.getAbsolutePosition();
319
+ otherNode.setAbsolutePosition({
320
+ x: otherAbs.x + dx,
321
+ y: otherAbs.y + dy
322
+ });
323
+ otherNode.startDrag();
324
+ });
325
+ lastPos = null;
326
+ });
327
+ };
328
+ Transformer.prototype.getNodes = function () {
329
+ return this._nodes;
168
330
  };
169
331
  Transformer.prototype.getActiveAnchor = function () {
170
332
  return this._movingAnchorName;
@@ -172,19 +334,45 @@ var Transformer = (function (_super) {
172
334
  Transformer.prototype.detach = function () {
173
335
  if (this.getNode()) {
174
336
  this.getNode().off('.' + EVENTS_NAME);
175
- this._node = undefined;
337
+ this._nodes = [];
176
338
  }
177
339
  this._resetTransformCache();
178
340
  };
179
341
  Transformer.prototype._resetTransformCache = function () {
180
- this._clearCache(NODE_RECT);
342
+ this._clearCache(NODES_RECT);
181
343
  this._clearCache('transform');
182
344
  this._clearSelfAndDescendantCache('absoluteTransform');
183
345
  };
184
346
  Transformer.prototype._getNodeRect = function () {
185
- return this._getCache(NODE_RECT, this.__getNodeRect);
347
+ return this._getCache(NODES_RECT, this.__getNodeRect);
348
+ };
349
+ Transformer.prototype.__getNodeShape = function (node, rot, relative) {
350
+ if (rot === void 0) { rot = this.rotation(); }
351
+ var rect = node.getClientRect({
352
+ skipTransform: true,
353
+ skipShadow: true,
354
+ skipStroke: this.ignoreStroke()
355
+ });
356
+ var absScale = node.getAbsoluteScale(relative);
357
+ var absPos = node.getAbsolutePosition(relative);
358
+ var dx = rect.x * absScale.x - node.offsetX() * absScale.x;
359
+ var dy = rect.y * absScale.y - node.offsetY() * absScale.y;
360
+ var rotation = (Global_1.Konva.getAngle(node.getAbsoluteRotation()) + Math.PI * 2) %
361
+ (Math.PI * 2);
362
+ var box = {
363
+ x: absPos.x + dx * Math.cos(rotation) + dy * Math.sin(-rotation),
364
+ y: absPos.y + dy * Math.cos(rotation) + dx * Math.sin(rotation),
365
+ width: rect.width * absScale.x,
366
+ height: rect.height * absScale.y,
367
+ rotation: rotation
368
+ };
369
+ return rotateAroundPoint(box, -Global_1.Konva.getAngle(rot), {
370
+ x: 0,
371
+ y: 0
372
+ });
186
373
  };
187
374
  Transformer.prototype.__getNodeRect = function () {
375
+ var _this = this;
188
376
  var node = this.getNode();
189
377
  if (!node) {
190
378
  return {
@@ -195,24 +383,14 @@ var Transformer = (function (_super) {
195
383
  rotation: 0
196
384
  };
197
385
  }
198
- if (node.parent && this.parent && node.parent !== this.parent) {
199
- Util_1.Util.warn('Transformer and attached node have different parents. Konva does not support such case right now. Please move Transformer to the parent of attaching node.');
200
- }
201
- var rect = node.getClientRect({
202
- skipTransform: true,
203
- skipShadow: true,
204
- skipStroke: this.ignoreStroke()
386
+ var shapes = this.nodes().map(function (node) {
387
+ return _this.__getNodeShape(node);
388
+ });
389
+ var box = getShapesRect(shapes);
390
+ return rotateAroundPoint(box, Global_1.Konva.getAngle(this.rotation()), {
391
+ x: 0,
392
+ y: 0
205
393
  });
206
- var rotation = Global_1.Konva.getAngle(node.rotation());
207
- var dx = rect.x * node.scaleX() - node.offsetX() * node.scaleX();
208
- var dy = rect.y * node.scaleY() - node.offsetY() * node.scaleY();
209
- return {
210
- x: node.x() + dx * Math.cos(rotation) + dy * Math.sin(-rotation),
211
- y: node.y() + dy * Math.cos(rotation) + dx * Math.sin(rotation),
212
- width: rect.width * node.scaleX(),
213
- height: rect.height * node.scaleY(),
214
- rotation: node.rotation()
215
- };
216
394
  };
217
395
  Transformer.prototype.getX = function () {
218
396
  return this._getNodeRect().x;
@@ -220,9 +398,6 @@ var Transformer = (function (_super) {
220
398
  Transformer.prototype.getY = function () {
221
399
  return this._getNodeRect().y;
222
400
  };
223
- Transformer.prototype.getRotation = function () {
224
- return this._getNodeRect().rotation;
225
- };
226
401
  Transformer.prototype.getWidth = function () {
227
402
  return this._getNodeRect().width;
228
403
  };
@@ -252,19 +427,15 @@ var Transformer = (function (_super) {
252
427
  self._handleMouseDown(e);
253
428
  });
254
429
  anchor.on('dragstart', function (e) {
255
- e.cancelBubble = true;
256
- });
257
- anchor.on('dragmove', function (e) {
430
+ anchor.stopDrag();
258
431
  e.cancelBubble = true;
259
432
  });
260
433
  anchor.on('dragend', function (e) {
261
434
  e.cancelBubble = true;
262
435
  });
263
436
  anchor.on('mouseenter', function () {
264
- var rad = Global_1.Konva.getAngle(_this.getAbsoluteRotation());
265
- var scale = _this.getNode().getAbsoluteScale();
266
- var isMirrored = scale.y * scale.x < 0;
267
- var cursor = getCursor(name, rad, isMirrored);
437
+ var rad = Global_1.Konva.getAngle(_this.rotation());
438
+ var cursor = getCursor(name, rad);
268
439
  anchor.getStage().content.style.cursor = cursor;
269
440
  _this._cursorChange = true;
270
441
  });
@@ -278,11 +449,12 @@ var Transformer = (function (_super) {
278
449
  this.add(anchor);
279
450
  };
280
451
  Transformer.prototype._createBack = function () {
452
+ var _this = this;
281
453
  var back = new Shape_1.Shape({
282
454
  name: 'back',
283
455
  width: 0,
284
456
  height: 0,
285
- listening: false,
457
+ draggable: true,
286
458
  sceneFunc: function (ctx) {
287
459
  var tr = this.getParent();
288
460
  var padding = tr.padding();
@@ -293,9 +465,19 @@ var Transformer = (function (_super) {
293
465
  ctx.lineTo(this.width() / 2, -tr.rotateAnchorOffset() * Util_1.Util._sign(this.height()) - padding);
294
466
  }
295
467
  ctx.fillStrokeShape(this);
468
+ },
469
+ hitFunc: function (ctx, shape) {
470
+ if (!_this.shouldOverdrawWholeArea()) {
471
+ return;
472
+ }
473
+ var padding = _this.padding();
474
+ ctx.beginPath();
475
+ ctx.rect(-padding, -padding, shape.width() + padding * 2, shape.height() + padding * 2);
476
+ ctx.fillStrokeShape(shape);
296
477
  }
297
478
  });
298
479
  this.add(back);
480
+ this._proxyDrag(back);
299
481
  };
300
482
  Transformer.prototype._handleMouseDown = function (e) {
301
483
  this._movingAnchorName = e.target.name().split(' ')[0];
@@ -310,6 +492,12 @@ var Transformer = (function (_super) {
310
492
  window.addEventListener('mouseup', this._handleMouseUp, true);
311
493
  window.addEventListener('touchend', this._handleMouseUp, true);
312
494
  this._transforming = true;
495
+ var ap = e.target.getAbsolutePosition();
496
+ var pos = e.target.getStage().getPointerPosition();
497
+ this._anchorDragOffset = {
498
+ x: pos.x - ap.x,
499
+ y: pos.y - ap.y
500
+ };
313
501
  this._fire('transformstart', { evt: e, target: this.getNode() });
314
502
  this.getNode()._fire('transformstart', { evt: e, target: this.getNode() });
315
503
  };
@@ -318,23 +506,55 @@ var Transformer = (function (_super) {
318
506
  var anchorNode = this.findOne('.' + this._movingAnchorName);
319
507
  var stage = anchorNode.getStage();
320
508
  stage.setPointersPositions(e);
321
- anchorNode.setAbsolutePosition(stage.getPointerPosition());
509
+ var pp = stage.getPointerPosition();
510
+ var newNodePos = {
511
+ x: pp.x - this._anchorDragOffset.x,
512
+ y: pp.y - this._anchorDragOffset.y
513
+ };
514
+ var oldAbs = anchorNode.getAbsolutePosition();
515
+ anchorNode.setAbsolutePosition(newNodePos);
516
+ var newAbs = anchorNode.getAbsolutePosition();
517
+ if (oldAbs.x === newAbs.x && oldAbs.y === newAbs.y) {
518
+ return;
519
+ }
520
+ if (this._movingAnchorName === 'rotater') {
521
+ var attrs = this._getNodeRect();
522
+ x = anchorNode.x() - attrs.width / 2;
523
+ y = -anchorNode.y() + attrs.height / 2;
524
+ var delta = Math.atan2(-y, x) + Math.PI / 2;
525
+ if (attrs.height < 0) {
526
+ delta -= Math.PI;
527
+ }
528
+ var oldRotation = Global_1.Konva.getAngle(this.rotation());
529
+ var newRotation = oldRotation + delta;
530
+ var tol = Global_1.Konva.getAngle(this.rotationSnapTolerance());
531
+ var snappedRot = getSnap(this.rotationSnaps(), newRotation, tol);
532
+ var diff = snappedRot - attrs.rotation;
533
+ var shape = rotateAroundCenter(attrs, diff);
534
+ this._fitNodesInto(shape, e);
535
+ return;
536
+ }
322
537
  var keepProportion = this.keepRatio() || e.shiftKey;
323
- var padding = this.padding();
538
+ var centeredScaling = this.centeredScaling() || e.altKey;
324
539
  if (this._movingAnchorName === 'top-left') {
325
540
  if (keepProportion) {
326
- newHypotenuse = Math.sqrt(Math.pow(this.findOne('.bottom-right').x() - anchorNode.x() - padding * 2, 2) +
327
- Math.pow(this.findOne('.bottom-right').y() - anchorNode.y() - padding * 2, 2));
328
- var reverseX = this.findOne('.top-left').x() > this.findOne('.bottom-right').x()
329
- ? -1
330
- : 1;
331
- var reverseY = this.findOne('.top-left').y() > this.findOne('.bottom-right').y()
332
- ? -1
333
- : 1;
541
+ var comparePoint = centeredScaling
542
+ ? {
543
+ x: this.width() / 2,
544
+ y: this.height() / 2
545
+ }
546
+ : {
547
+ x: this.findOne('.bottom-right').x(),
548
+ y: this.findOne('.bottom-right').y()
549
+ };
550
+ newHypotenuse = Math.sqrt(Math.pow(comparePoint.x - anchorNode.x(), 2) +
551
+ Math.pow(comparePoint.y - anchorNode.y(), 2));
552
+ var reverseX = this.findOne('.top-left').x() > comparePoint.x ? -1 : 1;
553
+ var reverseY = this.findOne('.top-left').y() > comparePoint.y ? -1 : 1;
334
554
  x = newHypotenuse * this.cos * reverseX;
335
555
  y = newHypotenuse * this.sin * reverseY;
336
- this.findOne('.top-left').x(this.findOne('.bottom-right').x() - x - padding * 2);
337
- this.findOne('.top-left').y(this.findOne('.bottom-right').y() - y - padding * 2);
556
+ this.findOne('.top-left').x(comparePoint.x - x);
557
+ this.findOne('.top-left').y(comparePoint.y - y);
338
558
  }
339
559
  }
340
560
  else if (this._movingAnchorName === 'top-center') {
@@ -342,18 +562,23 @@ var Transformer = (function (_super) {
342
562
  }
343
563
  else if (this._movingAnchorName === 'top-right') {
344
564
  if (keepProportion) {
345
- newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - this.findOne('.bottom-left').x() - padding * 2, 2) +
346
- Math.pow(this.findOne('.bottom-left').y() - anchorNode.y() - padding * 2, 2));
347
- var reverseX = this.findOne('.top-right').x() < this.findOne('.top-left').x()
348
- ? -1
349
- : 1;
350
- var reverseY = this.findOne('.top-right').y() > this.findOne('.bottom-left').y()
351
- ? -1
352
- : 1;
565
+ var comparePoint = centeredScaling
566
+ ? {
567
+ x: this.width() / 2,
568
+ y: this.height() / 2
569
+ }
570
+ : {
571
+ x: this.findOne('.bottom-left').x(),
572
+ y: this.findOne('.bottom-left').y()
573
+ };
574
+ newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - comparePoint.x, 2) +
575
+ Math.pow(comparePoint.y - anchorNode.y(), 2));
576
+ var reverseX = this.findOne('.top-right').x() < comparePoint.x ? -1 : 1;
577
+ var reverseY = this.findOne('.top-right').y() > comparePoint.y ? -1 : 1;
353
578
  x = newHypotenuse * this.cos * reverseX;
354
579
  y = newHypotenuse * this.sin * reverseY;
355
- this.findOne('.top-right').x(x + padding);
356
- this.findOne('.top-right').y(this.findOne('.bottom-left').y() - y - padding * 2);
580
+ this.findOne('.top-right').x(comparePoint.x + x);
581
+ this.findOne('.top-right').y(comparePoint.y - y);
357
582
  }
358
583
  var pos = anchorNode.position();
359
584
  this.findOne('.top-left').y(pos.y);
@@ -367,18 +592,23 @@ var Transformer = (function (_super) {
367
592
  }
368
593
  else if (this._movingAnchorName === 'bottom-left') {
369
594
  if (keepProportion) {
370
- newHypotenuse = Math.sqrt(Math.pow(this.findOne('.top-right').x() - anchorNode.x() - padding * 2, 2) +
371
- Math.pow(anchorNode.y() - this.findOne('.top-right').y() - padding * 2, 2));
372
- var reverseX = this.findOne('.top-right').x() < this.findOne('.bottom-left').x()
373
- ? -1
374
- : 1;
375
- var reverseY = this.findOne('.bottom-right').y() < this.findOne('.top-left').y()
376
- ? -1
377
- : 1;
595
+ var comparePoint = centeredScaling
596
+ ? {
597
+ x: this.width() / 2,
598
+ y: this.height() / 2
599
+ }
600
+ : {
601
+ x: this.findOne('.top-right').x(),
602
+ y: this.findOne('.top-right').y()
603
+ };
604
+ newHypotenuse = Math.sqrt(Math.pow(comparePoint.x - anchorNode.x(), 2) +
605
+ Math.pow(anchorNode.y() - comparePoint.y, 2));
606
+ var reverseX = comparePoint.x < anchorNode.x() ? -1 : 1;
607
+ var reverseY = anchorNode.y() < comparePoint.y ? -1 : 1;
378
608
  x = newHypotenuse * this.cos * reverseX;
379
609
  y = newHypotenuse * this.sin * reverseY;
380
- this.findOne('.bottom-left').x(this.findOne('.top-right').x() - x - padding * 2);
381
- this.findOne('.bottom-left').y(y + padding);
610
+ anchorNode.x(comparePoint.x - x);
611
+ anchorNode.y(comparePoint.y + y);
382
612
  }
383
613
  pos = anchorNode.position();
384
614
  this.findOne('.top-left').x(pos.x);
@@ -389,77 +619,37 @@ var Transformer = (function (_super) {
389
619
  }
390
620
  else if (this._movingAnchorName === 'bottom-right') {
391
621
  if (keepProportion) {
392
- newHypotenuse = Math.sqrt(Math.pow(this.findOne('.bottom-right').x() - padding, 2) +
393
- Math.pow(this.findOne('.bottom-right').y() - padding, 2));
394
- var reverseX = this.findOne('.top-left').x() > this.findOne('.bottom-right').x()
395
- ? -1
396
- : 1;
397
- var reverseY = this.findOne('.top-left').y() > this.findOne('.bottom-right').y()
398
- ? -1
399
- : 1;
622
+ var comparePoint = centeredScaling
623
+ ? {
624
+ x: this.width() / 2,
625
+ y: this.height() / 2
626
+ }
627
+ : {
628
+ x: this.findOne('.top-left').x(),
629
+ y: this.findOne('.top-left').y()
630
+ };
631
+ newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - comparePoint.x, 2) +
632
+ Math.pow(anchorNode.y() - comparePoint.y, 2));
633
+ var reverseX = this.findOne('.bottom-right').x() < comparePoint.x ? -1 : 1;
634
+ var reverseY = this.findOne('.bottom-right').y() < comparePoint.y ? -1 : 1;
400
635
  x = newHypotenuse * this.cos * reverseX;
401
636
  y = newHypotenuse * this.sin * reverseY;
402
- this.findOne('.bottom-right').x(x + padding);
403
- this.findOne('.bottom-right').y(y + padding);
404
- }
405
- }
406
- else if (this._movingAnchorName === 'rotater') {
407
- var attrs = this._getNodeRect();
408
- x = anchorNode.x() - attrs.width / 2;
409
- y = -anchorNode.y() + attrs.height / 2;
410
- var dAlpha = Math.atan2(-y, x) + Math.PI / 2;
411
- if (attrs.height < 0) {
412
- dAlpha -= Math.PI;
637
+ this.findOne('.bottom-right').x(comparePoint.x + x);
638
+ this.findOne('.bottom-right').y(comparePoint.y + y);
413
639
  }
414
- var rot = Global_1.Konva.getAngle(this.rotation());
415
- var newRotation = Util_1.Util._radToDeg(rot) + Util_1.Util._radToDeg(dAlpha);
416
- var alpha = Global_1.Konva.getAngle(this.getNode().rotation());
417
- var newAlpha = Util_1.Util._degToRad(newRotation);
418
- var snaps = this.rotationSnaps();
419
- var offset = Global_1.Konva.getAngle(this.rotationSnapTolerance());
420
- for (var i = 0; i < snaps.length; i++) {
421
- var angle = Global_1.Konva.getAngle(snaps[i]);
422
- var dif = Math.abs(angle - Util_1.Util._degToRad(newRotation)) % (Math.PI * 2);
423
- if (dif < offset) {
424
- newRotation = Util_1.Util._radToDeg(angle);
425
- newAlpha = Util_1.Util._degToRad(newRotation);
426
- }
427
- }
428
- var dx = padding;
429
- var dy = padding;
430
- this._fitNodeInto({
431
- rotation: Global_1.Konva.angleDeg ? newRotation : Util_1.Util._degToRad(newRotation),
432
- x: attrs.x +
433
- (attrs.width / 2 + padding) *
434
- (Math.cos(alpha) - Math.cos(newAlpha)) +
435
- (attrs.height / 2 + padding) *
436
- (Math.sin(-alpha) - Math.sin(-newAlpha)) -
437
- (dx * Math.cos(rot) + dy * Math.sin(-rot)),
438
- y: attrs.y +
439
- (attrs.height / 2 + padding) *
440
- (Math.cos(alpha) - Math.cos(newAlpha)) +
441
- (attrs.width / 2 + padding) *
442
- (Math.sin(alpha) - Math.sin(newAlpha)) -
443
- (dy * Math.cos(rot) + dx * Math.sin(rot)),
444
- width: attrs.width + padding * 2,
445
- height: attrs.height + padding * 2
446
- }, e);
447
640
  }
448
641
  else {
449
642
  console.error(new Error('Wrong position argument of selection resizer: ' +
450
643
  this._movingAnchorName));
451
644
  }
452
- if (this._movingAnchorName === 'rotater') {
453
- return;
454
- }
455
645
  var centeredScaling = this.centeredScaling() || e.altKey;
456
646
  if (centeredScaling) {
457
647
  var topLeft = this.findOne('.top-left');
458
648
  var bottomRight = this.findOne('.bottom-right');
459
- var topOffsetX = topLeft.x() + padding;
460
- var topOffsetY = topLeft.y() + padding;
461
- var bottomOffsetX = this.getWidth() - bottomRight.x() + padding;
462
- var bottomOffsetY = this.getHeight() - bottomRight.y() + padding;
649
+ var topOffsetX = topLeft.x();
650
+ var topOffsetY = topLeft.y();
651
+ var bottomOffsetX = this.getWidth() - bottomRight.x();
652
+ var bottomOffsetY = this.getHeight() - bottomRight.y();
463
653
  bottomRight.move({
464
654
  x: -topOffsetX,
465
655
  y: -topOffsetY
@@ -469,21 +659,25 @@ var Transformer = (function (_super) {
469
659
  y: bottomOffsetY
470
660
  });
471
661
  }
472
- var absPos = this.findOne('.top-left').getAbsolutePosition(this.getParent());
662
+ var absPos = this.findOne('.top-left').getAbsolutePosition();
473
663
  x = absPos.x;
474
664
  y = absPos.y;
475
665
  var width = this.findOne('.bottom-right').x() - this.findOne('.top-left').x();
476
666
  var height = this.findOne('.bottom-right').y() - this.findOne('.top-left').y();
477
- this._fitNodeInto({
478
- x: x + this.offsetX(),
479
- y: y + this.offsetY(),
667
+ this._fitNodesInto({
668
+ x: x,
669
+ y: y,
480
670
  width: width,
481
- height: height
671
+ height: height,
672
+ rotation: Global_1.Konva.getAngle(this.rotation())
482
673
  }, e);
483
674
  };
484
675
  Transformer.prototype._handleMouseUp = function (e) {
485
676
  this._removeEvents(e);
486
677
  };
678
+ Transformer.prototype.getAbsoluteTransform = function () {
679
+ return this.getTransform();
680
+ };
487
681
  Transformer.prototype._removeEvents = function (e) {
488
682
  if (this._transforming) {
489
683
  this._transforming = false;
@@ -499,39 +693,144 @@ var Transformer = (function (_super) {
499
693
  this._movingAnchorName = null;
500
694
  }
501
695
  };
502
- Transformer.prototype._fitNodeInto = function (newAttrs, evt) {
503
- var boundBoxFunc = this.boundBoxFunc();
504
- if (boundBoxFunc) {
505
- var oldAttrs = this._getNodeRect();
506
- newAttrs = boundBoxFunc.call(this, oldAttrs, newAttrs);
696
+ Transformer.prototype._fitNodesInto = function (newAttrs, evt) {
697
+ var _this = this;
698
+ var oldAttrs = this._getNodeRect();
699
+ var minSize = 1;
700
+ if (Util_1.Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize)) {
701
+ this.update();
702
+ return;
507
703
  }
508
- var node = this.getNode();
509
- if (newAttrs.rotation !== undefined) {
510
- this.getNode().rotation(newAttrs.rotation);
704
+ if (Util_1.Util._inRange(newAttrs.height, -this.padding() * 2 - minSize, minSize)) {
705
+ this.update();
706
+ return;
511
707
  }
708
+ var allowNegativeScale = true;
709
+ var t = new Util_1.Transform();
710
+ t.rotate(Global_1.Konva.getAngle(this.rotation()));
711
+ if (this._movingAnchorName &&
712
+ newAttrs.width < 0 &&
713
+ this._movingAnchorName.indexOf('left') >= 0) {
714
+ var offset = t.point({
715
+ x: -this.padding() * 2,
716
+ y: 0
717
+ });
718
+ newAttrs.x += offset.x;
719
+ newAttrs.y += offset.y;
720
+ newAttrs.width += this.padding() * 2;
721
+ this._movingAnchorName = this._movingAnchorName.replace('left', 'right');
722
+ this._anchorDragOffset.x -= offset.x;
723
+ this._anchorDragOffset.y -= offset.y;
724
+ if (!allowNegativeScale) {
725
+ this.update();
726
+ return;
727
+ }
728
+ }
729
+ else if (this._movingAnchorName &&
730
+ newAttrs.width < 0 &&
731
+ this._movingAnchorName.indexOf('right') >= 0) {
732
+ var offset = t.point({
733
+ x: this.padding() * 2,
734
+ y: 0
735
+ });
736
+ this._movingAnchorName = this._movingAnchorName.replace('right', 'left');
737
+ this._anchorDragOffset.x -= offset.x;
738
+ this._anchorDragOffset.y -= offset.y;
739
+ newAttrs.width += this.padding() * 2;
740
+ if (!allowNegativeScale) {
741
+ this.update();
742
+ return;
743
+ }
744
+ }
745
+ if (this._movingAnchorName &&
746
+ newAttrs.height < 0 &&
747
+ this._movingAnchorName.indexOf('top') >= 0) {
748
+ var offset = t.point({
749
+ x: 0,
750
+ y: -this.padding() * 2
751
+ });
752
+ newAttrs.x += offset.x;
753
+ newAttrs.y += offset.y;
754
+ this._movingAnchorName = this._movingAnchorName.replace('top', 'bottom');
755
+ this._anchorDragOffset.x -= offset.x;
756
+ this._anchorDragOffset.y -= offset.y;
757
+ newAttrs.height += this.padding() * 2;
758
+ if (!allowNegativeScale) {
759
+ this.update();
760
+ return;
761
+ }
762
+ }
763
+ else if (this._movingAnchorName &&
764
+ newAttrs.height < 0 &&
765
+ this._movingAnchorName.indexOf('bottom') >= 0) {
766
+ var offset = t.point({
767
+ x: 0,
768
+ y: this.padding() * 2
769
+ });
770
+ this._movingAnchorName = this._movingAnchorName.replace('bottom', 'top');
771
+ this._anchorDragOffset.x -= offset.x;
772
+ this._anchorDragOffset.y -= offset.y;
773
+ newAttrs.height += this.padding() * 2;
774
+ if (!allowNegativeScale) {
775
+ this.update();
776
+ return;
777
+ }
778
+ }
779
+ this._nodes.forEach(function (node) {
780
+ var oldRect = _this.__getNodeShape(node, 0);
781
+ var newRect = transformAndRotateShape(oldRect, oldAttrs, newAttrs);
782
+ _this._fitNodeInto(node, newRect, evt);
783
+ });
784
+ this.rotation(Util_1.Util._getRotation(newAttrs.rotation));
785
+ this._resetTransformCache();
786
+ this.update();
787
+ this.getLayer().batchDraw();
788
+ };
789
+ Transformer.prototype._fitNodeInto = function (node, newAttrs, evt) {
790
+ if (this.boundBoxFunc()) {
791
+ var oldAttrs = this.__getNodeShape(node, node.rotation(), node.getParent());
792
+ var bounded = this.boundBoxFunc()(oldAttrs, newAttrs, node);
793
+ if (bounded) {
794
+ newAttrs = bounded;
795
+ }
796
+ else {
797
+ Util_1.Util.warn('boundBoxFunc returned falsy. You should return new bound rect from it!');
798
+ }
799
+ }
800
+ var parentRot = Global_1.Konva.getAngle(node.getParent().getAbsoluteRotation());
801
+ node.rotation(Util_1.Util._getRotation(newAttrs.rotation - parentRot));
512
802
  var pure = node.getClientRect({
513
803
  skipTransform: true,
514
804
  skipShadow: true,
515
805
  skipStroke: this.ignoreStroke()
516
806
  });
517
- var padding = this.padding();
518
- var scaleX = pure.width ? (newAttrs.width - padding * 2) / pure.width : 1;
519
- var scaleY = pure.height
520
- ? (newAttrs.height - padding * 2) / pure.height
521
- : 1;
807
+ var parentTransform = node
808
+ .getParent()
809
+ .getAbsoluteTransform()
810
+ .copy();
811
+ parentTransform.invert();
812
+ var invertedPoint = parentTransform.point({
813
+ x: newAttrs.x,
814
+ y: newAttrs.y
815
+ });
816
+ newAttrs.x = invertedPoint.x;
817
+ newAttrs.y = invertedPoint.y;
818
+ var absScale = node.getParent().getAbsoluteScale();
819
+ pure.width *= absScale.x;
820
+ pure.height *= absScale.y;
821
+ var scaleX = pure.width ? newAttrs.width / pure.width : 1;
822
+ var scaleY = pure.height ? newAttrs.height / pure.height : 1;
522
823
  var rotation = Global_1.Konva.getAngle(node.rotation());
523
- var dx = pure.x * scaleX - padding - node.offsetX() * scaleX;
524
- var dy = pure.y * scaleY - padding - node.offsetY() * scaleY;
525
- this.getNode().setAttrs({
824
+ var dx = pure.x * scaleX - node.offsetX() * scaleX;
825
+ var dy = pure.y * scaleY - node.offsetY() * scaleY;
826
+ node.setAttrs({
526
827
  scaleX: scaleX,
527
828
  scaleY: scaleY,
528
829
  x: newAttrs.x - (dx * Math.cos(rotation) + dy * Math.sin(-rotation)),
529
830
  y: newAttrs.y - (dy * Math.cos(rotation) + dx * Math.sin(rotation))
530
831
  });
531
- this._fire('transform', { evt: evt, target: this.getNode() });
532
- this.getNode()._fire('transform', { evt: evt, target: this.getNode() });
533
- this.update();
534
- this.getLayer().batchDraw();
832
+ this._fire('transform', { evt: evt, target: node });
833
+ node._fire('transform', { evt: evt, target: node });
535
834
  };
536
835
  Transformer.prototype.forceUpdate = function () {
537
836
  this._resetTransformCache();
@@ -540,15 +839,7 @@ var Transformer = (function (_super) {
540
839
  Transformer.prototype.update = function () {
541
840
  var _this = this;
542
841
  var attrs = this._getNodeRect();
543
- var node = this.getNode();
544
- var scale = { x: 1, y: 1 };
545
- if (node && node.getParent()) {
546
- scale = node.getParent().getAbsoluteScale();
547
- }
548
- var invertedScale = {
549
- x: 1 / scale.x,
550
- y: 1 / scale.y
551
- };
842
+ this.rotation(Util_1.Util._getRotation(attrs.rotation));
552
843
  var width = attrs.width;
553
844
  var height = attrs.height;
554
845
  var enabledAnchors = this.enabledAnchors();
@@ -568,68 +859,71 @@ var Transformer = (function (_super) {
568
859
  });
569
860
  });
570
861
  this.findOne('.top-left').setAttrs({
571
- x: -padding,
572
- y: -padding,
573
- scale: invertedScale,
862
+ x: 0,
863
+ y: 0,
864
+ offsetX: anchorSize / 2 + padding,
865
+ offsetY: anchorSize / 2 + padding,
574
866
  visible: resizeEnabled && enabledAnchors.indexOf('top-left') >= 0
575
867
  });
576
868
  this.findOne('.top-center').setAttrs({
577
869
  x: width / 2,
578
- y: -padding,
579
- scale: invertedScale,
870
+ y: 0,
871
+ offsetY: anchorSize / 2 + padding,
580
872
  visible: resizeEnabled && enabledAnchors.indexOf('top-center') >= 0
581
873
  });
582
874
  this.findOne('.top-right').setAttrs({
583
- x: width + padding,
584
- y: -padding,
585
- scale: invertedScale,
875
+ x: width,
876
+ y: 0,
877
+ offsetX: anchorSize / 2 - padding,
878
+ offsetY: anchorSize / 2 + padding,
586
879
  visible: resizeEnabled && enabledAnchors.indexOf('top-right') >= 0
587
880
  });
588
881
  this.findOne('.middle-left').setAttrs({
589
- x: -padding,
882
+ x: 0,
590
883
  y: height / 2,
591
- scale: invertedScale,
884
+ offsetX: anchorSize / 2 + padding,
592
885
  visible: resizeEnabled && enabledAnchors.indexOf('middle-left') >= 0
593
886
  });
594
887
  this.findOne('.middle-right').setAttrs({
595
- x: width + padding,
888
+ x: width,
596
889
  y: height / 2,
597
- scale: invertedScale,
890
+ offsetX: anchorSize / 2 - padding,
598
891
  visible: resizeEnabled && enabledAnchors.indexOf('middle-right') >= 0
599
892
  });
600
893
  this.findOne('.bottom-left').setAttrs({
601
- x: -padding,
602
- y: height + padding,
603
- scale: invertedScale,
894
+ x: 0,
895
+ y: height,
896
+ offsetX: anchorSize / 2 + padding,
897
+ offsetY: anchorSize / 2 - padding,
604
898
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-left') >= 0
605
899
  });
606
900
  this.findOne('.bottom-center').setAttrs({
607
901
  x: width / 2,
608
- y: height + padding,
609
- scale: invertedScale,
902
+ y: height,
903
+ offsetY: anchorSize / 2 - padding,
610
904
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-center') >= 0
611
905
  });
612
906
  this.findOne('.bottom-right').setAttrs({
613
- x: width + padding,
614
- y: height + padding,
615
- scale: invertedScale,
907
+ x: width,
908
+ y: height,
909
+ offsetX: anchorSize / 2 - padding,
910
+ offsetY: anchorSize / 2 - padding,
616
911
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0
617
912
  });
618
- var scaledRotateAnchorOffset = -this.rotateAnchorOffset() * Math.abs(invertedScale.y);
619
913
  this.findOne('.rotater').setAttrs({
620
914
  x: width / 2,
621
- y: scaledRotateAnchorOffset * Util_1.Util._sign(height) - padding,
622
- scale: invertedScale,
915
+ y: -this.rotateAnchorOffset() * Util_1.Util._sign(height) - padding,
623
916
  visible: this.rotateEnabled()
624
917
  });
625
918
  this.findOne('.back').setAttrs({
626
- width: width * scale.x,
627
- height: height * scale.y,
628
- scale: invertedScale,
919
+ width: width,
920
+ height: height,
629
921
  visible: this.borderEnabled(),
630
922
  stroke: this.borderStroke(),
631
923
  strokeWidth: this.borderStrokeWidth(),
632
- dash: this.borderDash()
924
+ dash: this.borderDash(),
925
+ x: 0,
926
+ y: 0
633
927
  });
634
928
  };
635
929
  Transformer.prototype.isTransforming = function () {
@@ -697,7 +991,9 @@ Factory_1.Factory.addGetterSetter(Transformer, 'centeredScaling', false);
697
991
  Factory_1.Factory.addGetterSetter(Transformer, 'ignoreStroke', false);
698
992
  Factory_1.Factory.addGetterSetter(Transformer, 'padding', 0, Validators_1.getNumberValidator());
699
993
  Factory_1.Factory.addGetterSetter(Transformer, 'node');
994
+ Factory_1.Factory.addGetterSetter(Transformer, 'nodes');
700
995
  Factory_1.Factory.addGetterSetter(Transformer, 'boundBoxFunc');
996
+ Factory_1.Factory.addGetterSetter(Transformer, 'shouldOverdrawWholeArea', false);
701
997
  Factory_1.Factory.backCompat(Transformer, {
702
998
  lineEnabled: 'borderEnabled',
703
999
  rotateHandlerOffset: 'rotateAnchorOffset',