konva 10.5.0 → 10.7.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 (79) hide show
  1. package/README.md +16 -12
  2. package/konva.js +3623 -3819
  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 +10 -3
  9. package/lib/Canvas.js +91 -10
  10. package/lib/Container.d.ts +3 -2
  11. package/lib/Container.js +31 -17
  12. package/lib/Context.d.ts +9 -7
  13. package/lib/Context.js +99 -73
  14. package/lib/DragAndDrop.d.ts +4 -3
  15. package/lib/DragAndDrop.js +39 -25
  16. package/lib/Factory.d.ts +3 -2
  17. package/lib/Factory.js +10 -9
  18. package/lib/Global.d.ts +2 -7
  19. package/lib/Global.js +8 -9
  20. package/lib/Group.d.ts +7 -1
  21. package/lib/Group.js +61 -0
  22. package/lib/Layer.d.ts +1 -1
  23. package/lib/Layer.js +13 -8
  24. package/lib/Node.d.ts +25 -19
  25. package/lib/Node.js +178 -169
  26. package/lib/PointerEvents.d.ts +1 -2
  27. package/lib/PointerEvents.js +20 -18
  28. package/lib/Shape.d.ts +9 -15
  29. package/lib/Shape.js +190 -86
  30. package/lib/Stage.d.ts +19 -15
  31. package/lib/Stage.js +184 -99
  32. package/lib/Tween.js +59 -66
  33. package/lib/Util.d.ts +3 -2
  34. package/lib/Util.js +46 -16
  35. package/lib/_CoreInternals.d.ts +23 -11
  36. package/lib/_FullInternals.d.ts +23 -11
  37. package/lib/filters/Blur.js +16 -12
  38. package/lib/filters/Brighten.d.ts +1 -0
  39. package/lib/filters/Brighten.js +1 -0
  40. package/lib/filters/Brightness.d.ts +2 -1
  41. package/lib/filters/Brightness.js +4 -4
  42. package/lib/filters/Emboss.js +16 -58
  43. package/lib/filters/Kaleidoscope.js +8 -15
  44. package/lib/filters/Mask.js +8 -8
  45. package/lib/filters/Pixelate.js +16 -29
  46. package/lib/filters/Threshold.d.ts +1 -1
  47. package/lib/filters/Threshold.js +1 -1
  48. package/lib/shapes/Arc.js +8 -6
  49. package/lib/shapes/Arrow.d.ts +3 -6
  50. package/lib/shapes/Arrow.js +75 -47
  51. package/lib/shapes/Circle.d.ts +1 -0
  52. package/lib/shapes/Circle.js +4 -1
  53. package/lib/shapes/Ellipse.d.ts +1 -0
  54. package/lib/shapes/Ellipse.js +4 -1
  55. package/lib/shapes/Image.d.ts +2 -1
  56. package/lib/shapes/Image.js +22 -22
  57. package/lib/shapes/Label.d.ts +2 -3
  58. package/lib/shapes/Label.js +25 -41
  59. package/lib/shapes/Line.d.ts +2 -1
  60. package/lib/shapes/Line.js +5 -2
  61. package/lib/shapes/Path.d.ts +11 -2
  62. package/lib/shapes/Path.js +133 -143
  63. package/lib/shapes/Rect.d.ts +1 -0
  64. package/lib/shapes/Rect.js +4 -1
  65. package/lib/shapes/RegularPolygon.js +2 -2
  66. package/lib/shapes/Ring.d.ts +7 -0
  67. package/lib/shapes/Ring.js +8 -1
  68. package/lib/shapes/Sprite.d.ts +2 -1
  69. package/lib/shapes/Sprite.js +8 -9
  70. package/lib/shapes/Star.d.ts +6 -0
  71. package/lib/shapes/Star.js +5 -1
  72. package/lib/shapes/Text.d.ts +16 -5
  73. package/lib/shapes/Text.js +134 -65
  74. package/lib/shapes/TextPath.d.ts +11 -3
  75. package/lib/shapes/TextPath.js +56 -27
  76. package/lib/shapes/Transformer.js +95 -96
  77. package/lib/shapes/Wedge.js +1 -1
  78. package/lib/types.d.ts +2 -2
  79. package/package.json +5 -20
