konva 8.1.4

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 (125) hide show
  1. package/CHANGELOG.md +1140 -0
  2. package/LICENSE +22 -0
  3. package/README.md +189 -0
  4. package/konva.js +18090 -0
  5. package/konva.min.js +12 -0
  6. package/lib/Animation.d.ts +24 -0
  7. package/lib/Animation.js +141 -0
  8. package/lib/Canvas.d.ts +32 -0
  9. package/lib/Canvas.js +107 -0
  10. package/lib/Container.d.ts +47 -0
  11. package/lib/Container.js +318 -0
  12. package/lib/Context.d.ts +76 -0
  13. package/lib/Context.js +520 -0
  14. package/lib/Core.d.ts +3 -0
  15. package/lib/Core.js +3 -0
  16. package/lib/DragAndDrop.d.ts +17 -0
  17. package/lib/DragAndDrop.js +104 -0
  18. package/lib/Factory.d.ts +11 -0
  19. package/lib/Factory.js +117 -0
  20. package/lib/FastLayer.d.ts +4 -0
  21. package/lib/FastLayer.js +12 -0
  22. package/lib/Global.d.ts +30 -0
  23. package/lib/Global.js +53 -0
  24. package/lib/Group.d.ts +6 -0
  25. package/lib/Group.js +13 -0
  26. package/lib/Layer.d.ts +60 -0
  27. package/lib/Layer.js +301 -0
  28. package/lib/Node.d.ts +318 -0
  29. package/lib/Node.js +1381 -0
  30. package/lib/PointerEvents.d.ts +11 -0
  31. package/lib/PointerEvents.js +37 -0
  32. package/lib/Shape.d.ts +184 -0
  33. package/lib/Shape.js +515 -0
  34. package/lib/Stage.d.ts +85 -0
  35. package/lib/Stage.js +687 -0
  36. package/lib/Tween.d.ts +86 -0
  37. package/lib/Tween.js +516 -0
  38. package/lib/Util.d.ts +87 -0
  39. package/lib/Util.js +742 -0
  40. package/lib/Validators.d.ts +11 -0
  41. package/lib/Validators.js +171 -0
  42. package/lib/_CoreInternals.d.ts +144 -0
  43. package/lib/_CoreInternals.js +34 -0
  44. package/lib/_FullInternals.d.ts +188 -0
  45. package/lib/_FullInternals.js +78 -0
  46. package/lib/filters/Blur.d.ts +2 -0
  47. package/lib/filters/Blur.js +697 -0
  48. package/lib/filters/Brighten.d.ts +2 -0
  49. package/lib/filters/Brighten.js +12 -0
  50. package/lib/filters/Contrast.d.ts +2 -0
  51. package/lib/filters/Contrast.js +34 -0
  52. package/lib/filters/Emboss.d.ts +2 -0
  53. package/lib/filters/Emboss.js +102 -0
  54. package/lib/filters/Enhance.d.ts +2 -0
  55. package/lib/filters/Enhance.js +83 -0
  56. package/lib/filters/Grayscale.d.ts +2 -0
  57. package/lib/filters/Grayscale.js +9 -0
  58. package/lib/filters/HSL.d.ts +2 -0
  59. package/lib/filters/HSL.js +24 -0
  60. package/lib/filters/HSV.d.ts +2 -0
  61. package/lib/filters/HSV.js +24 -0
  62. package/lib/filters/Invert.d.ts +2 -0
  63. package/lib/filters/Invert.js +8 -0
  64. package/lib/filters/Kaleidoscope.d.ts +2 -0
  65. package/lib/filters/Kaleidoscope.js +132 -0
  66. package/lib/filters/Mask.d.ts +2 -0
  67. package/lib/filters/Mask.js +140 -0
  68. package/lib/filters/Noise.d.ts +2 -0
  69. package/lib/filters/Noise.js +12 -0
  70. package/lib/filters/Pixelate.d.ts +2 -0
  71. package/lib/filters/Pixelate.js +60 -0
  72. package/lib/filters/Posterize.d.ts +2 -0
  73. package/lib/filters/Posterize.js +10 -0
  74. package/lib/filters/RGB.d.ts +2 -0
  75. package/lib/filters/RGB.js +39 -0
  76. package/lib/filters/RGBA.d.ts +2 -0
  77. package/lib/filters/RGBA.js +49 -0
  78. package/lib/filters/Sepia.d.ts +2 -0
  79. package/lib/filters/Sepia.js +11 -0
  80. package/lib/filters/Solarize.d.ts +2 -0
  81. package/lib/filters/Solarize.js +25 -0
  82. package/lib/filters/Threshold.d.ts +2 -0
  83. package/lib/filters/Threshold.js +10 -0
  84. package/lib/index-node.d.ts +2 -0
  85. package/lib/index-node.js +18 -0
  86. package/lib/index-types.d.ts +179 -0
  87. package/lib/index.d.ts +2 -0
  88. package/lib/index.js +2 -0
  89. package/lib/shapes/Arc.d.ts +19 -0
  90. package/lib/shapes/Arc.js +35 -0
  91. package/lib/shapes/Arrow.d.ts +24 -0
  92. package/lib/shapes/Arrow.js +94 -0
  93. package/lib/shapes/Circle.d.ts +13 -0
  94. package/lib/shapes/Circle.js +33 -0
  95. package/lib/shapes/Ellipse.d.ts +16 -0
  96. package/lib/shapes/Ellipse.js +37 -0
  97. package/lib/shapes/Image.d.ts +23 -0
  98. package/lib/shapes/Image.js +100 -0
  99. package/lib/shapes/Label.d.ts +35 -0
  100. package/lib/shapes/Label.js +166 -0
  101. package/lib/shapes/Line.d.ts +28 -0
  102. package/lib/shapes/Line.js +155 -0
  103. package/lib/shapes/Path.d.ts +37 -0
  104. package/lib/shapes/Path.js +627 -0
  105. package/lib/shapes/Rect.d.ts +9 -0
  106. package/lib/shapes/Rect.js +42 -0
  107. package/lib/shapes/RegularPolygon.d.ts +22 -0
  108. package/lib/shapes/RegularPolygon.js +65 -0
  109. package/lib/shapes/Ring.d.ts +16 -0
  110. package/lib/shapes/Ring.js +33 -0
  111. package/lib/shapes/Sprite.d.ts +30 -0
  112. package/lib/shapes/Sprite.js +108 -0
  113. package/lib/shapes/Star.d.ts +17 -0
  114. package/lib/shapes/Star.js +38 -0
  115. package/lib/shapes/Text.d.ts +59 -0
  116. package/lib/shapes/Text.js +347 -0
  117. package/lib/shapes/TextPath.d.ts +54 -0
  118. package/lib/shapes/TextPath.js +343 -0
  119. package/lib/shapes/Transformer.d.ts +112 -0
  120. package/lib/shapes/Transformer.js +944 -0
  121. package/lib/shapes/Wedge.d.ts +17 -0
  122. package/lib/shapes/Wedge.js +38 -0
  123. package/lib/types.d.ts +50 -0
  124. package/lib/types.js +22 -0
  125. package/package.json +111 -0
