konva 10.9.0 → 10.10.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 (72) hide show
  1. package/README.md +22 -10
  2. package/konva.js +18996 -18838
  3. package/konva.min.js +50 -50
  4. package/lib/Animation.js +3 -3
  5. package/lib/Canvas.js +11 -3
  6. package/lib/Container.js +3 -3
  7. package/lib/Context.d.ts +2 -2
  8. package/lib/Context.js +17 -20
  9. package/lib/Core.js +2 -2
  10. package/lib/DragAndDrop.js +1 -1
  11. package/lib/Factory.js +2 -2
  12. package/lib/FastLayer.js +3 -3
  13. package/lib/Global.d.ts +0 -1
  14. package/lib/Global.js +13 -7
  15. package/lib/Group.js +5 -5
  16. package/lib/Layer.d.ts +1 -1
  17. package/lib/Layer.js +8 -8
  18. package/lib/Node.d.ts +2 -0
  19. package/lib/Node.js +76 -76
  20. package/lib/PointerEvents.js +1 -1
  21. package/lib/Shape.d.ts +5 -1
  22. package/lib/Shape.js +37 -43
  23. package/lib/Stage.js +8 -8
  24. package/lib/Tween.js +4 -4
  25. package/lib/Util.d.ts +67 -3
  26. package/lib/Util.js +124 -41
  27. package/lib/Validators.js +2 -2
  28. package/lib/_CoreInternals.d.ts +15 -11
  29. package/lib/_CoreInternals.js +16 -14
  30. package/lib/_FullInternals.d.ts +14 -10
  31. package/lib/_FullInternals.js +38 -38
  32. package/lib/canvas-backend.js +6 -15
  33. package/lib/filters/Blur.js +27 -3
  34. package/lib/filters/Brighten.js +3 -3
  35. package/lib/filters/Brightness.js +9 -3
  36. package/lib/filters/Contrast.js +9 -5
  37. package/lib/filters/Emboss.js +17 -9
  38. package/lib/filters/Enhance.js +3 -3
  39. package/lib/filters/HSL.js +3 -3
  40. package/lib/filters/HSV.js +3 -3
  41. package/lib/filters/Kaleidoscope.js +3 -3
  42. package/lib/filters/Mask.js +3 -3
  43. package/lib/filters/Noise.js +3 -3
  44. package/lib/filters/Pixelate.js +4 -4
  45. package/lib/filters/Posterize.js +3 -3
  46. package/lib/filters/RGB.js +3 -3
  47. package/lib/filters/RGBA.js +3 -3
  48. package/lib/filters/Threshold.js +3 -3
  49. package/lib/index.js +1 -1
  50. package/lib/napi-backend.d.ts +2 -0
  51. package/lib/napi-backend.js +15 -0
  52. package/lib/shapes/Arc.js +5 -5
  53. package/lib/shapes/Arrow.js +5 -5
  54. package/lib/shapes/Circle.js +4 -4
  55. package/lib/shapes/Ellipse.js +4 -4
  56. package/lib/shapes/Image.js +5 -5
  57. package/lib/shapes/Label.js +7 -7
  58. package/lib/shapes/Line.js +6 -6
  59. package/lib/shapes/Path.js +5 -5
  60. package/lib/shapes/Rect.js +5 -5
  61. package/lib/shapes/RegularPolygon.js +5 -5
  62. package/lib/shapes/Ring.js +4 -4
  63. package/lib/shapes/Sprite.js +5 -5
  64. package/lib/shapes/Star.js +4 -4
  65. package/lib/shapes/Text.d.ts +0 -1
  66. package/lib/shapes/Text.js +36 -34
  67. package/lib/shapes/TextPath.d.ts +3 -2
  68. package/lib/shapes/TextPath.js +71 -46
  69. package/lib/shapes/Transformer.js +12 -11
  70. package/lib/shapes/Wedge.js +5 -5
  71. package/lib/skia-backend.js +17 -25
  72. package/package.json +31 -34
