konva 10.3.1 → 10.3.2

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.3.1
8
+ * Konva JavaScript Framework v10.3.2
9
9
  * http://konvajs.org/
10
10
  * Licensed under the MIT
11
- * Date: Fri Aug 14 2026
11
+ * Date: Wed Aug 26 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.3.1',
38
+ version: '10.3.2',
39
39
  isBrowser: detectBrowser(),
40
40
  isUnminified: /param/.test(function (param) { }.toString()),
41
41
  dblClickWindow: 400,
@@ -616,13 +616,24 @@ js: import "konva/skia-backend";
616
616
  yellow: [255, 255, 0],
617
617
  yellowgreen: [154, 205, 50],
618
618
  };
619
- let animQueue = [];
620
619
  // Cache for canvas farbling detection
621
620
  let _isCanvasFarblingActive = null;
622
- const req = (typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame) ||
623
- function (f) {
621
+ // A stage may be rendered in another window than the one Konva was imported
622
+ // into, and a window that is not visible gives almost no frames. So frames are
623
+ // asked from the window of the stage, and the callbacks are kept per window
624
+ const defaultWindow = typeof window !== 'undefined' ? window : {};
625
+ const animQueues = new WeakMap();
626
+ const requestFrame = (win, f) => {
627
+ if (typeof win.requestAnimationFrame === 'function') {
628
+ win.requestAnimationFrame(f);
629
+ }
630
+ else if (typeof requestAnimationFrame !== 'undefined') {
631
+ requestAnimationFrame(f);
632
+ }
633
+ else {
624
634
  setTimeout(f, 16); // 60fps ≈ 16.67ms per frame
625
- };
635
+ }
636
+ };
626
637
  const capitalizeCache = new Map();
627
638
  // Split the components of rgb()/hsl(). CSS separates them with commas (legacy
628
639
  // syntax), or with spaces and a slash before the alpha (CSS Color 4 syntax),
@@ -720,17 +731,20 @@ js: import "konva/skia-backend";
720
731
  return -1;
721
732
  }
722
733
  },
