konva 10.4.0 → 10.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +4 -4
  2. package/konva.js +3432 -3746
  3. package/konva.min.js +4 -4
  4. package/lib/Animation.d.ts +2 -2
  5. package/lib/Animation.js +2 -2
  6. package/lib/BezierFunctions.d.ts +1 -1
  7. package/lib/BezierFunctions.js +76 -116
  8. package/lib/Canvas.d.ts +1 -1
  9. package/lib/Canvas.js +8 -5
  10. package/lib/Container.js +9 -6
  11. package/lib/Context.d.ts +4 -5
  12. package/lib/Context.js +77 -72
  13. package/lib/DragAndDrop.d.ts +5 -3
  14. package/lib/DragAndDrop.js +65 -29
  15. package/lib/Factory.d.ts +3 -2
  16. package/lib/Factory.js +10 -9
  17. package/lib/Global.d.ts +0 -6
  18. package/lib/Global.js +2 -8
  19. package/lib/Layer.js +9 -6
  20. package/lib/Node.d.ts +33 -15
  21. package/lib/Node.js +179 -135
  22. package/lib/PointerEvents.d.ts +1 -2
  23. package/lib/PointerEvents.js +20 -18
  24. package/lib/Shape.d.ts +2 -2
  25. package/lib/Shape.js +51 -7
  26. package/lib/Stage.d.ts +23 -14
  27. package/lib/Stage.js +221 -93
  28. package/lib/Tween.js +59 -66
  29. package/lib/Util.d.ts +3 -2
  30. package/lib/Util.js +43 -16
  31. package/lib/_CoreInternals.d.ts +25 -11
  32. package/lib/_FullInternals.d.ts +25 -11
  33. package/lib/filters/Blur.js +16 -12
  34. package/lib/filters/Brighten.d.ts +1 -0
  35. package/lib/filters/Brighten.js +1 -0
  36. package/lib/filters/Brightness.d.ts +2 -1
  37. package/lib/filters/Brightness.js +4 -4
  38. package/lib/filters/Emboss.js +16 -58
  39. package/lib/filters/Kaleidoscope.js +8 -15
  40. package/lib/filters/Mask.js +8 -8
  41. package/lib/filters/Pixelate.js +16 -29
  42. package/lib/filters/Threshold.d.ts +1 -1
  43. package/lib/filters/Threshold.js +1 -1
  44. package/lib/shapes/Arc.js +5 -3
  45. package/lib/shapes/Arrow.d.ts +2 -6
  46. package/lib/shapes/Arrow.js +68 -46
  47. package/lib/shapes/Image.js +16 -19
  48. package/lib/shapes/Label.d.ts +2 -3
  49. package/lib/shapes/Label.js +7 -21
  50. package/lib/shapes/Line.d.ts +1 -1
  51. package/lib/shapes/Line.js +1 -1
  52. package/lib/shapes/Path.d.ts +27 -2
  53. package/lib/shapes/Path.js +148 -142
  54. package/lib/shapes/RegularPolygon.js +1 -1
  55. package/lib/shapes/Sprite.js +2 -6
  56. package/lib/shapes/Text.d.ts +12 -4
  57. package/lib/shapes/Text.js +96 -54
  58. package/lib/shapes/TextPath.d.ts +4 -3
  59. package/lib/shapes/TextPath.js +40 -26
  60. package/lib/shapes/Transformer.d.ts +16 -1
  61. package/lib/shapes/Transformer.js +209 -130
  62. package/lib/types.d.ts +2 -2
  63. package/package.json +4 -3