@@ -1,9 +1,9 @@
1
- import { getCubicExtremaPoints, getQuadraticExtremaPoints, } from "../BezierFunctions.js";
2
- import { Factory } from "../Factory.js";
3
- import { _registerNode } from "../Global.js";
4
- import { Shape } from "../Shape.js";
5
- import { Util } from "../Util.js";
6
- import { getNumberArrayValidator, getNumberValidator } from "../Validators.js";
1
+ import { getCubicExtremaPoints, getQuadraticExtremaPoints, } from '../BezierFunctions.js';
2
+ import { Factory } from '../Factory.js';
3
+ import { _registerNode } from '../Global.js';
4
+ import { Shape } from '../Shape.js';
5
+ import { Util } from '../Util.js';
6
+ import { getNumberArrayValidator, getNumberValidator } from '../Validators.js';
7
7
  function getControlPoints(x0, y0, x1, y1, x2, y2, t) {
8
8
  const d01 = Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2)), d12 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)), dSum = d01 + d12;
9
9
  // all three points are the same, so the control points collapse into that
@@ -1,8 +1,8 @@
1
- import { Factory } from "../Factory.js";
2
- import { _registerNode } from "../Global.js";
3
- import { Shape } from "../Shape.js";
4
- import { Util } from "../Util.js";
5
- import { getCubicArcLength, getCubicExtremaPoints, getQuadraticArcLength, getQuadraticExtremaPoints, t2length, } from "../BezierFunctions.js";
1
+ import { Factory } from '../Factory.js';
2
+ import { _registerNode } from '../Global.js';
3
+ import { Shape } from '../Shape.js';
4
+ import { Util } from '../Util.js';
5
+ import { getCubicArcLength, getCubicExtremaPoints, getQuadraticArcLength, getQuadraticExtremaPoints, t2length, } from '../BezierFunctions.js';
6
6
  // how many numbers each path command takes