723
- requestAnimFrame(callback) {
724
- animQueue.push(callback);
725
- if (animQueue.length === 1) {
726
- req(function () {
727
- const queue = animQueue;
728
- animQueue = [];
734
+ requestAnimFrame(callback, win) {
735
+ const target = (win && !win.closed && win) || defaultWindow;
736
+ let queue = animQueues.get(target);
737
+ if (!queue) {
738
+ queue = [];
739
+ animQueues.set(target, queue);
740
+ requestFrame(target, function () {
741
+ animQueues.delete(target);
729
742
  queue.forEach(function (cb) {
730
743
  cb();
731
744
  });
732
745
  });
733
746
  }
747
+ queue.push(callback);
734
748
  },
735
749
  createCanvasElement() {
736
750
  ensureBrowser();
@@ -747,12 +761,9 @@ js: import "konva/skia-backend";
747
761
  return document.createElement('img');
748
762
  },
749
763
  _isInDocument(el) {
750
- while ((el = el.parentNode)) {
751
- if (el == document) {
752
- return true;
753
- }
754
- }
755
- return false;
764
+ // not `el.parentNode == document`, because the element may belong to
765
+ // another document: an iframe, or a popout window
766
+ return !!el.isConnected;
756
767
  },
757
768
  /*
758
769
  * arg can be an image object or image data
@@ -2272,6 +2283,14 @@ js: import "konva/skia-backend";
2272
2283
  this.setWidth(width || 0);
2273
2284
  this.setHeight(height || 0);
2274
2285
  }
2286
+ // setSize() re-allocates and clears the canvas even for the same size,
2287
+ // so lazily sized canvases use this to stay untouched when nothing changed
2288
+ setSizeIfChanged(width, height) {
2289
+ if (this.width !== width * this.pixelRatio ||
2290
+ this.height !== height * this.pixelRatio) {
2291
+ this.setSize(width, height);
2292
+ }
2293
+ }
2275
2294
  /**
2276
2295
  * to data url
2277
2296
  * @method
@@ -2337,13 +2356,36 @@ js: import "konva/skia-backend";
2337
2356
  return node;
2338
2357
  },
2339
2358
  _dragElements: new Map(),
2359
+ // Konva is imported into one window, but a stage may be rendered in another
2360
+ // one: an iframe, or a window opened with `window.open`. Such a window sends
2361
+ // its pointer events to itself, so `Konva.Stage` asks for every window it is
2362
+ // rendered in. The handlers get the window they listen to, as only that
2363
+ // window has pointer positions the stages of that window can use
2364
+ _listenToWindow(win) {
2365
+ const endDragBefore = (evt) => DD._endDragBefore(evt, win);
2366
+ const drag = (evt) => DD._drag(evt, win);
2367
+ win.addEventListener('mouseup', endDragBefore, true);
2368
+ win.addEventListener('touchend', endDragBefore, true);
2369
+ // add touchcancel to fix this: https://github.com/konvajs/konva/issues/1843
2370
+ win.addEventListener('touchcancel', endDragBefore, true);
2371
+ win.addEventListener('mousemove', drag);
2372
+ win.addEventListener('touchmove', drag);
2373
+ win.addEventListener('mouseup', DD._endDragAfter, false);
2374
+ win.addEventListener('touchend', DD._endDragAfter, false);
2375
+ win.addEventListener('touchcancel', DD._endDragAfter, false);
2376
+ },
2340
2377
  // methods
2341
- _drag(evt) {
2378
+ _drag(evt, win) {
2342
2379
  const nodesToFireEvents = [];
2343
2380
  DD._dragElements.forEach((elem, key) => {
2344
2381
  const { node } = elem;
2345
2382
  // we need to find pointer relative to that node
2346
2383
  const stage = node.getStage();
2384
+ // a pointer position of another window is relative to another viewport,
2385
+ // it would drag the node to a random place
2386
+ if (win && stage._getOwnerWindow() !== win) {
2387
+ return;
2388
+ }
2347
2389
  stage.setPointersPositions(evt);
2348
2390
  // it is possible that user call startDrag without any event
2349
2391
  // it that case we need to detect first movable pointer and attach it into the node
@@ -2385,13 +2427,15 @@ js: import "konva/skia-backend";
2385
2427
  },
2386
2428
  // dragBefore and dragAfter allows us to set correct order of events
2387
2429
  // setup all in dragbefore, and stop dragging only after pointerup triggered.
2388
- _endDragBefore(evt) {
2430
+ _endDragBefore(evt, win) {
2389
2431
  const drawNodes = [];
2390
2432
  DD._dragElements.forEach((elem) => {
2391
2433
  const { node } = elem;
2392
2434
  // we need to find pointer relative to that node
2393
2435
  const stage = node.getStage();
2394
- if (evt) {
2436
+ // a pointer released in another window ends the drag too - the pointer
2437
+ // is up everywhere - but its position is not used for this stage
2438
+ if (evt && (!win || stage._getOwnerWindow() === win)) {
2395
2439
  stage.setPointersPositions(evt);
2396
2440
  }
2397
2441
  const pos = stage._changedPointerPositions.find((pos) => pos.id === elem.pointerId);
@@ -2404,6 +2448,9 @@ js: import "konva/skia-backend";
2404
2448
  DD.justDragged = true;
2405
2449
  Konva$2._mouseListenClick = false;
2406
2450
  Konva$2._touchListenClick = false;
2451
+ // for real pointer input this reset comes too late — "pointerup" fired
2452
+ // before this handler — so Stage._pointerup also checks the live drag
2453
+ // state for the pointer family (issue #1756)
2407
2454
  Konva$2._pointerListenClick = false;
2408
2455
  elem.dragStatus = 'stopped';
2409
2456
  }
@@ -2435,17 +2482,6 @@ js: import "konva/skia-backend";
2435
2482
  });
2436
2483
  },
2437
2484
  };
2438
- if (Konva$2.isBrowser) {
2439
- window.addEventListener('mouseup', DD._endDragBefore, true);
2440
- window.addEventListener('touchend', DD._endDragBefore, true);
2441
- // add touchcancel to fix this: https://github.com/konvajs/konva/issues/1843
2442
- window.addEventListener('touchcancel', DD._endDragBefore, true);
2443
- window.addEventListener('mousemove', DD._drag);
2444
- window.addEventListener('touchmove', DD._drag);
2445
- window.addEventListener('mouseup', DD._endDragAfter, false);
2446
- window.addEventListener('touchend', DD._endDragAfter, false);
2447
- window.addEventListener('touchcancel', DD._endDragAfter, false);
2448
- }
2449
2485
 
2450
2486
  function _formatValue(val) {
2451
2487
  if (Util._isString(val)) {
@@ -2950,7 +2986,7 @@ js: import "konva/skia-backend";
2950
2986
  clearCache() {
2951
2987
  if (this._cache.has(CANVAS)) {
2952
2988
  const { scene, filter, hit } = this._cache.get(CANVAS);
2953
- Util.releaseCanvas(scene._canvas, filter._canvas, hit._canvas);
2989
+ Util.releaseCanvas(scene._canvas, filter._canvas, ...(hit ? [hit._canvas] : []));
2954
2990
  this._cache.delete(CANVAS);
2955
2991
  }
2956
2992
  this._clearSelfAndDescendantCache();
@@ -3047,11 +3083,7 @@ js: import "konva/skia-backend";
3047
3083
  width: 0,
3048
3084
  height: 0,
3049
3085
  willReadFrequently: true,
3050
- }), cachedHitCanvas = new HitCanvas({
3051
- pixelRatio: hitCanvasPixelRatio,
3052
- width: width,
3053
- height: height,
3054
- }), sceneContext = cachedSceneCanvas.getContext(), hitContext = cachedHitCanvas.getContext();
3086
+ }), sceneContext = cachedSceneCanvas.getContext();
3055
3087
  const bufferCanvas = new SceneCanvas({
3056
3088
  // width and height already multiplied by pixelRatio
3057
3089
  // so we need to revert that
@@ -3060,7 +3092,6 @@ js: import "konva/skia-backend";
3060
3092
  height: cachedSceneCanvas.height / cachedSceneCanvas.pixelRatio + Math.abs(y),
3061
3093
  pixelRatio: cachedSceneCanvas.pixelRatio,
3062
3094
  }), bufferContext = bufferCanvas.getContext();
3063
- cachedHitCanvas.isCache = true;
3064
3095
  cachedSceneCanvas.isCache = true;
3065
3096
  this._cache.delete(CANVAS);
3066
3097
  this._filterUpToDate = false;
@@ -3069,10 +3100,8 @@ js: import "konva/skia-backend";
3069
3100
  cachedFilterCanvas.getContext()._context.imageSmoothingEnabled = false;
3070
3101
  }
3071
3102
  sceneContext.save();
3072
- hitContext.save();
3073
3103
  bufferContext.save();
3074
3104
  sceneContext.translate(-x, -y);
3075
- hitContext.translate(-x, -y);
3076
3105
  bufferContext.translate(-x, -y);
3077
3106
  // hard-code offset to make sure content fits canvas
3078
3107
  // @ts-ignore
@@ -3084,10 +3113,8 @@ js: import "konva/skia-backend";
3084
3113
  this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
3085
3114
  this._clearSelfAndDescendantCache(ABSOLUTE_SCALE);
3086
3115
  this.drawScene(cachedSceneCanvas, this, bufferCanvas);
3087
- this.drawHit(cachedHitCanvas, this);
3088
3116
  this._isUnderCache = false;
3089
3117
  sceneContext.restore();
3090
- hitContext.restore();
3091
3118
  // this will draw a red border around the cached box for
3092
3119
  // debugging purposes
3093
3120
  if (drawBorder) {
@@ -3106,13 +3133,46 @@ js: import "konva/skia-backend";
3106
3133
  this._cache.set(CANVAS, {
3107
3134
  scene: cachedSceneCanvas,
3108
3135
  filter: cachedFilterCanvas,
3109
- hit: cachedHitCanvas,
3136
+ // the hit canvas is built on demand (see _getCachedHitCanvas)
3137
+ hit: null,
3138
+ hitConfig: {
3139
+ pixelRatio: hitCanvasPixelRatio,
3140
+ width: width,
3141
+ height: height,
3142
+ },
3110
3143
  x: x,
3111
3144
  y: y,
3112
3145
  });
3113
3146
  this._requestDraw();
3114
3147
  return this;
3115
3148
  }
3149
+ // build the hit canvas of a cached node lazily - only when the hit graph
3150
+ // is actually needed, so a non-listening node never allocates it
3151
+ // https://github.com/konvajs/konva/issues/2009
3152
+ _getCachedHitCanvas(top) {
3153
+ // top === this means the node is drawing into its own cache right now:
3154
+ // the build below re-enters drawHit for this very node, and consulting
3155
+ // the not-yet-stored canvas again would recurse forever
3156
+ if (top === this) {
3157
+ return null;
3158
+ }
3159
+ const cache = this._getCanvasCache();
3160
+ if (!cache) {
3161
+ return null;
3162
+ }
3163
+ if (cache.hit) {
3164
+ return cache.hit;
3165
+ }
3166
+ const hitCanvas = new HitCanvas(cache.hitConfig);
3167
+ hitCanvas.isCache = true;
3168
+ const hitContext = hitCanvas.getContext();
3169
+ hitContext.save();
3170
+ hitContext.translate(-cache.x, -cache.y);
3171
+ this.drawHit(hitCanvas, this);
3172
+ hitContext.restore();
3173
+ cache.hit = hitCanvas;
3174
+ return hitCanvas;
3175
+ }
3116
3176
  /**
3117
3177
  * determine if node is currently cached
3118
3178
  * @method
@@ -3201,8 +3261,8 @@ js: import "konva/skia-backend";
3201
3261
  context.drawImage(cacheCanvas._canvas, 0, 0, cacheCanvas.width / ratio, cacheCanvas.height / ratio);
3202
3262
  context.restore();
3203
3263
  }
3204
- _drawCachedHitCanvas(context) {
3205
- const canvasCache = this._getCanvasCache(), hitCanvas = canvasCache.hit;
3264
+ _drawCachedHitCanvas(context, hitCanvas) {
3265
+ const canvasCache = this._getCanvasCache();
3206
3266
  context.save();
3207
3267
  context.translate(canvasCache.x, canvasCache.y);
3208
3268
  context.drawImage(hitCanvas._canvas, 0, 0, hitCanvas.width / hitCanvas.pixelRatio, hitCanvas.height / hitCanvas.pixelRatio);
@@ -3259,6 +3319,67 @@ js: import "konva/skia-backend";
3259
3319
  this._filterUpToDate = true;
3260
3320
  return filterCanvas;
3261
3321
  }
3322
+ /**
3323
+ * bind events to the node. KonvaJS supports mouseover, mousemove,
3324
+ * mouseout, mouseenter, mouseleave, mousedown, mouseup, wheel, contextmenu, click, dblclick, touchstart, touchmove,
3325
+ * touchend, tap, dbltap, dragstart, dragmove, and dragend events.
3326
+ * Pass in a string of events delimited by a space to bind multiple events at once
3327
+ * such as 'mousedown mouseup mousemove'. Include a namespace to bind an
3328
+ * event by name such as 'click.foobar'.
3329
+ * @method
3330
+ * @name Konva.Node#on
3331
+ * @param {String} evtStr e.g. 'click', 'mousedown touchstart', 'mousedown.foo touchstart.foo'
3332
+ * @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.
3333
+ * @returns {Konva.Node}
3334
+ * @example
3335
+ * // add click listener
3336
+ * node.on('click', function() {
3337
+ * console.log('you clicked me!');
3338
+ * });
3339
+ *
3340
+ * // get the target node
3341
+ * node.on('click', function(evt) {
3342
+ * console.log(evt.target);
3343
+ * });
3344
+ *
3345
+ * // stop event propagation
3346
+ * node.on('click', function(evt) {
3347
+ * evt.cancelBubble = true;
3348
+ * });
3349
+ *
3350
+ * // bind multiple listeners
3351
+ * node.on('click touchstart', function() {
3352
+ * console.log('you clicked/touched me!');
3353
+ * });
3354
+ *
3355
+ * // namespace listener
3356
+ * node.on('click.foo', function() {
3357
+ * console.log('you clicked/touched me!');
3358
+ * });
3359
+ *
3360
+ * // get the event type
3361
+ * node.on('click tap', function(evt) {
3362
+ * var eventType = evt.type;
3363
+ * });
3364
+ *
3365
+ * // get native event object
3366
+ * node.on('click tap', function(evt) {
3367
+ * var nativeEvent = evt.evt;
3368
+ * });
3369
+ *
3370
+ * // for change events, get the old and new val
3371
+ * node.on('xChange', function(evt) {
3372
+ * var oldVal = evt.oldVal;
3373
+ * var newVal = evt.newVal;
3374
+ * });
3375
+ *
3376
+ * // get event targets
3377
+ * // with event delegations
3378
+ * layer.on('click', 'Group', function(evt) {
3379
+ * var shape = evt.target;
3380
+ * var group = evt.currentTarget;
3381
+ * });
3382
+ */
3262
3383
  on(...args) {
3263
3384
  const evtStr = args[0];
3264
3385
  const selectorOrHandler = args[1];
@@ -3719,7 +3840,7 @@ js: import "konva/skia-backend";
3719
3840
  * get absolute position of a node. That function can be used to calculate absolute position, but relative to any ancestor
3720
3841
  * @method
3721
3842
  * @name Konva.Node#getAbsolutePosition
3722
- * @param {Object} Ancestor optional ancestor node
3843
+ * @param {Object} [top] optional ancestor node
3723
3844
  * @returns {Konva.Node}
3724
3845
  * @example
3725
3846
  *
@@ -5031,7 +5152,7 @@ js: import "konva/skia-backend";
5031
5152
  * and setImage() methods
5032
5153
  * @method
5033
5154
  * @memberof Konva.Node
5034
- * @param {String|Object} json string or object
5155
+ * @param {String|Object} data string or object
5035
5156
  * @param {Element} [container] optional container dom element used only if you're
5036
5157
  * creating a stage node
5037
5158
  */
@@ -5441,6 +5562,8 @@ js: import "konva/skia-backend";
5441
5562
  * by taking into account its parents, use the isListening() method
5442
5563
  * nodes with listening set to false will not be detected in hit graph
5443
5564
  * so they will be ignored in container.getIntersection() method
5565
+ * a non-listening layer (or a layer of a non-listening stage) also
5566
+ * releases its hit canvas, so it uses less memory
5444
5567
  * @name Konva.Node#listening
5445
5568
  * @method
5446
5569
  * @param {Boolean} listening Can be true, or false. The default is true.
@@ -5970,12 +6093,12 @@ js: import "konva/skia-backend";
5970
6093
  if (!this.shouldDrawHit(top)) {
5971
6094
  return this;
5972
6095
  }
5973
- const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedHitCanvas = cachedCanvas && cachedCanvas.hit;
6096
+ const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas), context = canvas && canvas.getContext(), cachedHitCanvas = this._getCachedHitCanvas(top);
5974
6097
  if (cachedHitCanvas) {
5975
6098
  context.save();
5976
6099
  const m = this.getAbsoluteTransform(top).getMatrix();
5977
6100
  context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
5978
- this._drawCachedHitCanvas(context);
6101
+ this._drawCachedHitCanvas(context, cachedHitCanvas);
5979
6102
  context.restore();
5980
6103
  }
5981
6104
  else {
@@ -6220,23 +6343,41 @@ js: import "konva/skia-backend";
6220
6343
  return Captures.get(pointerId) === shape;
6221
6344
  }
6222
6345
  function setPointerCapture(pointerId, shape) {
6346
+ var _a;
6223
6347
  releaseCapture(pointerId);
6224
6348
  const stage = shape.getStage();
6225
6349
  if (!stage)
6226
6350
  return;
6227
6351
  Captures.set(pointerId, shape);
6228
6352
  if (SUPPORT_POINTER_EVENTS) {
6353
+ // capture on the DOM level too, so the stage keeps receiving the events
6354
+ // of that pointer even when it moves outside of the stage container
6355
+ // https://github.com/konvajs/konva/issues/1992
6356
+ try {
6357
+ (_a = stage.content) === null || _a === void 0 ? void 0 : _a.setPointerCapture(pointerId);
6358
+ }
6359
+ catch (e) {
6360
+ // capture is possible only for an active pointer;
6361
+ // ids of mouse and touch events (999 and touch identifiers) and
6362
+ // programmatic calls outside of a pointer event land here
6363
+ }
6229
6364
  shape._fire('gotpointercapture', createEvent(new PointerEvent('gotpointercapture')));
6230
6365
  }
6231
6366
  }
6232
6367
  function releaseCapture(pointerId, target) {
6368
+ var _a;
6233
6369
  const shape = Captures.get(pointerId);
6234
6370
  if (!shape)
6235
6371
  return;
6236
6372
  const stage = shape.getStage();
6237
- if (stage && stage.content) ;
6238
6373
  Captures.delete(pointerId);
6239
6374
  if (SUPPORT_POINTER_EVENTS) {
6375
+ try {
6376
+ (_a = stage === null || stage === void 0 ? void 0 : stage.content) === null || _a === void 0 ? void 0 : _a.releasePointerCapture(pointerId);
6377
+ }
6378
+ catch (e) {
6379
+ // same as in setPointerCapture: the pointer may not be active
6380
+ }
6240
6381
  shape._fire('lostpointercapture', createEvent(new PointerEvent('lostpointercapture')));
6241
6382
  }
6242
6383
  }
@@ -6329,6 +6470,27 @@ js: import "konva/skia-backend";
6329
6470
  }
6330
6471
  const NO_POINTERS_MESSAGE = `Pointer position is missing and not registered by the stage. Looks like it is outside of the stage container. You can set it manually from event: stage.setPointersPositions(event);`;
6331
6472
  const stages = [];
6473
+ // A stage may be rendered in another window than the one Konva was imported
6474
+ // into: an iframe, or a window opened with `window.open`. Such a window sends
6475
+ // its own events and has its own visibility state, so Konva listens to every
6476
+ // window it renders a stage in
6477
+ const listeningWindows = new WeakSet();
6478
+ const listenToWindow = (win) => {
6479
+ if (!win || listeningWindows.has(win)) {
6480
+ return;
6481
+ }
6482
+ listeningWindows.add(win);
6483
+ DD._listenToWindow(win);
6484
+ // chrome is clearing canvas in inactive browser window, causing layer content to be erased
6485
+ // so let's redraw layers as soon as window becomes active
6486
+ // TODO: any other way to solve this issue?
6487
+ // TODO: should we remove it if chrome fixes the issue?
6488
+ win.document.addEventListener('visibilitychange', () => {
6489
+ stages.forEach((stage) => {
6490
+ stage.batchDraw();
6491
+ });
6492
+ });
6493
+ };
6332
6494
  /**
6333
6495
  * Stage constructor. A stage is used to contain multiple layers
6334
6496
  * @constructor
@@ -6393,6 +6555,18 @@ js: import "konva/skia-backend";
6393
6555
  const style = this.visible() ? '' : 'none';
6394
6556
  this.content.style.display = style;
6395
6557
  }
6558
+ /**
6559
+ * The window the stage is rendered in. It is not always the window Konva was
6560
+ * imported into: the container may belong to an iframe, or to a window
6561
+ * opened with `window.open`.
6562
+ */
6563
+ _getOwnerWindow() {
6564
+ var _a;
6565
+ const element = this.content || this.container();
6566
+ // a document with no browsing context, such as one of `DOMParser`, has no
6567
+ // window of its own. The window Konva was imported into is used then
6568
+ return (((_a = element === null || element === void 0 ? void 0 : element.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView) || (Konva$2.isBrowser ? window : null));
6569
+ }
6396
6570
  /**
6397
6571
  * set container dom element which contains the stage wrapper div element
6398
6572
  * @method
@@ -6425,6 +6599,8 @@ js: import "konva/skia-backend";
6425
6599
  this.content.parentElement.removeChild(this.content);
6426
6600
  }
6427
6601
  container.appendChild(this.content);
6602
+ // the new container may be in another window, with its own events
6603
+ listenToWindow(this._getOwnerWindow());
6428
6604
  }
6429
6605
  return this;
6430
6606
  }
@@ -6554,14 +6730,19 @@ js: import "konva/skia-backend";
6554
6730
  this.content.style.width = width + PX;
6555
6731
  this.content.style.height = height + PX;
6556
6732
  }
6557
- this.bufferCanvas.setSize(width, height);
6558
- this.bufferHitCanvas.setSize(width, height);
6559
6733
  // set layer dimensions
6560
6734
  this.children.forEach((layer) => {
6561
6735
  layer.setSize({ width, height });
6562
6736
  layer.draw();
6563
6737
  });
6564
6738
  }
6739
+ // "perfect drawing" uses bufferCanvas, shape.intersects() uses
6740
+ // bufferHitCanvas; both are lazy - created at 0x0 and sized to the stage
6741
+ // on first use
6742
+ _syncBufferSize(canvas) {
6743
+ canvas.setSizeIfChanged(this.width(), this.height());
6744
+ return canvas;
6745
+ }
6565
6746
  add(layer, ...rest) {
6566
6747
  if (arguments.length > 1) {
6567
6748
  for (let i = 0; i < arguments.length; i++) {
@@ -6803,6 +6984,13 @@ js: import "konva/skia-backend";
6803
6984
  return;
6804
6985
  }
6805
6986
  this.setPointersPositions(evt);
6987
+ // the mouse/touch families get their "listen click" flags reset by
6988
+ // DD._endDragBefore, but native "pointerup" fires before the
6989
+ // "mouseup"/"touchend" that ends the drag, so for the pointer family the
6990
+ // flags are still set — check the live drag state as well, otherwise
6991
+ // "pointerclick" fires after a drag while "click" does not (issue #1756)
6992
+ const wasDragged = DD.justDragged || (eventType === 'pointer' && Konva$2.isDragging());
6993
+ const listenClick = Konva$2['_' + eventType + 'ListenClick'] && !wasDragged;
6806
6994
  const clickStartShape = this[eventType + 'ClickStartShape'];
6807
6995
  const clickEndShape = this[eventType + 'ClickEndShape'];
6808
6996
  const processedShapesIds = {};
@@ -6824,7 +7012,7 @@ js: import "konva/skia-backend";
6824
7012
  fireDblClick = true;
6825
7013
  clearTimeout(this[eventType + 'DblTimeout']);
6826
7014
  }
6827
- else if (!DD.justDragged) {
7015
+ else if (!wasDragged) {
6828
7016
  // don't set inDblClickWindow after dragging
6829
7017
  Konva$2['_' + eventType + 'InDblClickWindow'] = true;
6830
7018
  clearTimeout(this[eventType + 'DblTimeout']);
@@ -6837,9 +7025,7 @@ js: import "konva/skia-backend";
6837
7025
  this[eventType + 'ClickEndShape'] = shape;
6838
7026
  shape._fireAndBubble(events.pointerup, { ...event });
6839
7027
  // detect if click or double click occurred
6840
- if (Konva$2['_' + eventType + 'ListenClick'] &&
6841
- clickStartShape &&
6842
- clickStartShape === shape) {
7028
+ if (listenClick && clickStartShape === shape) {
6843
7029
  shape._fireAndBubble(events.pointerclick, { ...event });
6844
7030
  if (fireDblClick && clickEndShape && clickEndShape === shape) {
6845
7031
  shape._fireAndBubble(events.pointerdblclick, { ...event });
@@ -6857,7 +7043,7 @@ js: import "konva/skia-backend";
6857
7043
  });
6858
7044
  skipPointerUpTrigger = true;
6859
7045
  }
6860
- if (Konva$2['_' + eventType + 'ListenClick']) {
7046
+ if (listenClick) {
6861
7047
  this._fire(events.pointerclick, {
6862
7048
  evt: evt,
6863
7049
  target: this,
@@ -6940,7 +7126,7 @@ js: import "konva/skia-backend";
6940
7126
  * is triggered outside of the stage, but you still want to use Konva methods to get pointers position.
6941
7127
  * @method
6942
7128
  * @name Konva.Stage#setPointersPositions
6943
- * @param {Object} event Event object
7129
+ * @param {Object} evt Event object
6944
7130
  * @example
6945
7131
  *
6946
7132
  * window.addEventListener('mousemove', (e) => {
@@ -7010,14 +7196,16 @@ js: import "konva/skia-backend";
7010
7196
  };
7011
7197
  }
7012
7198
  _buildDOM() {
7199
+ // lazy buffer canvases, see _syncBufferSize
7200
+ // https://github.com/konvajs/konva/issues/2009
7013
7201
  this.bufferCanvas = new SceneCanvas({
7014
- width: this.width(),
7015
- height: this.height(),
7202
+ width: 0,
7203
+ height: 0,
7016
7204
  });
7017
7205
  this.bufferHitCanvas = new HitCanvas({
7018
7206
  pixelRatio: 1,
7019
- width: this.width(),
7020
- height: this.height(),
7207
+ width: 0,
7208
+ height: 0,
7021
7209
  });
7022
7210
  if (!Konva$2.isBrowser) {
7023
7211
  return;
@@ -7029,12 +7217,15 @@ js: import "konva/skia-backend";
7029
7217
  // clear content inside container
7030
7218
  container.innerHTML = '';
7031
7219
  // content
7032
- this.content = document.createElement('div');
7220
+ // the container may belong to another document - an iframe, or a popout
7221
+ // window - so the content is created in the document of the container
7222
+ this.content = container.ownerDocument.createElement('div');
7033
7223
  this.content.style.position = 'relative';
7034
7224
  this.content.style.userSelect = 'none';
7035
7225
  this.content.className = 'konvajs-content';
7036
7226
  this.content.setAttribute('role', 'presentation');
7037
7227
  container.appendChild(this.content);
7228
+ listenToWindow(this._getOwnerWindow());
7038
7229
  this._resizeDOM();
7039
7230
  }
7040
7231
  // currently cache function is now working for stage, because stage has no its own canvas element
@@ -7061,7 +7252,9 @@ js: import "konva/skia-backend";
7061
7252
  Stage.prototype.nodeType = STAGE;
7062
7253
  _registerNode(Stage);
7063
7254
  /**
7064
- * get/set container DOM element
7255
+ * get/set container DOM element. The container may belong to another window
7256
+ * than the one Konva was imported into: an iframe, or a window opened with
7257
+ * `window.open`. The stage then uses the events and the frames of that window.
7065
7258
  * @method
7066
7259
  * @name Konva.Stage#container
7067
7260
  * @returns {DomElement} container
@@ -7072,19 +7265,12 @@ js: import "konva/skia-backend";
7072
7265
  * var container = document.createElement('div');
7073
7266
  * body.appendChild(container);
7074
7267
  * stage.container(container);
7268
+ *
7269
+ * // a stage in a popout window
7270
+ * var popout = window.open('', '', 'width=600,height=400');
7271
+ * stage.container(popout.document.body);
7075
7272
  */
7076
7273
  Factory.addGetterSetter(Stage, 'container');
7077
- // chrome is clearing canvas in inactive browser window, causing layer content to be erased
7078
- // so let's redraw layers as soon as window becomes active
7079
- // TODO: any other way to solve this issue?
7080
- // TODO: should we remove it if chrome fixes the issue?
7081
- if (Konva$2.isBrowser) {
7082
- document.addEventListener('visibilitychange', () => {
7083
- stages.forEach((stage) => {
7084
- stage.batchDraw();
7085
- });
7086
- });
7087
- }
7088
7274
 
7089
7275
  const HAS_SHADOW = 'hasShadow';
7090
7276
  const SHADOW_RGBA = 'shadowRGBA';
@@ -7457,7 +7643,7 @@ js: import "konva/skia-backend";
7457
7643
  if (!stage) {
7458
7644
  return false;
7459
7645
  }
7460
- const bufferHitCanvas = stage.bufferHitCanvas;
7646
+ const bufferHitCanvas = stage._syncBufferSize(stage.bufferHitCanvas);
7461
7647
  bufferHitCanvas.getContext().clear();
7462
7648
  this.drawHit(bufferHitCanvas, undefined, true);
7463
7649
  const p = bufferHitCanvas.context.getImageData(Math.round(point.x), Math.round(point.y), 1, 1).data;
@@ -7604,7 +7790,7 @@ js: import "konva/skia-backend";
7604
7790
  // if buffer canvas is needed
7605
7791
  if (this._useBufferCanvas() && true) {
7606
7792
  stage = this.getStage();
7607
- const bc = bufferCanvas || stage.bufferCanvas;
7793
+ const bc = bufferCanvas || stage._syncBufferSize(stage.bufferCanvas);
7608
7794
  const bufferContext = bc.getContext();
7609
7795
  // When caching, the buffer canvas may have a translation applied.
7610
7796
  // We need to reset the transform before clearing to ensure the entire canvas is cleared.
@@ -7671,7 +7857,7 @@ js: import "konva/skia-backend";
7671
7857
  if (!this.shouldDrawHit(top, skipDragCheck)) {
7672
7858
  return this;
7673
7859
  }
7674
- const layer = this.getLayer(), canvas = can || layer.hitCanvas, context = canvas && canvas.getContext(), drawFunc = this.hitFunc() || this.sceneFunc(), cachedCanvas = this._getCanvasCache(), cachedHitCanvas = cachedCanvas && cachedCanvas.hit;
7860
+ const layer = this.getLayer(), canvas = can || layer.hitCanvas, context = canvas && canvas.getContext(), drawFunc = this.hitFunc() || this.sceneFunc(), cachedHitCanvas = this._getCachedHitCanvas(top);
7675
7861
  if (!this.colorKey) {
7676
7862
  Util.warn('Looks like your canvas has a destroyed shape in it. Do not reuse shape after you destroyed it. If you want to reuse shape you should call remove() instead of destroy()');
7677
7863
  }
@@ -7679,7 +7865,7 @@ js: import "konva/skia-backend";
7679
7865
  context.save();
7680
7866
  const m = this.getAbsoluteTransform(top).getMatrix();
7681
7867
  context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
7682
- this._drawCachedHitCanvas(context);
7868
+ this._drawCachedHitCanvas(context, cachedHitCanvas);
7683
7869
  context.restore();
7684
7870
  return this;
7685
7871
  }
@@ -7711,7 +7897,7 @@ js: import "konva/skia-backend";
7711
7897
  * shape.drawHitFromCache();
7712
7898
  */
7713
7899
  drawHitFromCache(alphaThreshold = 0) {
7714
- const cachedCanvas = this._getCanvasCache(), sceneCanvas = this._getCachedSceneCanvas(), hitCanvas = cachedCanvas.hit, hitContext = hitCanvas.getContext(), hitWidth = hitCanvas.getWidth(), hitHeight = hitCanvas.getHeight();
7900
+ const sceneCanvas = this._getCachedSceneCanvas(), hitCanvas = this._getCachedHitCanvas(), hitContext = hitCanvas.getContext(), hitWidth = hitCanvas.getWidth(), hitHeight = hitCanvas.getHeight();
7715
7901
  hitContext.clear();
7716
7902
  hitContext.drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight);
7717
7903
  try {
@@ -8989,10 +9175,18 @@ js: import "konva/skia-backend";
8989
9175
  }
8990
9176
  setSize({ width, height }) {
8991
9177
  this.canvas.setSize(width, height);
8992
- this.hitCanvas.setSize(width, height);
9178
+ this._syncHitCanvasSize();
8993
9179
  this._setSmoothEnabled();
8994
9180
  return this;
8995
9181
  }
9182
+ // the hit canvas is only allocated while the layer is listening;
9183
+ // for a non-listening layer it is kept released (0x0), so a purely
9184
+ // presentational layer does not pay for a stage-sized hit bitmap
9185
+ // https://github.com/konvajs/konva/issues/2009
9186
+ _syncHitCanvasSize() {
9187
+ const listening = this.isListening();
9188
+ this.hitCanvas.setSizeIfChanged((listening ? this.getWidth() : 0) || 0, (listening ? this.getHeight() : 0) || 0);
9189
+ }
8996
9190
  _validateAdd(child) {
8997
9191
  const type = child.getType();
8998
9192
  if (type !== 'Group' && type !== 'Shape') {
@@ -9062,12 +9256,15 @@ js: import "konva/skia-backend";
9062
9256
  * @return {Konva.Layer} this
9063
9257
  */
9064
9258
  batchDraw() {
9259
+ var _a;
9065
9260
  if (!this._waitingForDraw) {
9066
9261
  this._waitingForDraw = true;
9262
+ // ask the window the stage is rendered in for the frame: a stage in a
9263
+ // popout window keeps drawing while the opener window is not visible
9067
9264
  Util.requestAnimFrame(() => {
9068
9265
  this.draw();
9069
9266
  this._waitingForDraw = false;
9070
- });
9267
+ }, (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow());
9071
9268
  }
9072
9269
  return this;
9073
9270
  }
@@ -9122,6 +9319,11 @@ js: import "konva/skia-backend";
9122
9319
  }
9123
9320
  }
9124
9321
  _getIntersection(pos) {
9322
+ // a non-listening layer keeps its hit canvas released (0x0),
9323
+ // and a just-enabled layer may not have drawn its hit graph yet
9324
+ if (!this.hitCanvas.width || !this.hitCanvas.height) {
9325
+ return {};
9326
+ }
9125
9327
  const ratio = this.hitCanvas.pixelRatio;
9126
9328
  const p = this.hitCanvas.context.getImageData(Math.round(pos.x * ratio), Math.round(pos.y * ratio), 1, 1).data;
9127
9329
  const p3 = p[3];
@@ -9163,8 +9365,13 @@ js: import "konva/skia-backend";
9163
9365
  }
9164
9366
  drawHit(can, top) {
9165
9367
  const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas);
9166
- if (layer && layer.clearBeforeDraw()) {
9167
- layer.getHitCanvas().getContext().clear();
9368
+ if (!can && layer) {
9369
+ // allocate or release the hit canvas to match the current listening
9370
+ // state - this also picks up a listening change made after the last draw
9371
+ layer._syncHitCanvasSize();
9372
+ if (layer.clearBeforeDraw()) {
9373
+ layer.getHitCanvas().getContext().clear();
9374
+ }
9168
9375
  }
9169
9376
  Container.prototype.drawHit.call(this, canvas, top);
9170
9377
  return this;
@@ -14946,10 +15153,17 @@ js: import "konva/skia-backend";
14946
15153
  return dummyContext;
14947
15154
  }
14948
15155
  function _fillFunc$1(context) {
15156
+ // re-apply a per-character style set by charRenderFunc (see _sceneFunc)
15157
+ if (this._partialFillStyle) {
15158
+ context.setAttr('fillStyle', this._partialFillStyle);
15159
+ }
14949
15160
  context.fillText(this._partialText, this._partialTextX, this._partialTextY);
14950
15161
  }
14951
15162
  function _strokeFunc$1(context) {
14952
15163
  context.setAttr('miterLimit', 2);
15164
+ if (this._partialStrokeStyle) {
15165
+ context.setAttr('strokeStyle', this._partialStrokeStyle);
15166
+ }
14953
15167
  context.strokeText(this._partialText, this._partialTextX, this._partialTextY);
14954
15168
  }
14955
15169
  function checkDefaultFill(config) {
@@ -15108,6 +15322,11 @@ js: import "konva/skia-backend";
15108
15322
  alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
15109
15323
  }
15110
15324
  context.translate(padding, alignY + padding);
15325
+ // context styles entering the loop, used to detect styles set by
15326
+ // charRenderFunc; every character is drawn inside save()/restore(),
15327
+ // so the entry state is the same for each one
15328
+ const fillStyleBefore = charRenderFunc ? context.fillStyle : undefined;
15329
+ const strokeStyleBefore = charRenderFunc ? context.strokeStyle : undefined;
15111
15330
  // draw text lines
15112
15331
  for (n = 0; n < textArrLen; n++) {
15113
15332
  let lineTranslateX = 0;
@@ -15179,9 +15398,22 @@ js: import "konva/skia-backend";
15179
15398
  width: this.measureSize(letter).width,
15180
15399
  context,
15181
15400
  });
15401
+ // a style the callback set on the context wins for this character;
15402
+ // fillStrokeShape() overwrites the context styles from the shape
15403
+ // attributes, so stash them for _fillFunc/_strokeFunc to re-apply
15404
+ const fillStyleAfter = context.fillStyle;
15405
+ if (fillStyleAfter !== fillStyleBefore) {
15406
+ this._partialFillStyle = fillStyleAfter;
15407
+ }
15408
+ const strokeStyleAfter = context.strokeStyle;
15409
+ if (strokeStyleAfter !== strokeStyleBefore) {
15410
+ this._partialStrokeStyle = strokeStyleAfter;
15411
+ }
15182
15412
  }
15183
15413
  context.fillStrokeShape(this);
15184
15414
  if (charRenderFunc) {
15415
+ this._partialFillStyle = undefined;
15416
+ this._partialStrokeStyle = undefined;
15185
15417
  context.restore();
15186
15418
  }
15187
15419
  lineTranslateX += this.measureSize(letter).width + letterSpacing;
@@ -15776,10 +16008,11 @@ js: import "konva/skia-backend";
15776
16008
  * @param {Function} charRenderFunc
15777
16009
  * @returns {Function}
15778
16010
  * @example
15779
- * // apply small x-translation to every second character
16011
+ * // apply small x-translation and a custom color to every second character
15780
16012
  * text.charRenderFunc(function(props) {
15781
16013
  * if (props.index % 2 === 1) {
15782
16014
  * props.context.translate(2, 0);
16015
+ * props.context.fillStyle = 'red';
15783
16016
  * }
15784
16017
  * });
15785
16018
  */
@@ -16577,6 +16810,9 @@ js: import "konva/skia-backend";
16577
16810
  super(config);
16578
16811
  this._movingAnchorName = null;
16579
16812
  this._transforming = false;
16813
+ // the window the transform events are listened to. The stage may be
16814
+ // rendered in another window than the one Konva was imported into
16815
+ this._transformWindow = null;
16580
16816
  this._elementsCreated = false;
16581
16817
  this._updateScheduled = false;
16582
16818
  this._createElements();
@@ -16983,6 +17219,7 @@ js: import "konva/skia-backend";
16983
17219
  });
