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
@@ -6,8 +6,8 @@ import type { IFrame, AnimationFn } from './types.ts';
6
6
  * @memberof Konva
7
7
  * @param {AnimationFn} func function executed on each animation frame. The function is passed a frame object, which contains
8
8
  * timeDiff, lastTime, time, and frameRate properties. The timeDiff property is the number of milliseconds that have passed
9
- * since the last animation frame. The time property is the time in milliseconds that elapsed from the moment the animation started
10
- * to the current animation frame. The lastTime property is a `time` value from the previous frame. The frameRate property is the current frame rate in frames / second.
9
+ * since the last animation frame. The time property accumulates elapsed animation time in milliseconds across stop/start calls, excluding paused time.
10
+ * The lastTime property is the current frame's clock timestamp in milliseconds. The frameRate property is the current frame rate in frames / second.
11
11
  * Return false from function, if you don't need to redraw layer/layers on some frames.
12
12
  * @param {Konva.Layer|Array} [layers] layer(s) to be redrawn on each animation frame. Can be a layer, an array of layers, or null.
13
13
  * Not specifying a node will result in no redraw.
package/lib/Animation.js CHANGED
@@ -10,8 +10,8 @@ const now = ((_a = glob.performance) === null || _a === void 0 ? void 0 : _a.now
10
10
  * @memberof Konva
11
11
  * @param {AnimationFn} func function executed on each animation frame. The function is passed a frame object, which contains
12
12
  * timeDiff, lastTime, time, and frameRate properties. The timeDiff property is the number of milliseconds that have passed
13
- * since the last animation frame. The time property is the time in milliseconds that elapsed from the moment the animation started
14
- * to the current animation frame. The lastTime property is a `time` value from the previous frame. The frameRate property is the current frame rate in frames / second.
13
+ * since the last animation frame. The time property accumulates elapsed animation time in milliseconds across stop/start calls, excluding paused time.
14
+ * The lastTime property is the current frame's clock timestamp in milliseconds. The frameRate property is the current frame rate in frames / second.
15
15
  * Return false from function, if you don't need to redraw layer/layers on some frames.
16
16
  * @param {Konva.Layer|Array} [layers] layer(s) to be redrawn on each animation frame. Can be a layer, an array of layers, or null.
17
17
  * Not specifying a node will result in no redraw.
@@ -2,7 +2,7 @@ export declare const tValues: number[][];
2
2
  export declare const cValues: number[][];
3
3
  export declare const binomialCoefficients: number[][];
4
4
  export declare const getCubicArcLength: (xs: number[], ys: number[], t: number) => number;
5
- export declare const getQuadraticArcLength: (xs: number[], ys: number[], t: number) => number;
5
+ export declare const getQuadraticArcLength: (xs: number[], ys: number[], t?: number) => number;
6
6
  export declare const t2length: (length: number, totalLength: number, func: (t: number) => number) => number;
7
7
  /**
8
8
  * Points where a quadratic bezier segment reaches its highest or lowest value
@@ -1,5 +1,28 @@
1
1
  // Credits: rveciana/svg-path-properties
2
+ // Keep order 20 separate so the main bundle can omit the other quadrature rows.
2
3
  // Legendre-Gauss abscissae (xi values, defined at i=n as the roots of the nth order Legendre polynomial Pn(x))
4
+ const tValues20 = [
5
+ -0.0765265211334973337546404093988382110047,
6
+ 0.0765265211334973337546404093988382110047,
7
+ -0.227785851141645078080496195368574624743,
8
+ 0.227785851141645078080496195368574624743,
9
+ -0.3737060887154195606725481770249272373957,
10
+ 0.3737060887154195606725481770249272373957,
11
+ -0.5108670019508270980043640509552509984254,
12
+ 0.5108670019508270980043640509552509984254,
13
+ -0.6360536807265150254528366962262859367433,
14
+ 0.6360536807265150254528366962262859367433,
15
+ -0.7463319064601507926143050703556415903107,
16
+ 0.7463319064601507926143050703556415903107,
17
+ -0.8391169718222188233945290617015206853296,
18
+ 0.8391169718222188233945290617015206853296,
19
+ -0.9122344282513259058677524412032981130491,
20
+ 0.9122344282513259058677524412032981130491,
21
+ -0.963971927277913791267666131197277221912,
22
+ 0.963971927277913791267666131197277221912,
23
+ -0.9931285991850949247861223884713202782226,
24
+ 0.9931285991850949247861223884713202782226,
25
+ ];
3
26
  export const tValues = [
4
27
  [],
5
28
  [],
@@ -219,28 +242,7 @@ export const tValues = [
219
242
  -0.9924068438435844031890176702532604935893,
220
243
  0.9924068438435844031890176702532604935893,
221
244
  ],
222
- [
223
- -0.0765265211334973337546404093988382110047,
224
- 0.0765265211334973337546404093988382110047,
225
- -0.227785851141645078080496195368574624743,
226
- 0.227785851141645078080496195368574624743,
227
- -0.3737060887154195606725481770249272373957,
228
- 0.3737060887154195606725481770249272373957,
229
- -0.5108670019508270980043640509552509984254,
230
- 0.5108670019508270980043640509552509984254,
231
- -0.6360536807265150254528366962262859367433,
232
- 0.6360536807265150254528366962262859367433,
233
- -0.7463319064601507926143050703556415903107,
234
- 0.7463319064601507926143050703556415903107,
235
- -0.8391169718222188233945290617015206853296,
236
- 0.8391169718222188233945290617015206853296,
237
- -0.9122344282513259058677524412032981130491,
238
- 0.9122344282513259058677524412032981130491,
239
- -0.963971927277913791267666131197277221912,
240
- 0.963971927277913791267666131197277221912,
241
- -0.9931285991850949247861223884713202782226,
242
- 0.9931285991850949247861223884713202782226,
243
- ],
245
+ tValues20,
244
246
  [
245
247
  0, -0.1455618541608950909370309823386863301163,
246
248
  0.1455618541608950909370309823386863301163,
@@ -339,6 +341,28 @@ export const tValues = [
339
341
  ],
340
342
  ];
341
343
  // Legendre-Gauss weights (wi values, defined by a function linked to in the Bezier primer article)
344
+ const cValues20 = [
345
+ 0.1527533871307258506980843319550975934919,
346
+ 0.1527533871307258506980843319550975934919,
347
+ 0.1491729864726037467878287370019694366926,
348
+ 0.1491729864726037467878287370019694366926,
349
+ 0.1420961093183820513292983250671649330345,
350
+ 0.1420961093183820513292983250671649330345,
351
+ 0.1316886384491766268984944997481631349161,
352
+ 0.1316886384491766268984944997481631349161,
353
+ 0.118194531961518417312377377711382287005,
354
+ 0.118194531961518417312377377711382287005,
355
+ 0.1019301198172404350367501354803498761666,
356
+ 0.1019301198172404350367501354803498761666,
357
+ 0.0832767415767047487247581432220462061001,
358
+ 0.0832767415767047487247581432220462061001,
359
+ 0.0626720483341090635695065351870416063516,
360
+ 0.0626720483341090635695065351870416063516,
361
+ 0.040601429800386941331039952274932109879,
362
+ 0.040601429800386941331039952274932109879,
363
+ 0.0176140071391521183118619623518528163621,
364
+ 0.0176140071391521183118619623518528163621,
365
+ ];
342
366
  export const cValues = [
343
367
  [],
344
368
  [],
@@ -564,28 +588,7 @@ export const cValues = [
564
588
  0.0194617882297264770363120414644384357529,
565
589
  0.0194617882297264770363120414644384357529,
566
590
  ],
567
- [
568
- 0.1527533871307258506980843319550975934919,
569
- 0.1527533871307258506980843319550975934919,
570
- 0.1491729864726037467878287370019694366926,
571
- 0.1491729864726037467878287370019694366926,
572
- 0.1420961093183820513292983250671649330345,
573
- 0.1420961093183820513292983250671649330345,
574
- 0.1316886384491766268984944997481631349161,
575
- 0.1316886384491766268984944997481631349161,
576
- 0.118194531961518417312377377711382287005,
577
- 0.118194531961518417312377377711382287005,
578
- 0.1019301198172404350367501354803498761666,
579
- 0.1019301198172404350367501354803498761666,
580
- 0.0832767415767047487247581432220462061001,
581
- 0.0832767415767047487247581432220462061001,
582
- 0.0626720483341090635695065351870416063516,
583
- 0.0626720483341090635695065351870416063516,
584
- 0.040601429800386941331039952274932109879,
585
- 0.040601429800386941331039952274932109879,
586
- 0.0176140071391521183118619623518528163621,
587
- 0.0176140071391521183118619623518528163621,
588
- ],
591
+ cValues20,
589
592
  [
590
593
  0.1460811336496904271919851476833711882448,
591
594
  0.1445244039899700590638271665537525436099,
@@ -688,84 +691,41 @@ export const cValues = [
688
691
  // LUT for binomial coefficient arrays per curve order 'n'
689
692
  export const binomialCoefficients = [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]];
690
693
  export const getCubicArcLength = (xs, ys, t) => {
691
- let sum;
692
- let correctedT;
693
- /*if (xs.length >= tValues.length) {
694
- throw new Error('too high n bezier');
695
- }*/
696
- const n = 20;
697
694
  const z = t / 2;
698
- sum = 0;
699
- for (let i = 0; i < n; i++) {
700
- correctedT = z * tValues[n][i] + z;
701
- sum += cValues[n][i] * BFunc(xs, ys, correctedT);
695
+ let sum = 0;
696
+ for (let i = 0; i < tValues20.length; i++) {
697
+ const correctedT = z * tValues20[i] + z;
698
+ sum += cValues20[i] * BFunc(xs, ys, correctedT);
702
699
  }
703
700
  return z * sum;
704
701
  };
705
- export const getQuadraticArcLength = (xs, ys, t) => {
706
- if (t === undefined) {
707
- t = 1;
708
- }
709
- const ax = xs[0] - 2 * xs[1] + xs[2];
710
- const ay = ys[0] - 2 * ys[1] + ys[2];
711
- const bx = 2 * xs[1] - 2 * xs[0];
712
- const by = 2 * ys[1] - 2 * ys[0];
713
- const A = 4 * (ax * ax + ay * ay);
714
- const B = 4 * (ax * bx + ay * by);
715
- const C = bx * bx + by * by;
716
- if (A === 0) {
717
- return (t * Math.sqrt(Math.pow(xs[2] - xs[0], 2) + Math.pow(ys[2] - ys[0], 2)));
718
- }
719
- const b = B / (2 * A);
720
- const c = C / A;
721
- const u = t + b;
722
- const k = c - b * b;
723
- const uuk = u * u + k > 0 ? Math.sqrt(u * u + k) : 0;
724
- const bbk = b * b + k > 0 ? Math.sqrt(b * b + k) : 0;
725
- const term = b + Math.sqrt(b * b + k) !== 0
726
- ? k * Math.log(Math.abs((u + uuk) / (b + bbk)))
727
- : 0;
728
- return (Math.sqrt(A) / 2) * (u * uuk - b * bbk + term);
729
- };
702
+ // a quadratic elevated to a cubic is the same curve, and the cubic quadrature
703
+ // is stable where the closed form for a quadratic cancels itself out
704
+ export const getQuadraticArcLength = (xs, ys, t = 1) => getCubicArcLength([
705
+ xs[0],
706
+ xs[0] + (2 / 3) * (xs[1] - xs[0]),
707
+ xs[2] + (2 / 3) * (xs[1] - xs[2]),
708
+ xs[2],
709
+ ], [
710
+ ys[0],
711
+ ys[0] + (2 / 3) * (ys[1] - ys[0]),
712
+ ys[2] + (2 / 3) * (ys[1] - ys[2]),
713
+ ys[2],
714
+ ], t);
730
715
  function BFunc(xs, ys, t) {
731
- const xbase = getDerivative(1, t, xs);
732
- const ybase = getDerivative(1, t, ys);
733
- const combined = xbase * xbase + ybase * ybase;
734
- return Math.sqrt(combined);
716
+ // Interpolate the three derivative control points of a cubic curve.
717
+ const mt = 1 - t;
718
+ const a = mt * mt;
719
+ const b = 2 * mt * t;
720
+ const c = t * t;
721
+ const dx = a * (3 * (xs[1] - xs[0])) +
722
+ b * (3 * (xs[2] - xs[1])) +
723
+ c * (3 * (xs[3] - xs[2]));
724
+ const dy = a * (3 * (ys[1] - ys[0])) +
725
+ b * (3 * (ys[2] - ys[1])) +
726
+ c * (3 * (ys[3] - ys[2]));
727
+ return Math.sqrt(dx * dx + dy * dy);
735
728
  }
736
- /**
737
- * Compute the curve derivative (hodograph) at t.
738
- */
739
- const getDerivative = (derivative, t, vs) => {
740
- // the derivative of any 't'-less function is zero.
741
- const n = vs.length - 1;
742
- let _vs;
743
- let value;
744
- if (n === 0) {
745
- return 0;
746
- }
747
- // direct values? compute!
748
- if (derivative === 0) {
749
- value = 0;
750
- for (let k = 0; k <= n; k++) {
751
- value +=
752
- binomialCoefficients[n][k] *
753
- Math.pow(1 - t, n - k) *
754
- Math.pow(t, k) *
755
- vs[k];
756
- }
757
- return value;
758
- }
759
- else {
760
- // Still some derivative? go down one order, then try
761
- // for the lower order curve's.
762
- _vs = new Array(n);
763
- for (let k = 0; k < n; k++) {
764
- _vs[k] = n * (vs[k + 1] - vs[k]);
765
- }
766
- return getDerivative(derivative - 1, t, _vs);
767
- }
768
- };
769
729
  export const t2length = (length, totalLength, func) => {
770
730
  let error = 1;
771
731
  let t = length / totalLength;
package/lib/Canvas.d.ts CHANGED
@@ -41,7 +41,7 @@ export declare class Canvas {
41
41
  * @name Konva.Canvas#getPixelRatio
42
42
  * @returns {Number} pixel ratio
43
43
  * @example
44
- * var pixelRatio = layer.getCanvas.getPixelRatio();
44
+ * var pixelRatio = layer.getCanvas().getPixelRatio();
45
45
  */
46
46
  getPixelRatio(): number;
47
47
  /**
package/lib/Canvas.js CHANGED
@@ -58,7 +58,7 @@ export class Canvas {
58
58
  * @name Konva.Canvas#getPixelRatio
59
59
  * @returns {Number} pixel ratio
60
60
  * @example
61
- * var pixelRatio = layer.getCanvas.getPixelRatio();
61
+ * var pixelRatio = layer.getCanvas().getPixelRatio();
62
62
  */
63
63
  getPixelRatio() {
64
64
  return this.pixelRatio;
@@ -108,6 +108,8 @@ export class Canvas {
108
108
  width = width || 0;
109
109
  height = height || 0;
110
110
  const pixelRatio = this.pixelRatio;
111
+ const context = this.getContext()._context;
112
+ const imageSmoothingEnabled = context.imageSmoothingEnabled;
111
113
  this._logicalWidth = width;
112
114
  this._logicalHeight = height;
113
115
  // take into account pixel ratio. Assigning a dimension reallocates the
@@ -116,7 +118,8 @@ export class Canvas {
116
118
  this.height = this._canvas.height = this._bitmapSize(height);
117
119
  this._canvas.style.width = width + 'px';
118
120
  this._canvas.style.height = height + 'px';
119
- this.getContext()._context.scale(pixelRatio, pixelRatio);
121
+ context.scale(pixelRatio, pixelRatio);
122
+ context.imageSmoothingEnabled = imageSmoothingEnabled;
120
123
  }
121
124
  // setSize() re-allocates and clears the canvas even for the same size,
122
125
  // so lazily sized canvases use this to stay untouched when nothing changed
@@ -136,8 +139,7 @@ export class Canvas {
136
139
  */
137
140
  toDataURL(mimeType, quality) {
138
141
  try {
139
- // If this call fails (due to browser bug, like in Firefox 3.6),
140
- // then revert to previous no-parameter image/png behavior
142
+ // If the requested encoding fails, retry with the default PNG encoding.
141
143
  return this._canvas.toDataURL(mimeType, quality);
142
144
  }
143
145
  catch (e) {
@@ -148,7 +150,7 @@ export class Canvas {
148
150
  Util.error('Unable to get data URL. ' +
149
151
  err.message +
150
152
  ' For more info read https://konvajs.org/docs/posts/Tainted_Canvas.html.');
151
- return '';
153
+ throw err;
152
154
  }
153
155
  }
154
156
  }
@@ -167,6 +169,7 @@ export class HitCanvas extends Canvas {
167
169
  super(config);
168
170
  this.hitCanvas = true;
169
171
  this.context = new HitContext(this);
172
+ this.context.imageSmoothingEnabled = false;
170
173
  this.setSize(config.width, config.height);
171
174
  }
172
175
  }
package/lib/Container.js CHANGED
@@ -285,7 +285,7 @@ export class Container extends Node {
285
285
  // Perf: bracket absoluteTransform cascades with a depth counter so
286
286
  // Node._runAfterAbsTransformCascade can defer work until the outermost
287
287
  // walk returns. try/finally so depth/flush survive listener exceptions.
288
- const isAbsTransform = attr === 'absoluteTransform';
288
+ const isAbsTransform = attr === undefined || attr === 'absoluteTransform';
289
289
  if (isAbsTransform)
290
290
  Node._absTransformCascadeDepth++;
291
291
  try {
@@ -293,7 +293,7 @@ export class Container extends Node {
293
293
  // a cached container is drawn from its bitmap, so its descendants keep
294
294
  // their transform caches while it moves (perf); Node.getAbsoluteTransform
295
295
  // skips those caches under a cached ancestor
296
- if (isAbsTransform && this.isCached())
296
+ if (attr === 'absoluteTransform' && this.isCached())
297
297
  return;
298
298
  (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) {
299
299
  node._clearSelfAndDescendantCache(attr);
@@ -319,8 +319,8 @@ export class Container extends Node {
319
319
  }
320
320
  drawScene(can, top, bufferCanvas) {
321
321
  const layer = this.getLayer(), canvas = can || (layer && layer.getCanvas()), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedSceneCanvas = cachedCanvas && cachedCanvas.scene;
322
- const caching = canvas && canvas.isCache;
323
- if (!this.isVisible() && !caching) {
322
+ const caching = top === this;
323
+ if (!(top ? this._isVisible(top) : this.isVisible()) && !caching) {
324
324
  return this;
325
325
  }
326
326
  if (cachedSceneCanvas) {
@@ -397,6 +397,9 @@ export class Container extends Node {
397
397
  var _a;
398
398
  const skipTransform = config.skipTransform;
399
399
  const relativeTo = config.relativeTo;
400
+ const [a, b, c, d] = this.getAbsoluteTransform().getMatrix();
401
+ // A zero scale cannot project local padding for a non-scaling stroke.
402
+ const measureTransformed = !skipTransform && a * d - b * c === 0;
400
403
  let minX, minY, maxX, maxY;
401
404
  let selfRect = {
402
405
  x: Infinity,
@@ -411,7 +414,7 @@ export class Container extends Node {
411
414
  return;
412
415
  }
413
416
  const rect = child.getClientRect({
414
- relativeTo: that,
417
+ relativeTo: measureTransformed ? relativeTo : that,
415
418
  skipShadow: config.skipShadow,
416
419
  skipStroke: config.skipStroke,
417
420
  });
@@ -451,7 +454,7 @@ export class Container extends Node {
451
454
  height: 0,
452
455
  };
453
456
  }
454
- if (!skipTransform) {
457
+ if (!skipTransform && !measureTransformed) {
455
458
  return this._transformedRect(selfRect, relativeTo);
456
459
  }
457
460
  return selfRect;
package/lib/Context.d.ts CHANGED
@@ -324,12 +324,11 @@ export declare class SceneContext extends Context {
324
324
  constructor(canvas: Canvas, { willReadFrequently }?: {
325
325
  willReadFrequently?: boolean | undefined;
326
326
  });
327
- _fillColor(shape: Shape): void;
328
- _fillPattern(shape: Shape): void;
329
- _fillLinearGradient(shape: Shape): void;
330
- _fillRadialGradient(shape: Shape): void;
331
- _fill(shape: any): void;
327
+ _getFillPattern(shape: Shape): any;
328
+ _getFillStyle(shape: Shape): any;
329
+ _fill(shape: Shape): void;
332
330
  _strokeLinearGradient(shape: any): void;
331
+ _applyStrokeStyle(shape: Shape): void;
333
332
  _stroke(shape: any): void;
334
333
  _applyShadow(shape: any): void;
335
334
  }
package/lib/Context.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Util } from "./Util.js";
1
+ import { Transform, Util } from "./Util.js";
2
2
  import { Konva } from "./Global.js";
3
3
  function simplifyArray(arr) {
4
4
  const retArr = [], len = arr.length, util = Util;
@@ -648,77 +648,68 @@ export class SceneContext extends Context {
648
648
  willReadFrequently,
649
649
  });
650
650
  }
651
- _fillColor(shape) {
652
- const fill = shape.fill();
653
- this.setAttr('fillStyle', fill);
654
- shape._fillFunc(this);
655
- }
656
- _fillPattern(shape) {
657
- // `imageSmoothingEnabled` (set on the layer/context) controls pattern
658
- // smoothing in the browser. node-canvas ignores it for pattern fills and
659
- // instead exposes a non-standard `patternQuality` property, so mirror the
660
- // current smoothing setting onto it to keep both backends consistent.
651
+ _getFillPattern(shape) {
652
+ // node-canvas exposes pattern smoothing through patternQuality.
661
653
  const context = this._context;
662
654
  if ('patternQuality' in context) {
663
655
  context.patternQuality = context.imageSmoothingEnabled
664
656
  ? 'good'
665
657
  : 'nearest';
666
658
  }
667
- this.setAttr('fillStyle', shape._getFillPattern());
668
- shape._fillFunc(this);
669
- }
670
- _fillLinearGradient(shape) {
671
- const grd = shape._getLinearGradient();
672
- if (grd) {
673
- this.setAttr('fillStyle', grd);
674
- shape._fillFunc(this);
675
- }
676
- }
677
- _fillRadialGradient(shape) {
678
- const grd = shape._getRadialGradient();
679
- if (grd) {
680
- this.setAttr('fillStyle', grd);
681
- shape._fillFunc(this);
682
- }
659
+ return shape._getFillPattern();
660
+ }
661
+ _getFillStyle(shape) {
662
+ const color = shape.fill();
663
+ const priority = shape.fillPriority();
664
+ if (color && priority === 'color')
665
+ return color;
666
+ const pattern = shape.fillPatternImage();
667
+ if (pattern && priority === 'pattern')
668
+ return this._getFillPattern(shape);
669
+ const linear = shape.fillLinearGradientColorStops();
670
+ if (linear && priority === 'linear-gradient')
671
+ return shape._getLinearGradient();
672
+ const radial = shape.fillRadialGradientColorStops();
673
+ if (radial && priority === 'radial-gradient')
674
+ return shape._getRadialGradient();
675
+ if (color)
676
+ return color;
677
+ if (pattern)
678
+ return this._getFillPattern(shape);
679
+ if (linear)
680
+ return shape._getLinearGradient();
681
+ if (radial)
682
+ return shape._getRadialGradient();
683
683
  }
684
684
  _fill(shape) {
685
- const hasColor = shape.fill(), fillPriority = shape.getFillPriority();
686
- // priority fills
687
- if (hasColor && fillPriority === 'color') {
688
- this._fillColor(shape);
689
- return;
690
- }
691
- const hasPattern = shape.getFillPatternImage();
692
- if (hasPattern && fillPriority === 'pattern') {
693
- this._fillPattern(shape);
694
- return;
695
- }
696
- const hasLinearGradient = shape.getFillLinearGradientColorStops();
697
- if (hasLinearGradient && fillPriority === 'linear-gradient') {
698
- this._fillLinearGradient(shape);
699
- return;
700
- }
701
- const hasRadialGradient = shape.getFillRadialGradientColorStops();
702
- if (hasRadialGradient && fillPriority === 'radial-gradient') {
703
- this._fillRadialGradient(shape);
704
- return;
705
- }
706
- // now just try and fill with whatever is available
707
- if (hasColor) {
708
- this._fillColor(shape);
709
- }
710
- else if (hasPattern) {
711
- this._fillPattern(shape);
712
- }
713
- else if (hasLinearGradient) {
714
- this._fillLinearGradient(shape);
715
- }
716
- else if (hasRadialGradient) {
717
- this._fillRadialGradient(shape);
685
+ const style = this._getFillStyle(shape);
686
+ if (style !== undefined) {
687
+ this.setAttr('fillStyle', style);
688
+ shape._fillFunc(this);
718
689
  }
719
690
  }
720
691
  _strokeLinearGradient(shape) {
721
- const start = shape.getStrokeLinearGradientStartPoint(), end = shape.getStrokeLinearGradientEndPoint(), colorStops = shape.getStrokeLinearGradientColorStops(), grd = this.createLinearGradient(start.x, start.y, end.x, end.y);
692
+ let start = shape.getStrokeLinearGradientStartPoint(), end = shape.getStrokeLinearGradientEndPoint();
693
+ if (!shape.getStrokeScaleEnabled()) {
694
+ const { a, b, c, d, e, f } = this._context.getTransform();
695
+ const transform = new Transform([a, b, c, d, e, f]);
696
+ const ratio = this.canvas.getPixelRatio();
697
+ const dx = end.x - start.x;
698
+ const dy = end.y - start.y;
699
+ // Gradient colors follow the inverse-transposed direction. Transforming
700
+ // the endpoints alone distorts them under skew or nonuniform scaling.
701
+ const nx = d * dx - b * dy;
702
+ const ny = a * dy - c * dx;
703
+ const lengthSquared = nx * nx + ny * ny;
704
+ const scale = lengthSquared
705
+ ? ((dx * dx + dy * dy) * (a * d - b * c)) / lengthSquared
706
+ : 0;
707
+ start = transform.point(start);
708
+ end = { x: start.x + nx * scale, y: start.y + ny * scale };
709
+ start = { x: start.x / ratio, y: start.y / ratio };
710
+ end = { x: end.x / ratio, y: end.y / ratio };
711
+ }
712
+ const colorStops = shape.getStrokeLinearGradientColorStops(), grd = this.createLinearGradient(start.x, start.y, end.x, end.y);
722
713
  if (colorStops) {
723
714
  // build color stops
724
715
  for (let n = 0; n < colorStops.length; n += 2) {
@@ -727,14 +718,20 @@ export class SceneContext extends Context {
727
718
  this.setAttr('strokeStyle', grd);
728
719
  }
729
720
  }
721
+ _applyStrokeStyle(shape) {
722
+ if (shape.strokeLinearGradientColorStops()) {
723
+ this._strokeLinearGradient(shape);
724
+ }
725
+ else {
726
+ this.setAttr('strokeStyle', shape.stroke());
727
+ }
728
+ }
730
729
  _stroke(shape) {
731
730
  const dash = shape.dash(),
732
731
  // ignore strokeScaleEnabled for Text
733
732
  strokeScaleEnabled = shape.getStrokeScaleEnabled();
734
733
  if (!strokeScaleEnabled) {
735
734
  this.save();
736
- const pixelRatio = this.getCanvas().getPixelRatio();
737
- this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
738
735
  }
739
736
  this._applyLineCap(shape);
740
737
  if (dash && shape.dashEnabled()) {
@@ -742,19 +739,27 @@ export class SceneContext extends Context {
742
739
  this.setAttr('lineDashOffset', shape.dashOffset());
743
740
  }
744
741
  this.setAttr('lineWidth', shape.strokeWidth());
745
- if (!shape.getShadowForStrokeEnabled()) {
742
+ const shadowColor = this.shadowColor;
743
+ const shadowForStrokeEnabled = shape.getShadowForStrokeEnabled();
744
+ if (!shadowForStrokeEnabled) {
746
745
  this.setAttr('shadowColor', 'rgba(0,0,0,0)');
747
746
  }
748
- const hasLinearGradient = shape.getStrokeLinearGradientColorStops();
749
- if (hasLinearGradient) {
750
- this._strokeLinearGradient(shape);
747
+ this._applyStrokeStyle(shape);
748
+ // Resolve the gradient in local coordinates before drawing an unscaled stroke.
749
+ if (!strokeScaleEnabled) {
750
+ const pixelRatio = this.getCanvas().getPixelRatio();
751
+ this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
751
752
  }
752
- else {
753
- this.setAttr('strokeStyle', shape.stroke());
753
+ try {
754
+ shape._strokeFunc(this);
754
755
  }
755
- shape._strokeFunc(this);
756
- if (!strokeScaleEnabled) {
757
- this.restore();
756
+ finally {
757
+ if (!shadowForStrokeEnabled) {
758
+ this.setAttr('shadowColor', shadowColor);
759
+ }
760
+ if (!strokeScaleEnabled) {
761
+ this.restore();
762
+ }
758
763
  }
759
764
  }
760
765
  _applyShadow(shape) {
@@ -1,22 +1,24 @@
1
1
  import type { Node } from './Node.ts';
2
+ import type { Stage } from './Stage.ts';
2
3
  import type { Vector2d } from './types.ts';
3
4
  type DragElement = {
4
5
  node: Node;
5
6
  startPointerPos: Vector2d;
6
7
  offset: Vector2d;
7
8
  pointerId?: number;
9
+ pointerEventType?: Stage['_pointerEventType'];
8
10
  lastPos?: Vector2d;
9
11
  startEvent?: any;
10
12
  dragStatus: 'ready' | 'dragging' | 'stopped';
11
13
  };
12
14
  export declare const DD: {
13
15
  readonly isDragging: boolean;
14
- justDragged: boolean;
15
16
  readonly node: Node<import("./Node.ts").NodeConfig> | undefined;
16
17
  _dragElements: Map<number, DragElement>;
17
18
  _listenToWindow(win: Window): void;
19
+ _batchEvents(callback: () => void, win?: Window): void;
18
20
  _drag(evt: any, win?: Window): void;
19
- _endDragBefore(evt?: any, win?: Window): void;
20
- _endDragAfter(evt: any): void;
21
+ _endDragBefore(evt?: any, win?: Window, only?: DragElement): void;
22
+ _endDragAfter(evt: any, only?: DragElement): void;
21
23
  };
22
24
  export {};