@@ -16,6 +16,7 @@ const ATTR_CHANGE_LIST = [
16
16
  'enabledAnchorsChange',
17
17
  'anchorSizeChange',
18
18
  'borderEnabledChange',
19
+ 'shouldOverdrawWholeAreaChange',
19
20
  'borderStrokeChange',
20
21
  'borderStrokeWidthChange',
21
22
  'borderDashChange',
@@ -42,6 +43,10 @@ const TRANSFORM_CHANGE_STR = [
42
43
  'offsetYChange',
43
44
  'transformsEnabledChange',
44
45
  'strokeWidthChange',
46
+ 'strokeChange',
47
+ 'strokeLinearGradientColorStopsChange',
48
+ 'strokeScaleEnabledChange',
49
+ 'strokeEnabledChange',
45
50
  'draggableChange',
46
51
  ];
47
52
  const ANGLES = {
@@ -140,16 +145,50 @@ function rotateAroundCenter(shape, deltaRad) {
140
145
  }
141
146
  function getSnap(snaps, newRotationRad, tol) {
142
147
  let snapped = newRotationRad;
148
+ let nearest = tol;
143
149
  for (let i = 0; i < snaps.length; i++) {
144
150
  const angle = Konva.getAngle(snaps[i]);
145
151
  const absDiff = Math.abs(angle - newRotationRad) % (Math.PI * 2);
146
152
  const dif = Math.min(absDiff, Math.PI * 2 - absDiff);
147
- if (dif < tol) {
153
+ if (dif < nearest) {
154
+ nearest = dif;
148
155
  snapped = angle;
149
156
  }
150
157
  }
151
158
  return snapped;
152
159
  }
160
+ // start and end point of the rotate line, in the transformer box coordinates
161
+ function getRotaterLine(tr, width, height) {
162
+ const rad = Util.degToRad(tr.rotateAnchorAngle());
163
+ // direction vector (0 degrees = up/top)
164
+ const dirX = Math.sin(rad);
165
+ const dirY = -Math.cos(rad);
166
+ const cx = width / 2;
167
+ const cy = height / 2;
168
+ // distance from the center to the first box edge in that direction
169
+ let t = Infinity;
170
+ if (dirY < 0) {
171
+ t = Math.min(t, -cy / dirY);
172
+ }
173
+ else if (dirY > 0) {
174
+ t = Math.min(t, (height - cy) / dirY);
175
+ }
176
+ if (dirX < 0) {
177
+ t = Math.min(t, -cx / dirX);
178
+ }
179
+ else if (dirX > 0) {
180
+ t = Math.min(t, (width - cx) / dirX);
181
+ }
182
+ const edgeX = cx + dirX * t;
183
+ const edgeY = cy + dirY * t;
184
+ const offset = tr.rotateAnchorOffset() + tr.padding();
185
+ return {
186
+ edgeX,
187
+ edgeY,
188
+ endX: edgeX + dirX * offset,
189
+ endY: edgeY + dirY * offset,
190
+ };
191
+ }
153
192
  const activeTransformers = new Set();
154
193
  /**
155
194
  * Transformer constructor. Transformer is a special type of group that allow you transform Konva
@@ -226,7 +265,13 @@ export class Transformer extends Group {
226
265
  this._handleMouseUp = this._handleMouseUp.bind(this);
227
266
  this.update = this.update.bind(this);
228
267
  // update transformer data for certain attr changes
229
- this.on(ATTR_CHANGE_LIST, this.update);
268
+ this.on(ATTR_CHANGE_LIST, (event) => {
269
+ if (event.type === 'ignoreStrokeChange')
270
+ this._resetTransformCache();
271
+ this.update();
272
+ });
273
+ // the memoized rect bakes in our own rotation, so a manual rotation must drop it
274
+ this.on(`rotationChange.${EVENTS_NAME}`, () => this._clearCache(NODES_RECT));
230
275
  if (this.getNode()) {
231
276
  this.update();
232
277
  }
@@ -257,11 +302,19 @@ export class Transformer extends Group {
257
302
  if (this._nodes && this._nodes.length) {
258
303
  this.detach();
259
304
  }
260
- const filteredNodes = nodes.filter((node) => {
305
+ const selected = new Set(nodes);
306
+ for (const node of selected) {
261
307
  // check if ancestor of the transformer
262
308
  if (node.isAncestorOf(this)) {
263
309
  Util.error('Konva.Transformer cannot be an a child of the node you are trying to attach');
264
- return false;
310
+ selected.delete(node);
311
+ }
312
+ }
313
+ const filteredNodes = Array.from(selected).filter((node) => {
314
+ // Transform a selected subtree once, through its selected ancestor.
315
+ for (let parent = node.getParent(); parent; parent = parent.getParent()) {
316
+ if (selected.has(parent))
317
+ return false;
265
318
  }
266
319
  return true;
267
320
  });
@@ -274,21 +327,24 @@ export class Transformer extends Group {
274
327
  }
275
328
  this._nodes.forEach((node) => {
276
329
  const onChange = () => {
330
+ var _a;
277
331
  // Perf: skip the changes _fitNodesInto makes itself — it does the
278
332
  // final _resetTransformCache + update, so per-attr fan-out here is
279
333
  // pure waste (was O(N*events) per resize step). A change made between
280
334
  // two pointer moves (e.g. a deferred state update) must get through.
281
- if (this._fitting)
335
+ if (this._fitting) {
336
+ (_a = this._nodeRectCache) === null || _a === void 0 ? void 0 : _a.delete(node);
282
337
  return;
338
+ }
283
339
  if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
284
340
  this.rotation(this.nodes()[0].getAbsoluteRotation());
285
341
  }
286
342
  // Cache reset stays synchronous so sync reads of tr.x() etc. are fresh.
287
- this._resetTransformCache();
343
+ this._resetTransformCache(node);
288
344
  if (!this.isDragging()) {
289
345
  // Perf: an ancestor cascade (e.g. stage drag) fires
290
346
  // absoluteTransformChange on every attached node — without batching,
291
- // update() runs once per node and each call walks all N nodes via
347
+ // a refresh runs once per node and each call walks all N nodes via
292
348
  // __getNodeRect, giving O(N^2) per drag frame.
293
349
  this._scheduleUpdate();
294
350
  }
@@ -372,6 +428,7 @@ export class Transformer extends Group {
372
428
  * transformer.detach();
373
429
  */
374
430
  detach() {
431
+ var _a;
375
432
  // remove events
376
433
  if (this._nodes) {
377
434
  this._nodes.forEach((node) => {
@@ -380,6 +437,7 @@ export class Transformer extends Group {
380
437
  }
381
438
  this._nodes = [];
382
439
  this._resetTransformCache();
440
+ (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
383
441
  }
384
442
  /**
385
443
  * bind events to the Transformer. You can use events: `transform`, `transformstart`, `transformend`, `dragstart`, `dragmove`, `dragend`
@@ -394,7 +452,12 @@ export class Transformer extends Group {
394
452
  * console.log('transform started');
395
453
  * });
396
454
  */
397
- _resetTransformCache() {
455
+ _resetTransformCache(changedNode) {
456
+ var _a, _b;
457
+ if (changedNode)
458
+ (_a = this._nodeRectCache) === null || _a === void 0 ? void 0 : _a.delete(changedNode);
459
+ else
460
+ (_b = this._nodeRectCache) === null || _b === void 0 ? void 0 : _b.clear();
398
461
  this._clearCache(NODES_RECT);
399
462
  this._clearCache('transform');
400
463
  this._clearSelfAndDescendantCache('absoluteTransform');
@@ -403,6 +466,7 @@ export class Transformer extends Group {
403
466
  return this._getCache(NODES_RECT, this.__getNodeRect);
404
467
  }
405
468
  __getNodeRect() {
469
+ var _a;
406
470
  const node = this.getNode();
407
471
  if (!node) {
408
472
  return {
@@ -413,35 +477,80 @@ export class Transformer extends Group {
413
477
  rotation: 0,
414
478
  };
415
479
  }
416
- const totalPoints = [];
417
- this.nodes().map((node) => {
418
- const box = node.getClientRect({
419
- skipTransform: true,
420
- skipShadow: true,
421
- skipStroke: this.ignoreStroke(),
422
- });
423
- const points = [
424
- { x: box.x, y: box.y },
425
- { x: box.x + box.width, y: box.y },
426
- { x: box.x + box.width, y: box.y + box.height },
427
- { x: box.x, y: box.y + box.height },
428
- ];
429
- const trans = node.getAbsoluteTransform();
430
- points.forEach(function (point) {
431
- const transformed = trans.point(point);
432
- totalPoints.push(transformed);
433
- });
434
- });
480
+ const rotation = Konva.getAngle(this.rotation());
481
+ const ignoreStroke = this.ignoreStroke();
435
482
  const tr = new Transform();
436
- tr.rotate(-Konva.getAngle(this.rotation()));
483
+ tr.rotate(-rotation);
484
+ const cache = this._nodeRectCache || (this._nodeRectCache = new Map());
437
485
  let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
438
- totalPoints.forEach(function (point) {
439
- const transformed = tr.point(point);
440
- minX = Math.min(minX, transformed.x);
441
- minY = Math.min(minY, transformed.y);
442
- maxX = Math.max(maxX, transformed.x);
443
- maxY = Math.max(maxY, transformed.y);
444
- });
486
+ for (const node of this.nodes()) {
487
+ // Bounds may only be reused when they are a pure function of the node's
488
+ // own size: then every change reaches us either through _setAttr or
489
+ // through the size probe below. A shape with its own getSelfRect - Line
490
+ // mutates its points array in place, and a user subclass can measure
491
+ // anything at all - may change with no signal, so it is measured every
492
+ // time. Groups depend on their descendants, and a cached ancestor
493
+ // suppresses descendant transform-change notifications, so both are
494
+ // measured too.
495
+ const cacheable = node instanceof Shape &&
496
+ node.getSelfRect === Shape.prototype.getSelfRect &&
497
+ !node._hasCachedAncestor();
498
+ // width() and height() are the only inputs of the default getSelfRect and
499
+ // they can come from outside the attrs (an Image source that loads or
500
+ // resizes, Text metrics), so the attr revision alone cannot key them.
501
+ const width = cacheable ? node.width() : 0;
502
+ const height = cacheable ? node.height() : 0;
503
+ if (cacheable)
504
+ (_a = node._attrsVersion) !== null && _a !== void 0 ? _a : (node._attrsVersion = 0);
505
+ let bounds = cacheable ? cache.get(node) : undefined;
506
+ if (!bounds ||
507
+ bounds.rotation !== rotation ||
508
+ bounds.ignoreStroke !== ignoreStroke ||
509
+ bounds.version !== node._attrsVersion ||
510
+ bounds.width !== width ||
511
+ bounds.height !== height) {
512
+ let trans = node.getAbsoluteTransform();
513
+ const [a, b, c, d] = trans.getMatrix();
514
+ const collapsed = a * d - b * c === 0;
515
+ const box = node.getClientRect({
516
+ skipTransform: !collapsed,
517
+ skipShadow: true,
518
+ skipStroke: ignoreStroke,
519
+ });
520
+ const points = [
521
+ { x: box.x, y: box.y },
522
+ { x: box.x + box.width, y: box.y },
523
+ { x: box.x + box.width, y: box.y + box.height },
524
+ { x: box.x, y: box.y + box.height },
525
+ ];
526
+ if (collapsed)
527
+ trans = new Transform();
528
+ bounds = {
529
+ rotation,
530
+ ignoreStroke,
531
+ width,
532
+ height,
533
+ version: node._attrsVersion || 0,
534
+ minX: Infinity,
535
+ minY: Infinity,
536
+ maxX: -Infinity,
537
+ maxY: -Infinity,
538
+ };
539
+ for (const point of points) {
540
+ const projected = tr.point(trans.point(point));
541
+ bounds.minX = Math.min(bounds.minX, projected.x);
542
+ bounds.minY = Math.min(bounds.minY, projected.y);
543
+ bounds.maxX = Math.max(bounds.maxX, projected.x);
544
+ bounds.maxY = Math.max(bounds.maxY, projected.y);
545
+ }
546
+ if (cacheable)
547
+ cache.set(node, bounds);
548
+ }
549
+ minX = Math.min(minX, bounds.minX);
550
+ minY = Math.min(minY, bounds.minY);
551
+ maxX = Math.max(maxX, bounds.maxX);
552
+ maxY = Math.max(maxY, bounds.maxY);
553
+ }
445
554
  tr.invert();
446
555
  const p = tr.point({ x: minX, y: minY });
447
556
  return {
@@ -498,6 +607,8 @@ export class Transformer extends Group {
498
607
  });
499
608
  // add hover styling
500
609
  anchor.on('mouseenter', () => {
610
+ if (this.isTransforming())
611
+ return;
501
612
  const rad = Konva.getAngle(this.rotation());
502
613
  const rotateCursor = this.rotateAnchorCursor();
503
614
  const cursor = getCursor(name, rad, rotateCursor);
@@ -506,9 +617,11 @@ export class Transformer extends Group {
506
617
  this._cursorChange = true;
507
618
  });
508
619
  anchor.on('mouseout', () => {
620
+ this._cursorChange = false;
621
+ if (this.isTransforming())
622
+ return;
509
623
  anchor.getStage().content &&
510
624
  (anchor.getStage().content.style.cursor = '');
511
- this._cursorChange = false;
512
625
  });
513
626
  this.add(anchor);
514
627
  }
@@ -519,45 +632,17 @@ export class Transformer extends Group {
519
632
  height: 0,
520
633
  sceneFunc(ctx, shape) {
521
634
  const tr = shape.getParent();
635
+ if (!tr.borderEnabled())
636
+ return;
522
637
  const padding = tr.padding();
523
638
  const width = shape.width();
524
639
  const height = shape.height();
525
640
  ctx.beginPath();
526
641
  ctx.rect(-padding, -padding, width + padding * 2, height + padding * 2);
527
642
  if (tr.rotateEnabled() && tr.rotateLineVisible()) {
528
- // Calculate rotation line position based on rotateAnchorAngle
529
- const rotateAnchorAngle = tr.rotateAnchorAngle();
530
- const rotateAnchorOffset = tr.rotateAnchorOffset();
531
- const rad = Util.degToRad(rotateAnchorAngle);
532
- // Direction vector (0 degrees = up/top)
533
- const dirX = Math.sin(rad);
534
- const dirY = -Math.cos(rad);
535
- // Center of the box
536
- const cx = width / 2;
537
- const cy = height / 2;
538
- // Find intersection with box edge
539
- let t = Infinity;
540
- if (dirY < 0) {
541
- t = Math.min(t, -cy / dirY);
542
- }
543
- else if (dirY > 0) {
544
- t = Math.min(t, (height - cy) / dirY);
545
- }
546
- if (dirX < 0) {
547
- t = Math.min(t, -cx / dirX);
548
- }
549
- else if (dirX > 0) {
550
- t = Math.min(t, (width - cx) / dirX);
551
- }
552
- // Edge point (start of line)
553
- const edgeX = cx + dirX * t;
554
- const edgeY = cy + dirY * t;
555
- // End point with offset
556
- const sign = Util._sign(height);
557
- const endX = edgeX + dirX * rotateAnchorOffset * sign;
558
- const endY = edgeY + dirY * rotateAnchorOffset * sign;
559
- ctx.moveTo(edgeX, edgeY);
560
- ctx.lineTo(endX, endY);
643
+ const line = getRotaterLine(tr, width, height);
644
+ ctx.moveTo(line.edgeX, line.edgeY);
645
+ ctx.lineTo(line.endX, line.endY);
561
646
  }
562
647
  ctx.fillStrokeShape(shape);
563
648
  },
@@ -593,6 +678,8 @@ export class Transformer extends Group {
593
678
  }
594
679
  _handleMouseDown(e) {
595
680
  var _a, _b;
681
+ if (e.evt.button !== undefined && !Konva.dragButtons.includes(e.evt.button))
682
+ return;
596
683
  // do nothing if we already transforming
597
684
  // that is possible to trigger with multitouch
598
685
  if (this._transforming) {
@@ -613,6 +700,7 @@ export class Transformer extends Group {
613
700
  win.addEventListener('touchmove', this._handleMouseMove);
614
701
  win.addEventListener('mouseup', this._handleMouseUp, true);
615
702
  win.addEventListener('touchend', this._handleMouseUp, true);
703
+ win.addEventListener('touchcancel', this._handleMouseUp, true);
616
704
  }
617
705
  this._transforming = true;
618
706
  // the pointer that hit the anchor (a touchstart can land several fingers)
@@ -630,6 +718,10 @@ export class Transformer extends Group {
630
718
  });
631
719
  }
632
720
  _handleMouseMove(e) {
721
+ var _a;
722
+ (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._batchEvents(() => this._moveTransform(e));
723
+ }
724
+ _moveTransform(e) {
633
725
  let x, y, newHypotenuse;
634
726
  const anchorNode = this._anchors[this._movingAnchorName];
635
727
  const stage = anchorNode.getStage();
@@ -659,11 +751,8 @@ export class Transformer extends Group {
659
751
  y = -anchorNode.y() + attrs.height / 2;
660
752
  // Calculate angle from center to current anchor position
661
753
  // Offset by rotateAnchorAngle so we measure rotation from the anchor's starting position
662
- const rotateAnchorAngleRad = Konva.getAngle(this.rotateAnchorAngle());
754
+ const rotateAnchorAngleRad = Util.degToRad(this.rotateAnchorAngle());
663
755
  let delta = Math.atan2(-y, x) + Math.PI / 2 - rotateAnchorAngleRad;
664
- if (attrs.height < 0) {
665
- delta -= Math.PI;
666
- }
667
756
  const oldRotation = Konva.getAngle(this.rotation());
668
757
  const newRotation = oldRotation + delta;
669
758
  const tol = Konva.getAngle(this.rotationSnapTolerance());
@@ -838,7 +927,10 @@ export class Transformer extends Group {
838
927
  return;
839
928
  }
840
929
  }
841
- this._removeEvents(e);
930
+ if (stage)
931
+ stage._batchEvents(() => this._removeEvents(e));
932
+ else
933
+ this._removeEvents(e);
842
934
  }
843
935
  // the transformer positions itself in absolute coordinates (see
844
936
  // _getNodeRect), whatever the transforms of its ancestors are
@@ -847,9 +939,12 @@ export class Transformer extends Group {
847
939
  return top ? top.getAbsoluteTransform().copy().invert().multiply(at) : at;
848
940
  }
849
941
  _removeEvents(e) {
850
- var _a;
942
+ var _a, _b;
851
943
  if (this._transforming) {
852
944
  this._transforming = false;
945
+ const content = (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a.content;
946
+ if (content && !this._cursorChange)
947
+ content.style.cursor = '';
853
948
  const win = this._transformWindow;
854
949
  this._transformWindow = null;
855
950
  if (win) {
@@ -857,12 +952,13 @@ export class Transformer extends Group {
857
952
  win.removeEventListener('touchmove', this._handleMouseMove);
858
953
  win.removeEventListener('mouseup', this._handleMouseUp, true);
859
954
  win.removeEventListener('touchend', this._handleMouseUp, true);
955
+ win.removeEventListener('touchcancel', this._handleMouseUp, true);
860
956
  }
861
957
  const node = this.getNode();
862
958
  activeTransformers.delete(this);
863
959
  this._fire('transformend', { evt: e, target: node });
864
960
  // redraw layer to restore hit graph
865
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
961
+ (_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
866
962
  if (node) {
867
963
  this._nodes.forEach((target) => {
868
964
  var _a;
@@ -875,17 +971,12 @@ export class Transformer extends Group {
875
971
  }
876
972
  }
877
973
  _fitNodesInto(newAttrs, evt, anchorProjected = false) {
878
- // Perf: suspend autoDraw for the whole method so per-attr _setAttr writes
879
- // don't each trigger _requestDraw -> getLayer parent-chain walks.
880
- // batchDraw per affected layer is issued at the end.
881
- const prevAutoDraw = Konva.autoDrawEnabled;
882
- Konva.autoDrawEnabled = false;
974
+ // Defer our own bounds updates while preserving drawing in user callbacks.
883
975
  this._fitting = true;
884
976
  try {
885
977
  return this._doFitNodesInto(newAttrs, evt, anchorProjected);
886
978
  }
887
979
  finally {
888
- Konva.autoDrawEnabled = prevAutoDraw;
889
980
  this._fitting = false;
890
981
  }
891
982
  }
@@ -1063,7 +1154,7 @@ export class Transformer extends Group {
1063
1154
  if (!((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) || this._fitting || this.isDragging()) {
1064
1155
  return;
1065
1156
  }
1066
- this.update();
1157
+ this._update();
1067
1158
  });
1068
1159
  }
1069
1160
  /**
@@ -1077,9 +1168,39 @@ export class Transformer extends Group {
1077
1168
  this.update();
1078
1169
  }
1079
1170
  update() {
1171
+ // Explicit updates and Transformer configuration changes also refresh
1172
+ // styles and layout when the selected nodes' bounds are unchanged.
1173
+ this._lastNodeRect = undefined;
1174
+ this._update();
1175
+ }
1176
+ _update() {
1080
1177
  var _a;
1081
1178
  const attrs = this._getNodeRect();
1082
- this.rotation(Util._getRotation(attrs.rotation));
1179
+ this._updateElements(attrs);
1180
+ const draggable = this.nodes().some((node) => node.draggable());
1181
+ if (this._back.draggable() !== draggable) {
1182
+ this._back.draggable(draggable);
1183
+ }
1184
+ const styleFunc = this.anchorStyleFunc();
1185
+ if (styleFunc) {
1186
+ Object.values(this._anchors).forEach((node) => styleFunc(node));
1187
+ }
1188
+ (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
1189
+ }
1190
+ _updateElements(attrs) {
1191
+ const previous = this._lastNodeRect;
1192
+ // Only element layout is cached. Drag handling, user callbacks and drawing
1193
+ // remain in _update(). Custom styling needs fresh defaults on every update.
1194
+ if (previous &&
1195
+ !this.anchorStyleFunc() &&
1196
+ attrs.x === previous.x &&
1197
+ attrs.y === previous.y &&
1198
+ attrs.width === previous.width &&
1199
+ attrs.height === previous.height &&
1200
+ attrs.rotation === previous.rotation) {
1201
+ return;
1202
+ }
1203
+ this._lastNodeRect = { ...attrs };
1083
1204
  const width = attrs.width;
1084
1205
  const height = attrs.height;
1085
1206
  const enabledAnchors = this.enabledAnchors();
@@ -1151,64 +1272,22 @@ export class Transformer extends Group {
1151
1272
  offsetY: anchorSize / 2 - padding,
1152
1273
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
1153
1274
  });
1154
- // Calculate rotation anchor position based on rotateAnchorAngle
1155
- const rotateAnchorAngle = this.rotateAnchorAngle();
1156
- const rotateAnchorOffset = this.rotateAnchorOffset();
1157
- const rad = Util.degToRad(rotateAnchorAngle);
1158
- // Direction vector (0 degrees = up/top)
1159
- const dirX = Math.sin(rad);
1160
- const dirY = -Math.cos(rad);
1161
- // Center of the box
1162
- const cx = width / 2;
1163
- const cy = height / 2;
1164
- // Find intersection with box edge
1165
- // Calculate time to hit each edge from center
1166
- let t = Infinity;
1167
- // Handle each direction
1168
- if (dirY < 0) {
1169
- // Moving up, check top edge (y = 0)
1170
- t = Math.min(t, -cy / dirY);
1171
- }
1172
- else if (dirY > 0) {
1173
- // Moving down, check bottom edge (y = height)
1174
- t = Math.min(t, (height - cy) / dirY);
1175
- }
1176
- if (dirX < 0) {
1177
- // Moving left, check left edge (x = 0)
1178
- t = Math.min(t, -cx / dirX);
1179
- }
1180
- else if (dirX > 0) {
1181
- // Moving right, check right edge (x = width)
1182
- t = Math.min(t, (width - cx) / dirX);
1183
- }
1184
- // Edge point
1185
- const edgeX = cx + dirX * t;
1186
- const edgeY = cy + dirY * t;
1187
- // Final position with offset (accounting for height sign and padding)
1188
- const sign = Util._sign(height);
1275
+ const rotaterLine = getRotaterLine(this, width, height);
1189
1276
  this._anchors['rotater'].setAttrs({
1190
- x: edgeX + dirX * rotateAnchorOffset * sign,
1191
- y: edgeY + dirY * rotateAnchorOffset * sign - padding * dirY,
1277
+ x: rotaterLine.endX,
1278
+ y: rotaterLine.endY,
1192
1279
  visible: this.rotateEnabled(),
1193
1280
  });
1194
1281
  this._back.setAttrs({
1195
1282
  width: width,
1196
1283
  height: height,
1197
- visible: this.borderEnabled(),
1284
+ visible: this.borderEnabled() || this.shouldOverdrawWholeArea(),
1198
1285
  stroke: this.borderStroke(),
1199
1286
  strokeWidth: this.borderStrokeWidth(),
1200
1287
  dash: this.borderDash(),
1201
- draggable: this.nodes().some((node) => node.draggable()),
1202
1288
  x: 0,
1203
1289
  y: 0,
1204
1290
  });
1205
- const styleFunc = this.anchorStyleFunc();
1206
- if (styleFunc) {
1207
- anchors.forEach((node) => {
1208
- styleFunc(node);
1209
- });
1210
- }
1211
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
1212
1291
  }
1213
1292
  /**
1214
1293
  * determine if transformer is in active transform
package/lib/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export interface GetSet<Type, This, SetType = Type> {
1
+ export interface GetSet<Type, This, SetType = Type | null | undefined> {
2
2
  (): Type;
3
- (v: SetType | null | undefined): This;
3
+ (v: SetType): This;
4
4
  }
5
5
  export interface Vector2d {
6
6
  x: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "konva",
3
- "version": "10.4.0",
3
+ "version": "10.6.0",
4
4
  "description": "HTML5 2d canvas library for interactive graphics, design editors, whiteboards, and diagrams.",
5
5
  "author": "Anton Lavrenov",
6
6
  "type": "module",
@@ -48,8 +48,8 @@
48
48
  "test:build": "rimraf ./.parcel-cache && PARCEL_WORKER_BACKEND=process parcel build ./test/unit-tests.html --dist-dir ./test-build --target none --public-url ./ --no-source-maps",
49
49
  "test:browser": "npm run test:build && mocha-headless-chrome -f ./test-build/unit-tests.html -a disable-web-security -a no-sandbox -a disable-setuid-sandbox",
50
50
  "test:watch": "rimraf ./.parcel-cache && PARCEL_WORKERS=0 parcel serve ./test/unit-tests.html ./test/manual-tests.html ./test/sandbox.html ./test/text-paths.html ./test/bunnies.html",
51
- "test:node:canvas": "mocha -r tsx -r ./test/node-canvas-global-setup.mjs --extension ts --recursive './test/unit/'",
52
- "test:node:skia": "mocha -r tsx -r ./test/node-skia-global-setup.mjs --extension ts --recursive './test/unit/'",
51
+ "test:node:canvas": "mocha -r tsx -r ./test/node-canvas-global-setup.mjs --file ./test/setup.ts --extension ts --recursive './test/unit/'",
52
+ "test:node:skia": "mocha -r tsx -r ./test/node-skia-global-setup.mjs --file ./test/setup.ts --extension ts --recursive './test/unit/'",
53
53
  "test:node": "npm run test:node:canvas && npm run test:node:skia",
54
54
  "tsc": "tsc",
55
55
  "rollup": "rollup -c",
@@ -110,6 +110,7 @@
110
110
  "rollup-plugin-typescript2": "^0.37.0",
111
111
  "size-limit": "^11.2.0",
112
112
  "skia-canvas": "^3.0.4",
113
+ "stats.js": "0.17.0",
113
114
  "tsx": "^4.21.0",
114
115
  "typescript": "^5.9.3"
115
116
  },