konva 10.5.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 (62) hide show
  1. package/README.md +4 -4
  2. package/konva.js +3247 -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 +4 -3
  14. package/lib/DragAndDrop.js +39 -25
  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 +21 -12
  21. package/lib/Node.js +159 -126
  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 +19 -13
  27. package/lib/Stage.js +182 -86
  28. package/lib/Tween.js +59 -66
  29. package/lib/Util.d.ts +2 -2
  30. package/lib/Util.js +21 -16
  31. package/lib/_CoreInternals.d.ts +23 -11
  32. package/lib/_FullInternals.d.ts +23 -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 +11 -2
  53. package/lib/shapes/Path.js +132 -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.js +88 -91
  61. package/lib/types.d.ts +2 -2
  62. 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
  });
@@ -375,6 +428,7 @@ export class Transformer extends Group {
375
428
  * transformer.detach();
376
429
  */
377
430
  detach() {
431
+ var _a;
378
432
  // remove events
379
433
  if (this._nodes) {
380
434
  this._nodes.forEach((node) => {
@@ -383,6 +437,7 @@ export class Transformer extends Group {
383
437
  }
384
438
  this._nodes = [];
385
439
  this._resetTransformCache();
440
+ (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
386
441
  }
387
442
  /**
388
443
  * bind events to the Transformer. You can use events: `transform`, `transformstart`, `transformend`, `dragstart`, `dragmove`, `dragend`
@@ -454,8 +509,11 @@ export class Transformer extends Group {
454
509
  bounds.version !== node._attrsVersion ||
455
510
  bounds.width !== width ||
456
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;
457
515
  const box = node.getClientRect({
458
- skipTransform: true,
516
+ skipTransform: !collapsed,
459
517
  skipShadow: true,
460
518
  skipStroke: ignoreStroke,
461
519
  });
@@ -465,7 +523,8 @@ export class Transformer extends Group {
465
523
  { x: box.x + box.width, y: box.y + box.height },
466
524
  { x: box.x, y: box.y + box.height },
467
525
  ];
468
- const trans = node.getAbsoluteTransform();
526
+ if (collapsed)
527
+ trans = new Transform();
469
528
  bounds = {
470
529
  rotation,
471
530
  ignoreStroke,
@@ -548,6 +607,8 @@ export class Transformer extends Group {
548
607
  });
549
608
  // add hover styling
550
609
  anchor.on('mouseenter', () => {
610
+ if (this.isTransforming())
611
+ return;
551
612
  const rad = Konva.getAngle(this.rotation());
552
613
  const rotateCursor = this.rotateAnchorCursor();
553
614
  const cursor = getCursor(name, rad, rotateCursor);
@@ -556,9 +617,11 @@ export class Transformer extends Group {
556
617
  this._cursorChange = true;
557
618
  });
558
619
  anchor.on('mouseout', () => {
620
+ this._cursorChange = false;
621
+ if (this.isTransforming())
622
+ return;
559
623
  anchor.getStage().content &&
560
624
  (anchor.getStage().content.style.cursor = '');
561
- this._cursorChange = false;
562
625
  });
563
626
  this.add(anchor);
564
627
  }
@@ -569,45 +632,17 @@ export class Transformer extends Group {
569
632
  height: 0,
570
633
  sceneFunc(ctx, shape) {
571
634
  const tr = shape.getParent();
635
+ if (!tr.borderEnabled())
636
+ return;
572
637
  const padding = tr.padding();
573
638
  const width = shape.width();
574
639
  const height = shape.height();
575
640
  ctx.beginPath();
576
641
  ctx.rect(-padding, -padding, width + padding * 2, height + padding * 2);
577
642
  if (tr.rotateEnabled() && tr.rotateLineVisible()) {
578
- // Calculate rotation line position based on rotateAnchorAngle
579
- const rotateAnchorAngle = tr.rotateAnchorAngle();
580
- const rotateAnchorOffset = tr.rotateAnchorOffset();
581
- const rad = Util.degToRad(rotateAnchorAngle);
582
- // Direction vector (0 degrees = up/top)
583
- const dirX = Math.sin(rad);
584
- const dirY = -Math.cos(rad);
585
- // Center of the box
586
- const cx = width / 2;
587
- const cy = height / 2;
588
- // Find intersection with box edge
589
- let t = Infinity;
590
- if (dirY < 0) {
591
- t = Math.min(t, -cy / dirY);
592
- }
593
- else if (dirY > 0) {
594
- t = Math.min(t, (height - cy) / dirY);
595
- }
596
- if (dirX < 0) {
597
- t = Math.min(t, -cx / dirX);
598
- }
599
- else if (dirX > 0) {
600
- t = Math.min(t, (width - cx) / dirX);
601
- }
602
- // Edge point (start of line)
603
- const edgeX = cx + dirX * t;
604
- const edgeY = cy + dirY * t;
605
- // End point with offset
606
- const sign = Util._sign(height);
607
- const endX = edgeX + dirX * rotateAnchorOffset * sign;
608
- const endY = edgeY + dirY * rotateAnchorOffset * sign;
609
- ctx.moveTo(edgeX, edgeY);
610
- 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);
611
646
  }
612
647
  ctx.fillStrokeShape(shape);
613
648
  },
@@ -643,6 +678,8 @@ export class Transformer extends Group {
643
678
  }
644
679
  _handleMouseDown(e) {
645
680
  var _a, _b;
681
+ if (e.evt.button !== undefined && !Konva.dragButtons.includes(e.evt.button))
682
+ return;
646
683
  // do nothing if we already transforming
647
684
  // that is possible to trigger with multitouch
648
685
  if (this._transforming) {
@@ -714,11 +751,8 @@ export class Transformer extends Group {
714
751
  y = -anchorNode.y() + attrs.height / 2;
715
752
  // Calculate angle from center to current anchor position
716
753
  // Offset by rotateAnchorAngle so we measure rotation from the anchor's starting position
717
- const rotateAnchorAngleRad = Konva.getAngle(this.rotateAnchorAngle());
754
+ const rotateAnchorAngleRad = Util.degToRad(this.rotateAnchorAngle());
718
755
  let delta = Math.atan2(-y, x) + Math.PI / 2 - rotateAnchorAngleRad;
719
- if (attrs.height < 0) {
720
- delta -= Math.PI;
721
- }
722
756
  const oldRotation = Konva.getAngle(this.rotation());
723
757
  const newRotation = oldRotation + delta;
724
758
  const tol = Konva.getAngle(this.rotationSnapTolerance());
@@ -905,9 +939,12 @@ export class Transformer extends Group {
905
939
  return top ? top.getAbsoluteTransform().copy().invert().multiply(at) : at;
906
940
  }
907
941
  _removeEvents(e) {
908
- var _a;
942
+ var _a, _b;
909
943
  if (this._transforming) {
910
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 = '';
911
948
  const win = this._transformWindow;
912
949
  this._transformWindow = null;
913
950
  if (win) {
@@ -921,7 +958,7 @@ export class Transformer extends Group {
921
958
  activeTransformers.delete(this);
922
959
  this._fire('transformend', { evt: e, target: node });
923
960
  // redraw layer to restore hit graph
924
- (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
961
+ (_b = this.getLayer()) === null || _b === void 0 ? void 0 : _b.batchDraw();
925
962
  if (node) {
926
963
  this._nodes.forEach((target) => {
927
964
  var _a;
@@ -934,17 +971,12 @@ export class Transformer extends Group {
934
971
  }
935
972
  }
936
973
  _fitNodesInto(newAttrs, evt, anchorProjected = false) {
937
- // Perf: suspend autoDraw for the whole method so per-attr _setAttr writes
938
- // don't each trigger _requestDraw -> getLayer parent-chain walks.
939
- // batchDraw per affected layer is issued at the end.
940
- const prevAutoDraw = Konva.autoDrawEnabled;
941
- Konva.autoDrawEnabled = false;
974
+ // Defer our own bounds updates while preserving drawing in user callbacks.
942
975
  this._fitting = true;
943
976
  try {
944
977
  return this._doFitNodesInto(newAttrs, evt, anchorProjected);
945
978
  }
946
979
  finally {
947
- Konva.autoDrawEnabled = prevAutoDraw;
948
980
  this._fitting = false;
949
981
  }
950
982
  }
@@ -1169,7 +1201,6 @@ export class Transformer extends Group {
1169
1201
  return;
1170
1202
  }
1171
1203
  this._lastNodeRect = { ...attrs };
1172
- this.rotation(Util._getRotation(attrs.rotation));
1173
1204
  const width = attrs.width;
1174
1205
  const height = attrs.height;
1175
1206
  const enabledAnchors = this.enabledAnchors();
@@ -1241,50 +1272,16 @@ export class Transformer extends Group {
1241
1272
  offsetY: anchorSize / 2 - padding,
1242
1273
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
1243
1274
  });
1244
- // Calculate rotation anchor position based on rotateAnchorAngle
1245
- const rotateAnchorAngle = this.rotateAnchorAngle();
1246
- const rotateAnchorOffset = this.rotateAnchorOffset();
1247
- const rad = Util.degToRad(rotateAnchorAngle);
1248
- // Direction vector (0 degrees = up/top)
1249
- const dirX = Math.sin(rad);
1250
- const dirY = -Math.cos(rad);
1251
- // Center of the box
1252
- const cx = width / 2;
1253
- const cy = height / 2;
1254
- // Find intersection with box edge
1255
- // Calculate time to hit each edge from center
1256
- let t = Infinity;
1257
- // Handle each direction
1258
- if (dirY < 0) {
1259
- // Moving up, check top edge (y = 0)
1260
- t = Math.min(t, -cy / dirY);
1261
- }
1262
- else if (dirY > 0) {
1263
- // Moving down, check bottom edge (y = height)
1264
- t = Math.min(t, (height - cy) / dirY);
1265
- }
1266
- if (dirX < 0) {
1267
- // Moving left, check left edge (x = 0)
1268
- t = Math.min(t, -cx / dirX);
1269
- }
1270
- else if (dirX > 0) {
1271
- // Moving right, check right edge (x = width)
1272
- t = Math.min(t, (width - cx) / dirX);
1273
- }
1274
- // Edge point
1275
- const edgeX = cx + dirX * t;
1276
- const edgeY = cy + dirY * t;
1277
- // Final position with offset (accounting for height sign and padding)
1278
- const sign = Util._sign(height);
1275
+ const rotaterLine = getRotaterLine(this, width, height);
1279
1276
  this._anchors['rotater'].setAttrs({
1280
- x: edgeX + dirX * rotateAnchorOffset * sign,
1281
- y: edgeY + dirY * rotateAnchorOffset * sign - padding * dirY,
1277
+ x: rotaterLine.endX,
1278
+ y: rotaterLine.endY,
1282
1279
  visible: this.rotateEnabled(),
1283
1280
  });
1284
1281
  this._back.setAttrs({
1285
1282
  width: width,
1286
1283
  height: height,
1287
- visible: this.borderEnabled(),
1284
+ visible: this.borderEnabled() || this.shouldOverdrawWholeArea(),
1288
1285
  stroke: this.borderStroke(),
1289
1286
  strokeWidth: this.borderStrokeWidth(),
1290
1287
  dash: this.borderDash(),
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.5.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
  },