konva 10.1.0 → 10.2.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.
package/konva.js CHANGED
@@ -5,10 +5,10 @@
5
5
  })(this, (function () { 'use strict';
6
6
 
7
7
  /*
8
- * Konva JavaScript Framework v10.1.0
8
+ * Konva JavaScript Framework v10.2.1
9
9
  * http://konvajs.org/
10
10
  * Licensed under the MIT
11
- * Date: Wed Jan 14 2026
11
+ * Date: Fri Mar 13 2026
12
12
  *
13
13
  * Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
14
14
  * Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva)
@@ -35,7 +35,7 @@
35
35
  : {};
36
36
  const Konva$2 = {
37
37
  _global: glob,
38
- version: '10.1.0',
38
+ version: '10.2.1',
39
39
  isBrowser: detectBrowser(),
40
40
  isUnminified: /param/.test(function (param) { }.toString()),
41
41
  dblClickWindow: 400,
@@ -3220,73 +3220,15 @@ js: import "konva/skia-backend";
3220
3220
  this._filterUpToDate = true;
3221
3221
  return filterCanvas;
3222
3222
  }
3223
- /**
3224
- * bind events to the node. KonvaJS supports mouseover, mousemove,
3225
- * mouseout, mouseenter, mouseleave, mousedown, mouseup, wheel, contextmenu, click, dblclick, touchstart, touchmove,
3226
- * touchend, tap, dbltap, dragstart, dragmove, and dragend events.
3227
- * Pass in a string of events delimited by a space to bind multiple events at once
3228
- * such as 'mousedown mouseup mousemove'. Include a namespace to bind an
3229
- * event by name such as 'click.foobar'.
3230
- * @method
3231
- * @name Konva.Node#on
3232
- * @param {String} evtStr e.g. 'click', 'mousedown touchstart', 'mousedown.foo touchstart.foo'
3233
- * @param {Function} handler The handler function. The first argument of that function is event object. Event object has `target` as main target of the event, `currentTarget` as current node listener and `evt` as native browser event.
3234
- * @returns {Konva.Node}
3235
- * @example
3236
- * // add click listener
3237
- * node.on('click', function() {
3238
- * console.log('you clicked me!');
3239
- * });
3240
- *
3241
- * // get the target node
3242
- * node.on('click', function(evt) {
3243
- * console.log(evt.target);
3244
- * });
3245
- *
3246
- * // stop event propagation
3247
- * node.on('click', function(evt) {
3248
- * evt.cancelBubble = true;
3249
- * });
3250
- *
3251
- * // bind multiple listeners
3252
- * node.on('click touchstart', function() {
3253
- * console.log('you clicked/touched me!');
3254
- * });
3255
- *
3256
- * // namespace listener
3257
- * node.on('click.foo', function() {
3258
- * console.log('you clicked/touched me!');
3259
- * });
3260
- *
3261
- * // get the event type
3262
- * node.on('click tap', function(evt) {
3263
- * var eventType = evt.type;
3264
- * });
3265
- *
3266
- * // get native event object
3267
- * node.on('click tap', function(evt) {
3268
- * var nativeEvent = evt.evt;
3269
- * });
3270
- *
3271
- * // for change events, get the old and new val
3272
- * node.on('xChange', function(evt) {
3273
- * var oldVal = evt.oldVal;
3274
- * var newVal = evt.newVal;
3275
- * });
3276
- *
3277
- * // get event targets
3278
- * // with event delegations
3279
- * layer.on('click', 'Group', function(evt) {
3280
- * var shape = evt.target;
3281
- * var group = evt.currentTarget;
3282
- * });
3283
- */
3284
- on(evtStr, handler) {
3223
+ on(...args) {
3224
+ const evtStr = args[0];
3225
+ const selectorOrHandler = args[1];
3226
+ args[2];
3285
3227
  if (this._cache) {
3286
3228
  this._cache.delete(ALL_LISTENERS);
3287
3229
  }
3288
- if (arguments.length === 3) {
3289
- return this._delegate.apply(this, arguments);
3230
+ if (args.length === 3) {
3231
+ return this._delegate.apply(this, args);
3290
3232
  }
3291
3233
  const events = evtStr.split(SPACE$1);
3292
3234
  /*
@@ -3303,7 +3245,7 @@ js: import "konva/skia-backend";
3303
3245
  if (!this.eventListeners[baseEvent]) {
3304
3246
  this.eventListeners[baseEvent] = [];
3305
3247
  }
3306
- this.eventListeners[baseEvent].push({ name, handler });
3248
+ this.eventListeners[baseEvent].push({ name, handler: selectorOrHandler });
3307
3249
  }
3308
3250
  return this;
3309
3251
  }
@@ -3387,6 +3329,7 @@ js: import "konva/skia-backend";
3387
3329
  handler.call(targets[i], evt);
3388
3330
  }
3389
3331
  });
3332
+ return this;
3390
3333
  }
3391
3334
  /**
3392
3335
  * remove a node from parent, but don't destroy. You can reuse the node later.
@@ -3403,6 +3346,13 @@ js: import "konva/skia-backend";
3403
3346
  // we can have drag element but that is not dragged yet
3404
3347
  // so just clear it
3405
3348
  DD._dragElements.delete(this._id);
3349
+ // also remove any dragging descendants, otherwise they'll
3350
+ // have a broken parent chain and crash on getStage()
3351
+ DD._dragElements.forEach((elem, key) => {
3352
+ if (this.isAncestorOf(elem.node)) {
3353
+ DD._dragElements.delete(key);
3354
+ }
3355
+ });
3406
3356
  this._remove();
3407
3357
  return this;
3408
3358
  }
@@ -4851,6 +4801,7 @@ js: import "konva/skia-backend";
4851
4801
  },
4852
4802
  dragStatus: 'ready',
4853
4803
  pointerId,
4804
+ startEvent: evt,
4854
4805
  });
4855
4806
  }
4856
4807
  /**
@@ -4867,7 +4818,9 @@ js: import "konva/skia-backend";
4867
4818
  this.fire('dragstart', {
4868
4819
  type: 'dragstart',
4869
4820
  target: this,
4870
- evt: evt && evt.evt,
4821
+ // Use the stored start event if available (from mousedown/touchstart),
4822
+ // otherwise fall back to the provided event (when startDrag is called programmatically)
4823
+ evt: (elem.startEvent && elem.startEvent.evt) || (evt && evt.evt),
4871
4824
  }, bubbleEvent);
4872
4825
  }
4873
4826
  _setDragPosition(evt, elem) {
@@ -5056,7 +5009,7 @@ js: import "konva/skia-backend";
5056
5009
  // attache events listeners once into prototype
5057
5010
  // that way we don't spend too much time on making an new instance
5058
5011
  Node.prototype.eventListeners = {};
5059
- Node.prototype.on.call(Node.prototype, TRANSFORM_CHANGE_STR$1, function () {
5012
+ Node.prototype.on(TRANSFORM_CHANGE_STR$1, function () {
5060
5013
  if (this._batchingTransformChange) {
5061
5014
  this._needClearTransformCache = true;
5062
5015
  return;
@@ -5064,13 +5017,13 @@ js: import "konva/skia-backend";
5064
5017
  this._clearCache(TRANSFORM);
5065
5018
  this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
5066
5019
  });
5067
- Node.prototype.on.call(Node.prototype, 'visibleChange.konva', function () {
5020
+ Node.prototype.on('visibleChange.konva', function () {
5068
5021
  this._clearSelfAndDescendantCache(VISIBLE);
5069
5022
  });
5070
- Node.prototype.on.call(Node.prototype, 'listeningChange.konva', function () {
5023
+ Node.prototype.on('listeningChange.konva', function () {
5071
5024
  this._clearSelfAndDescendantCache(LISTENING);
5072
5025
  });
5073
- Node.prototype.on.call(Node.prototype, 'opacityChange.konva', function () {
5026
+ Node.prototype.on('opacityChange.konva', function () {
5074
5027
  this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
5075
5028
  });
5076
5029
  const addGetterSetter = Factory.addGetterSetter;
@@ -7717,11 +7670,11 @@ js: import "konva/skia-backend";
7717
7670
  Shape.prototype.nodeType = 'Shape';
7718
7671
  _registerNode(Shape);
7719
7672
  Shape.prototype.eventListeners = {};
7720
- Shape.prototype.on.call(Shape.prototype, 'shadowColorChange.konva shadowBlurChange.konva shadowOffsetChange.konva shadowOpacityChange.konva shadowEnabledChange.konva', _clearHasShadowCache);
7721
- Shape.prototype.on.call(Shape.prototype, 'shadowColorChange.konva shadowOpacityChange.konva shadowEnabledChange.konva', _clearGetShadowRGBACache);
7722
- Shape.prototype.on.call(Shape.prototype, 'fillPriorityChange.konva fillPatternImageChange.konva fillPatternRepeatChange.konva fillPatternScaleXChange.konva fillPatternScaleYChange.konva fillPatternOffsetXChange.konva fillPatternOffsetYChange.konva fillPatternXChange.konva fillPatternYChange.konva fillPatternRotationChange.konva', _clearFillPatternCache);
7723
- Shape.prototype.on.call(Shape.prototype, 'fillPriorityChange.konva fillLinearGradientColorStopsChange.konva fillLinearGradientStartPointXChange.konva fillLinearGradientStartPointYChange.konva fillLinearGradientEndPointXChange.konva fillLinearGradientEndPointYChange.konva', _clearLinearGradientCache);
7724
- Shape.prototype.on.call(Shape.prototype, 'fillPriorityChange.konva fillRadialGradientColorStopsChange.konva fillRadialGradientStartPointXChange.konva fillRadialGradientStartPointYChange.konva fillRadialGradientEndPointXChange.konva fillRadialGradientEndPointYChange.konva fillRadialGradientStartRadiusChange.konva fillRadialGradientEndRadiusChange.konva', _clearRadialGradientCache);
7673
+ Shape.prototype.on('shadowColorChange.konva shadowBlurChange.konva shadowOffsetChange.konva shadowOpacityChange.konva shadowEnabledChange.konva', _clearHasShadowCache);
7674
+ Shape.prototype.on('shadowColorChange.konva shadowOpacityChange.konva shadowEnabledChange.konva', _clearGetShadowRGBACache);
7675
+ Shape.prototype.on('fillPriorityChange.konva fillPatternImageChange.konva fillPatternRepeatChange.konva fillPatternScaleXChange.konva fillPatternScaleYChange.konva fillPatternOffsetXChange.konva fillPatternOffsetYChange.konva fillPatternXChange.konva fillPatternYChange.konva fillPatternRotationChange.konva', _clearFillPatternCache);
7676
+ Shape.prototype.on('fillPriorityChange.konva fillLinearGradientColorStopsChange.konva fillLinearGradientStartPointXChange.konva fillLinearGradientStartPointYChange.konva fillLinearGradientEndPointXChange.konva fillLinearGradientEndPointYChange.konva', _clearLinearGradientCache);
7677
+ Shape.prototype.on('fillPriorityChange.konva fillRadialGradientColorStopsChange.konva fillRadialGradientStartPointXChange.konva fillRadialGradientStartPointYChange.konva fillRadialGradientEndPointXChange.konva fillRadialGradientEndPointYChange.konva fillRadialGradientStartRadiusChange.konva fillRadialGradientEndRadiusChange.konva', _clearRadialGradientCache);
7725
7678
  // add getters and setters
7726
7679
  Factory.addGetterSetter(Shape, 'stroke', undefined, getStringOrGradientValidator());
7727
7680
  /**
@@ -15878,7 +15831,7 @@ js: import "konva/skia-backend";
15878
15831
  });
15879
15832
  width += chars[i].width;
15880
15833
  }
15881
- const { height } = this._getTextSize(this.attrs.text);
15834
+ const { width: fullTextWidth, height } = this._getTextSize(this.attrs.text);
15882
15835
  this.textWidth = width;
15883
15836
  this.textHeight = height;
15884
15837
  this.glyphInfo = [];
@@ -15888,6 +15841,12 @@ js: import "konva/skia-backend";
15888
15841
  const letterSpacing = this.letterSpacing();
15889
15842
  const align = this.align();
15890
15843
  const kerningFunc = this.kerningFunc();
15844
+ // The sum of individual character widths can exceed the whole-string width
15845
+ // due to kerning (browsers place adjacent glyphs closer together than the
15846
+ // individual measurements suggest). This difference is the maximum amount
15847
+ // by which the accumulated glyph offset can legitimately overshoot the
15848
+ // path length when the path is sized to match the visually-measured text.
15849
+ const kerningAdjustment = Math.max(0, width - fullTextWidth);
15891
15850
  // defines the width of the text on a straight line
15892
15851
  const textWidth = Math.max(this.textWidth + ((this.attrs.text || '').length - 1) * letterSpacing, 0);
15893
15852
  let offset = 0;
@@ -15913,7 +15872,15 @@ js: import "konva/skia-backend";
15913
15872
  const numberOfSpaces = this.text().split(' ').length - 1;
15914
15873
  glyphWidth += (this.pathLength - textWidth) / numberOfSpaces;
15915
15874
  }
15916
- const charEndPoint = this._getPointAtLength(offsetToGlyph + glyphWidth);
15875
+ const charEndLength = offsetToGlyph + glyphWidth;
15876
+ // When the path length is sized to match the visually-measured (kerned)
15877
+ // text width, the accumulated per-glyph offset can exceed pathLength by
15878
+ // up to kerningAdjustment pixels. Clamp to pathLength in that case so
15879
+ // the last character is not silently dropped.
15880
+ const charEndPoint = this._getPointAtLength(charEndLength > this.pathLength &&
15881
+ charEndLength - this.pathLength <= kerningAdjustment
15882
+ ? this.pathLength
15883
+ : charEndLength);
15917
15884
  if (!charEndPoint) {
15918
15885
  return;
15919
15886
  }
@@ -16167,6 +16134,7 @@ js: import "konva/skia-backend";
16167
16134
  const ATTR_CHANGE_LIST = [
16168
16135
  'resizeEnabledChange',
16169
16136
  'rotateAnchorOffsetChange',
16137
+ 'rotateAnchorAngleChange',
16170
16138
  'rotateEnabledChange',
16171
16139
  'enabledAnchorsChange',
16172
16140
  'anchorSizeChange',
@@ -16673,11 +16641,44 @@ js: import "konva/skia-backend";
16673
16641
  sceneFunc(ctx, shape) {
16674
16642
  const tr = shape.getParent();
16675
16643
  const padding = tr.padding();
16644
+ const width = shape.width();
16645
+ const height = shape.height();
16676
16646
  ctx.beginPath();
16677
- ctx.rect(-padding, -padding, shape.width() + padding * 2, shape.height() + padding * 2);
16678
- ctx.moveTo(shape.width() / 2, -padding);
16647
+ ctx.rect(-padding, -padding, width + padding * 2, height + padding * 2);
16679
16648
  if (tr.rotateEnabled() && tr.rotateLineVisible()) {
16680
- ctx.lineTo(shape.width() / 2, -tr.rotateAnchorOffset() * Util._sign(shape.height()) - padding);
16649
+ // Calculate rotation line position based on rotateAnchorAngle
16650
+ const rotateAnchorAngle = tr.rotateAnchorAngle();
16651
+ const rotateAnchorOffset = tr.rotateAnchorOffset();
16652
+ const rad = Util.degToRad(rotateAnchorAngle);
16653
+ // Direction vector (0 degrees = up/top)
16654
+ const dirX = Math.sin(rad);
16655
+ const dirY = -Math.cos(rad);
16656
+ // Center of the box
16657
+ const cx = width / 2;
16658
+ const cy = height / 2;
16659
+ // Find intersection with box edge
16660
+ let t = Infinity;
16661
+ if (dirY < 0) {
16662
+ t = Math.min(t, -cy / dirY);
16663
+ }
16664
+ else if (dirY > 0) {
16665
+ t = Math.min(t, (height - cy) / dirY);
16666
+ }
16667
+ if (dirX < 0) {
16668
+ t = Math.min(t, -cx / dirX);
16669
+ }
16670
+ else if (dirX > 0) {
16671
+ t = Math.min(t, (width - cx) / dirX);
16672
+ }
16673
+ // Edge point (start of line)
16674
+ const edgeX = cx + dirX * t;
16675
+ const edgeY = cy + dirY * t;
16676
+ // End point with offset
16677
+ const sign = Util._sign(height);
16678
+ const endX = edgeX + dirX * rotateAnchorOffset * sign;
16679
+ const endY = edgeY + dirY * rotateAnchorOffset * sign;
16680
+ ctx.moveTo(edgeX, edgeY);
16681
+ ctx.lineTo(endX, endY);
16681
16682
  }
16682
16683
  ctx.fillStrokeShape(shape);
16683
16684
  },
@@ -16767,8 +16768,10 @@ js: import "konva/skia-backend";
16767
16768
  const attrs = this._getNodeRect();
16768
16769
  x = anchorNode.x() - attrs.width / 2;
16769
16770
  y = -anchorNode.y() + attrs.height / 2;
16770
- // hor angle is changed?
16771
- let delta = Math.atan2(-y, x) + Math.PI / 2;
16771
+ // Calculate angle from center to current anchor position
16772
+ // Offset by rotateAnchorAngle so we measure rotation from the anchor's starting position
16773
+ const rotateAnchorAngleRad = Konva$2.getAngle(this.rotateAnchorAngle());
16774
+ let delta = Math.atan2(-y, x) + Math.PI / 2 - rotateAnchorAngleRad;
16772
16775
  if (attrs.height < 0) {
16773
16776
  delta -= Math.PI;
16774
16777
  }
@@ -17195,9 +17198,44 @@ js: import "konva/skia-backend";
17195
17198
  offsetY: anchorSize / 2 - padding,
17196
17199
  visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
17197
17200
  });
17201
+ // Calculate rotation anchor position based on rotateAnchorAngle
17202
+ const rotateAnchorAngle = this.rotateAnchorAngle();
17203
+ const rotateAnchorOffset = this.rotateAnchorOffset();
17204
+ const rad = Util.degToRad(rotateAnchorAngle);
17205
+ // Direction vector (0 degrees = up/top)
17206
+ const dirX = Math.sin(rad);
17207
+ const dirY = -Math.cos(rad);
17208
+ // Center of the box
17209
+ const cx = width / 2;
17210
+ const cy = height / 2;
17211
+ // Find intersection with box edge
17212
+ // Calculate time to hit each edge from center
17213
+ let t = Infinity;
17214
+ // Handle each direction
17215
+ if (dirY < 0) {
17216
+ // Moving up, check top edge (y = 0)
17217
+ t = Math.min(t, -cy / dirY);
17218
+ }
17219
+ else if (dirY > 0) {
17220
+ // Moving down, check bottom edge (y = height)
17221
+ t = Math.min(t, (height - cy) / dirY);
17222
+ }
17223
+ if (dirX < 0) {
17224
+ // Moving left, check left edge (x = 0)
17225
+ t = Math.min(t, -cx / dirX);
17226
+ }
17227
+ else if (dirX > 0) {
17228
+ // Moving right, check right edge (x = width)
17229
+ t = Math.min(t, (width - cx) / dirX);
17230
+ }
17231
+ // Edge point
17232
+ const edgeX = cx + dirX * t;
17233
+ const edgeY = cy + dirY * t;
17234
+ // Final position with offset (accounting for height sign and padding)
17235
+ const sign = Util._sign(height);
17198
17236
  this._batchChangeChild('.rotater', {
17199
- x: width / 2,
17200
- y: -this.rotateAnchorOffset() * Util._sign(height) - padding,
17237
+ x: edgeX + dirX * rotateAnchorOffset * sign,
17238
+ y: edgeY + dirY * rotateAnchorOffset * sign - padding * dirY,
17201
17239
  visible: this.rotateEnabled(),
17202
17240
  });
17203
17241
  this._batchChangeChild('.back', {
@@ -17406,6 +17444,24 @@ js: import "konva/skia-backend";
17406
17444
  * transformer.rotateAnchorOffset(100);
17407
17445
  */
17408
17446
  Factory.addGetterSetter(Transformer, 'rotateAnchorOffset', 50, getNumberValidator());
17447
+ /**
17448
+ * get/set the angle (in degrees) of the rotation anchor position around the bounding box.
17449
+ * 0 = top-center (default), 90 = middle-right, 180 = bottom-center, -90 = middle-left
17450
+ * @name Konva.Transformer#rotateAnchorAngle
17451
+ * @method
17452
+ * @param {Number} angle
17453
+ * @returns {Number}
17454
+ * @example
17455
+ * // get
17456
+ * var rotateAnchorAngle = transformer.rotateAnchorAngle();
17457
+ *
17458
+ * // set rotation anchor to the right side
17459
+ * transformer.rotateAnchorAngle(90);
17460
+ *
17461
+ * // set rotation anchor to the bottom
17462
+ * transformer.rotateAnchorAngle(180);
17463
+ */
17464
+ Factory.addGetterSetter(Transformer, 'rotateAnchorAngle', 0, getNumberValidator());
17409
17465
  /**
17410
17466
  * get/set rotation anchor cursor
17411
17467
  * @name Konva.Transformer#rotateAnchorCursor