16984
17220
  }
16985
17221
  _handleMouseDown(e) {
17222
+ var _a;
16986
17223
  // do nothing if we already transforming
16987
17224
  // that is possible to trigger with multitouch
16988
17225
  if (this._transforming) {
@@ -16995,11 +17232,14 @@ js: import "konva/skia-backend";
16995
17232
  const hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
16996
17233
  this.sin = Math.abs(height / hypotenuse);
16997
17234
  this.cos = Math.abs(width / hypotenuse);
16998
- if (typeof window !== 'undefined') {
16999
- window.addEventListener('mousemove', this._handleMouseMove);
17000
- window.addEventListener('touchmove', this._handleMouseMove);
17001
- window.addEventListener('mouseup', this._handleMouseUp, true);
17002
- window.addEventListener('touchend', this._handleMouseUp, true);
17235
+ // the window that owns the stage is the one that gets the pointer events
17236
+ const win = (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow();
17237
+ this._transformWindow = win || null;
17238
+ if (win) {
17239
+ win.addEventListener('mousemove', this._handleMouseMove);
17240
+ win.addEventListener('touchmove', this._handleMouseMove);
17241
+ win.addEventListener('mouseup', this._handleMouseUp, true);
17242
+ win.addEventListener('touchend', this._handleMouseUp, true);
17003
17243
  }
17004
17244
  this._transforming = true;
17005
17245
  const ap = e.target.getAbsolutePosition();
@@ -17067,8 +17307,13 @@ js: import "konva/skia-backend";
17067
17307
  keepProportion = this.keepRatio() || e.shiftKey;
17068
17308
  }
17069
17309
  let centeredScaling = this.centeredScaling() || e.altKey;
17310
+ // with keepRatio, a corner anchor doesn't follow the pointer — the blocks
17311
+ // below project it onto the ratio diagonal. _doFitNodesInto must know
17312
+ // that to skip the padding flip compensation (issue #1878)
17313
+ let anchorProjected = false;
17070
17314
  if (this._movingAnchorName === 'top-left') {
17071
17315
  if (keepProportion) {
17316
+ anchorProjected = true;
17072
17317
  const comparePoint = centeredScaling
17073
17318
  ? {
17074
17319
  x: this.width() / 2,
@@ -17093,6 +17338,7 @@ js: import "konva/skia-backend";
17093
17338
  }
17094
17339
  else if (this._movingAnchorName === 'top-right') {
17095
17340
  if (keepProportion) {
17341
+ anchorProjected = true;
17096
17342
  const comparePoint = centeredScaling
17097
17343
  ? {
17098
17344
  x: this.width() / 2,
@@ -17123,6 +17369,7 @@ js: import "konva/skia-backend";
17123
17369
  }
17124
17370
  else if (this._movingAnchorName === 'bottom-left') {
17125
17371
  if (keepProportion) {
17372
+ anchorProjected = true;
17126
17373
  const comparePoint = centeredScaling
17127
17374
  ? {
17128
17375
  x: this.width() / 2,
@@ -17150,6 +17397,7 @@ js: import "konva/skia-backend";
17150
17397
  }
17151
17398
  else if (this._movingAnchorName === 'bottom-right') {
17152
17399
  if (keepProportion) {
17400
+ anchorProjected = true;
17153
17401
  const comparePoint = centeredScaling
17154
17402
  ? {
17155
17403
  x: this.width() / 2,
@@ -17201,7 +17449,7 @@ js: import "konva/skia-backend";
17201
17449
  width: width,
17202
17450
  height: height,
17203
17451
  rotation: Konva$2.getAngle(this.rotation()),
17204
- }, e);
17452
+ }, e, anchorProjected);
17205
17453
  }
17206
17454
  _handleMouseUp(e) {
17207
17455
  this._removeEvents(e);
@@ -17213,11 +17461,13 @@ js: import "konva/skia-backend";
17213
17461
  var _a;
17214
17462
  if (this._transforming) {
17215
17463
  this._transforming = false;
17216
- if (typeof window !== 'undefined') {
17217
- window.removeEventListener('mousemove', this._handleMouseMove);
17218
- window.removeEventListener('touchmove', this._handleMouseMove);
17219
- window.removeEventListener('mouseup', this._handleMouseUp, true);
17220
- window.removeEventListener('touchend', this._handleMouseUp, true);
17464
+ const win = this._transformWindow;
17465
+ this._transformWindow = null;
17466
+ if (win) {
17467
+ win.removeEventListener('mousemove', this._handleMouseMove);
17468
+ win.removeEventListener('touchmove', this._handleMouseMove);
17469
+ win.removeEventListener('mouseup', this._handleMouseUp, true);
17470
+ win.removeEventListener('touchend', this._handleMouseUp, true);
17221
17471
  }
17222
17472
  const node = this.getNode();
17223
17473
  activeTransformersCount--;
@@ -17235,20 +17485,20 @@ js: import "konva/skia-backend";
17235
17485
  this._movingAnchorName = null;
17236
17486
  }
17237
17487
  }
17238
- _fitNodesInto(newAttrs, evt) {
17488
+ _fitNodesInto(newAttrs, evt, anchorProjected = false) {
17239
17489
  // Perf: suspend autoDraw for the whole method so per-attr _setAttr writes
17240
17490
  // don't each trigger _requestDraw -> getLayer parent-chain walks.
17241
17491
  // batchDraw per affected layer is issued at the end.
17242
17492
  const prevAutoDraw = Konva$2.autoDrawEnabled;
17243
17493
  Konva$2.autoDrawEnabled = false;
17244
17494
  try {
17245
- return this._doFitNodesInto(newAttrs, evt);
17495
+ return this._doFitNodesInto(newAttrs, evt, anchorProjected);
17246
17496
  }
17247
17497
  finally {
17248
17498
  Konva$2.autoDrawEnabled = prevAutoDraw;
17249
17499
  }
17250
17500
  }
17251
- _doFitNodesInto(newAttrs, evt) {
17501
+ _doFitNodesInto(newAttrs, evt, anchorProjected = false) {
17252
17502
  const oldAttrs = this._getNodeRect();
17253
17503
  const minSize = 1;
17254
17504
  if (Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize)) {
@@ -17261,57 +17511,58 @@ js: import "konva/skia-backend";
17261
17511
  }
17262
17512
  const t = new Transform();
17263
17513
  t.rotate(Konva$2.getAngle(this.rotation()));
17514
+ // the ±2*padding shifts below assume the box size passed through zero
17515
+ // continuously (the anchor tied to the pointer). A projected anchor snaps
17516
+ // the box across zero instead; the shifts then leave the drag state
17517
+ // inconsistent with the real anchor position and the box flips back and
17518
+ // forth on every following move (issue #1878) — so flip only the anchor
17519
+ // name, exactly as when padding is 0.
17520
+ const flipPadding = anchorProjected ? 0 : this.padding() * 2;
17521
+ let widthFlip = null;
17522
+ let heightFlip = null;
17264
17523
  if (this._movingAnchorName &&
17265
17524
  newAttrs.width < 0 &&
17266
17525
  this._movingAnchorName.indexOf('left') >= 0) {
17267
17526
  const offset = t.point({
17268
- x: -this.padding() * 2,
17527
+ x: -flipPadding,
17269
17528
  y: 0,
17270
17529
  });
17271
17530
  newAttrs.x += offset.x;
17272
17531
  newAttrs.y += offset.y;
17273
- newAttrs.width += this.padding() * 2;
17274
- this._movingAnchorName = this._movingAnchorName.replace('left', 'right');
17275
- this._anchorDragOffset.x -= offset.x;
17276
- this._anchorDragOffset.y -= offset.y;
17532
+ newAttrs.width += flipPadding;
17533
+ widthFlip = { axis: 'width', from: 'left', to: 'right', offset };
17277
17534
  }
17278
17535
  else if (this._movingAnchorName &&
17279
17536
  newAttrs.width < 0 &&
17280
17537
  this._movingAnchorName.indexOf('right') >= 0) {
17281
17538
  const offset = t.point({
17282
- x: this.padding() * 2,
17539
+ x: flipPadding,
17283
17540
  y: 0,
17284
17541
  });
17285
- this._movingAnchorName = this._movingAnchorName.replace('right', 'left');
17286
- this._anchorDragOffset.x -= offset.x;
17287
- this._anchorDragOffset.y -= offset.y;
17288
- newAttrs.width += this.padding() * 2;
17542
+ newAttrs.width += flipPadding;
17543
+ widthFlip = { axis: 'width', from: 'right', to: 'left', offset };
17289
17544
  }
17290
17545
  if (this._movingAnchorName &&
17291
17546
  newAttrs.height < 0 &&
17292
17547
  this._movingAnchorName.indexOf('top') >= 0) {
17293
17548
  const offset = t.point({
17294
17549
  x: 0,
17295
- y: -this.padding() * 2,
17550
+ y: -flipPadding,
17296
17551
  });
17297
17552
  newAttrs.x += offset.x;
17298
17553
  newAttrs.y += offset.y;
17299
- this._movingAnchorName = this._movingAnchorName.replace('top', 'bottom');
17300
- this._anchorDragOffset.x -= offset.x;
17301
- this._anchorDragOffset.y -= offset.y;
17302
- newAttrs.height += this.padding() * 2;
17554
+ newAttrs.height += flipPadding;
17555
+ heightFlip = { axis: 'height', from: 'top', to: 'bottom', offset };
17303
17556
  }
17304
17557
  else if (this._movingAnchorName &&
17305
17558
  newAttrs.height < 0 &&
17306
17559
  this._movingAnchorName.indexOf('bottom') >= 0) {
17307
17560
  const offset = t.point({
17308
17561
  x: 0,
17309
- y: this.padding() * 2,
17562
+ y: flipPadding,
17310
17563
  });
17311
- this._movingAnchorName = this._movingAnchorName.replace('bottom', 'top');
17312
- this._anchorDragOffset.x -= offset.x;
17313
- this._anchorDragOffset.y -= offset.y;
17314
- newAttrs.height += this.padding() * 2;
17564
+ newAttrs.height += flipPadding;
17565
+ heightFlip = { axis: 'height', from: 'bottom', to: 'top', offset };
17315
17566
  }
17316
17567
  if (this.boundBoxFunc()) {
17317
17568
  const bounded = this.boundBoxFunc()(oldAttrs, newAttrs);
@@ -17322,6 +17573,15 @@ js: import "konva/skia-backend";
17322
17573
  Util.warn('boundBoxFunc returned falsy. You should return new bound rect from it!');
17323
17574
  }
17324
17575
  }
17576
+ // a staged flip survived only if the bounded size is still negative on
17577
+ // its axis
17578
+ for (const flip of [widthFlip, heightFlip]) {
17579
+ if (flip && newAttrs[flip.axis] < 0 && this._movingAnchorName) {
17580
+ this._movingAnchorName = this._movingAnchorName.replace(flip.from, flip.to);
17581
+ this._anchorDragOffset.x -= flip.offset.x;
17582
+ this._anchorDragOffset.y -= flip.offset.y;
17583
+ }
17584
+ }
17325
17585
  // base size value doesn't really matter
17326
17586
  // we just need to think about bounding boxes as transforms
17327
17587
  // but how?
@@ -19245,7 +19505,7 @@ js: import "konva/skia-backend";
19245
19505
  //Konva.Filters.ToPolar = Util._FilterWrapDoubleBuffer(ToPolar);
19246
19506
  //Konva.Filters.FromPolar = Util._FilterWrapDoubleBuffer(FromPolar);
19247
19507
  // create a temporary canvas for working - shared between multiple calls
19248
- /*
19508
+ /**
19249
19509
  * Kaleidoscope Filter.
19250
19510
  * @function
19251
19511
  * @name Kaleidoscope