konva 9.3.15 → 9.3.16
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/konva.js +726 -724
- package/konva.min.js +3 -3
- package/lib/Animation.js +1 -1
- package/lib/BezierFunctions.js +1 -2
- package/lib/Canvas.js +9 -9
- package/lib/Container.d.ts +1 -1
- package/lib/Container.js +29 -29
- package/lib/Context.d.ts +2 -2
- package/lib/Context.js +26 -26
- package/lib/DragAndDrop.d.ts +2 -2
- package/lib/DragAndDrop.js +6 -4
- package/lib/Factory.js +16 -16
- package/lib/Global.js +1 -1
- package/lib/Group.js +1 -1
- package/lib/Layer.js +17 -17
- package/lib/Node.d.ts +1 -1
- package/lib/Node.js +87 -87
- package/lib/PointerEvents.js +5 -6
- package/lib/Shape.js +31 -31
- package/lib/Stage.d.ts +2 -2
- package/lib/Stage.js +28 -28
- package/lib/Tween.js +21 -21
- package/lib/Util.d.ts +4 -4
- package/lib/Util.js +62 -62
- package/lib/Validators.js +16 -17
- package/lib/_CoreInternals.d.ts +8 -8
- package/lib/_FullInternals.d.ts +8 -8
- package/lib/filters/Blur.js +6 -6
- package/lib/filters/Brighten.js +1 -1
- package/lib/filters/Contrast.js +2 -2
- package/lib/filters/Emboss.js +19 -19
- package/lib/filters/Enhance.js +4 -4
- package/lib/filters/Grayscale.js +1 -1
- package/lib/filters/HSL.js +6 -6
- package/lib/filters/HSV.js +7 -7
- package/lib/filters/Invert.js +1 -1
- package/lib/filters/Kaleidoscope.js +20 -20
- package/lib/filters/Mask.js +57 -57
- package/lib/filters/Noise.js +2 -2
- package/lib/filters/Pixelate.js +1 -1
- package/lib/filters/Posterize.js +1 -1
- package/lib/filters/RGB.js +1 -1
- package/lib/filters/RGBA.js +3 -3
- package/lib/filters/Sepia.js +1 -1
- package/lib/filters/Solarize.js +8 -7
- package/lib/filters/Threshold.js +2 -2
- package/lib/shapes/Arc.js +1 -1
- package/lib/shapes/Arrow.js +10 -10
- package/lib/shapes/Ellipse.js +1 -1
- package/lib/shapes/Image.js +3 -3
- package/lib/shapes/Label.d.ts +1 -1
- package/lib/shapes/Label.js +6 -6
- package/lib/shapes/Line.d.ts +1 -1
- package/lib/shapes/Line.js +14 -14
- package/lib/shapes/Path.js +62 -62
- package/lib/shapes/Rect.js +1 -1
- package/lib/shapes/RegularPolygon.js +6 -6
- package/lib/shapes/Ring.js +1 -1
- package/lib/shapes/Sprite.js +9 -9
- package/lib/shapes/Star.js +5 -5
- package/lib/shapes/Text.js +37 -37
- package/lib/shapes/TextPath.js +23 -23
- package/lib/shapes/Transformer.d.ts +1 -1
- package/lib/shapes/Transformer.js +63 -63
- package/package.json +2 -5
package/lib/shapes/Text.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Text =
|
|
3
|
+
exports.Text = void 0;
|
|
4
|
+
exports.stringToArray = stringToArray;
|
|
4
5
|
const Util_1 = require("../Util");
|
|
5
6
|
const Factory_1 = require("../Factory");
|
|
6
7
|
const Shape_1 = require("../Shape");
|
|
@@ -24,8 +25,7 @@ function stringToArray(string) {
|
|
|
24
25
|
return acc;
|
|
25
26
|
}, []);
|
|
26
27
|
}
|
|
27
|
-
|
|
28
|
-
var AUTO = 'auto', CENTER = 'center', INHERIT = 'inherit', JUSTIFY = 'justify', CHANGE_KONVA = 'Change.konva', CONTEXT_2D = '2d', DASH = '-', LEFT = 'left', LTR = 'ltr', 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
|
+
const AUTO = 'auto', 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 = [
|
|
29
29
|
'direction',
|
|
30
30
|
'fontFamily',
|
|
31
31
|
'fontSize',
|
|
@@ -56,7 +56,7 @@ function normalizeFontFamily(fontFamily) {
|
|
|
56
56
|
})
|
|
57
57
|
.join(', ');
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
let dummyContext;
|
|
60
60
|
function getDummyContext() {
|
|
61
61
|
if (dummyContext) {
|
|
62
62
|
return dummyContext;
|
|
@@ -85,22 +85,22 @@ class Text extends Shape_1.Shape {
|
|
|
85
85
|
super(checkDefaultFill(config));
|
|
86
86
|
this._partialTextX = 0;
|
|
87
87
|
this._partialTextY = 0;
|
|
88
|
-
for (
|
|
88
|
+
for (let n = 0; n < attrChangeListLen; n++) {
|
|
89
89
|
this.on(ATTR_CHANGE_LIST[n] + CHANGE_KONVA, this._setTextData);
|
|
90
90
|
}
|
|
91
91
|
this._setTextData();
|
|
92
92
|
}
|
|
93
93
|
_sceneFunc(context) {
|
|
94
|
-
|
|
94
|
+
const textArr = this.textArr, textArrLen = textArr.length;
|
|
95
95
|
if (!this.text()) {
|
|
96
96
|
return;
|
|
97
97
|
}
|
|
98
|
-
|
|
98
|
+
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(), fill = this.fill(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n;
|
|
99
99
|
direction = direction === INHERIT ? context.direction : direction;
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
let translateY = lineHeightPx / 2;
|
|
101
|
+
let baseline = MIDDLE;
|
|
102
102
|
if (Global_1.Konva._fixTextRendering) {
|
|
103
|
-
|
|
103
|
+
const metrics = this.measureSize('M');
|
|
104
104
|
baseline = 'alphabetic';
|
|
105
105
|
translateY =
|
|
106
106
|
(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2 +
|
|
@@ -135,7 +135,7 @@ class Text extends Shape_1.Shape {
|
|
|
135
135
|
if (shouldUnderline) {
|
|
136
136
|
context.save();
|
|
137
137
|
context.beginPath();
|
|
138
|
-
|
|
138
|
+
const yOffset = Global_1.Konva._fixTextRendering
|
|
139
139
|
? Math.round(fontSize / 4)
|
|
140
140
|
: Math.round(fontSize / 2);
|
|
141
141
|
const x = lineTranslateX;
|
|
@@ -155,7 +155,7 @@ class Text extends Shape_1.Shape {
|
|
|
155
155
|
if (shouldLineThrough) {
|
|
156
156
|
context.save();
|
|
157
157
|
context.beginPath();
|
|
158
|
-
|
|
158
|
+
const yOffset = Global_1.Konva._fixTextRendering ? -Math.round(fontSize / 4) : 0;
|
|
159
159
|
context.moveTo(lineTranslateX, translateY + lineTranslateY + yOffset);
|
|
160
160
|
spacesNumber = text.split(' ').length - 1;
|
|
161
161
|
oneWord = spacesNumber === 0;
|
|
@@ -172,9 +172,9 @@ class Text extends Shape_1.Shape {
|
|
|
172
172
|
}
|
|
173
173
|
if (direction !== RTL && (letterSpacing !== 0 || align === JUSTIFY)) {
|
|
174
174
|
spacesNumber = text.split(' ').length - 1;
|
|
175
|
-
|
|
176
|
-
for (
|
|
177
|
-
|
|
175
|
+
const array = stringToArray(text);
|
|
176
|
+
for (let li = 0; li < array.length; li++) {
|
|
177
|
+
const letter = array[li];
|
|
178
178
|
if (letter === ' ' && !lastLine && align === JUSTIFY) {
|
|
179
179
|
lineTranslateX += (totalWidth - padding * 2 - width) / spacesNumber;
|
|
180
180
|
}
|
|
@@ -201,14 +201,14 @@ class Text extends Shape_1.Shape {
|
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
203
|
_hitFunc(context) {
|
|
204
|
-
|
|
204
|
+
const width = this.getWidth(), height = this.getHeight();
|
|
205
205
|
context.beginPath();
|
|
206
206
|
context.rect(0, 0, width, height);
|
|
207
207
|
context.closePath();
|
|
208
208
|
context.fillStrokeShape(this);
|
|
209
209
|
}
|
|
210
210
|
setText(text) {
|
|
211
|
-
|
|
211
|
+
const str = Util_1.Util._isString(text)
|
|
212
212
|
? text
|
|
213
213
|
: text === null || text === undefined
|
|
214
214
|
? ''
|
|
@@ -217,11 +217,11 @@ class Text extends Shape_1.Shape {
|
|
|
217
217
|
return this;
|
|
218
218
|
}
|
|
219
219
|
getWidth() {
|
|
220
|
-
|
|
220
|
+
const isAuto = this.attrs.width === AUTO || this.attrs.width === undefined;
|
|
221
221
|
return isAuto ? this.getTextWidth() + this.padding() * 2 : this.attrs.width;
|
|
222
222
|
}
|
|
223
223
|
getHeight() {
|
|
224
|
-
|
|
224
|
+
const isAuto = this.attrs.height === AUTO || this.attrs.height === undefined;
|
|
225
225
|
return isAuto
|
|
226
226
|
? this.fontSize() * this.textArr.length * this.lineHeight() +
|
|
227
227
|
this.padding() * 2
|
|
@@ -236,7 +236,7 @@ class Text extends Shape_1.Shape {
|
|
|
236
236
|
}
|
|
237
237
|
measureSize(text) {
|
|
238
238
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
239
|
-
|
|
239
|
+
let _context = getDummyContext(), fontSize = this.fontSize(), metrics;
|
|
240
240
|
_context.save();
|
|
241
241
|
_context.font = this._getContextFont();
|
|
242
242
|
metrics = _context.measureText(text);
|
|
@@ -271,7 +271,7 @@ class Text extends Shape_1.Shape {
|
|
|
271
271
|
if (align === JUSTIFY) {
|
|
272
272
|
line = line.trim();
|
|
273
273
|
}
|
|
274
|
-
|
|
274
|
+
const width = this._getTextWidth(line);
|
|
275
275
|
return this.textArr.push({
|
|
276
276
|
text: line,
|
|
277
277
|
width: width,
|
|
@@ -279,24 +279,24 @@ class Text extends Shape_1.Shape {
|
|
|
279
279
|
});
|
|
280
280
|
}
|
|
281
281
|
_getTextWidth(text) {
|
|
282
|
-
|
|
283
|
-
|
|
282
|
+
const letterSpacing = this.letterSpacing();
|
|
283
|
+
const length = text.length;
|
|
284
284
|
return (getDummyContext().measureText(text).width +
|
|
285
285
|
(length ? letterSpacing * (length - 1) : 0));
|
|
286
286
|
}
|
|
287
287
|
_setTextData() {
|
|
288
|
-
|
|
288
|
+
let 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();
|
|
289
289
|
this.textArr = [];
|
|
290
290
|
getDummyContext().font = this._getContextFont();
|
|
291
|
-
|
|
292
|
-
for (
|
|
293
|
-
|
|
294
|
-
|
|
291
|
+
const additionalWidth = shouldAddEllipsis ? this._getTextWidth(ELLIPSIS) : 0;
|
|
292
|
+
for (let i = 0, max = lines.length; i < max; ++i) {
|
|
293
|
+
let line = lines[i];
|
|
294
|
+
let lineWidth = this._getTextWidth(line);
|
|
295
295
|
if (fixedWidth && lineWidth > maxWidth) {
|
|
296
296
|
while (line.length > 0) {
|
|
297
|
-
|
|
297
|
+
let low = 0, high = line.length, match = '', matchWidth = 0;
|
|
298
298
|
while (low < high) {
|
|
299
|
-
|
|
299
|
+
const mid = (low + high) >>> 1, substr = line.slice(0, mid + 1), substrWidth = this._getTextWidth(substr) + additionalWidth;
|
|
300
300
|
if (substrWidth <= maxWidth) {
|
|
301
301
|
low = mid + 1;
|
|
302
302
|
match = substr;
|
|
@@ -309,8 +309,8 @@ class Text extends Shape_1.Shape {
|
|
|
309
309
|
if (match) {
|
|
310
310
|
if (wrapAtWord) {
|
|
311
311
|
var wrapIndex;
|
|
312
|
-
|
|
313
|
-
|
|
312
|
+
const nextChar = line[match.length];
|
|
313
|
+
const nextIsSpaceOrDash = nextChar === SPACE || nextChar === DASH;
|
|
314
314
|
if (nextIsSpaceOrDash && matchWidth <= maxWidth) {
|
|
315
315
|
wrapIndex = match.length;
|
|
316
316
|
}
|
|
@@ -329,7 +329,7 @@ class Text extends Shape_1.Shape {
|
|
|
329
329
|
this._addTextLine(match);
|
|
330
330
|
textWidth = Math.max(textWidth, matchWidth);
|
|
331
331
|
currentHeightPx += lineHeightPx;
|
|
332
|
-
|
|
332
|
+
const shouldHandleEllipsis = this._shouldHandleEllipsis(currentHeightPx);
|
|
333
333
|
if (shouldHandleEllipsis) {
|
|
334
334
|
this._tryToAddEllipsisToLastLine();
|
|
335
335
|
break;
|
|
@@ -370,18 +370,18 @@ class Text extends Shape_1.Shape {
|
|
|
370
370
|
this.textWidth = textWidth;
|
|
371
371
|
}
|
|
372
372
|
_shouldHandleEllipsis(currentHeightPx) {
|
|
373
|
-
|
|
373
|
+
const fontSize = +this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, height = this.attrs.height, fixedHeight = height !== AUTO && height !== undefined, padding = this.padding(), maxHeightPx = height - padding * 2, wrap = this.wrap(), shouldWrap = wrap !== NONE;
|
|
374
374
|
return (!shouldWrap ||
|
|
375
375
|
(fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx));
|
|
376
376
|
}
|
|
377
377
|
_tryToAddEllipsisToLastLine() {
|
|
378
|
-
|
|
379
|
-
|
|
378
|
+
const width = this.attrs.width, fixedWidth = width !== AUTO && width !== undefined, padding = this.padding(), maxWidth = width - padding * 2, shouldAddEllipsis = this.ellipsis();
|
|
379
|
+
const lastLine = this.textArr[this.textArr.length - 1];
|
|
380
380
|
if (!lastLine || !shouldAddEllipsis) {
|
|
381
381
|
return;
|
|
382
382
|
}
|
|
383
383
|
if (fixedWidth) {
|
|
384
|
-
|
|
384
|
+
const haveSpace = this._getTextWidth(lastLine.text + ELLIPSIS) < maxWidth;
|
|
385
385
|
if (!haveSpace) {
|
|
386
386
|
lastLine.text = lastLine.text.slice(0, lastLine.text.length - 3);
|
|
387
387
|
}
|
package/lib/shapes/TextPath.js
CHANGED
|
@@ -8,7 +8,7 @@ const Path_1 = require("./Path");
|
|
|
8
8
|
const Text_1 = require("./Text");
|
|
9
9
|
const Validators_1 = require("../Validators");
|
|
10
10
|
const Global_1 = require("../Global");
|
|
11
|
-
|
|
11
|
+
const EMPTY_STRING = '', NORMAL = 'normal';
|
|
12
12
|
function _fillFunc(context) {
|
|
13
13
|
context.fillText(this.partialText, 0, 0);
|
|
14
14
|
}
|
|
@@ -50,16 +50,16 @@ class TextPath extends Shape_1.Shape {
|
|
|
50
50
|
context.setAttr('textBaseline', this.textBaseline());
|
|
51
51
|
context.setAttr('textAlign', 'left');
|
|
52
52
|
context.save();
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
const textDecoration = this.textDecoration();
|
|
54
|
+
const fill = this.fill();
|
|
55
|
+
const fontSize = this.fontSize();
|
|
56
|
+
const glyphInfo = this.glyphInfo;
|
|
57
57
|
if (textDecoration === 'underline') {
|
|
58
58
|
context.beginPath();
|
|
59
59
|
}
|
|
60
|
-
for (
|
|
60
|
+
for (let i = 0; i < glyphInfo.length; i++) {
|
|
61
61
|
context.save();
|
|
62
|
-
|
|
62
|
+
const p0 = glyphInfo[i].p0;
|
|
63
63
|
context.translate(p0.x, p0.y);
|
|
64
64
|
context.rotate(glyphInfo[i].rotation);
|
|
65
65
|
this.partialText = glyphInfo[i].text;
|
|
@@ -81,13 +81,13 @@ class TextPath extends Shape_1.Shape {
|
|
|
81
81
|
}
|
|
82
82
|
_hitFunc(context) {
|
|
83
83
|
context.beginPath();
|
|
84
|
-
|
|
84
|
+
const glyphInfo = this.glyphInfo;
|
|
85
85
|
if (glyphInfo.length >= 1) {
|
|
86
|
-
|
|
86
|
+
const p0 = glyphInfo[0].p0;
|
|
87
87
|
context.moveTo(p0.x, p0.y);
|
|
88
88
|
}
|
|
89
|
-
for (
|
|
90
|
-
|
|
89
|
+
for (let i = 0; i < glyphInfo.length; i++) {
|
|
90
|
+
const p1 = glyphInfo[i].p1;
|
|
91
91
|
context.lineTo(p1.x, p1.y);
|
|
92
92
|
}
|
|
93
93
|
context.setAttr('lineWidth', this.fontSize());
|
|
@@ -108,11 +108,11 @@ class TextPath extends Shape_1.Shape {
|
|
|
108
108
|
return Text_1.Text.prototype._getContextFont.call(this);
|
|
109
109
|
}
|
|
110
110
|
_getTextSize(text) {
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
const dummyCanvas = this.dummyCanvas;
|
|
112
|
+
const _context = dummyCanvas.getContext('2d');
|
|
113
113
|
_context.save();
|
|
114
114
|
_context.font = this._getContextFont();
|
|
115
|
-
|
|
115
|
+
const metrics = _context.measureText(text);
|
|
116
116
|
_context.restore();
|
|
117
117
|
return {
|
|
118
118
|
width: metrics.width,
|
|
@@ -140,7 +140,7 @@ class TextPath extends Shape_1.Shape {
|
|
|
140
140
|
}
|
|
141
141
|
const charArr = (0, Text_1.stringToArray)(this.text());
|
|
142
142
|
let offsetToGlyph = offset;
|
|
143
|
-
for (
|
|
143
|
+
for (let i = 0; i < charArr.length; i++) {
|
|
144
144
|
const charStartPoint = this._getPointAtLength(offsetToGlyph);
|
|
145
145
|
if (!charStartPoint)
|
|
146
146
|
return;
|
|
@@ -187,19 +187,19 @@ class TextPath extends Shape_1.Shape {
|
|
|
187
187
|
height: 0,
|
|
188
188
|
};
|
|
189
189
|
}
|
|
190
|
-
|
|
190
|
+
const points = [];
|
|
191
191
|
this.glyphInfo.forEach(function (info) {
|
|
192
192
|
points.push(info.p0.x);
|
|
193
193
|
points.push(info.p0.y);
|
|
194
194
|
points.push(info.p1.x);
|
|
195
195
|
points.push(info.p1.y);
|
|
196
196
|
});
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
for (
|
|
197
|
+
let minX = points[0] || 0;
|
|
198
|
+
let maxX = points[0] || 0;
|
|
199
|
+
let minY = points[1] || 0;
|
|
200
|
+
let maxY = points[1] || 0;
|
|
201
|
+
let x, y;
|
|
202
|
+
for (let i = 0; i < points.length / 2; i++) {
|
|
203
203
|
x = points[i * 2];
|
|
204
204
|
y = points[i * 2 + 1];
|
|
205
205
|
minX = Math.min(minX, x);
|
|
@@ -207,7 +207,7 @@ class TextPath extends Shape_1.Shape {
|
|
|
207
207
|
minY = Math.min(minY, y);
|
|
208
208
|
maxY = Math.max(maxY, y);
|
|
209
209
|
}
|
|
210
|
-
|
|
210
|
+
const fontSize = this.fontSize();
|
|
211
211
|
return {
|
|
212
212
|
x: minX - fontSize / 2,
|
|
213
213
|
y: minY - fontSize / 2,
|