@@ -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
@@ -1,3 +1,4 @@
1
+ import type { IRect } from './types.ts';
1
2
  import type { Context } from './Context.ts';
2
3
  interface ICanvasConfig {
3
4
  width?: number;
@@ -25,8 +26,6 @@ export declare class Canvas {
25
26
  _logicalWidth: number;
26
27
  _logicalHeight: number;
27
28
  isCache: boolean;
28
- x: number;
29
- y: number;
30
29
  constructor(config: ICanvasConfig);
31
30
  /**
32
31
  * get canvas context
@@ -41,7 +40,7 @@ export declare class Canvas {
41
40
  * @name Konva.Canvas#getPixelRatio
42
41
  * @returns {Number} pixel ratio
43
42
  * @example
44
- * var pixelRatio = layer.getCanvas.getPixelRatio();
43
+ * var pixelRatio = layer.getCanvas().getPixelRatio();
45
44
  */
46
45
  getPixelRatio(): number;
47
46
  /**
@@ -78,6 +77,14 @@ export declare class Canvas {
78
77
  toDataURL(mimeType: any, quality: any): string;
79
78
  }
80
79
  export declare class SceneCanvas extends Canvas {
80
+ private _isolationCanvas?;
81
+ _isolationRect?: IRect;
82
+ private _isolationPeak;
83
+ private _isolationLowFrames;
84
+ _prepareIsolationCanvas(rect?: IRect): SceneCanvas;
85
+ _trimIsolationCanvas(): void;
86
+ _releaseIsolationCanvas(): void;
87
+ setSize(width: any, height: any): void;
81
88
  constructor(config?: ICanvasConfig);
82
89
  }
83
90
  export declare class HitCanvas extends Canvas {
package/lib/Canvas.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Util } from "./Util.js";
1
+ import { Transform, Util } from "./Util.js";
2
2
  import { SceneContext, HitContext } from "./Context.js";
3
3
  import { Konva } from "./Global.js";
4
4
  /**
@@ -22,10 +22,6 @@ export class Canvas {
22
22
  this._logicalWidth = 0;
23
23
  this._logicalHeight = 0;
24
24
  this.isCache = false;
25
- // origin of a buffer canvas in the coordinate space of the canvas it is
26
- // drawn back into, see Shape.drawScene
27
- this.x = 0;
28
- this.y = 0;
29
25
  const conf = config || {};
30
26
  // a Konva.pixelRatio reset to undefined or 0 means the device ratio
31
27
  const pixelRatio = conf.pixelRatio ||
@@ -58,7 +54,7 @@ export class Canvas {
58
54
  * @name Konva.Canvas#getPixelRatio
59
55
  * @returns {Number} pixel ratio
60
56
  * @example
61
- * var pixelRatio = layer.getCanvas.getPixelRatio();
57
+ * var pixelRatio = layer.getCanvas().getPixelRatio();
62
58
  */
63
59
  getPixelRatio() {
64
60
  return this.pixelRatio;
@@ -108,6 +104,8 @@ export class Canvas {
108
104
  width = width || 0;
109
105
  height = height || 0;
110
106
  const pixelRatio = this.pixelRatio;
107
+ const context = this.getContext()._context;
108
+ const imageSmoothingEnabled = context.imageSmoothingEnabled;
111
109
  this._logicalWidth = width;
112
110
  this._logicalHeight = height;
113
111
  // take into account pixel ratio. Assigning a dimension reallocates the
@@ -116,7 +114,8 @@ export class Canvas {
116
114
  this.height = this._canvas.height = this._bitmapSize(height);
117
115
  this._canvas.style.width = width + 'px';
118
116
  this._canvas.style.height = height + 'px';
119
- this.getContext()._context.scale(pixelRatio, pixelRatio);
117
+ context.scale(pixelRatio, pixelRatio);
118
+ context.imageSmoothingEnabled = imageSmoothingEnabled;
120
119
  }
121
120
  // setSize() re-allocates and clears the canvas even for the same size,
122
121
  // so lazily sized canvases use this to stay untouched when nothing changed
@@ -136,8 +135,7 @@ export class Canvas {
136
135
  */
137
136
  toDataURL(mimeType, quality) {
138
137
  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
138
+ // If the requested encoding fails, retry with the default PNG encoding.
141
139
  return this._canvas.toDataURL(mimeType, quality);
142
140
  }
143
141
  catch (e) {
@@ -148,14 +146,96 @@ export class Canvas {
148
146
  Util.error('Unable to get data URL. ' +
149
147
  err.message +
150
148
  ' For more info read https://konvajs.org/docs/posts/Tainted_Canvas.html.');
151
- return '';
149
+ throw err;
152
150
  }
153
151
  }
154
152
  }
155
153
  }
156
154
  export class SceneCanvas extends Canvas {
155
+ // A cleared surface over `rect` (in the current drawing space), or over the
156
+ // whole canvas, in this canvas's device pixels. Siblings reuse it; nested
157
+ // groups borrow from the surface itself, so they cannot clear a parent's
158
+ // unfinished image.
159
+ _prepareIsolationCanvas(rect) {
160
+ const { a, b, c, d, e, f } = this.getContext()._context.getTransform();
161
+ const view = this._isolationRect || this;
162
+ let x = 0, y = 0, width = view.width, height = view.height;
163
+ if (rect) {
164
+ const box = new Transform([a, b, c, d, e, f])._getTransformedRect(rect);
165
+ // Round outwards, with a pixel for antialiasing.
166
+ x = Math.max(0, Math.floor(box.x) - 1);
167
+ y = Math.max(0, Math.floor(box.y) - 1);
168
+ width = Math.min(width, Math.ceil(box.x + box.width) + 1) - x;
169
+ height = Math.min(height, Math.ceil(box.y + box.height) + 1) - y;
170
+ }
171
+ if (!(width > 0 && height > 0)) {
172
+ // An empty source must still composite for copy/destination-in/etc.
173
+ x = y = 0;
174
+ width = height = 1;
175
+ }
176
+ const surface = (this._isolationCanvas || (this._isolationCanvas = new SceneCanvas({
177
+ width: 0,
178
+ height: 0,
179
+ pixelRatio: this.pixelRatio,
180
+ })));
181
+ if (surface.width < width || surface.height < height) {
182
+ // Headroom on a short side, so a growing group does not reallocate
183
+ // every frame.
184
+ const ratio = this.pixelRatio;
185
+ const grow = (need, have, max) => Math.min(max, Math.ceil((have < need ? Math.max(need, have * 1.5) : have) / ratio));
186
+ surface.setSize(grow(width, surface.width, this._logicalWidth), grow(height, surface.height, this._logicalHeight));
187
+ }
188
+ surface._isolationRect = { x, y, width, height };
189
+ const peak = surface._isolationPeak;
190
+ peak.width = Math.max(peak.width, width);
191
+ peak.height = Math.max(peak.height, height);
192
+ const context = surface.getContext();
193
+ context.setTransform(1, 0, 0, 1, 0, 0);
194
+ context.clearRect(0, 0, width, height);
195
+ context.setTransform(a, b, c, d, e - x, f - y);
196
+ const parent = this.getContext();
197
+ context.imageSmoothingEnabled = parent.imageSmoothingEnabled;
198
+ context.imageSmoothingQuality = parent.imageSmoothingQuality;
199
+ context.direction = parent.direction;
200
+ return surface;
201
+ }
202
+ // After a frame, free surfaces that ten frames in a row did not use or used
203
+ // a small part of. Content that changes size keeps its surface.
204
+ _trimIsolationCanvas() {
205
+ const surface = this._isolationCanvas;
206
+ if (!surface)
207
+ return;
208
+ const peak = surface._isolationPeak;
209
+ const low = surface.width * surface.height > 4 * peak.width * peak.height;
210
+ surface._isolationLowFrames = low ? surface._isolationLowFrames + 1 : 0;
211
+ if (surface._isolationLowFrames >= 10) {
212
+ this._releaseIsolationCanvas();
213
+ }
214
+ else {
215
+ peak.width = peak.height = 0;
216
+ surface._trimIsolationCanvas();
217
+ }
218
+ }
219
+ _releaseIsolationCanvas() {
220
+ const surface = this._isolationCanvas;
221
+ if (surface) {
222
+ surface._releaseIsolationCanvas();
223
+ Util.releaseCanvas(surface._canvas);
224
+ this._isolationCanvas = undefined;
225
+ }
226
+ }
227
+ setSize(width, height) {
228
+ // A bitmap reset also resets its origin-clean state. A surface from the
229
+ // previous lifetime may be tainted even after clearing its pixels.
230
+ this._releaseIsolationCanvas();
231
+ super.setSize(width, height);
232
+ }
157
233
  constructor(config = { width: 0, height: 0, willReadFrequently: false }) {
158
234
  super(config);
235
+ // The largest width and height requested since the last trim.
236
+ this._isolationPeak = { width: 0, height: 0 };
237
+ // Consecutive frames that used less than a quarter of the surface.
238
+ this._isolationLowFrames = 0;
159
239
  this.context = new SceneContext(this, {
160
240
  willReadFrequently: config.willReadFrequently,
161
241
  });
@@ -167,6 +247,7 @@ export class HitCanvas extends Canvas {
167
247
  super(config);
168
248
  this.hitCanvas = true;
169
249
  this.context = new HitContext(this);
250
+ this.context.imageSmoothingEnabled = false;
170
251
  this.setSize(config.width, config.height);
171
252
  }
172
253
  }
@@ -168,9 +168,10 @@ export declare abstract class Container<ChildType extends Node = Node, Config ex
168
168
  getAllIntersections(pos: any): Shape<import("./Shape.ts").ShapeConfig>[];
169
169
  _clearSelfAndDescendantCache(attr?: string): void;
170
170
  _setChildrenIndices(): void;
171
- drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas): this;
171
+ drawScene(can?: SceneCanvas, top?: Node): this;
172
172
  drawHit(can?: HitCanvas, top?: Node): this;
173
- _drawChildren(drawMethod: any, canvas: any, top: any, bufferCanvas?: any): void;
173
+ _drawChildren(drawMethod: any, canvas: any, top: any): void;
174
+ _drawChildNodes(drawMethod: any, canvas: any, top?: Node): void;
174
175
  getClientRect(config?: GetClientRectConfig): IRect;
175
176
  clip: GetSet<IRect, this>;
176
177
  clipX: GetSet<number, this>;
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);
@@ -317,10 +317,10 @@ export class Container extends Node {
317
317
  });
318
318
  this._requestDraw();
319
319
  }