7
7
  const PARAM_COUNT = {
8
8
  m: 2,
@@ -1,8 +1,8 @@
1
- import { Factory } from "../Factory.js";
2
- import { Shape } from "../Shape.js";
3
- import { _registerNode } from "../Global.js";
4
- import { Util } from "../Util.js";
5
- import { getNumberOrArrayOfNumbersValidator } from "../Validators.js";
1
+ import { Factory } from '../Factory.js';
2
+ import { Shape } from '../Shape.js';
3
+ import { _registerNode } from '../Global.js';
4
+ import { Util } from '../Util.js';
5
+ import { getNumberOrArrayOfNumbersValidator } from '../Validators.js';
6
6
  /**
7
7
  * Rect constructor
8
8
  * @constructor
@@ -1,8 +1,8 @@
1
- import { Factory } from "../Factory.js";
2
- import { Shape } from "../Shape.js";
3
- import { getNumberOrArrayOfNumbersValidator, getNumberValidator, } from "../Validators.js";
4
- import { _registerNode } from "../Global.js";
5
- import { Util } from "../Util.js";
1
+ import { Factory } from '../Factory.js';
2
+ import { Shape } from '../Shape.js';
3
+ import { getNumberOrArrayOfNumbersValidator, getNumberValidator, } from '../Validators.js';
4
+ import { _registerNode } from '../Global.js';
5
+ import { Util } from '../Util.js';
6
6
  /**
7
7
  * RegularPolygon constructor. Examples include triangles, squares, pentagons, hexagons, etc.
8
8
  * @constructor
@@ -1,7 +1,7 @@
1
- import { Factory } from "../Factory.js";
2
- import { Shape } from "../Shape.js";
3
- import { getNumberValidator } from "../Validators.js";
4
- import { _registerNode } from "../Global.js";
1
+ import { Factory } from '../Factory.js';
2
+ import { Shape } from '../Shape.js';
3
+ import { getNumberValidator } from '../Validators.js';
4
+ import { _registerNode } from '../Global.js';
5
5
  const PIx2 = Math.PI * 2;
6
6
  /**
7
7
  * Ring constructor
@@ -1,8 +1,8 @@
1
- import { Factory } from "../Factory.js";
2
- import { Shape } from "../Shape.js";
3
- import { Animation } from "../Animation.js";
4
- import { getNumberValidator } from "../Validators.js";
5
- import { _registerNode } from "../Global.js";
1
+ import { Factory } from '../Factory.js';
2
+ import { Shape } from '../Shape.js';
3
+ import { Animation } from '../Animation.js';
4
+ import { getNumberValidator } from '../Validators.js';
5
+ import { _registerNode } from '../Global.js';
6
6
  /**
7
7
  * Sprite constructor
8
8
  * @constructor
@@ -1,7 +1,7 @@
1
- import { Factory } from "../Factory.js";
2
- import { Shape } from "../Shape.js";
3
- import { getNumberValidator } from "../Validators.js";
4
- import { _registerNode } from "../Global.js";
1
+ import { Factory } from '../Factory.js';
2
+ import { Shape } from '../Shape.js';
3
+ import { getNumberValidator } from '../Validators.js';
4
+ import { _registerNode } from '../Global.js';
5
5
  /**
6
6
  * Star constructor
7
7
  * @constructor
@@ -33,7 +33,6 @@ export interface TextConfig extends ShapeConfig {
33
33
  ellipsis?: boolean;
34
34
  charRenderFunc?: null | ((props: CharRenderProps) => void);
35
35
  }
36
- export declare function getDummyContext(): CanvasRenderingContext2D;
37
36
  /**
38
37
  * Text constructor
39
38
  * @constructor
@@ -1,9 +1,9 @@
1
- import { Util } from "../Util.js";
2
- import { Factory } from "../Factory.js";
3
- import { Shape } from "../Shape.js";
4
- import { Konva } from "../Global.js";
5
- import { getNumberValidator, getStringValidator, getNumberOrAutoValidator, getBooleanValidator, } from "../Validators.js";
6
- import { _registerNode } from "../Global.js";
1
+ import { Util, _perCanvasBackend, getDummyContext } from '../Util.js';
2
+ import { Factory } from '../Factory.js';
3
+ import { Shape } from '../Shape.js';
4
+ import { Konva } from '../Global.js';
5
+ import { getNumberValidator, getStringValidator, getNumberOrAutoValidator, getBooleanValidator, } from '../Validators.js';
6
+ import { _registerNode } from '../Global.js';
7
7
  // grapheme segmentation: one entry per user-perceived character, so flags,
8
8
  // ZWJ emoji sequences and combining marks stay together
9
9
  const segmenter = typeof Intl !== 'undefined' && Intl.Segmenter
@@ -25,7 +25,13 @@ export function stringToArray(string) {
25
25
  // constants
26
26
  const AUTO = 'auto',
27
27
  //CANVAS = 'canvas',
28
- CENTER = 'center', INHERIT = 'inherit', JUSTIFY = 'justify', CHANGE_KONVA = 'Change.konva', CONTEXT_2D = '2d', DASH = '-', LEFT = 'left', TEXT = 'text', TEXT_UPPER = 'Text', TOP = 'top', BOTTOM = 'bottom', MIDDLE = 'middle', NORMAL = 'normal', PX_SPACE = 'px ', SPACE = ' ', RIGHT = 'right', RTL = 'rtl', WORD = 'word', CHAR = 'char', NONE = 'none', ELLIPSIS = '…', ATTR_CHANGE_LIST = [
28
+ CENTER = 'center', INHERIT = 'inherit', JUSTIFY = 'justify', CHANGE_KONVA = 'Change.konva', CONTEXT_2D = '2d', DASH = '-', LEFT = 'left', TEXT = 'text', TEXT_UPPER = 'Text', TOP = 'top', BOTTOM = 'bottom', MIDDLE = 'middle', NORMAL = 'normal', PX_SPACE = 'px ', SPACE = ' ', RIGHT = 'right', RTL = 'rtl', WORD = 'word', CHAR = 'char', NONE = 'none', ELLIPSIS = '…',
29
+ // Chinese and Japanese have no spaces between words, so a line can break next
30
+ // to any of their characters. Korean does use spaces, so it is left out
31
+ CJK = /[\p{Script=Han}\u3000-\u30FF\uFF00-\uFF9F]/u,
32
+ // except where punctuation, or a mark like 々 that repeats the character
33
+ // before it, has to stay with the text it belongs to
34
+ NO_LINE_START = /[\p{Pe}\p{Pf}\p{Pd}、。,.!?:;…・,.!?:;々〻ゝゞヽヾ〃。、・%]/u, NO_LINE_END = /[\p{Ps}\p{Pi}]/u, ATTR_CHANGE_LIST = [
29
35
  'direction',
30
36
  'fontFamily',
31
37
  'fontSize',
@@ -46,12 +52,8 @@ CENTER = 'center', INHERIT = 'inherit', JUSTIFY = 'justify', CHANGE_KONVA = 'Cha
46
52
  // This feature check detects that bug so we can work around it with a buffer canvas.
47
53
  // Not covered by automated tests (our CI doesn't run Safari).
48
54
  // Once Safari fixes the issue this check can be removed.
49
- let _shadowOpacityBuggy = null;
50
- function hasShadowOpacityBug() {
51
- if (_shadowOpacityBuggy !== null) {
52
- return _shadowOpacityBuggy;
53
- }
54
- _shadowOpacityBuggy = false;
55
+ const hasShadowOpacityBug = _perCanvasBackend(() => {
56
+ let buggy = false;
55
57
  let c;
56
58
  try {
57
59
  c = Util.createCanvasElement();
@@ -70,7 +72,7 @@ function hasShadowOpacityBug() {
70
72
  const data = ctx.getImageData(0, 0, 10, 10).data;
71
73
  for (let i = 3; i < data.length; i += 4) {
72
74
  if (data[i] > 0) {
73
- _shadowOpacityBuggy = true;
75
+ buggy = true;
74
76
  break;
75
77
  }
76
78
  }
@@ -84,8 +86,8 @@ function hasShadowOpacityBug() {
84
86
  Util.releaseCanvas(c);
85
87
  }
86
88
  }
87
- return _shadowOpacityBuggy;
88
- }
89
+ return buggy;
90
+ });
89
91
  function normalizeFontFamily(fontFamily) {
90
92
  return fontFamily
91
93
  .split(',')
@@ -100,14 +102,6 @@ function normalizeFontFamily(fontFamily) {
100
102
  })
101
103
  .join(', ');
102
104
  }
103
- let dummyContext;
104
- export function getDummyContext() {
105
- if (dummyContext) {
106
- return dummyContext;
107
- }
108
- dummyContext = Util.createCanvasElement().getContext(CONTEXT_2D);
109
- return dummyContext;
110
- }
111
105
  function _fillFunc(context) {
112
106
  // re-apply a per-character style set by charRenderFunc (see _sceneFunc)
113
107
  if (this._partialFillStyle) {
@@ -239,8 +233,8 @@ export class Text extends Shape {
239
233
  const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
240
234
  context.lineTo(x + Math.round(lineWidth), y);
241
235
  context.lineWidth = getDecorationLineWidth(fontSize);
242
- context.strokeStyle = context._getFillStyle(this);
243
- context.stroke();
236
+ if (context._applyDecorationStyle(this))
237
+ context.stroke();
244
238
  context.restore();
245
239
  }
246
240
  // store the starting x position for line-through which is drawn after text
@@ -318,8 +312,8 @@ export class Text extends Shape {
318
312
  const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
319
313
  context.lineTo(x + Math.round(lineWidth), translateY + lineTranslateY + yOffset);
320
314
  context.lineWidth = getDecorationLineWidth(fontSize);
321
- context.strokeStyle = context._getFillStyle(this);
322
- context.stroke();
315
+ if (context._applyDecorationStyle(this))
316
+ context.stroke();
323
317
  context.restore();
324
318
  }
325
319
  context.restore();
@@ -488,7 +482,7 @@ export class Text extends Shape {
488
482
  return getDummyContext().measureText(text).width + spacing;
489
483
  }
490
484
  _setTextData() {
491
- let lines = this.text().split('\n'), fontSize = +this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, width = this.attrs.width, height = this.attrs.height, fixedWidth = width !== AUTO && width !== undefined, fixedHeight = height !== AUTO && height !== undefined, padding = this.padding(), maxWidth = width - padding * 2, maxHeightPx = height - padding * 2, currentHeightPx = 0, wrap = this.wrap(),
485
+ let lines = this.text().split(/\r?\n/), fontSize = +this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, width = this.attrs.width, height = this.attrs.height, fixedWidth = width !== AUTO && width !== undefined, fixedHeight = height !== AUTO && height !== undefined, padding = this.padding(), maxWidth = width - padding * 2, maxHeightPx = height - padding * 2, currentHeightPx = 0, wrap = this.wrap(),
492
486
  // align = this.align(),
493
487
  shouldWrap = wrap !== NONE, wrapAtWord = wrap !== CHAR && shouldWrap, shouldAddEllipsis = this.ellipsis();
494
488
  // measured here so getSelfRect, which runs per drag frame, needs no
@@ -523,7 +517,14 @@ export class Text extends Shape {
523
517
  const length = graphemes.length;
524
518
  let start = 0;
525
519
  const text = (end) => graphemes.slice(start, end).join('');
526
- const isBreak = (char) => char === SPACE || char === DASH;
520
+ // a line can end before or at a space, after a dash (never before
521
+ // one), or next to a Chinese or Japanese character
522
+ const isBreak = (i) => graphemes[i] === SPACE ||
523
+ graphemes[i + 1] === SPACE ||
524
+ (graphemes[i] === DASH && graphemes[i + 1] !== DASH) ||
525
+ ((CJK.test(graphemes[i]) || CJK.test(graphemes[i + 1])) &&
526
+ !NO_LINE_START.test(graphemes[i + 1]) &&
527
+ !NO_LINE_END.test(graphemes[i]));
527
528
  // graphemes per line, estimated from the average grapheme width
528
529
  const perLine = Math.max(1, Math.ceil((length * maxWidth) / lineWidth));
529
530
  while (start < length) {
@@ -580,10 +581,10 @@ export class Text extends Shape {
580
581
  }
581
582
  break;
582
583
  }
583
- if (wrapAtWord && !isBreak(graphemes[low])) {
584
- // wrap at the last space or dash of the line instead
584
+ if (wrapAtWord) {
585
+ // end the line at its last break opportunity
585
586
  let wrapIndex = low - 1;
586
- while (wrapIndex >= start && !isBreak(graphemes[wrapIndex])) {
587
+ while (wrapIndex >= start && !isBreak(wrapIndex)) {
587
588
  wrapIndex--;
588
589
  }
589
590
  if (wrapIndex >= start) {
@@ -864,7 +865,8 @@ Factory.addGetterSetter(Text, 'lineHeight', 1, getNumberValidator());
864
865
  /**
865
866
  * get/set wrap. Can be "word", "char", or "none". Default is "word".
866
867
  * In "word" wrapping any word still can be wrapped if it can't be placed in the required width
867
- * without breaks.
868
+ * without breaks. Chinese and Japanese text can wrap between characters, but not
869
+ * before closing punctuation or after opening punctuation.
868
870
  * @name Konva.Text#wrap
869
871
  * @method
870
872
  * @param {String} wrap
@@ -1,4 +1,4 @@
1
- import type { Context } from '../Context.ts';
1
+ import type { Context, SceneContext } from '../Context.ts';
2
2
  import type { ShapeConfig } from '../Shape.ts';
3
3
  import { Shape } from '../Shape.ts';
4
4
  import { Text } from './Text.ts';
@@ -84,7 +84,7 @@ export declare class TextPath extends Shape<TextPathConfig> {
84
84
  y: number;
85
85
  } | null;
86
86
  _readDataAttribute(): void;
87
- _sceneFunc(context: Context): void;
87
+ _sceneFunc(context: SceneContext): void;
88
88
  _hitFunc(context: Context): void;
89
89
  /**
90
90
  * get text width in pixels
@@ -106,6 +106,7 @@ export declare class TextPath extends Shape<TextPathConfig> {
106
106
  width: number;
107
107
  height: number;
108
108
  };
109
+ _getUnderlineOffset(): number;
109
110
  getStrokeScaleEnabled(): boolean;
110
111
  _getSelfRectForDrawing(): {
111
112
  x: number;
@@ -1,10 +1,10 @@
1
- import { Util } from "../Util.js";
2
- import { Factory } from "../Factory.js";
3
- import { Shape } from "../Shape.js";
4
- import { Path } from "./Path.js";
5
- import { Text, getDecorationLineWidth, getDummyContext, stringToArray, } from "./Text.js";
6
- import { getNumberValidator } from "../Validators.js";
7
- import { _registerNode } from "../Global.js";
1
+ import { Transform, Util, getDummyContext } from '../Util.js';
2
+ import { Factory } from '../Factory.js';
3
+ import { Shape } from '../Shape.js';
4
+ import { Path } from './Path.js';
5
+ import { Text, getDecorationLineWidth, stringToArray } from './Text.js';
6
+ import { getNumberValidator } from '../Validators.js';
7
+ import { _registerNode } from '../Global.js';
8
8
  const EMPTY_STRING = '', NORMAL = 'normal';
9
9
  function _fillFunc(context) {
10
10
  context.fillText(this.partialText, 0, 0);
@@ -88,40 +88,49 @@ export class TextPath extends Shape {
88
88
  context.setAttr('textAlign', 'left');
89
89
  context.save();
90
90
  const textDecoration = this.textDecoration();
91
- const fill = this.fill();
92
91
  const fontSize = this.fontSize();
92
+ const underlineOffset = this._getUnderlineOffset();
93
93
  const glyphInfo = this.glyphInfo;
94
94
  const hasUnderline = textDecoration.indexOf('underline') !== -1;
95
95
  const hasLineThrough = textDecoration.indexOf('line-through') !== -1;
96
96
  if (hasUnderline) {
97
97
  context.beginPath();
98
98
  }
99
- for (let i = 0; i < glyphInfo.length; i++) {
100
- context.save();
101
- const p0 = glyphInfo[i].p0;
102
- context.translate(p0.x, p0.y);
103
- context.rotate(glyphInfo[i].rotation);
104
- this.partialText = glyphInfo[i].text;
105
- context.fillStrokeShape(this);
106
- if (hasUnderline) {
107
- if (i === 0) {
108
- context.moveTo(0, fontSize / 2 + 1);
99
+ try {
100
+ for (let i = 0; i < glyphInfo.length; i++) {
101
+ context.save();
102
+ const p0 = glyphInfo[i].p0;
103
+ const rotation = glyphInfo[i].rotation;
104
+ context.translate(p0.x, p0.y);
105
+ context.rotate(rotation);
106
+ this.partialText = glyphInfo[i].text;
107
+ // keep gradients and patterns in the shape's space, not the glyph's
108
+ this._styleTransform = new Transform()
109
+ .rotate(-rotation)
110
+ .translate(-p0.x, -p0.y);
111
+ context.fillStrokeShape(this);
112
+ if (hasUnderline) {
113
+ if (i === 0) {
114
+ context.moveTo(0, underlineOffset);
115
+ }
116
+ context.lineTo(glyphInfo[i].width, underlineOffset);
109
117
  }
110
- context.lineTo(glyphInfo[i].width, fontSize / 2 + 1);
118
+ context.restore();
119
+ //// To assist with debugging visually, uncomment following
120
+ //
121
+ // if (i % 2) context.strokeStyle = 'cyan';
122
+ // else context.strokeStyle = 'green';
123
+ // var p1 = glyphInfo[i].p1;
124
+ // context.moveTo(p0.x, p0.y);
125
+ // context.lineTo(p1.x, p1.y);
126
+ // context.stroke();
111
127
  }
112
- context.restore();
113
- //// To assist with debugging visually, uncomment following
114
- //
115
- // if (i % 2) context.strokeStyle = 'cyan';
116
- // else context.strokeStyle = 'green';
117
- // var p1 = glyphInfo[i].p1;
118
- // context.moveTo(p0.x, p0.y);
119
- // context.lineTo(p1.x, p1.y);
120
- // context.stroke();
121
128
  }
122
- if (hasUnderline) {
123
- context.strokeStyle = fill;
124
- context.lineWidth = getDecorationLineWidth(fontSize);
129
+ finally {
130
+ this._styleTransform = undefined;
131
+ }
132
+ context.lineWidth = getDecorationLineWidth(fontSize);
133
+ if (hasUnderline && context._applyDecorationStyle(this)) {
125
134
  context.stroke();
126
135
  }
127
136
  if (hasLineThrough) {
@@ -137,9 +146,8 @@ export class TextPath extends Shape {
137
146
  context.lineTo(glyphInfo[i].width, 0);
138
147
  context.restore();
139
148
  }
140
- context.strokeStyle = fill;
141
- context.lineWidth = getDecorationLineWidth(fontSize);
142
- context.stroke();
149
+ if (context._applyDecorationStyle(this))
150
+ context.stroke();
143
151
  }
144
152
  context.restore();
145
153
  }
@@ -306,12 +314,30 @@ export class TextPath extends Shape {
306
314
  });
307
315
  const rect = Util._getPointsRect(points);
308
316
  const fontSize = this.fontSize();
309
- return {
310
- x: rect.x - fontSize / 2,
311
- y: rect.y - fontSize / 2,
312
- width: rect.width + fontSize,
313
- height: rect.height + fontSize,
314
- };
317
+ let x = rect.x - fontSize / 2, y = rect.y - fontSize / 2, right = rect.x + rect.width + fontSize / 2, bottom = rect.y + rect.height + fontSize / 2;
318
+ if (this.textDecoration().indexOf('underline') !== -1) {
319
+ // the underline is drawn below the glyphs, past the padding
320
+ const lineWidth = getDecorationLineWidth(fontSize), lineY = this._getUnderlineOffset() - lineWidth / 2;
321
+ this.glyphInfo.forEach(function (info) {
322
+ const line = new Transform()
323
+ .translate(info.p0.x, info.p0.y)
324
+ .rotate(info.rotation)
325
+ ._getTransformedRect({
326
+ x: 0,
327
+ y: lineY,
328
+ width: info.width,
329
+ height: lineWidth,
330
+ });
331
+ x = Math.min(x, line.x);
332
+ y = Math.min(y, line.y);
333
+ right = Math.max(right, line.x + line.width);
334
+ bottom = Math.max(bottom, line.y + line.height);
335
+ });
336
+ }
337
+ return { x, y, width: right - x, height: bottom - y };
338
+ }
339
+ _getUnderlineOffset() {
340
+ return this.fontSize() / 2 + 1;
315
341
  }
316
342
  // Like Text: a glyph stroke is drawn with the text, so it always scales.
317
343
  getStrokeScaleEnabled() {
@@ -334,10 +360,9 @@ TextPath.prototype._strokeFunc = _strokeFunc;
334
360
  TextPath.prototype._fillFuncHit = _fillFunc;
335
361
  TextPath.prototype._strokeFuncHit = _strokeFunc;
336
362
  TextPath.prototype.className = 'TextPath';
337
- TextPath.prototype._attrsAffectingSize = [
363
+ const ATTR_CHANGE_LIST = [
338
364
  'text',
339
365
  'fontSize',
340
- 'data',
341
366
  'align',
342
367
  'letterSpacing',
343
368
  'kerningFunc',
@@ -346,16 +371,16 @@ TextPath.prototype._attrsAffectingSize = [
346
371
  'fontVariant',
347
372
  'direction',
348
373
  ];
374
+ // data relayouts in its own listener; textDecoration changes the bounds
375
+ // through getSelfRect, but needs no relayout
376
+ TextPath.prototype._attrsAffectingSize = ATTR_CHANGE_LIST.concat('data', 'textDecoration');
349
377
  _registerNode(TextPath, true);
350
378
  TextPath.prototype.on('dataChange.konva', function () {
351
379
  this._readDataAttribute();
352
380
  this._setTextData();
353
381
  });
354
382
  // update text data for certain attr changes
355
- TextPath.prototype.on(TextPath.prototype._attrsAffectingSize
356
- .filter((attr) => attr !== 'data')
357
- .map((attr) => attr + 'Change.konva')
358
- .join(' '), function () {
383
+ TextPath.prototype.on(ATTR_CHANGE_LIST.map((attr) => attr + 'Change.konva').join(' '), function () {
359
384
  this._setTextData();
360
385
  });
361
386
  /**
@@ -1,13 +1,13 @@
1
- import { Util, Transform } from "../Util.js";
2
- import { Factory } from "../Factory.js";
3
- import { Node } from "../Node.js";
4
- import { Shape } from "../Shape.js";
5
- import { Rect } from "./Rect.js";
6
- import { Group } from "../Group.js";
7
- import { Container } from "../Container.js";
8
- import { Konva } from "../Global.js";
9
- import { getBooleanValidator, getNumberValidator } from "../Validators.js";
10
- import { _registerNode } from "../Global.js";
1
+ import { Util, Transform } from '../Util.js';
2
+ import { Factory } from '../Factory.js';
3
+ import { Node } from '../Node.js';
4
+ import { Shape } from '../Shape.js';
5
+ import { Rect } from './Rect.js';
6
+ import { Group } from '../Group.js';
7
+ import { Container } from '../Container.js';
8
+ import { Konva } from '../Global.js';
9
+ import { getBooleanValidator, getNumberValidator } from '../Validators.js';
10
+ import { _registerNode } from '../Global.js';
11
11
  const EVENTS_NAME = 'tr-konva';
12
12
  const ATTR_CHANGE_LIST = [
13
13
  'resizeEnabledChange',
@@ -777,7 +777,8 @@ export class Transformer extends Group {
777
777
  });
778
778
  }
779
779
  _handleMouseDown(e) {
780
- var _a, _b;
780
+ var _a;
781
+ var _b;
781
782
  if (e.evt.button !== undefined && !Konva.dragButtons.includes(e.evt.button))
782
783
  return;
783
784
  // do nothing if we already transforming
@@ -1,8 +1,8 @@
1
- import { Factory } from "../Factory.js";
2
- import { Shape } from "../Shape.js";
3
- import { Konva } from "../Global.js";
4
- import { getNumberValidator } from "../Validators.js";
5
- import { _registerNode } from "../Global.js";
1
+ import { Factory } from '../Factory.js';
2
+ import { Shape } from '../Shape.js';
3
+ import { Konva } from '../Global.js';
4
+ import { getNumberValidator } from '../Validators.js';
5
+ import { _registerNode } from '../Global.js';
6
6
  /**
7
7
  * Wedge constructor
8
8
  * @constructor
@@ -1,33 +1,25 @@
1
- import { Konva } from "./_CoreInternals.js";
2
- // @ts-ignore
1
+ import { Konva } from './_CoreInternals.js';
3
2
  import { Canvas, DOMMatrix, Image, Path2D } from 'skia-canvas';
4
3
  // @ts-ignore
5
4
  global.DOMMatrix = DOMMatrix;
6
5
  // @ts-ignore
7
6
  global.Path2D = Path2D;
8
- Path2D.prototype.toString = () => '[object Path2D]';
9
- Konva.Util['createCanvasElement'] = () => {
10
- const node = new Canvas(300, 300);
11
- if (!node['style']) {
12
- node['style'] = {};
13
- }
14
- node.toString = () => '[object HTMLCanvasElement]';
15
- const ctx = node.getContext('2d');
16
- // Override the getter to return the canvas node directly
17
- // because in skia-canvas canvas is using weak ref to the canvas node
18
- // and somehow on many tests it fails to get the canvas node
19
- Object.defineProperty(ctx, 'canvas', {
20
- get: () => node,
21
- });
22
- return node;
23
- };
24
- // create image in Node env
25
- Konva.Util.createImageElement = () => {
26
- const node = new Image();
27
- node.toString = () => '[object HTMLImageElement]';
28
- return node;
29
- };
30
- Konva._renderBackend = 'skia-canvas';
7
+ Konva.setCanvasBackend({
8
+ name: 'skia-canvas',
9
+ createCanvas: () => {
10
+ const node = new Canvas(300, 300);
11
+ const ctx = node.getContext('2d');
12
+ // Override the getter to return the canvas node directly
13
+ // because in skia-canvas canvas is using weak ref to the canvas node
14
+ // and somehow on many tests it fails to get the canvas node
15
+ Object.defineProperty(ctx, 'canvas', {
16
+ get: () => node,
17
+ });
18
+ return node;
19
+ },
20
+ createImage: () => new Image(),
21
+ createPath2D: (path) => new Path2D(path),
22
+ });
31
23
  // the core Konva object, without shapes and filters. `import 'konva'` extends
32
24
  // this same object with them
33
25
  export default Konva;