konva 10.4.0 → 10.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/konva.js +3432 -3746
- package/konva.min.js +4 -4
- package/lib/Animation.d.ts +2 -2
- package/lib/Animation.js +2 -2
- package/lib/BezierFunctions.d.ts +1 -1
- package/lib/BezierFunctions.js +76 -116
- package/lib/Canvas.d.ts +1 -1
- package/lib/Canvas.js +8 -5
- package/lib/Container.js +9 -6
- package/lib/Context.d.ts +4 -5
- package/lib/Context.js +77 -72
- package/lib/DragAndDrop.d.ts +5 -3
- package/lib/DragAndDrop.js +65 -29
- package/lib/Factory.d.ts +3 -2
- package/lib/Factory.js +10 -9
- package/lib/Global.d.ts +0 -6
- package/lib/Global.js +2 -8
- package/lib/Layer.js +9 -6
- package/lib/Node.d.ts +33 -15
- package/lib/Node.js +179 -135
- package/lib/PointerEvents.d.ts +1 -2
- package/lib/PointerEvents.js +20 -18
- package/lib/Shape.d.ts +2 -2
- package/lib/Shape.js +51 -7
- package/lib/Stage.d.ts +23 -14
- package/lib/Stage.js +221 -93
- package/lib/Tween.js +59 -66
- package/lib/Util.d.ts +3 -2
- package/lib/Util.js +43 -16
- package/lib/_CoreInternals.d.ts +25 -11
- package/lib/_FullInternals.d.ts +25 -11
- package/lib/filters/Blur.js +16 -12
- package/lib/filters/Brighten.d.ts +1 -0
- package/lib/filters/Brighten.js +1 -0
- package/lib/filters/Brightness.d.ts +2 -1
- package/lib/filters/Brightness.js +4 -4
- package/lib/filters/Emboss.js +16 -58
- package/lib/filters/Kaleidoscope.js +8 -15
- package/lib/filters/Mask.js +8 -8
- package/lib/filters/Pixelate.js +16 -29
- package/lib/filters/Threshold.d.ts +1 -1
- package/lib/filters/Threshold.js +1 -1
- package/lib/shapes/Arc.js +5 -3
- package/lib/shapes/Arrow.d.ts +2 -6
- package/lib/shapes/Arrow.js +68 -46
- package/lib/shapes/Image.js +16 -19
- package/lib/shapes/Label.d.ts +2 -3
- package/lib/shapes/Label.js +7 -21
- package/lib/shapes/Line.d.ts +1 -1
- package/lib/shapes/Line.js +1 -1
- package/lib/shapes/Path.d.ts +27 -2
- package/lib/shapes/Path.js +148 -142
- package/lib/shapes/RegularPolygon.js +1 -1
- package/lib/shapes/Sprite.js +2 -6
- package/lib/shapes/Text.d.ts +12 -4
- package/lib/shapes/Text.js +96 -54
- package/lib/shapes/TextPath.d.ts +4 -3
- package/lib/shapes/TextPath.js +40 -26
- package/lib/shapes/Transformer.d.ts +16 -1
- package/lib/shapes/Transformer.js +209 -130
- package/lib/types.d.ts +2 -2
- package/package.json +4 -3
package/lib/shapes/Text.js
CHANGED
|
@@ -124,13 +124,12 @@ function _strokeFunc(context) {
|
|
|
124
124
|
}
|
|
125
125
|
function checkDefaultFill(config) {
|
|
126
126
|
config = config || {};
|
|
127
|
-
// set default color to black
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
config.
|
|
132
|
-
}
|
|
133
|
-
return config;
|
|
127
|
+
// set default color to black, without mutating the caller's config
|
|
128
|
+
const hasFill = config.fill ||
|
|
129
|
+
config.fillLinearGradientColorStops ||
|
|
130
|
+
config.fillRadialGradientColorStops ||
|
|
131
|
+
config.fillPatternImage;
|
|
132
|
+
return hasFill ? config : { ...config, fill: 'black' };
|
|
134
133
|
}
|
|
135
134
|
/**
|
|
136
135
|
* Text constructor
|
|
@@ -169,6 +168,8 @@ export class Text extends Shape {
|
|
|
169
168
|
super(checkDefaultFill(config));
|
|
170
169
|
this._partialTextX = 0;
|
|
171
170
|
this._partialTextY = 0;
|
|
171
|
+
// distance from the line box center to the baseline, see _setTextData
|
|
172
|
+
this._baselineShift = 0;
|
|
172
173
|
this._setTextData();
|
|
173
174
|
}
|
|
174
175
|
_sceneFunc(context) {
|
|
@@ -177,8 +178,7 @@ export class Text extends Shape {
|
|
|
177
178
|
if (!this.text()) {
|
|
178
179
|
return;
|
|
179
180
|
}
|
|
180
|
-
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(),
|
|
181
|
-
direction = direction === INHERIT ? context.direction : direction;
|
|
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;
|
|
182
182
|
let translateY = lineHeightPx / 2;
|
|
183
183
|
let baseline = MIDDLE;
|
|
184
184
|
if (!Konva.legacyTextRendering) {
|
|
@@ -188,9 +188,14 @@ export class Text extends Shape {
|
|
|
188
188
|
const descent = (_b = metrics.fontBoundingBoxDescent) !== null && _b !== void 0 ? _b : metrics.actualBoundingBoxDescent;
|
|
189
189
|
translateY = (ascent - descent) / 2 + lineHeightPx / 2;
|
|
190
190
|
}
|
|
191
|
-
if (direction
|
|
191
|
+
if (direction !== INHERIT) {
|
|
192
192
|
context.setAttr('direction', direction);
|
|
193
193
|
}
|
|
194
|
+
else {
|
|
195
|
+
// 'inherit' resolves to the direction the canvas already has, so that
|
|
196
|
+
// an inherited rtl still takes the single native run below
|
|
197
|
+
direction = context.direction;
|
|
198
|
+
}
|
|
194
199
|
context.setAttr('font', this._getContextFont());
|
|
195
200
|
context.setAttr('textBaseline', baseline);
|
|
196
201
|
context.setAttr('textAlign', LEFT);
|
|
@@ -202,6 +207,15 @@ export class Text extends Shape {
|
|
|
202
207
|
alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
|
|
203
208
|
}
|
|
204
209
|
context.translate(padding, alignY + padding);
|
|
210
|
+
// Start the callback with the same fill that normal drawing would use.
|
|
211
|
+
// Assigning that color again then needs no override, including black.
|
|
212
|
+
if (charRenderFunc) {
|
|
213
|
+
const style = context._getFillStyle(this);
|
|
214
|
+
if (style !== undefined)
|
|
215
|
+
context.fillStyle = style;
|
|
216
|
+
if (this.hasStroke())
|
|
217
|
+
context._applyStrokeStyle(this);
|
|
218
|
+
}
|
|
205
219
|
// context styles entering the loop, used to detect styles set by
|
|
206
220
|
// charRenderFunc; every character is drawn inside save()/restore(),
|
|
207
221
|
// so the entry state is the same for each one
|
|
@@ -225,28 +239,23 @@ export class Text extends Shape {
|
|
|
225
239
|
if (shouldUnderline) {
|
|
226
240
|
context.save();
|
|
227
241
|
context.beginPath();
|
|
228
|
-
const yOffset =
|
|
229
|
-
? Math.round(fontSize / 4)
|
|
230
|
-
: Math.round(fontSize / 2));
|
|
242
|
+
const yOffset = this._getUnderlineOffset();
|
|
231
243
|
const x = lineTranslateX;
|
|
232
244
|
const y = translateY + lineTranslateY + yOffset;
|
|
233
245
|
context.moveTo(x, y);
|
|
234
246
|
const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
|
|
235
247
|
context.lineTo(x + Math.round(lineWidth), y);
|
|
236
248
|
context.lineWidth = getDecorationLineWidth(fontSize);
|
|
237
|
-
|
|
238
|
-
context.strokeStyle = gradient || fill;
|
|
249
|
+
context.strokeStyle = context._getFillStyle(this);
|
|
239
250
|
context.stroke();
|
|
240
251
|
context.restore();
|
|
241
252
|
}
|
|
242
253
|
// store the starting x position for line-through which is drawn after text
|
|
243
254
|
const lineThroughStartX = lineTranslateX;
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
// be supported otherwise.
|
|
255
|
+
// Render graphemes separately for spacing, justification and callbacks.
|
|
256
|
+
// Keep RTL text in one native run to preserve shaping.
|
|
247
257
|
if (direction !== RTL &&
|
|
248
258
|
(letterSpacing !== 0 || align === JUSTIFY || charRenderFunc)) {
|
|
249
|
-
// var words = text.split(' ');
|
|
250
259
|
const spacesNumber = text.split(' ').length - 1;
|
|
251
260
|
const array = stringToArray(text);
|
|
252
261
|
for (let li = 0; li < array.length; li++) {
|
|
@@ -254,10 +263,6 @@ export class Text extends Shape {
|
|
|
254
263
|
// skip justify for the last line
|
|
255
264
|
if (letter === ' ' && !lastLine && align === JUSTIFY) {
|
|
256
265
|
lineTranslateX += (totalWidth - padding * 2 - width) / spacesNumber;
|
|
257
|
-
// context.translate(
|
|
258
|
-
// Math.floor((totalWidth - padding * 2 - width) / spacesNumber),
|
|
259
|
-
// 0
|
|
260
|
-
// );
|
|
261
266
|
}
|
|
262
267
|
this._partialTextX = lineTranslateX;
|
|
263
268
|
this._partialTextY = translateY + lineTranslateY;
|
|
@@ -320,8 +325,7 @@ export class Text extends Shape {
|
|
|
320
325
|
const lineWidth = align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
|
|
321
326
|
context.lineTo(x + Math.round(lineWidth), translateY + lineTranslateY + yOffset);
|
|
322
327
|
context.lineWidth = getDecorationLineWidth(fontSize);
|
|
323
|
-
|
|
324
|
-
context.strokeStyle = gradient || fill;
|
|
328
|
+
context.strokeStyle = context._getFillStyle(this);
|
|
325
329
|
context.stroke();
|
|
326
330
|
context.restore();
|
|
327
331
|
}
|
|
@@ -331,6 +335,32 @@ export class Text extends Shape {
|
|
|
331
335
|
}
|
|
332
336
|
}
|
|
333
337
|
}
|
|
338
|
+
_getUnderlineOffset() {
|
|
339
|
+
var _a;
|
|
340
|
+
return ((_a = this.underlineOffset()) !== null && _a !== void 0 ? _a : Math.round(this.fontSize() / (!Konva.legacyTextRendering ? 4 : 2)));
|
|
341
|
+
}
|
|
342
|
+
getSelfRect() {
|
|
343
|
+
const rect = super.getSelfRect();
|
|
344
|
+
const lines = this.textArr.length;
|
|
345
|
+
if (!lines || this.textDecoration().indexOf('underline') === -1) {
|
|
346
|
+
return rect;
|
|
347
|
+
}
|
|
348
|
+
// 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;
|
|
351
|
+
if (!Konva.legacyTextRendering) {
|
|
352
|
+
bottom += this._baselineShift;
|
|
353
|
+
}
|
|
354
|
+
if (verticalAlign === MIDDLE) {
|
|
355
|
+
bottom += (rect.height - blockHeight) / 2;
|
|
356
|
+
}
|
|
357
|
+
else if (verticalAlign === BOTTOM) {
|
|
358
|
+
bottom += rect.height - blockHeight;
|
|
359
|
+
}
|
|
360
|
+
bottom += this._getUnderlineOffset() + getDecorationLineWidth(fontSize) / 2;
|
|
361
|
+
rect.height = Math.max(rect.height, bottom);
|
|
362
|
+
return rect;
|
|
363
|
+
}
|
|
334
364
|
_hitFunc(context) {
|
|
335
365
|
const width = this.getWidth(), height = this.getHeight();
|
|
336
366
|
context.beginPath();
|
|
@@ -435,11 +465,26 @@ export class Text extends Shape {
|
|
|
435
465
|
return getDummyContext().measureText(text).width + spacing;
|
|
436
466
|
}
|
|
437
467
|
_setTextData() {
|
|
438
|
-
let lines = this.text().split('\n'), fontSize = +this.fontSize(),
|
|
468
|
+
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(),
|
|
439
469
|
// align = this.align(),
|
|
440
470
|
shouldWrap = wrap !== NONE, wrapAtWord = wrap !== CHAR && shouldWrap, shouldAddEllipsis = this.ellipsis();
|
|
471
|
+
// measured here so getSelfRect, which runs per drag frame, needs no
|
|
472
|
+
// measureText of its own
|
|
473
|
+
const sample = this.measureSize('M');
|
|
474
|
+
this._baselineShift =
|
|
475
|
+
(sample.fontBoundingBoxAscent - sample.fontBoundingBoxDescent) / 2;
|
|
441
476
|
this.textArr = [];
|
|
442
|
-
|
|
477
|
+
const dummyContext = getDummyContext();
|
|
478
|
+
dummyContext.font = this._getContextFont();
|
|
479
|
+
// the per-character draw path (same condition as in _sceneFunc) advances
|
|
480
|
+
// by unkerned glyph widths, so measure the line the same way
|
|
481
|
+
dummyContext.fontKerning =
|
|
482
|
+
this.direction() !== RTL &&
|
|
483
|
+
(this.letterSpacing() !== 0 ||
|
|
484
|
+
this.align() === JUSTIFY ||
|
|
485
|
+
!!this.charRenderFunc())
|
|
486
|
+
? 'none'
|
|
487
|
+
: 'auto';
|
|
443
488
|
const additionalWidth = shouldAddEllipsis
|
|
444
489
|
? this._getTextWidth(ELLIPSIS)
|
|
445
490
|
: 0;
|
|
@@ -466,14 +511,11 @@ export class Text extends Shape {
|
|
|
466
511
|
currentHeightPx + lineHeightPx > maxHeightPx
|
|
467
512
|
? additionalWidth
|
|
468
513
|
: 0;
|
|
469
|
-
// width of the longest fitting prefix found so far
|
|
470
|
-
let matchWidth = 0;
|
|
471
514
|
const fits = (end) => {
|
|
472
515
|
const width = this._getTextWidth(text(end), end - start);
|
|
473
516
|
if (width + extraWidth > maxWidth) {
|
|
474
517
|
return false;
|
|
475
518
|
}
|
|
476
|
-
matchWidth = width;
|
|
477
519
|
return true;
|
|
478
520
|
};
|
|
479
521
|
/*
|
|
@@ -508,7 +550,11 @@ export class Text extends Shape {
|
|
|
508
550
|
// without the ellipsis check, like a paragraph that never wrapped
|
|
509
551
|
this._addTextLine(text(length));
|
|
510
552
|
currentHeightPx += lineHeightPx;
|
|
511
|
-
|
|
553
|
+
if (fixedHeight &&
|
|
554
|
+
currentHeightPx + lineHeightPx > maxHeightPx &&
|
|
555
|
+
i < max - 1) {
|
|
556
|
+
this._tryToAddEllipsisToLastLine();
|
|
557
|
+
}
|
|
512
558
|
break;
|
|
513
559
|
}
|
|
514
560
|
if (wrapAtWord && !isBreak(graphemes[low])) {
|
|
@@ -519,11 +565,9 @@ export class Text extends Shape {
|
|
|
519
565
|
}
|
|
520
566
|
if (wrapIndex >= start) {
|
|
521
567
|
low = wrapIndex + 1;
|
|
522
|
-
matchWidth = this._getTextWidth(text(low), low - start);
|
|
523
568
|
}
|
|
524
569
|
}
|
|
525
570
|
this._addTextLine(text(low).trimRight());
|
|
526
|
-
textWidth = Math.max(textWidth, matchWidth);
|
|
527
571
|
currentHeightPx += lineHeightPx;
|
|
528
572
|
if (this._shouldHandleEllipsis(currentHeightPx)) {
|
|
529
573
|
this._tryToAddEllipsisToLastLine();
|
|
@@ -544,8 +588,9 @@ export class Text extends Shape {
|
|
|
544
588
|
// element width is automatically adjusted to max line width
|
|
545
589
|
this._addTextLine(line);
|
|
546
590
|
currentHeightPx += lineHeightPx;
|
|
547
|
-
|
|
548
|
-
|
|
591
|
+
if (fixedHeight &&
|
|
592
|
+
currentHeightPx + lineHeightPx > maxHeightPx &&
|
|
593
|
+
i < max - 1) {
|
|
549
594
|
this._tryToAddEllipsisToLastLine();
|
|
550
595
|
}
|
|
551
596
|
}
|
|
@@ -558,11 +603,7 @@ export class Text extends Shape {
|
|
|
558
603
|
}
|
|
559
604
|
}
|
|
560
605
|
this.textHeight = fontSize;
|
|
561
|
-
|
|
562
|
-
// for(var j = 0; j < this.textArr.length; j++) {
|
|
563
|
-
// maxTextWidth = Math.max(maxTextWidth, this.textArr[j].width);
|
|
564
|
-
// }
|
|
565
|
-
this.textWidth = textWidth;
|
|
606
|
+
this.textWidth = this.textArr.reduce((width, line) => Math.max(width, line.width), 0);
|
|
566
607
|
}
|
|
567
608
|
/**
|
|
568
609
|
* whether to handle ellipsis, there are two cases:
|
|
@@ -582,14 +623,18 @@ export class Text extends Shape {
|
|
|
582
623
|
if (!lastLine || !shouldAddEllipsis) {
|
|
583
624
|
return;
|
|
584
625
|
}
|
|
626
|
+
let text = lastLine.text + ELLIPSIS;
|
|
585
627
|
if (fixedWidth) {
|
|
586
|
-
const
|
|
587
|
-
|
|
588
|
-
|
|
628
|
+
const graphemes = stringToArray(lastLine.text);
|
|
629
|
+
while (graphemes.length && this._getTextWidth(text) > maxWidth) {
|
|
630
|
+
graphemes.pop();
|
|
631
|
+
text = graphemes.join('') + ELLIPSIS;
|
|
589
632
|
}
|
|
633
|
+
if (this._getTextWidth(text) > maxWidth)
|
|
634
|
+
text = '';
|
|
590
635
|
}
|
|
591
|
-
|
|
592
|
-
this.
|
|
636
|
+
lastLine.text = text;
|
|
637
|
+
lastLine.width = this._getTextWidth(text);
|
|
593
638
|
}
|
|
594
639
|
// for text we can't disable stroke scaling
|
|
595
640
|
// if we do, the result will be unexpected
|
|
@@ -597,6 +642,8 @@ export class Text extends Shape {
|
|
|
597
642
|
return true;
|
|
598
643
|
}
|
|
599
644
|
_useBufferCanvas() {
|
|
645
|
+
if (this.attrs.perfectDrawEnabled === false)
|
|
646
|
+
return false;
|
|
600
647
|
const hasLine = this.textDecoration().indexOf('underline') !== -1 ||
|
|
601
648
|
this.textDecoration().indexOf('line-through') !== -1;
|
|
602
649
|
const hasShadow = this.hasShadow();
|
|
@@ -612,14 +659,9 @@ export class Text extends Shape {
|
|
|
612
659
|
Text.prototype._fillFunc = _fillFunc;
|
|
613
660
|
Text.prototype._strokeFunc = _strokeFunc;
|
|
614
661
|
Text.prototype.className = TEXT_UPPER;
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
'padding',
|
|
619
|
-
'wrap',
|
|
620
|
-
'lineHeight',
|
|
621
|
-
'letterSpacing',
|
|
622
|
-
];
|
|
662
|
+
// the decoration attrs are not in ATTR_CHANGE_LIST: they change the bounds
|
|
663
|
+
// through getSelfRect, but need no relayout
|
|
664
|
+
Text.prototype._attrsAffectingSize = ATTR_CHANGE_LIST.filter((attr) => attr !== 'width' && attr !== 'height').concat(['textDecoration', 'underlineOffset']);
|
|
623
665
|
_registerNode(Text);
|
|
624
666
|
// update text data for certain attr changes
|
|
625
667
|
Text.prototype.on(ATTR_CHANGE_LIST.map((attr) => attr + CHANGE_KONVA).join(' '), function () {
|
|
@@ -880,7 +922,7 @@ Factory.addGetterSetter(Text, 'textDecoration', '');
|
|
|
880
922
|
*/
|
|
881
923
|
Factory.addGetterSetter(Text, 'underlineOffset', undefined, getNumberValidator());
|
|
882
924
|
/**
|
|
883
|
-
* get/set per-character render hook. The callback is invoked for each grapheme before drawing.
|
|
925
|
+
* get/set per-character render hook. The callback is invoked for each grapheme before drawing, except for RTL text.
|
|
884
926
|
* It can mutate the provided context (e.g. translate, rotate, change styles) and should return void.
|
|
885
927
|
* Note: per-character rendering may disable native kerning/ligatures.
|
|
886
928
|
* @name Konva.Text#charRenderFunc
|
package/lib/shapes/TextPath.d.ts
CHANGED
|
@@ -64,8 +64,6 @@ export interface TextPathConfig extends ShapeConfig {
|
|
|
64
64
|
export declare class TextPath extends Shape<TextPathConfig> {
|
|
65
65
|
dataArray: PathSegment[];
|
|
66
66
|
glyphInfo: Array<{
|
|
67
|
-
transposeX: number;
|
|
68
|
-
transposeY: number;
|
|
69
67
|
text: string;
|
|
70
68
|
rotation: number;
|
|
71
69
|
p0: Vector2d;
|
|
@@ -78,7 +76,10 @@ export declare class TextPath extends Shape<TextPathConfig> {
|
|
|
78
76
|
textHeight: number;
|
|
79
77
|
constructor(config?: TextPathConfig);
|
|
80
78
|
_getTextPathLength(): number;
|
|
81
|
-
_getPointAtLength(length: number
|
|
79
|
+
_getPointAtLength(length: number, cursor?: {
|
|
80
|
+
index: number;
|
|
81
|
+
offset: number;
|
|
82
|
+
}): {
|
|
82
83
|
x: number;
|
|
83
84
|
y: number;
|
|
84
85
|
} | null;
|
package/lib/shapes/TextPath.js
CHANGED
|
@@ -67,7 +67,7 @@ export class TextPath extends Shape {
|
|
|
67
67
|
_getTextPathLength() {
|
|
68
68
|
return Path.getPathLength(this.dataArray);
|
|
69
69
|
}
|
|
70
|
-
_getPointAtLength(length) {
|
|
70
|
+
_getPointAtLength(length, cursor) {
|
|
71
71
|
// if path is not defined yet, do nothing
|
|
72
72
|
if (!this.attrs.data) {
|
|
73
73
|
return null;
|
|
@@ -76,7 +76,7 @@ export class TextPath extends Shape {
|
|
|
76
76
|
if (length > totalLength) {
|
|
77
77
|
return null;
|
|
78
78
|
}
|
|
79
|
-
return Path.getPointAtLengthOfDataArray(length, this.dataArray);
|
|
79
|
+
return Path.getPointAtLengthOfDataArray(length, this.dataArray, cursor);
|
|
80
80
|
}
|
|
81
81
|
_readDataAttribute() {
|
|
82
82
|
this.dataArray = Path.parsePathData(this.attrs.data);
|
|
@@ -180,6 +180,8 @@ export class TextPath extends Shape {
|
|
|
180
180
|
const _context = getDummyContext();
|
|
181
181
|
_context.save();
|
|
182
182
|
_context.font = this._getContextFont();
|
|
183
|
+
// Text leaves its own kerning mode on the shared dummy context
|
|
184
|
+
_context.fontKerning = 'auto';
|
|
183
185
|
const metrics = _context.measureText(text);
|
|
184
186
|
_context.restore();
|
|
185
187
|
return {
|
|
@@ -198,14 +200,24 @@ export class TextPath extends Shape {
|
|
|
198
200
|
if (this.direction() === 'rtl') {
|
|
199
201
|
charArr.reverse();
|
|
200
202
|
}
|
|
203
|
+
const kerningFunc = this.kerningFunc();
|
|
201
204
|
const chars = [];
|
|
202
205
|
let width = 0;
|
|
203
206
|
for (let i = 0; i < charArr.length; i++) {
|
|
207
|
+
let kern = 0;
|
|
208
|
+
if (kerningFunc && i > 0) {
|
|
209
|
+
try {
|
|
210
|
+
// kerningFunc is a user provided getter. Make sure it never breaks our logic
|
|
211
|
+
kern = kerningFunc(charArr[i - 1], charArr[i]) * this.fontSize();
|
|
212
|
+
}
|
|
213
|
+
catch (e) { }
|
|
214
|
+
}
|
|
204
215
|
chars.push({
|
|
205
216
|
char: charArr[i],
|
|
206
217
|
width: this._getTextSize(charArr[i]).width,
|
|
218
|
+
kern: kern,
|
|
207
219
|
});
|
|
208
|
-
width += chars[i].width;
|
|
220
|
+
width += chars[i].width + kern;
|
|
209
221
|
}
|
|
210
222
|
const { width: fullTextWidth, height } = this._getTextSize(this.attrs.text);
|
|
211
223
|
this.textWidth = width;
|
|
@@ -216,7 +228,7 @@ export class TextPath extends Shape {
|
|
|
216
228
|
}
|
|
217
229
|
const letterSpacing = this.letterSpacing();
|
|
218
230
|
const align = this.align();
|
|
219
|
-
const
|
|
231
|
+
const numberOfSpaces = align === 'justify' ? this.text().split(' ').length - 1 : 0;
|
|
220
232
|
// The sum of individual character widths can exceed the whole-string width
|
|
221
233
|
// due to kerning (browsers place adjacent glyphs closer together than the
|
|
222
234
|
// individual measurements suggest). This difference is the maximum amount
|
|
@@ -238,14 +250,18 @@ export class TextPath extends Shape {
|
|
|
238
250
|
// 3. Calculate the rotation, width, and midpoint of the glyph using the start and end points,
|
|
239
251
|
// 4. Add glyph width to the offsetToGlyph and repeat
|
|
240
252
|
let offsetToGlyph = offset;
|
|
253
|
+
// Keep the original boundary selection across gaps between subpaths.
|
|
254
|
+
const cursor = this.dataArray.some((segment, index) => index > 0 && segment.command === 'M')
|
|
255
|
+
? undefined
|
|
256
|
+
: { index: 0, offset: 0 };
|
|
241
257
|
for (let i = 0; i < chars.length; i++) {
|
|
242
|
-
|
|
258
|
+
offsetToGlyph += chars[i].kern;
|
|
259
|
+
const charStartPoint = this._getPointAtLength(offsetToGlyph, cursor);
|
|
243
260
|
if (!charStartPoint)
|
|
244
261
|
return;
|
|
245
262
|
const char = chars[i].char;
|
|
246
|
-
let glyphWidth = chars[i].width + letterSpacing;
|
|
263
|
+
let glyphWidth = chars[i].width + (i < chars.length - 1 ? letterSpacing : 0);
|
|
247
264
|
if (char === ' ' && align === 'justify') {
|
|
248
|
-
const numberOfSpaces = this.text().split(' ').length - 1;
|
|
249
265
|
glyphWidth += (this.pathLength - textWidth) / numberOfSpaces;
|
|
250
266
|
}
|
|
251
267
|
const charEndLength = offsetToGlyph + glyphWidth;
|
|
@@ -256,29 +272,13 @@ export class TextPath extends Shape {
|
|
|
256
272
|
const charEndPoint = this._getPointAtLength(charEndLength > this.pathLength &&
|
|
257
273
|
charEndLength - this.pathLength <= kerningAdjustment
|
|
258
274
|
? this.pathLength
|
|
259
|
-
: charEndLength);
|
|
275
|
+
: charEndLength, cursor);
|
|
260
276
|
if (!charEndPoint) {
|
|
261
277
|
return;
|
|
262
278
|
}
|
|
263
279
|
const width = Path.getLineLength(charStartPoint.x, charStartPoint.y, charEndPoint.x, charEndPoint.y);
|
|
264
|
-
let kern = 0;
|
|
265
|
-
if (kerningFunc && i > 0) {
|
|
266
|
-
try {
|
|
267
|
-
// getKerning is a user provided getter. Make sure it never breaks our logic
|
|
268
|
-
kern = kerningFunc(chars[i - 1].char, char) * this.fontSize();
|
|
269
|
-
}
|
|
270
|
-
catch (e) {
|
|
271
|
-
kern = 0;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
charStartPoint.x += kern;
|
|
275
|
-
charEndPoint.x += kern;
|
|
276
|
-
this.textWidth += kern;
|
|
277
|
-
const midpoint = Path.getPointOnLine(kern + width / 2.0, charStartPoint.x, charStartPoint.y, charEndPoint.x, charEndPoint.y);
|
|
278
280
|
const rotation = Math.atan2(charEndPoint.y - charStartPoint.y, charEndPoint.x - charStartPoint.x);
|
|
279
281
|
this.glyphInfo.push({
|
|
280
|
-
transposeX: midpoint.x,
|
|
281
|
-
transposeY: midpoint.y,
|
|
282
282
|
text: charArr[i],
|
|
283
283
|
rotation: rotation,
|
|
284
284
|
p0: charStartPoint,
|
|
@@ -319,14 +319,28 @@ TextPath.prototype._strokeFunc = _strokeFunc;
|
|
|
319
319
|
TextPath.prototype._fillFuncHit = _fillFunc;
|
|
320
320
|
TextPath.prototype._strokeFuncHit = _strokeFunc;
|
|
321
321
|
TextPath.prototype.className = 'TextPath';
|
|
322
|
-
TextPath.prototype._attrsAffectingSize = [
|
|
322
|
+
TextPath.prototype._attrsAffectingSize = [
|
|
323
|
+
'text',
|
|
324
|
+
'fontSize',
|
|
325
|
+
'data',
|
|
326
|
+
'align',
|
|
327
|
+
'letterSpacing',
|
|
328
|
+
'kerningFunc',
|
|
329
|
+
'fontFamily',
|
|
330
|
+
'fontStyle',
|
|
331
|
+
'fontVariant',
|
|
332
|
+
'direction',
|
|
333
|
+
];
|
|
323
334
|
_registerNode(TextPath);
|
|
324
335
|
TextPath.prototype.on('dataChange.konva', function () {
|
|
325
336
|
this._readDataAttribute();
|
|
326
337
|
this._setTextData();
|
|
327
338
|
});
|
|
328
339
|
// update text data for certain attr changes
|
|
329
|
-
TextPath.prototype.on(
|
|
340
|
+
TextPath.prototype.on(TextPath.prototype._attrsAffectingSize
|
|
341
|
+
.filter((attr) => attr !== 'data')
|
|
342
|
+
.map((attr) => attr + 'Change.konva')
|
|
343
|
+
.join(' '), function () {
|
|
330
344
|
this._setTextData();
|
|
331
345
|
});
|
|
332
346
|
/**
|
|
@@ -85,6 +85,17 @@ export interface TransformerConfig extends ContainerConfig {
|
|
|
85
85
|
*/
|
|
86
86
|
export declare class Transformer extends Group {
|
|
87
87
|
_nodes: Array<Node>;
|
|
88
|
+
_nodeRectCache?: Map<Node, {
|
|
89
|
+
rotation: number;
|
|
90
|
+
ignoreStroke: boolean;
|
|
91
|
+
version: number;
|
|
92
|
+
width: number;
|
|
93
|
+
height: number;
|
|
94
|
+
minX: number;
|
|
95
|
+
minY: number;
|
|
96
|
+
maxX: number;
|
|
97
|
+
maxY: number;
|
|
98
|
+
}>;
|
|
88
99
|
_movingAnchorName: string | null;
|
|
89
100
|
_pointerId?: number;
|
|
90
101
|
_anchors: Record<string, Rect>;
|
|
@@ -98,6 +109,7 @@ export declare class Transformer extends Group {
|
|
|
98
109
|
_cursorChange: boolean;
|
|
99
110
|
_elementsCreated: boolean;
|
|
100
111
|
_updateScheduled: boolean;
|
|
112
|
+
_lastNodeRect?: Readonly<Box>;
|
|
101
113
|
static isTransforming: () => boolean;
|
|
102
114
|
static _isLayerTransforming(layer: Node): boolean;
|
|
103
115
|
constructor(config?: TransformerConfig);
|
|
@@ -147,7 +159,7 @@ export declare class Transformer extends Group {
|
|
|
147
159
|
* console.log('transform started');
|
|
148
160
|
* });
|
|
149
161
|
*/
|
|
150
|
-
_resetTransformCache(): void;
|
|
162
|
+
_resetTransformCache(changedNode?: Node): void;
|
|
151
163
|
_getNodeRect(): any;
|
|
152
164
|
__getNodeRect(): {
|
|
153
165
|
x: number;
|
|
@@ -165,6 +177,7 @@ export declare class Transformer extends Group {
|
|
|
165
177
|
_createBack(): void;
|
|
166
178
|
_handleMouseDown(e: any): void;
|
|
167
179
|
_handleMouseMove(e: any): void;
|
|
180
|
+
_moveTransform(e: any): void;
|
|
168
181
|
_handleMouseUp(e: any): void;
|
|
169
182
|
getAbsoluteTransform(top?: Node | null): Transform;
|
|
170
183
|
_removeEvents(e?: any): void;
|
|
@@ -179,6 +192,8 @@ export declare class Transformer extends Group {
|
|
|
179
192
|
*/
|
|
180
193
|
forceUpdate(): void;
|
|
181
194
|
update(): void;
|
|
195
|
+
_update(): void;
|
|
196
|
+
_updateElements(attrs: Box): void;
|
|
182
197
|
/**
|
|
183
198
|
* determine if transformer is in active transform
|
|
184
199
|
* @method
|