konva 10.6.0 → 10.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +12 -8
  2. package/konva.js +617 -231
  3. package/konva.min.js +4 -4
  4. package/lib/Canvas.d.ts +9 -2
  5. package/lib/Canvas.js +83 -5
  6. package/lib/Container.d.ts +3 -2
  7. package/lib/Container.js +27 -12
  8. package/lib/Context.d.ts +5 -2
  9. package/lib/Context.js +22 -1
  10. package/lib/Global.d.ts +2 -1
  11. package/lib/Global.js +8 -3
  12. package/lib/Group.d.ts +7 -1
  13. package/lib/Group.js +61 -0
  14. package/lib/Layer.d.ts +1 -1
  15. package/lib/Layer.js +4 -2
  16. package/lib/Node.d.ts +12 -7
  17. package/lib/Node.js +53 -44
  18. package/lib/Shape.d.ts +9 -15
  19. package/lib/Shape.js +145 -85
  20. package/lib/Stage.d.ts +0 -2
  21. package/lib/Stage.js +2 -13
  22. package/lib/Util.d.ts +1 -0
  23. package/lib/Util.js +25 -0
  24. package/lib/shapes/Arc.js +3 -3
  25. package/lib/shapes/Arrow.d.ts +1 -0
  26. package/lib/shapes/Arrow.js +7 -1
  27. package/lib/shapes/Circle.d.ts +1 -0
  28. package/lib/shapes/Circle.js +4 -1
  29. package/lib/shapes/Ellipse.d.ts +1 -0
  30. package/lib/shapes/Ellipse.js +4 -1
  31. package/lib/shapes/Image.d.ts +2 -1
  32. package/lib/shapes/Image.js +6 -3
  33. package/lib/shapes/Label.js +18 -20
  34. package/lib/shapes/Line.d.ts +1 -0
  35. package/lib/shapes/Line.js +4 -1
  36. package/lib/shapes/Path.js +1 -1
  37. package/lib/shapes/Rect.d.ts +1 -0
  38. package/lib/shapes/Rect.js +4 -1
  39. package/lib/shapes/RegularPolygon.js +1 -1
  40. package/lib/shapes/Ring.d.ts +7 -0
  41. package/lib/shapes/Ring.js +8 -1
  42. package/lib/shapes/Sprite.d.ts +2 -1
  43. package/lib/shapes/Sprite.js +6 -3
  44. package/lib/shapes/Star.d.ts +6 -0
  45. package/lib/shapes/Star.js +5 -1
  46. package/lib/shapes/Text.d.ts +5 -2
  47. package/lib/shapes/Text.js +49 -22
  48. package/lib/shapes/TextPath.d.ts +7 -0
  49. package/lib/shapes/TextPath.js +16 -1
  50. package/lib/shapes/Transformer.d.ts +10 -2
  51. package/lib/shapes/Transformer.js +55 -6
  52. package/lib/shapes/Wedge.js +1 -1
  53. package/package.json +2 -18
package/lib/shapes/Arc.js CHANGED
@@ -49,8 +49,8 @@ export class Arc extends Shape {
49
49
  this.outerRadius(height / 2);
50
50
  }
51
51
  getSelfRect() {
52
- const innerRadius = Math.abs(this.innerRadius());
53
- const outerRadius = Math.abs(this.outerRadius());
52
+ const r1 = Math.abs(this.innerRadius()), r2 = Math.abs(this.outerRadius());
53
+ const innerRadius = Math.min(r1, r2), outerRadius = Math.max(r1, r2);
54
54
  const clockwise = this.clockwise();
55
55
  // canvas draws a full circle for any non-zero full-turn end angle regardless
56
56
  // of direction, and nothing for angle 0. Keep exact turns distinct from
@@ -93,7 +93,7 @@ Arc.prototype._attrsAffectingSize = [
93
93
  'angle',
94
94
  'clockwise',
95
95
  ];
96
- _registerNode(Arc);
96
+ _registerNode(Arc, true);
97
97
  // add getters setters
98
98
  Factory.addGetterSetter(Arc, 'innerRadius', 0, getNumberValidator());
99
99
  /**
@@ -35,6 +35,7 @@ export interface ArrowConfig extends LineConfig {
35
35
  * });
36
36
  */
37
37
  export declare class Arrow extends Line<ArrowConfig> {
38
+ _getStrokePadding(): number;
38
39
  _sceneFunc(ctx: Context): void;
39
40
  _getPointerAngle(atBeginning: boolean): number;
40
41
  __fillStroke(ctx: Context): void;
@@ -28,6 +28,12 @@ import { Path } from "./Path.js";
28
28
  * });