320
- drawScene(can, top, bufferCanvas) {
320
+ drawScene(can, top) {
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) {
@@ -331,7 +331,7 @@ export class Container extends Node {
331
331
  context.restore();
332
332
  }
333
333
  else {
334
- this._drawChildren('drawScene', canvas, top, bufferCanvas);
334
+ this._drawChildren('drawScene', canvas, top);
335
335
  }
336
336
  return this;
337
337
  }
@@ -352,8 +352,7 @@ export class Container extends Node {
352
352
  }
353
353
  return this;
354
354
  }
355
- _drawChildren(drawMethod, canvas, top, bufferCanvas) {
356
- var _a;
355
+ _drawChildren(drawMethod, canvas, top) {
357
356
  const context = canvas && canvas.getContext(), clipWidth = this.clipWidth(), clipHeight = this.clipHeight(), clipFunc = this.clipFunc(), hasClip = (typeof clipWidth === 'number' && typeof clipHeight === 'number') ||
358
357
  clipFunc;
359
358
  const selfCache = top === this;
@@ -383,20 +382,34 @@ export class Container extends Node {
383
382
  context.save();
384
383
  context._applyGlobalCompositeOperation(this);
385
384
  }
386
- (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
387
- child[drawMethod](canvas, top, bufferCanvas);
388
- });
389
- if (hasComposition) {
390
- context.restore();
385
+ try {
386
+ this._drawChildNodes(drawMethod, canvas, top);
391
387
  }
392
- if (hasClip) {
393
- context.restore();
388
+ finally {
389
+ if (hasComposition) {
390
+ context.restore();
391
+ }
392
+ if (hasClip) {
393
+ context.restore();
394
+ }
394
395
  }
395
396
  }
397
+ _drawChildNodes(drawMethod, canvas, top) {
398
+ var _a;
399
+ (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
400
+ child[drawMethod](canvas, top);
401
+ });
402
+ }
396
403
  getClientRect(config = {}) {
397
404
  var _a;
405
+ const cachedRect = this._getCachedSceneRect(config);
406
+ if (cachedRect)
407
+ return cachedRect;
398
408
  const skipTransform = config.skipTransform;
399
409
  const relativeTo = config.relativeTo;
410
+ const [a, b, c, d] = this.getAbsoluteTransform().getMatrix();
411
+ // A zero scale cannot project local padding for a non-scaling stroke.
412
+ const measureTransformed = !skipTransform && (config._forDrawing || a * d - b * c === 0);
400
413
  let minX, minY, maxX, maxY;
401
414
  let selfRect = {
402
415
  x: Infinity,
@@ -411,7 +424,8 @@ export class Container extends Node {
411
424
  return;
412
425
  }
413
426
  const rect = child.getClientRect({
414
- relativeTo: that,
427
+ relativeTo: measureTransformed ? relativeTo : that,
428
+ _forDrawing: config._forDrawing,
415
429
  skipShadow: config.skipShadow,
416
430
  skipStroke: config.skipStroke,
417
431
  });
@@ -451,7 +465,7 @@ export class Container extends Node {
451
465
  height: 0,
452
466
  };
453
467
  }
454
- if (!skipTransform) {
468
+ if (!skipTransform && !measureTransformed) {
455
469
  return this._transformedRect(selfRect, relativeTo);
456
470
  }
457
471
  return selfRect;
package/lib/Context.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import type { Canvas } from './Canvas.ts';
1
+ import type { Canvas, SceneCanvas } from './Canvas.ts';
2
2
  import type { Shape } from './Shape.ts';
3
3
  import type { IRect } from './types.ts';
4
4
  import type { Node } from './Node.ts';
5
- declare const CONTEXT_PROPERTIES: readonly ["fillStyle", "strokeStyle", "shadowColor", "shadowBlur", "shadowOffsetX", "shadowOffsetY", "letterSpacing", "lineCap", "lineDashOffset", "lineJoin", "lineWidth", "miterLimit", "direction", "font", "textAlign", "textBaseline", "globalAlpha", "globalCompositeOperation", "imageSmoothingEnabled", "filter"];
5
+ declare const CONTEXT_PROPERTIES: readonly ["fillStyle", "strokeStyle", "shadowColor", "shadowBlur", "shadowOffsetX", "shadowOffsetY", "letterSpacing", "lineCap", "lineDashOffset", "lineJoin", "lineWidth", "miterLimit", "direction", "font", "textAlign", "textBaseline", "globalAlpha", "globalCompositeOperation", "imageSmoothingEnabled", "imageSmoothingQuality", "filter"];
6
6
  export declare function isCSSFiltersSupported(): boolean;
7
7
  interface ExtendedCanvasRenderingContext2D extends CanvasRenderingContext2D {
8
8
  letterSpacing: string;
@@ -84,7 +84,10 @@ export declare class Context {
84
84
  * @param {Number} [bounds.height]
85
85
  */
86
86
  clear(bounds?: IRect): void;
87
+ _drawDeviceBuffer(canvas: SceneCanvas): void;
87
88
  _applyLineCap(shape: Shape): void;
89
+ _opacityRoot: Node | undefined;
90
+ _getOpacity(shape: Node): any;
88
91
  _applyOpacity(shape: Node): void;
89
92
  _applyLineJoin(shape: Shape): void;
90
93
  _applyMiterLimit(shape: Shape): void;
@@ -324,12 +327,11 @@ export declare class SceneContext extends Context {
324
327
  constructor(canvas: Canvas, { willReadFrequently }?: {
325
328
  willReadFrequently?: boolean | undefined;
326
329
  });
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;
330
+ _getFillPattern(shape: Shape): any;
331
+ _getFillStyle(shape: Shape): any;
332
+ _fill(shape: Shape): void;
332
333
  _strokeLinearGradient(shape: any): void;
334
+ _applyStrokeStyle(shape: Shape): void;
333
335
  _stroke(shape: any): void;
334
336
  _applyShadow(shape: any): void;
335
337
  }