@@ -0,0 +1,347 @@
1
+ import { Util } from '../Util.js';
2
+ import { Factory } from '../Factory.js';
3
+ import { Shape } from '../Shape.js';
4
+ import { getNumberValidator, getStringValidator, getNumberOrAutoValidator, getBooleanValidator, } from '../Validators.js';
5
+ import { _registerNode } from '../Global.js';
6
+ export function stringToArray(string) {
7
+ return Array.from(string);
8
+ }
9
+ var AUTO = 'auto', CENTER = 'center', 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', WORD = 'word', CHAR = 'char', NONE = 'none', ELLIPSIS = '…', ATTR_CHANGE_LIST = [
10
+ 'fontFamily',
11
+ 'fontSize',
12
+ 'fontStyle',
13
+ 'fontVariant',
14
+ 'padding',
15
+ 'align',
16
+ 'verticalAlign',
17
+ 'lineHeight',
18
+ 'text',
19
+ 'width',
20
+ 'height',
21
+ 'wrap',
22
+ 'ellipsis',
23
+ 'letterSpacing',
24
+ ], attrChangeListLen = ATTR_CHANGE_LIST.length;
25
+ function normalizeFontFamily(fontFamily) {
26
+ return fontFamily
27
+ .split(',')
28
+ .map((family) => {
29
+ family = family.trim();
30
+ const hasSpace = family.indexOf(' ') >= 0;
31
+ const hasQuotes = family.indexOf('"') >= 0 || family.indexOf("'") >= 0;
32
+ if (hasSpace && !hasQuotes) {
33
+ family = `"${family}"`;
34
+ }
35
+ return family;
36
+ })
37
+ .join(', ');
38
+ }
39
+ var dummyContext;
40
+ function getDummyContext() {
41
+ if (dummyContext) {
42
+ return dummyContext;
43
+ }
44
+ dummyContext = Util.createCanvasElement().getContext(CONTEXT_2D);
45
+ return dummyContext;
46
+ }
47
+ function _fillFunc(context) {
48
+ context.fillText(this._partialText, this._partialTextX, this._partialTextY);
49
+ }
50
+ function _strokeFunc(context) {
51
+ context.strokeText(this._partialText, this._partialTextX, this._partialTextY);
52
+ }
53
+ function checkDefaultFill(config) {
54
+ config = config || {};
55
+ if (!config.fillLinearGradientColorStops &&
56
+ !config.fillRadialGradientColorStops &&
57
+ !config.fillPatternImage) {
58
+ config.fill = config.fill || 'black';
59
+ }
60
+ return config;
61
+ }
62
+ export class Text extends Shape {
63
+ constructor(config) {
64
+ super(checkDefaultFill(config));
65
+ this._partialTextX = 0;
66
+ this._partialTextY = 0;
67
+ for (var n = 0; n < attrChangeListLen; n++) {
68
+ this.on(ATTR_CHANGE_LIST[n] + CHANGE_KONVA, this._setTextData);
69
+ }
70
+ this._setTextData();
71
+ }
72
+ _sceneFunc(context) {
73
+ var textArr = this.textArr, textArrLen = textArr.length;
74
+ if (!this.text()) {
75
+ return;
76
+ }
77
+ var padding = this.padding(), fontSize = this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, verticalAlign = this.verticalAlign(), alignY = 0, align = this.align(), totalWidth = this.getWidth(), letterSpacing = this.letterSpacing(), fill = this.fill(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n;
78
+ var translateY = 0;
79
+ var translateY = lineHeightPx / 2;
80
+ var lineTranslateX = 0;
81
+ var lineTranslateY = 0;
82
+ context.setAttr('font', this._getContextFont());
83
+ context.setAttr('textBaseline', MIDDLE);
84
+ context.setAttr('textAlign', LEFT);
85
+ if (verticalAlign === MIDDLE) {
86
+ alignY = (this.getHeight() - textArrLen * lineHeightPx - padding * 2) / 2;
87
+ }
88
+ else if (verticalAlign === BOTTOM) {
89
+ alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
90
+ }
91
+ context.translate(padding, alignY + padding);
92
+ for (n = 0; n < textArrLen; n++) {
93
+ var lineTranslateX = 0;
94
+ var lineTranslateY = 0;
95
+ var obj = textArr[n], text = obj.text, width = obj.width, lastLine = n !== textArrLen - 1, spacesNumber, oneWord, lineWidth;
96
+ context.save();
97
+ if (align === RIGHT) {
98
+ lineTranslateX += totalWidth - width - padding * 2;
99
+ }
100
+ else if (align === CENTER) {
101
+ lineTranslateX += (totalWidth - width - padding * 2) / 2;
102
+ }
103
+ if (shouldUnderline) {
104
+ context.save();
105
+ context.beginPath();
106
+ context.moveTo(lineTranslateX, translateY + lineTranslateY + Math.round(fontSize / 2));
107
+ spacesNumber = text.split(' ').length - 1;
108
+ oneWord = spacesNumber === 0;
109
+ lineWidth =
110
+ align === JUSTIFY && lastLine && !oneWord
111
+ ? totalWidth - padding * 2
112
+ : width;
113
+ context.lineTo(lineTranslateX + Math.round(lineWidth), translateY + lineTranslateY + Math.round(fontSize / 2));
114
+ context.lineWidth = fontSize / 15;
115
+ context.strokeStyle = fill;
116
+ context.stroke();
117
+ context.restore();
118
+ }
119
+ if (shouldLineThrough) {
120
+ context.save();
121
+ context.beginPath();
122
+ context.moveTo(lineTranslateX, translateY + lineTranslateY);
123
+ spacesNumber = text.split(' ').length - 1;
124
+ oneWord = spacesNumber === 0;
125
+ lineWidth =
126
+ align === JUSTIFY && lastLine && !oneWord
127
+ ? totalWidth - padding * 2
128
+ : width;
129
+ context.lineTo(lineTranslateX + Math.round(lineWidth), translateY + lineTranslateY);
130
+ context.lineWidth = fontSize / 15;
131
+ context.strokeStyle = fill;
132
+ context.stroke();
133
+ context.restore();
134
+ }
135
+ if (letterSpacing !== 0 || align === JUSTIFY) {
136
+ spacesNumber = text.split(' ').length - 1;
137
+ var array = stringToArray(text);
138
+ for (var li = 0; li < array.length; li++) {
139
+ var letter = array[li];
140
+ if (letter === ' ' && n !== textArrLen - 1 && align === JUSTIFY) {
141
+ lineTranslateX += (totalWidth - padding * 2 - width) / spacesNumber;
142
+ }
143
+ this._partialTextX = lineTranslateX;
144
+ this._partialTextY = translateY + lineTranslateY;
145
+ this._partialText = letter;
146
+ context.fillStrokeShape(this);
147
+ lineTranslateX += this.measureSize(letter).width + letterSpacing;
148
+ }
149
+ }
150
+ else {
151
+ this._partialTextX = lineTranslateX;
152
+ this._partialTextY = translateY + lineTranslateY;
153
+ this._partialText = text;
154
+ context.fillStrokeShape(this);
155
+ }
156
+ context.restore();
157
+ if (textArrLen > 1) {
158
+ translateY += lineHeightPx;
159
+ }
160
+ }
161
+ }
162
+ _hitFunc(context) {
163
+ var width = this.getWidth(), height = this.getHeight();
164
+ context.beginPath();
165
+ context.rect(0, 0, width, height);
166
+ context.closePath();
167
+ context.fillStrokeShape(this);
168
+ }
169
+ setText(text) {
170
+ var str = Util._isString(text)
171
+ ? text
172
+ : text === null || text === undefined
173
+ ? ''
174
+ : text + '';
175
+ this._setAttr(TEXT, str);
176
+ return this;
177
+ }
178
+ getWidth() {
179
+ var isAuto = this.attrs.width === AUTO || this.attrs.width === undefined;
180
+ return isAuto ? this.getTextWidth() + this.padding() * 2 : this.attrs.width;
181
+ }
182
+ getHeight() {
183
+ var isAuto = this.attrs.height === AUTO || this.attrs.height === undefined;
184
+ return isAuto
185
+ ? this.fontSize() * this.textArr.length * this.lineHeight() +
186
+ this.padding() * 2
187
+ : this.attrs.height;
188
+ }
189
+ getTextWidth() {
190
+ return this.textWidth;
191
+ }
192
+ getTextHeight() {
193
+ Util.warn('text.getTextHeight() method is deprecated. Use text.height() - for full height and text.fontSize() - for one line height.');
194
+ return this.textHeight;
195
+ }
196
+ measureSize(text) {
197
+ var _context = getDummyContext(), fontSize = this.fontSize(), metrics;
198
+ _context.save();
199
+ _context.font = this._getContextFont();
200
+ metrics = _context.measureText(text);
201
+ _context.restore();
202
+ return {
203
+ width: metrics.width,
204
+ height: fontSize,
205
+ };
206
+ }
207
+ _getContextFont() {
208
+ return (this.fontStyle() +
209
+ SPACE +
210
+ this.fontVariant() +
211
+ SPACE +
212
+ (this.fontSize() + PX_SPACE) +
213
+ normalizeFontFamily(this.fontFamily()));
214
+ }
215
+ _addTextLine(line) {
216
+ if (this.align() === JUSTIFY) {
217
+ line = line.trim();
218
+ }
219
+ var width = this._getTextWidth(line);
220
+ return this.textArr.push({ text: line, width: width });
221
+ }
222
+ _getTextWidth(text) {
223
+ var letterSpacing = this.letterSpacing();
224
+ var length = text.length;
225
+ return (getDummyContext().measureText(text).width +
226
+ (length ? letterSpacing * (length - 1) : 0));
227
+ }
228
+ _setTextData() {
229
+ var lines = this.text().split('\n'), fontSize = +this.fontSize(), textWidth = 0, 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(), shouldWrap = wrap !== NONE, wrapAtWord = wrap !== CHAR && shouldWrap, shouldAddEllipsis = this.ellipsis();
230
+ this.textArr = [];
231
+ getDummyContext().font = this._getContextFont();
232
+ var additionalWidth = shouldAddEllipsis ? this._getTextWidth(ELLIPSIS) : 0;
233
+ for (var i = 0, max = lines.length; i < max; ++i) {
234
+ var line = lines[i];
235
+ var lineWidth = this._getTextWidth(line);
236
+ if (fixedWidth && lineWidth > maxWidth) {
237
+ while (line.length > 0) {
238
+ var low = 0, high = line.length, match = '', matchWidth = 0;
239
+ while (low < high) {
240
+ var mid = (low + high) >>> 1, substr = line.slice(0, mid + 1), substrWidth = this._getTextWidth(substr) + additionalWidth;
241
+ if (substrWidth <= maxWidth) {
242
+ low = mid + 1;
243
+ match = substr;
244
+ matchWidth = substrWidth;
245
+ }
246
+ else {
247
+ high = mid;
248
+ }
249
+ }
250
+ if (match) {
251
+ if (wrapAtWord) {
252
+ var wrapIndex;
253
+ var nextChar = line[match.length];
254
+ var nextIsSpaceOrDash = nextChar === SPACE || nextChar === DASH;
255
+ if (nextIsSpaceOrDash && matchWidth <= maxWidth) {
256
+ wrapIndex = match.length;
257
+ }
258
+ else {
259
+ wrapIndex =
260
+ Math.max(match.lastIndexOf(SPACE), match.lastIndexOf(DASH)) +
261
+ 1;
262
+ }
263
+ if (wrapIndex > 0) {
264
+ low = wrapIndex;
265
+ match = match.slice(0, low);
266
+ matchWidth = this._getTextWidth(match);
267
+ }
268
+ }
269
+ match = match.trimRight();
270
+ this._addTextLine(match);
271
+ textWidth = Math.max(textWidth, matchWidth);
272
+ currentHeightPx += lineHeightPx;
273
+ if (!shouldWrap ||
274
+ (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx)) {
275
+ var lastLine = this.textArr[this.textArr.length - 1];
276
+ if (lastLine) {
277
+ if (shouldAddEllipsis) {
278
+ var haveSpace = this._getTextWidth(lastLine.text + ELLIPSIS) < maxWidth;
279
+ if (!haveSpace) {
280
+ lastLine.text = lastLine.text.slice(0, lastLine.text.length - 3);
281
+ }
282
+ this.textArr.splice(this.textArr.length - 1, 1);
283
+ this._addTextLine(lastLine.text + ELLIPSIS);
284
+ }
285
+ }
286
+ break;
287
+ }
288
+ line = line.slice(low);
289
+ line = line.trimLeft();
290
+ if (line.length > 0) {
291
+ lineWidth = this._getTextWidth(line);
292
+ if (lineWidth <= maxWidth) {
293
+ this._addTextLine(line);
294
+ currentHeightPx += lineHeightPx;
295
+ textWidth = Math.max(textWidth, lineWidth);
296
+ break;
297
+ }
298
+ }
299
+ }
300
+ else {
301
+ break;
302
+ }
303
+ }
304
+ }
305
+ else {
306
+ this._addTextLine(line);
307
+ currentHeightPx += lineHeightPx;
308
+ textWidth = Math.max(textWidth, lineWidth);
309
+ }
310
+ if (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) {
311
+ break;
312
+ }
313
+ }
314
+ this.textHeight = fontSize;
315
+ this.textWidth = textWidth;
316
+ }
317
+ getStrokeScaleEnabled() {
318
+ return true;
319
+ }
320
+ }
321
+ Text.prototype._fillFunc = _fillFunc;
322
+ Text.prototype._strokeFunc = _strokeFunc;
323
+ Text.prototype.className = TEXT_UPPER;
324
+ Text.prototype._attrsAffectingSize = [
325
+ 'text',
326
+ 'fontSize',
327
+ 'padding',
328
+ 'wrap',
329
+ 'lineHeight',
330
+ 'letterSpacing',
331
+ ];
332
+ _registerNode(Text);
333
+ Factory.overWriteSetter(Text, 'width', getNumberOrAutoValidator());
334
+ Factory.overWriteSetter(Text, 'height', getNumberOrAutoValidator());
335
+ Factory.addGetterSetter(Text, 'fontFamily', 'Arial');
336
+ Factory.addGetterSetter(Text, 'fontSize', 12, getNumberValidator());
337
+ Factory.addGetterSetter(Text, 'fontStyle', NORMAL);
338
+ Factory.addGetterSetter(Text, 'fontVariant', NORMAL);
339
+ Factory.addGetterSetter(Text, 'padding', 0, getNumberValidator());
340
+ Factory.addGetterSetter(Text, 'align', LEFT);
341
+ Factory.addGetterSetter(Text, 'verticalAlign', TOP);
342
+ Factory.addGetterSetter(Text, 'lineHeight', 1, getNumberValidator());
343
+ Factory.addGetterSetter(Text, 'wrap', WORD);
344
+ Factory.addGetterSetter(Text, 'ellipsis', false, getBooleanValidator());
345
+ Factory.addGetterSetter(Text, 'letterSpacing', 0, getNumberValidator());
346
+ Factory.addGetterSetter(Text, 'text', '', getStringValidator());
347
+ Factory.addGetterSetter(Text, 'textDecoration', '');
@@ -0,0 +1,54 @@
1
+ import { Shape, ShapeConfig } from '../Shape';
2
+ import { GetSet, Vector2d } from '../types';
3
+ export interface TextPathConfig extends ShapeConfig {
4
+ text?: string;
5
+ data?: string;
6
+ fontFamily?: string;
7
+ fontSize?: number;
8
+ fontStyle?: string;
9
+ letterSpacing?: number;
10
+ }
11
+ export declare class TextPath extends Shape<TextPathConfig> {
12
+ dummyCanvas: HTMLCanvasElement;
13
+ dataArray: any[];
14
+ glyphInfo: Array<{
15
+ transposeX: number;
16
+ transposeY: number;
17
+ text: string;
18
+ rotation: number;
19
+ p0: Vector2d;
20
+ p1: Vector2d;
21
+ }>;
22
+ partialText: string;
23
+ textWidth: number;
24
+ textHeight: number;
25
+ constructor(config?: TextPathConfig);
26
+ _sceneFunc(context: any): void;
27
+ _hitFunc(context: any): void;
28
+ getTextWidth(): number;
29
+ getTextHeight(): number;
30
+ setText(text: any): any;
31
+ _getContextFont(): any;
32
+ _getTextSize(text: any): {
33
+ width: number;
34
+ height: number;
35
+ };
36
+ _setTextData(): void;
37
+ getSelfRect(): {
38
+ x: number;
39
+ y: number;
40
+ width: number;
41
+ height: number;
42
+ };
43
+ fontFamily: GetSet<string, this>;
44
+ fontSize: GetSet<number, this>;
45
+ fontStyle: GetSet<string, this>;
46
+ fontVariant: GetSet<string, this>;
47
+ align: GetSet<string, this>;
48
+ letterSpacing: GetSet<number, this>;
49
+ text: GetSet<string, this>;
50
+ data: GetSet<string, this>;
51
+ kerningFunc: GetSet<(leftChar: string, rightChar: string) => number, this>;
52
+ textBaseline: GetSet<string, this>;
53
+ textDecoration: GetSet<string, this>;
54
+ }
@@ -0,0 +1,343 @@
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, stringToArray } from './Text.js';
6
+ import { getNumberValidator } from '../Validators.js';
7
+ import { _registerNode } from '../Global.js';
8
+ var EMPTY_STRING = '', NORMAL = 'normal';
9
+ function _fillFunc(context) {
10
+ context.fillText(this.partialText, 0, 0);
11
+ }
12
+ function _strokeFunc(context) {
13
+ context.strokeText(this.partialText, 0, 0);
14
+ }
15
+ export class TextPath extends Shape {
16
+ constructor(config) {
17
+ super(config);
18
+ this.dummyCanvas = Util.createCanvasElement();
19
+ this.dataArray = [];
20
+ this.dataArray = Path.parsePathData(this.attrs.data);
21
+ this.on('dataChange.konva', function () {
22
+ this.dataArray = Path.parsePathData(this.attrs.data);
23
+ this._setTextData();
24
+ });
25
+ this.on('textChange.konva alignChange.konva letterSpacingChange.konva kerningFuncChange.konva fontSizeChange.konva', this._setTextData);
26
+ this._setTextData();
27
+ }
28
+ _sceneFunc(context) {
29
+ context.setAttr('font', this._getContextFont());
30
+ context.setAttr('textBaseline', this.textBaseline());
31
+ context.setAttr('textAlign', 'left');
32
+ context.save();
33
+ var textDecoration = this.textDecoration();
34
+ var fill = this.fill();
35
+ var fontSize = this.fontSize();
36
+ var glyphInfo = this.glyphInfo;
37
+ if (textDecoration === 'underline') {
38
+ context.beginPath();
39
+ }
40
+ for (var i = 0; i < glyphInfo.length; i++) {
41
+ context.save();
42
+ var p0 = glyphInfo[i].p0;
43
+ context.translate(p0.x, p0.y);
44
+ context.rotate(glyphInfo[i].rotation);
45
+ this.partialText = glyphInfo[i].text;
46
+ context.fillStrokeShape(this);
47
+ if (textDecoration === 'underline') {
48
+ if (i === 0) {
49
+ context.moveTo(0, fontSize / 2 + 1);
50
+ }
51
+ context.lineTo(fontSize, fontSize / 2 + 1);
52
+ }
53
+ context.restore();
54
+ }
55
+ if (textDecoration === 'underline') {
56
+ context.strokeStyle = fill;
57
+ context.lineWidth = fontSize / 20;
58
+ context.stroke();
59
+ }
60
+ context.restore();
61
+ }
62
+ _hitFunc(context) {
63
+ context.beginPath();
64
+ var glyphInfo = this.glyphInfo;
65
+ if (glyphInfo.length >= 1) {
66
+ var p0 = glyphInfo[0].p0;
67
+ context.moveTo(p0.x, p0.y);
68
+ }
69
+ for (var i = 0; i < glyphInfo.length; i++) {
70
+ var p1 = glyphInfo[i].p1;
71
+ context.lineTo(p1.x, p1.y);
72
+ }
73
+ context.setAttr('lineWidth', this.fontSize());
74
+ context.setAttr('strokeStyle', this.colorKey);
75
+ context.stroke();
76
+ }
77
+ getTextWidth() {
78
+ return this.textWidth;
79
+ }
80
+ getTextHeight() {
81
+ Util.warn('text.getTextHeight() method is deprecated. Use text.height() - for full height and text.fontSize() - for one line height.');
82
+ return this.textHeight;
83
+ }
84
+ setText(text) {
85
+ return Text.prototype.setText.call(this, text);
86
+ }
87
+ _getContextFont() {
88
+ return Text.prototype._getContextFont.call(this);
89
+ }
90
+ _getTextSize(text) {
91
+ var dummyCanvas = this.dummyCanvas;
92
+ var _context = dummyCanvas.getContext('2d');
93
+ _context.save();
94
+ _context.font = this._getContextFont();
95
+ var metrics = _context.measureText(text);
96
+ _context.restore();
97
+ return {
98
+ width: metrics.width,
99
+ height: parseInt(this.attrs.fontSize, 10),
100
+ };
101
+ }
102
+ _setTextData() {
103
+ var that = this;
104
+ var size = this._getTextSize(this.attrs.text);
105
+ var letterSpacing = this.letterSpacing();
106
+ var align = this.align();
107
+ var kerningFunc = this.kerningFunc();
108
+ this.textWidth = size.width;
109
+ this.textHeight = size.height;
110
+ var textFullWidth = Math.max(this.textWidth + ((this.attrs.text || '').length - 1) * letterSpacing, 0);
111
+ this.glyphInfo = [];
112
+ var fullPathWidth = 0;
113
+ for (var l = 0; l < that.dataArray.length; l++) {
114
+ if (that.dataArray[l].pathLength > 0) {
115
+ fullPathWidth += that.dataArray[l].pathLength;
116
+ }
117
+ }
118
+ var offset = 0;
119
+ if (align === 'center') {
120
+ offset = Math.max(0, fullPathWidth / 2 - textFullWidth / 2);
121
+ }
122
+ if (align === 'right') {
123
+ offset = Math.max(0, fullPathWidth - textFullWidth);
124
+ }
125
+ var charArr = stringToArray(this.text());
126
+ var spacesNumber = this.text().split(' ').length - 1;
127
+ var p0, p1, pathCmd;
128
+ var pIndex = -1;
129
+ var currentT = 0;
130
+ var getNextPathSegment = function () {
131
+ currentT = 0;
132
+ var pathData = that.dataArray;
133
+ for (var j = pIndex + 1; j < pathData.length; j++) {
134
+ if (pathData[j].pathLength > 0) {
135
+ pIndex = j;
136
+ return pathData[j];
137
+ }
138
+ else if (pathData[j].command === 'M') {
139
+ p0 = {
140
+ x: pathData[j].points[0],
141
+ y: pathData[j].points[1],
142
+ };
143
+ }
144
+ }
145
+ return {};
146
+ };
147
+ var findSegmentToFitCharacter = function (c) {
148
+ var glyphWidth = that._getTextSize(c).width + letterSpacing;
149
+ if (c === ' ' && align === 'justify') {
150
+ glyphWidth += (fullPathWidth - textFullWidth) / spacesNumber;
151
+ }
152
+ var currLen = 0;
153
+ var attempts = 0;
154
+ p1 = undefined;
155
+ while (Math.abs(glyphWidth - currLen) / glyphWidth > 0.01 &&
156
+ attempts < 20) {
157
+ attempts++;
158
+ var cumulativePathLength = currLen;
159
+ while (pathCmd === undefined) {
160
+ pathCmd = getNextPathSegment();
161
+ if (pathCmd &&
162
+ cumulativePathLength + pathCmd.pathLength < glyphWidth) {
163
+ cumulativePathLength += pathCmd.pathLength;
164
+ pathCmd = undefined;
165
+ }
166
+ }
167
+ if (pathCmd === {} || p0 === undefined) {
168
+ return undefined;
169
+ }
170
+ var needNewSegment = false;
171
+ switch (pathCmd.command) {
172
+ case 'L':
173
+ if (Path.getLineLength(p0.x, p0.y, pathCmd.points[0], pathCmd.points[1]) > glyphWidth) {
174
+ p1 = Path.getPointOnLine(glyphWidth, p0.x, p0.y, pathCmd.points[0], pathCmd.points[1], p0.x, p0.y);
175
+ }
176
+ else {
177
+ pathCmd = undefined;
178
+ }
179
+ break;
180
+ case 'A':
181
+ var start = pathCmd.points[4];
182
+ var dTheta = pathCmd.points[5];
183
+ var end = pathCmd.points[4] + dTheta;
184
+ if (currentT === 0) {
185
+ currentT = start + 0.00000001;
186
+ }
187
+ else if (glyphWidth > currLen) {
188
+ currentT += ((Math.PI / 180.0) * dTheta) / Math.abs(dTheta);
189
+ }
190
+ else {
191
+ currentT -= ((Math.PI / 360.0) * dTheta) / Math.abs(dTheta);
192
+ }
193
+ if ((dTheta < 0 && currentT < end) ||
194
+ (dTheta >= 0 && currentT > end)) {
195
+ currentT = end;
196
+ needNewSegment = true;
197
+ }
198
+ p1 = Path.getPointOnEllipticalArc(pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], currentT, pathCmd.points[6]);
199
+ break;
200
+ case 'C':
201
+ if (currentT === 0) {
202
+ if (glyphWidth > pathCmd.pathLength) {
203
+ currentT = 0.00000001;
204
+ }
205
+ else {
206
+ currentT = glyphWidth / pathCmd.pathLength;
207
+ }
208
+ }
209
+ else if (glyphWidth > currLen) {
210
+ currentT += (glyphWidth - currLen) / pathCmd.pathLength / 2;
211
+ }
212
+ else {
213
+ currentT = Math.max(currentT - (currLen - glyphWidth) / pathCmd.pathLength / 2, 0);
214
+ }
215
+ if (currentT > 1.0) {
216
+ currentT = 1.0;
217
+ needNewSegment = true;
218
+ }
219
+ p1 = Path.getPointOnCubicBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], pathCmd.points[4], pathCmd.points[5]);
220
+ break;
221
+ case 'Q':
222
+ if (currentT === 0) {
223
+ currentT = glyphWidth / pathCmd.pathLength;
224
+ }
225
+ else if (glyphWidth > currLen) {
226
+ currentT += (glyphWidth - currLen) / pathCmd.pathLength;
227
+ }
228
+ else {
229
+ currentT -= (currLen - glyphWidth) / pathCmd.pathLength;
230
+ }
231
+ if (currentT > 1.0) {
232
+ currentT = 1.0;
233
+ needNewSegment = true;
234
+ }
235
+ p1 = Path.getPointOnQuadraticBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3]);
236
+ break;
237
+ }
238
+ if (p1 !== undefined) {
239
+ currLen = Path.getLineLength(p0.x, p0.y, p1.x, p1.y);
240
+ }
241
+ if (needNewSegment) {
242
+ needNewSegment = false;
243
+ pathCmd = undefined;
244
+ }
245
+ }
246
+ };
247
+ var testChar = 'C';
248
+ var glyphWidth = that._getTextSize(testChar).width + letterSpacing;
249
+ var lettersInOffset = offset / glyphWidth - 1;
250
+ for (var k = 0; k < lettersInOffset; k++) {
251
+ findSegmentToFitCharacter(testChar);
252
+ if (p0 === undefined || p1 === undefined) {
253
+ break;
254
+ }
255
+ p0 = p1;
256
+ }
257
+ for (var i = 0; i < charArr.length; i++) {
258
+ findSegmentToFitCharacter(charArr[i]);
259
+ if (p0 === undefined || p1 === undefined) {
260
+ break;
261
+ }
262
+ var width = Path.getLineLength(p0.x, p0.y, p1.x, p1.y);
263
+ var kern = 0;
264
+ if (kerningFunc) {
265
+ try {
266
+ kern = kerningFunc(charArr[i - 1], charArr[i]) * this.fontSize();
267
+ }
268
+ catch (e) {
269
+ kern = 0;
270
+ }
271
+ }
272
+ p0.x += kern;
273
+ p1.x += kern;
274
+ this.textWidth += kern;
275
+ var midpoint = Path.getPointOnLine(kern + width / 2.0, p0.x, p0.y, p1.x, p1.y);
276
+ var rotation = Math.atan2(p1.y - p0.y, p1.x - p0.x);
277
+ this.glyphInfo.push({
278
+ transposeX: midpoint.x,
279
+ transposeY: midpoint.y,
280
+ text: charArr[i],
281
+ rotation: rotation,
282
+ p0: p0,
283
+ p1: p1,
284
+ });
285
+ p0 = p1;
286
+ }
287
+ }
288
+ getSelfRect() {
289
+ if (!this.glyphInfo.length) {
290
+ return {
291
+ x: 0,
292
+ y: 0,
293
+ width: 0,
294
+ height: 0,
295
+ };
296
+ }
297
+ var points = [];
298
+ this.glyphInfo.forEach(function (info) {
299
+ points.push(info.p0.x);
300
+ points.push(info.p0.y);
301
+ points.push(info.p1.x);
302
+ points.push(info.p1.y);
303
+ });
304
+ var minX = points[0] || 0;
305
+ var maxX = points[0] || 0;
306
+ var minY = points[1] || 0;
307
+ var maxY = points[1] || 0;
308
+ var x, y;
309
+ for (var i = 0; i < points.length / 2; i++) {
310
+ x = points[i * 2];
311
+ y = points[i * 2 + 1];
312
+ minX = Math.min(minX, x);
313
+ maxX = Math.max(maxX, x);
314
+ minY = Math.min(minY, y);
315
+ maxY = Math.max(maxY, y);
316
+ }
317
+ var fontSize = this.fontSize();
318
+ return {
319
+ x: minX - fontSize / 2,
320
+ y: minY - fontSize / 2,
321
+ width: maxX - minX + fontSize,
322
+ height: maxY - minY + fontSize,
323
+ };
324
+ }
325
+ }
326
+ TextPath.prototype._fillFunc = _fillFunc;
327
+ TextPath.prototype._strokeFunc = _strokeFunc;
328
+ TextPath.prototype._fillFuncHit = _fillFunc;
329
+ TextPath.prototype._strokeFuncHit = _strokeFunc;
330
+ TextPath.prototype.className = 'TextPath';
331
+ TextPath.prototype._attrsAffectingSize = ['text', 'fontSize', 'data'];
332
+ _registerNode(TextPath);
333
+ Factory.addGetterSetter(TextPath, 'data');
334
+ Factory.addGetterSetter(TextPath, 'fontFamily', 'Arial');
335
+ Factory.addGetterSetter(TextPath, 'fontSize', 12, getNumberValidator());
336
+ Factory.addGetterSetter(TextPath, 'fontStyle', NORMAL);
337
+ Factory.addGetterSetter(TextPath, 'align', 'left');
338
+ Factory.addGetterSetter(TextPath, 'letterSpacing', 0, getNumberValidator());
339
+ Factory.addGetterSetter(TextPath, 'textBaseline', 'middle');
340
+ Factory.addGetterSetter(TextPath, 'fontVariant', NORMAL);
341
+ Factory.addGetterSetter(TextPath, 'text', EMPTY_STRING);
342
+ Factory.addGetterSetter(TextPath, 'textDecoration', null);
343
+ Factory.addGetterSetter(TextPath, 'kerningFunc', null);