29
29
  */
30
30
  export class Arrow extends Line {
31
+ _getStrokePadding() {
32
+ // Arrowheads have joins even when the shaft is a single straight line.
33
+ return super._getStrokePadding(this.pointerAtBeginning() || this.pointerAtEnding()
34
+ ? this.miterLimit() || 10
35
+ : undefined);
36
+ }
31
37
  _sceneFunc(ctx) {
32
38
  super._sceneFunc(ctx);
33
39
  const points = this.points();
@@ -144,7 +150,7 @@ Arrow.prototype._attrsAffectingSize = [
144
150
  'pointerAtBeginning',
145
151
  'pointerAtEnding',
146
152
  ];
147
- _registerNode(Arrow);
153
+ _registerNode(Arrow, true);
148
154
  /**
149
155
  * get/set pointerLength
150
156
  * @name Konva.Arrow#pointerLength
@@ -24,6 +24,7 @@ export interface CircleConfig extends ShapeConfig {
24
24
  * });
25
25
  */
26
26
  export declare class Circle extends Shape<CircleConfig> {
27
+ _getStrokePadding(): number;
27
28
  _sceneFunc(context: Context): void;
28
29
  getWidth(): number;
29
30
  getHeight(): number;
@@ -21,6 +21,9 @@ import { _registerNode } from "../Global.js";
21
21
  * });
22
22
  */
23
23
  export class Circle extends Shape {
24
+ _getStrokePadding() {
25
+ return super._getStrokePadding(1);
26
+ }
24
27
  _sceneFunc(context) {
25
28
  context.beginPath();
26
29
  context.arc(0, 0, Math.abs(this.attrs.radius || 0), 0, Math.PI * 2, false);
@@ -43,7 +46,7 @@ export class Circle extends Shape {
43
46
  Circle.prototype._centroid = true;
44
47
  Circle.prototype.className = 'Circle';
45
48
  Circle.prototype._attrsAffectingSize = ['radius'];
46
- _registerNode(Circle);
49
+ _registerNode(Circle, true);
47
50
  /**
48
51
  * get/set radius
49
52
  * @name Konva.Circle#radius
@@ -26,6 +26,7 @@ export interface EllipseConfig extends ShapeConfig {
26
26
  * });
27
27
  */
28
28
  export declare class Ellipse extends Shape<EllipseConfig> {
29
+ _getStrokePadding(): number;
29
30
  _sceneFunc(context: Context): void;
30
31
  getWidth(): number;
31
32
  getHeight(): number;
@@ -21,6 +21,9 @@ import { _registerNode } from "../Global.js";
21
21
  * });
22
22
  */
23
23
  export class Ellipse extends Shape {
24
+ _getStrokePadding() {
25
+ return super._getStrokePadding(1);
26
+ }
24
27
  _sceneFunc(context) {
25
28
  const rx = Math.abs(this.radiusX()), ry = Math.abs(this.radiusY());
26
29
  context.beginPath();
@@ -44,7 +47,7 @@ export class Ellipse extends Shape {
44
47
  Ellipse.prototype.className = 'Ellipse';
45
48
  Ellipse.prototype._centroid = true;
46
49
  Ellipse.prototype._attrsAffectingSize = ['radiusX', 'radiusY'];
47
- _registerNode(Ellipse);
50
+ _registerNode(Ellipse, true);
48
51
  // add getters setters
49
52
  Factory.addComponentsGetterSetter(Ellipse, 'radius', ['x', 'y']);
50
53
  /**
@@ -36,7 +36,8 @@ export declare class Image extends Shape<ImageConfig> {
36
36
  _setImageLoad(): void;
37
37
  _removeImageLoad(image: any): void;
38
38
  destroy(): this;
39
- _useBufferCanvas(): boolean;
39
+ _useBufferCanvas(forceFill?: boolean, opacity?: any): boolean;
40
+ _getStrokePadding(): number;
40
41
  _sceneFunc(context: Context): void;
41
42
  _hitFunc(context: Context): void;
42
43
  getWidth(): any;
@@ -57,7 +57,7 @@ export class Image extends Shape {
57
57
  super.destroy();
58
58
  return this;
59
59
  }
60
- _useBufferCanvas() {
60
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
61
61
  if (this.attrs.perfectDrawEnabled === false)
62
62
  return false;
63
63
  const hasCornerRadius = !!this.cornerRadius();
@@ -65,7 +65,10 @@ export class Image extends Shape {
65
65
  if (hasCornerRadius && hasShadow) {
66
66
  return true;
67
67
  }
68
- return super._useBufferCanvas(true);
68
+ return super._useBufferCanvas(true, opacity);
69
+ }
70
+ _getStrokePadding() {
71
+ return super._getStrokePadding(this.strokeScaleEnabled() ? 1 : undefined);
69
72
  }
70
73
  _sceneFunc(context) {
71
74
  const width = this.getWidth();
@@ -143,7 +146,7 @@ export class Image extends Shape {
143
146
  }
144
147
  Image.prototype.className = 'Image';
145
148
  Image.prototype._attrsAffectingSize = ['image'];
146
- _registerNode(Image);
149
+ _registerNode(Image, true);
147
150
  /**
148
151
  * get/set corner radius
149
152
  * @method
@@ -181,31 +181,29 @@ export class Tag extends Shape {
181
181
  context.fillStrokeShape(this);
182
182
  }
183
183
  getSelfRect() {
184
- let x = 0, y = 0, pointerWidth = this.pointerWidth(), pointerHeight = this.pointerHeight(), direction = this.pointerDirection(), width = this.width(), height = this.height();
185
- if (direction === UP) {
186
- y -= pointerHeight;
187
- height += pointerHeight;
184
+ const pointerWidth = this.pointerWidth(), pointerHeight = this.pointerHeight(), direction = this.pointerDirection(), width = this.width(), height = this.height();
185
+ // The pointer base is centred on its side and can be longer than it.
186
+ if (direction === UP || direction === DOWN) {
187
+ return {
188
+ x: Math.min(0, (width - pointerWidth) / 2),
189
+ y: direction === UP ? -pointerHeight : 0,
190
+ width: Math.max(width, pointerWidth),
191
+ height: height + pointerHeight,
192
+ };
188
193
  }
189
- else if (direction === DOWN) {
190
- height += pointerHeight;
194
+ if (direction === LEFT || direction === RIGHT) {
195
+ return {
196
+ x: direction === LEFT ? -pointerWidth : 0,
197
+ y: Math.min(0, (height - pointerHeight) / 2),
198
+ width: width + pointerWidth,
199
+ height: Math.max(height, pointerHeight),
200
+ };
191
201
  }
192
- else if (direction === LEFT) {
193
- x -= pointerWidth;
194
- width += pointerWidth;
195
- }
196
- else if (direction === RIGHT) {
197
- width += pointerWidth;
198
- }
199
- return {
200
- x: x,
201
- y: y,
202
- width: width,
203
- height: height,
204
- };
202
+ return { x: 0, y: 0, width, height };
205
203
  }
206
204
  }
207
205
  Tag.prototype.className = 'Tag';
208
- _registerNode(Tag);
206
+ _registerNode(Tag, true);
209
207
  /**
210
208
  * get/set pointer direction
211
209
  * @name Konva.Tag#pointerDirection
@@ -43,6 +43,7 @@ export declare class Line<Config extends LineConfig = LineConfig> extends Shape<
43
43
  * The handlers take plain numbers, because this runs on every frame.
44
44
  */
45
45
  _eachTensionSegment(onQuadratic: (x0: number, y0: number, cpx: number, cpy: number, x: number, y: number) => void, onCubic: (x0: number, y0: number, cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number) => void): void;
46
+ _getStrokePadding(miterLimit?: number | undefined): number;
46
47
  _sceneFunc(context: Context): void;
47
48
  getTensionPoints(): any;
48
49
  _getTensionPoints(): number[];
@@ -105,6 +105,9 @@ export class Line extends Shape {
105
105
  onQuadratic(x0, y0, tp[len - 2], tp[len - 1], points[length - 2], points[length - 1]);
106
106
  }
107
107
  }
108
+ _getStrokePadding(miterLimit = this.points().length === 4 && !this.closed() ? 1 : undefined) {
109
+ return super._getStrokePadding(miterLimit);
110
+ }
108
111
  _sceneFunc(context) {
109
112
  const points = this.points(), length = points.length, closed = this.closed(), bezier = this.bezier();
110
113
  if (!length) {
@@ -204,7 +207,7 @@ export class Line extends Shape {
204
207
  }
205
208
  Line.prototype.className = 'Line';
206
209
  Line.prototype._attrsAffectingSize = ['points', 'bezier', 'tension', 'closed'];
207
- _registerNode(Line);
210
+ _registerNode(Line, true);
208
211
  // add getters setters
209
212
  Factory.addGetterSetter(Line, 'closed', false);
210
213
  /**
@@ -703,7 +703,7 @@ export class Path extends Shape {
703
703
  }
704
704
  Path.prototype.className = 'Path';
705
705
  Path.prototype._attrsAffectingSize = ['data'];
706
- _registerNode(Path);
706
+ _registerNode(Path, true);
707
707
  /**
708
708
  * get/set SVG path data string. This method
709
709
  * also automatically parses the data string
@@ -24,6 +24,7 @@ export type RectConfig = ShapeConfig & {
24
24
  * });
25
25
  */
26
26
  export declare class Rect extends Shape<RectConfig> {
27
+ _getStrokePadding(): number;
27
28
  _sceneFunc(context: Context): void;
28
29
  cornerRadius: GetSet<number | number[], this>;
29
30
  }
@@ -22,6 +22,9 @@ import { getNumberOrArrayOfNumbersValidator } from "../Validators.js";
22
22
  * });
23
23
  */
24
24
  export class Rect extends Shape {
25
+ _getStrokePadding() {
26
+ return super._getStrokePadding(this.strokeScaleEnabled() ? 1 : undefined);
27
+ }
25
28
  _sceneFunc(context) {
26
29
  const cornerRadius = this.cornerRadius(), width = this.width(), height = this.height();
27
30
  context.beginPath();
@@ -37,7 +40,7 @@ export class Rect extends Shape {
37
40
  }
38
41
  }
39
42
  Rect.prototype.className = 'Rect';
40
- _registerNode(Rect);
43
+ _registerNode(Rect, true);
41
44
  /**
42
45
  * get/set corner radius
43
46
  * @method
@@ -94,7 +94,7 @@ export class RegularPolygon extends Shape {
94
94
  RegularPolygon.prototype.className = 'RegularPolygon';
95
95
  RegularPolygon.prototype._centroid = true;
96
96
  RegularPolygon.prototype._attrsAffectingSize = ['radius', 'sides'];
97
- _registerNode(RegularPolygon);
97
+ _registerNode(RegularPolygon, true);
98
98
  /**
99
99
  * get/set radius
100
100
  * @method
@@ -26,7 +26,14 @@ export interface RingConfig extends ShapeConfig {
26
26
  * });
27
27
  */
28
28
  export declare class Ring extends Shape<RingConfig> {
29
+ _getStrokePadding(): number;
29
30
  _sceneFunc(context: Context): void;
31
+ getSelfRect(): {
32
+ x: number;
33
+ y: number;
34
+ width: number;
35
+ height: number;
36
+ };
30
37
  getWidth(): number;
31
38
  getHeight(): number;
32
39
  setWidth(width: number): void;
@@ -23,6 +23,9 @@ const PIx2 = Math.PI * 2;
23
23
  * });
24
24
  */
25
25
  export class Ring extends Shape {
26
+ _getStrokePadding() {
27
+ return super._getStrokePadding(1);
28
+ }
26
29
  _sceneFunc(context) {
27
30
  const innerRadius = Math.abs(this.innerRadius()), outerRadius = Math.abs(this.outerRadius());
28
31
  context.beginPath();
@@ -32,6 +35,10 @@ export class Ring extends Shape {
32
35
  context.closePath();
33
36
  context.fillStrokeShape(this);
34
37
  }
38
+ getSelfRect() {
39
+ const radius = Math.max(Math.abs(this.innerRadius()), Math.abs(this.outerRadius()));
40
+ return { x: -radius, y: -radius, width: radius * 2, height: radius * 2 };
41
+ }
35
42
  getWidth() {
36
43
  return Math.abs(this.outerRadius()) * 2;
37
44
  }
@@ -48,7 +55,7 @@ export class Ring extends Shape {
48
55
  Ring.prototype.className = 'Ring';
49
56
  Ring.prototype._centroid = true;
50
57
  Ring.prototype._attrsAffectingSize = ['innerRadius', 'outerRadius'];
51
- _registerNode(Ring);
58
+ _registerNode(Ring, true);
52
59
  /**
53
60
  * get/set innerRadius
54
61
  * @method
@@ -70,6 +70,7 @@ export declare class Sprite extends Shape<SpriteConfig> {
70
70
  offsetX: any;
71
71
  offsetY: any;
72
72
  } | undefined;
73
+ _getStrokePadding(): number;
73
74
  _sceneFunc(context: Context): void;
74
75
  _hitFunc(context: Context): void;
75
76
  getSelfRect(): {
@@ -78,7 +79,7 @@ export declare class Sprite extends Shape<SpriteConfig> {
78
79
  width: any;
79
80
  height: any;
80
81
  };
81
- _useBufferCanvas(): boolean;
82
+ _useBufferCanvas(forceFill?: boolean, opacity?: any): boolean;
82
83
  _setInterval(): void;
83
84
  /**
84
85
  * start sprite animation
@@ -95,6 +95,9 @@ export class Sprite extends Shape {
95
95
  offsetY: offset[index * 2 + 1] || 0,
96
96
  };
97
97
  }
98
+ _getStrokePadding() {
99
+ return super._getStrokePadding(this.strokeScaleEnabled() ? 1 : undefined);
100
+ }
98
101
  _sceneFunc(context) {
99
102
  const frame = this._getFrame();
100
103
  if (!frame) {
@@ -130,8 +133,8 @@ export class Sprite extends Shape {
130
133
  }
131
134
  : { x: 0, y: 0, width: 0, height: 0 };
132
135
  }
133
- _useBufferCanvas() {
134
- return super._useBufferCanvas(true);
136
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
137
+ return super._useBufferCanvas(true, opacity);
135
138
  }
136
139
  _setInterval() {
137
140
  const that = this;
@@ -184,7 +187,7 @@ export class Sprite extends Shape {
184
187
  }
185
188
  }
186
189
  Sprite.prototype.className = 'Sprite';
187
- _registerNode(Sprite);
190
+ _registerNode(Sprite, true);
188
191
  // add getters setters
189
192
  Factory.addGetterSetter(Sprite, 'animation');
190
193
  /**
@@ -32,6 +32,12 @@ export interface StarConfig extends ShapeConfig {
32
32
  */
33
33
  export declare class Star extends Shape<StarConfig> {
34
34
  _sceneFunc(context: Context): void;
35
+ getSelfRect(): {
36
+ x: number;
37
+ y: number;
38
+ width: number;
39
+ height: number;
40
+ };
35
41
  getWidth(): number;
36
42
  getHeight(): number;
37
43
  setWidth(width: number): void;
@@ -39,6 +39,10 @@ export class Star extends Shape {
39
39
  context.closePath();
40
40
  context.fillStrokeShape(this);
41
41
  }
42
+ getSelfRect() {
43
+ const radius = Math.max(Math.abs(this.innerRadius()), Math.abs(this.outerRadius()));
44
+ return { x: -radius, y: -radius, width: radius * 2, height: radius * 2 };
45
+ }
42
46
  getWidth() {
43
47
  return this.outerRadius() * 2;
44
48
  }
@@ -55,7 +59,7 @@ export class Star extends Shape {
55
59
  Star.prototype.className = 'Star';
56
60
  Star.prototype._centroid = true;
57
61
  Star.prototype._attrsAffectingSize = ['innerRadius', 'outerRadius'];
58
- _registerNode(Star);
62
+ _registerNode(Star, true);
59
63
  /**
60
64
  * get/set number of points
61
65
  * @name Konva.Star#numPoints
@@ -82,8 +82,10 @@ export declare class Text extends Shape<TextConfig> {
82
82
  _baselineShift: number;
83
83
  constructor(config?: TextConfig);
84
84
  _sceneFunc(context: SceneContext): void;
85
+ _getTextTop(): number;
85
86
  _getUnderlineOffset(): number;
86
- getSelfRect(): {
87
+ getSelfRect(): import("../types.ts").IRect;
88
+ _getSelfRectForDrawing(): {
87
89
  x: number;
88
90
  y: number;
89
91
  width: number;
@@ -138,7 +140,8 @@ export declare class Text extends Shape<TextConfig> {
138
140
  _shouldHandleEllipsis(currentHeightPx: number): boolean;
139
141
  _tryToAddEllipsisToLastLine(): void;
140
142
  getStrokeScaleEnabled(): boolean;
141
- _useBufferCanvas(): boolean;
143
+ _getStrokePadding(): number;
144
+ _useBufferCanvas(forceFill?: boolean, opacity?: any): boolean;
142
145
  direction: GetSet<string, this>;
143
146
  fontFamily: GetSet<string, this>;
144
147
  fontSize: GetSet<number, this>;
@@ -178,7 +178,7 @@ export class Text extends Shape {
178
178
  if (!this.text()) {
179
179
  return;
180
180
  }
181
- let padding = this.padding(), fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, verticalAlign = this.verticalAlign(), direction = this.direction(), alignY = 0, align = this.align(), totalWidth = this.getWidth(), letterSpacing = this.letterSpacing(), charRenderFunc = this.charRenderFunc(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n;
181
+ let padding = this.padding(), fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, direction = this.direction(), align = this.align(), totalWidth = this.getWidth(), letterSpacing = this.letterSpacing(), charRenderFunc = this.charRenderFunc(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n;
182
182
  let translateY = lineHeightPx / 2;
183
183
  let baseline = MIDDLE;
184
184
  if (!Konva.legacyTextRendering) {
@@ -199,14 +199,7 @@ export class Text extends Shape {
199
199
  context.setAttr('font', this._getContextFont());
200
200
  context.setAttr('textBaseline', baseline);
201
201
  context.setAttr('textAlign', LEFT);
202
- // handle vertical alignment
203
- if (verticalAlign === MIDDLE) {
204
- alignY = (this.getHeight() - textArrLen * lineHeightPx - padding * 2) / 2;
205
- }
206
- else if (verticalAlign === BOTTOM) {
207
- alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
208
- }
209
- context.translate(padding, alignY + padding);
202
+ context.translate(padding, this._getTextTop());
210
203
  // Start the callback with the same fill that normal drawing would use.
211
204
  // Assigning that color again then needs no override, including black.
212
205
  if (charRenderFunc) {
@@ -335,6 +328,16 @@ export class Text extends Shape {
335
328
  }
336
329
  }
337
330
  }
331
+ _getTextTop() {
332
+ const padding = this.padding(), free = this.getHeight() -
333
+ this.textArr.length * this.lineHeight() * this.fontSize() -
334
+ padding * 2, verticalAlign = this.verticalAlign();
335
+ if (verticalAlign === MIDDLE)
336
+ return padding + free / 2;
337
+ if (verticalAlign === BOTTOM)
338
+ return padding + free;
339
+ return padding;
340
+ }
338
341
  _getUnderlineOffset() {
339
342
  var _a;
340
343
  return ((_a = this.underlineOffset()) !== null && _a !== void 0 ? _a : Math.round(this.fontSize() / (!Konva.legacyTextRendering ? 4 : 2)));
@@ -346,21 +349,41 @@ export class Text extends Shape {
346
349
  return rect;
347
350
  }
348
351
  // the underline of the last line is drawn below the text block
349
- const fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, padding = this.padding(), verticalAlign = this.verticalAlign(), blockHeight = lines * lineHeightPx + padding * 2;
350
- let bottom = lines * lineHeightPx - lineHeightPx / 2 + padding;
352
+ const fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize;
353
+ let bottom = this._getTextTop() + lines * lineHeightPx - lineHeightPx / 2;
351
354
  if (!Konva.legacyTextRendering) {
352
355
  bottom += this._baselineShift;
353
356
  }
354
- if (verticalAlign === MIDDLE) {
355
- bottom += (rect.height - blockHeight) / 2;
356
- }
357
- else if (verticalAlign === BOTTOM) {
358
- bottom += rect.height - blockHeight;
359
- }
360
357
  bottom += this._getUnderlineOffset() + getDecorationLineWidth(fontSize) / 2;
361
- rect.height = Math.max(rect.height, bottom);
358
+ rect.height = Math.max(rect.y + rect.height, bottom) - rect.y;
362
359
  return rect;
363
360
  }
361
+ _getSelfRectForDrawing() {
362
+ // Lines can be wider than the box, negative spacing moves the pen back,
363
+ // and glyph ink passes the advance box (italics, accents): up to 0.6em
364
+ // in common fonts, so pad by 1em.
365
+ const rect = this.getSelfRect(), fontSize = this.fontSize(), padding = this.padding(), lineHeightPx = this.lineHeight() * fontSize, available = this.getWidth() - padding * 2, align = this.align(), blockHeight = this.textArr.length * lineHeightPx, top = this._getTextTop();
366
+ let left = rect.x, right = rect.x + rect.width;
367
+ for (const { text, width } of this.textArr) {
368
+ const x = padding +
369
+ (align === RIGHT
370
+ ? available - width
371
+ : align === CENTER
372
+ ? (available - width) / 2
373
+ : 0);
374
+ const back = Math.min(0, this.letterSpacing()) * text.length;
375
+ left = Math.min(left, x + back);
376
+ right = Math.max(right, x + width - back);
377
+ }
378
+ const underline = this.textDecoration().includes('underline');
379
+ const y = Math.min(rect.y, top + (underline ? Math.min(0, this._getUnderlineOffset()) : 0));
380
+ return {
381
+ x: left - fontSize,
382
+ y: y - fontSize,
383
+ width: right - left + fontSize * 2,
384
+ height: Math.max(rect.y + rect.height, top + blockHeight) - y + fontSize * 2,
385
+ };
386
+ }
364
387
  _hitFunc(context) {
365
388
  const width = this.getWidth(), height = this.getHeight();
366
389
  context.beginPath();
@@ -641,7 +664,11 @@ export class Text extends Shape {
641
664
  getStrokeScaleEnabled() {
642
665
  return true;
643
666
  }
644
- _useBufferCanvas() {
667
+ _getStrokePadding() {
668
+ // _strokeFunc uses a miter limit of two for glyph outlines.
669
+ return super._getStrokePadding(2);
670
+ }
671
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
645
672
  if (this.attrs.perfectDrawEnabled === false)
646
673
  return false;
647
674
  const hasLine = this.textDecoration().indexOf('underline') !== -1 ||
@@ -650,10 +677,10 @@ export class Text extends Shape {
650
677
  if (hasLine && hasShadow) {
651
678
  return true;
652
679
  }
653
- if (hasShadow && this.getAbsoluteOpacity() !== 1 && hasShadowOpacityBug()) {
680
+ if (hasShadow && opacity !== 1 && hasShadowOpacityBug()) {
654
681
  return true;
655
682
  }
656
- return super._useBufferCanvas();
683
+ return super._useBufferCanvas(forceFill, opacity);
657
684
  }
658
685
  }
659
686
  Text.prototype._fillFunc = _fillFunc;
@@ -662,7 +689,7 @@ Text.prototype.className = TEXT_UPPER;
662
689
  // the decoration attrs are not in ATTR_CHANGE_LIST: they change the bounds
663
690
  // through getSelfRect, but need no relayout
664
691
  Text.prototype._attrsAffectingSize = ATTR_CHANGE_LIST.filter((attr) => attr !== 'width' && attr !== 'height').concat(['textDecoration', 'underlineOffset']);
665
- _registerNode(Text);
692
+ _registerNode(Text, true);
666
693
  // update text data for certain attr changes
667
694
  Text.prototype.on(ATTR_CHANGE_LIST.map((attr) => attr + CHANGE_KONVA).join(' '), function () {
668
695
  this._setTextData();
@@ -106,6 +106,13 @@ export declare class TextPath extends Shape<TextPathConfig> {
106
106
  width: number;
107
107
  height: number;
108
108
  };
109
+ getStrokeScaleEnabled(): boolean;
110
+ _getSelfRectForDrawing(): {
111
+ x: number;
112
+ y: number;
113
+ width: number;
114
+ height: number;
115
+ };
109
116
  fontFamily: GetSet<string, this>;
110
117
  fontSize: GetSet<number, this>;
111
118
  fontStyle: GetSet<string, this>;
@@ -313,6 +313,21 @@ export class TextPath extends Shape {
313
313
  height: rect.height + fontSize,
314
314
  };
315
315
  }
316
+ // Like Text: a glyph stroke is drawn with the text, so it always scales.
317
+ getStrokeScaleEnabled() {
318
+ return true;
319
+ }
320
+ _getSelfRectForDrawing() {
321
+ // getSelfRect() pads glyph anchors by half the font size; ascenders,
322
+ // accents and other baselines reach further from the path.
323
+ const rect = this.getSelfRect(), pad = this.fontSize();
324
+ return {
325
+ x: rect.x - pad,
326
+ y: rect.y - pad,
327
+ width: rect.width + pad * 2,
328
+ height: rect.height + pad * 2,
329
+ };
330
+ }
316
331
  }
317
332
  TextPath.prototype._fillFunc = _fillFunc;
318
333
  TextPath.prototype._strokeFunc = _strokeFunc;
@@ -331,7 +346,7 @@ TextPath.prototype._attrsAffectingSize = [
331
346
  'fontVariant',
332
347
  'direction',
333
348
  ];
334
- _registerNode(TextPath);
349
+ _registerNode(TextPath, true);
335
350
  TextPath.prototype.on('dataChange.konva', function () {
336
351
  this._readDataAttribute();
337
352
  this._setTextData();
@@ -3,8 +3,9 @@ import { Node } from '../Node.ts';
3
3
  import { Shape } from '../Shape.ts';
4
4
  import { Rect } from './Rect.ts';
5
5
  import { Group } from '../Group.ts';
6
- import type { ContainerConfig } from '../Container.ts';
6
+ import { type ContainerConfig } from '../Container.ts';
7
7
  import type { GetSet, IRect, Vector2d } from '../types.ts';
8
+ import type { HitCanvas, SceneCanvas } from '../Canvas.ts';
8
9
  export interface Box extends IRect {
9
10
  rotation: number;
10
11
  }
@@ -110,6 +111,7 @@ export declare class Transformer extends Group {
110
111
  _elementsCreated: boolean;
111
112
  _updateScheduled: boolean;
112
113
  _lastNodeRect?: Readonly<Box>;
114
+ _subtreeChanged: boolean;
113
115
  static isTransforming: () => boolean;
114
116
  static _isLayerTransforming(layer: Node): boolean;
115
117
  constructor(config?: TransformerConfig);
@@ -160,6 +162,10 @@ export declare class Transformer extends Group {
160
162
  * });
161
163
  */
162
164
  _resetTransformCache(changedNode?: Node): void;
165
+ _onSubtreeChange(node: Node): void;
166
+ _syncSubtreeChange(): void;
167
+ drawScene(can?: SceneCanvas, top?: Node): this;
168
+ drawHit(can?: HitCanvas, top?: Node): this;
163
169
  _getNodeRect(): any;
164
170
  __getNodeRect(): {
165
171
  x: number;
@@ -186,7 +192,9 @@ export declare class Transformer extends Group {
186
192
  _scheduleUpdate(): void;
187
193
  /**
188
194
  * force update of Konva.Transformer.
189
- * Use it when you updated attached Konva.Group and now you need to reset transformer size
195
+ * The transformer follows attribute changes of attached nodes and, for an attached group, any change of its descendants.
196
+ * Use it after a change that sets no attribute, for example a `Line` points array mutated in place
197
+ * or a custom `getSelfRect()` that depends on outside state.
190
198
  * @method
191
199
  * @name Konva.Transformer#forceUpdate
192
200
  */