konva 10.0.10 → 10.0.11

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 (127) hide show
  1. package/konva.js +2 -2
  2. package/konva.min.js +2 -2
  3. package/lib/Animation.d.ts +24 -0
  4. package/lib/Animation.js +142 -0
  5. package/lib/BezierFunctions.d.ts +6 -0
  6. package/lib/BezierFunctions.js +784 -0
  7. package/lib/Canvas.d.ts +33 -0
  8. package/lib/Canvas.js +107 -0
  9. package/lib/Container.d.ts +54 -0
  10. package/lib/Container.js +316 -0
  11. package/lib/Context.d.ts +95 -0
  12. package/lib/Context.js +561 -0
  13. package/lib/Core.d.ts +3 -0
  14. package/lib/Core.js +3 -0
  15. package/lib/DragAndDrop.d.ts +17 -0
  16. package/lib/DragAndDrop.js +110 -0
  17. package/lib/Factory.d.ts +22 -0
  18. package/lib/Factory.js +123 -0
  19. package/lib/FastLayer.d.ts +4 -0
  20. package/lib/FastLayer.js +12 -0
  21. package/lib/Global.d.ts +37 -0
  22. package/lib/Global.js +66 -0
  23. package/lib/Group.d.ts +9 -0
  24. package/lib/Group.js +13 -0
  25. package/lib/Layer.d.ts +62 -0
  26. package/lib/Layer.js +305 -0
  27. package/lib/Node.d.ts +323 -0
  28. package/lib/Node.js +1532 -0
  29. package/lib/PointerEvents.d.ts +11 -0
  30. package/lib/PointerEvents.js +37 -0
  31. package/lib/Shape.d.ts +196 -0
  32. package/lib/Shape.js +555 -0
  33. package/lib/Stage.d.ts +86 -0
  34. package/lib/Stage.js +715 -0
  35. package/lib/Tween.d.ts +90 -0
  36. package/lib/Tween.js +531 -0
  37. package/lib/Util.d.ts +137 -0
  38. package/lib/Util.js +909 -0
  39. package/lib/Validators.d.ts +11 -0
  40. package/lib/Validators.js +178 -0
  41. package/lib/_CoreInternals.d.ts +224 -0
  42. package/lib/_CoreInternals.js +34 -0
  43. package/lib/_FullInternals.d.ts +306 -0
  44. package/lib/_FullInternals.js +80 -0
  45. package/lib/canvas-backend.d.ts +2 -0
  46. package/lib/canvas-backend.js +27 -0
  47. package/lib/filters/Blur.d.ts +2 -0
  48. package/lib/filters/Blur.js +235 -0
  49. package/lib/filters/Brighten.d.ts +2 -0
  50. package/lib/filters/Brighten.js +12 -0
  51. package/lib/filters/Brightness.d.ts +2 -0
  52. package/lib/filters/Brightness.js +8 -0
  53. package/lib/filters/Contrast.d.ts +2 -0
  54. package/lib/filters/Contrast.js +35 -0
  55. package/lib/filters/Emboss.d.ts +2 -0
  56. package/lib/filters/Emboss.js +101 -0
  57. package/lib/filters/Enhance.d.ts +2 -0
  58. package/lib/filters/Enhance.js +84 -0
  59. package/lib/filters/Grayscale.d.ts +2 -0
  60. package/lib/filters/Grayscale.js +9 -0
  61. package/lib/filters/HSL.d.ts +2 -0
  62. package/lib/filters/HSL.js +24 -0
  63. package/lib/filters/HSV.d.ts +2 -0
  64. package/lib/filters/HSV.js +23 -0
  65. package/lib/filters/Invert.d.ts +2 -0
  66. package/lib/filters/Invert.js +8 -0
  67. package/lib/filters/Kaleidoscope.d.ts +2 -0
  68. package/lib/filters/Kaleidoscope.js +133 -0
  69. package/lib/filters/Mask.d.ts +2 -0
  70. package/lib/filters/Mask.js +151 -0
  71. package/lib/filters/Noise.d.ts +2 -0
  72. package/lib/filters/Noise.js +12 -0
  73. package/lib/filters/Pixelate.d.ts +2 -0
  74. package/lib/filters/Pixelate.js +60 -0
  75. package/lib/filters/Posterize.d.ts +2 -0
  76. package/lib/filters/Posterize.js +10 -0
  77. package/lib/filters/RGB.d.ts +2 -0
  78. package/lib/filters/RGB.js +38 -0
  79. package/lib/filters/RGBA.d.ts +2 -0
  80. package/lib/filters/RGBA.js +49 -0
  81. package/lib/filters/Sepia.d.ts +2 -0
  82. package/lib/filters/Sepia.js +11 -0
  83. package/lib/filters/Solarize.d.ts +2 -0
  84. package/lib/filters/Solarize.js +14 -0
  85. package/lib/filters/Threshold.d.ts +2 -0
  86. package/lib/filters/Threshold.js +10 -0
  87. package/lib/index.d.ts +2 -0
  88. package/lib/index.js +2 -0
  89. package/lib/shapes/Arc.d.ts +27 -0
  90. package/lib/shapes/Arc.js +60 -0
  91. package/lib/shapes/Arrow.d.ts +27 -0
  92. package/lib/shapes/Arrow.js +99 -0
  93. package/lib/shapes/Circle.d.ts +15 -0
  94. package/lib/shapes/Circle.js +33 -0
  95. package/lib/shapes/Ellipse.d.ts +18 -0
  96. package/lib/shapes/Ellipse.js +37 -0
  97. package/lib/shapes/Image.d.ts +29 -0
  98. package/lib/shapes/Image.js +131 -0
  99. package/lib/shapes/Label.d.ts +39 -0
  100. package/lib/shapes/Label.js +173 -0
  101. package/lib/shapes/Line.d.ts +29 -0
  102. package/lib/shapes/Line.js +156 -0
  103. package/lib/shapes/Path.d.ts +50 -0
  104. package/lib/shapes/Path.js +670 -0
  105. package/lib/shapes/Rect.d.ts +11 -0
  106. package/lib/shapes/Rect.js +22 -0
  107. package/lib/shapes/RegularPolygon.d.ts +26 -0
  108. package/lib/shapes/RegularPolygon.js +72 -0
  109. package/lib/shapes/Ring.d.ts +17 -0
  110. package/lib/shapes/Ring.js +33 -0
  111. package/lib/shapes/Sprite.d.ts +32 -0
  112. package/lib/shapes/Sprite.js +108 -0
  113. package/lib/shapes/Star.d.ts +19 -0
  114. package/lib/shapes/Star.js +38 -0
  115. package/lib/shapes/Text.d.ts +90 -0
  116. package/lib/shapes/Text.js +461 -0
  117. package/lib/shapes/TextPath.d.ts +72 -0
  118. package/lib/shapes/TextPath.js +268 -0
  119. package/lib/shapes/Transformer.d.ts +130 -0
  120. package/lib/shapes/Transformer.js +1009 -0
  121. package/lib/shapes/Wedge.d.ts +19 -0
  122. package/lib/shapes/Wedge.js +38 -0
  123. package/lib/skia-backend.d.ts +2 -0
  124. package/lib/skia-backend.js +24 -0
  125. package/lib/types.d.ts +56 -0
  126. package/lib/types.js +21 -0
  127. package/package.json +1 -1
@@ -0,0 +1,268 @@
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
+ const 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._readDataAttribute();
21
+ this.on('dataChange.konva', function () {
22
+ this._readDataAttribute();
23
+ this._setTextData();
24
+ });
25
+ this.on('textChange.konva alignChange.konva letterSpacingChange.konva kerningFuncChange.konva fontSizeChange.konva fontFamilyChange.konva', this._setTextData);
26
+ this._setTextData();
27
+ }
28
+ _getTextPathLength() {
29
+ return Path.getPathLength(this.dataArray);
30
+ }
31
+ _getPointAtLength(length) {
32
+ if (!this.attrs.data) {
33
+ return null;
34
+ }
35
+ const totalLength = this.pathLength;
36
+ if (length > totalLength) {
37
+ return null;
38
+ }
39
+ return Path.getPointAtLengthOfDataArray(length, this.dataArray);
40
+ }
41
+ _readDataAttribute() {
42
+ this.dataArray = Path.parsePathData(this.attrs.data);
43
+ this.pathLength = this._getTextPathLength();
44
+ }
45
+ _sceneFunc(context) {
46
+ context.setAttr('font', this._getContextFont());
47
+ context.setAttr('textBaseline', this.textBaseline());
48
+ context.setAttr('textAlign', 'left');
49
+ context.save();
50
+ const textDecoration = this.textDecoration();
51
+ const fill = this.fill();
52
+ const fontSize = this.fontSize();
53
+ const glyphInfo = this.glyphInfo;
54
+ const hasUnderline = textDecoration.indexOf('underline') !== -1;
55
+ const hasLineThrough = textDecoration.indexOf('line-through') !== -1;
56
+ if (hasUnderline) {
57
+ context.beginPath();
58
+ }
59
+ for (let i = 0; i < glyphInfo.length; i++) {
60
+ context.save();
61
+ const p0 = glyphInfo[i].p0;
62
+ context.translate(p0.x, p0.y);
63
+ context.rotate(glyphInfo[i].rotation);
64
+ this.partialText = glyphInfo[i].text;
65
+ context.fillStrokeShape(this);
66
+ if (hasUnderline) {
67
+ if (i === 0) {
68
+ context.moveTo(0, fontSize / 2 + 1);
69
+ }
70
+ context.lineTo(glyphInfo[i].width, fontSize / 2 + 1);
71
+ }
72
+ context.restore();
73
+ }
74
+ if (hasUnderline) {
75
+ context.strokeStyle = fill;
76
+ context.lineWidth = fontSize / 20;
77
+ context.stroke();
78
+ }
79
+ if (hasLineThrough) {
80
+ context.beginPath();
81
+ for (let i = 0; i < glyphInfo.length; i++) {
82
+ context.save();
83
+ const p0 = glyphInfo[i].p0;
84
+ context.translate(p0.x, p0.y);
85
+ context.rotate(glyphInfo[i].rotation);
86
+ if (i === 0) {
87
+ context.moveTo(0, 0);
88
+ }
89
+ context.lineTo(glyphInfo[i].width, 0);
90
+ context.restore();
91
+ }
92
+ context.strokeStyle = fill;
93
+ context.lineWidth = fontSize / 20;
94
+ context.stroke();
95
+ }
96
+ context.restore();
97
+ }
98
+ _hitFunc(context) {
99
+ context.beginPath();
100
+ const glyphInfo = this.glyphInfo;
101
+ if (glyphInfo.length >= 1) {
102
+ const p0 = glyphInfo[0].p0;
103
+ context.moveTo(p0.x, p0.y);
104
+ }
105
+ for (let i = 0; i < glyphInfo.length; i++) {
106
+ const p1 = glyphInfo[i].p1;
107
+ context.lineTo(p1.x, p1.y);
108
+ }
109
+ context.setAttr('lineWidth', this.fontSize());
110
+ context.setAttr('strokeStyle', this.colorKey);
111
+ context.stroke();
112
+ }
113
+ getTextWidth() {
114
+ return this.textWidth;
115
+ }
116
+ getTextHeight() {
117
+ Util.warn('text.getTextHeight() method is deprecated. Use text.height() - for full height and text.fontSize() - for one line height.');
118
+ return this.textHeight;
119
+ }
120
+ setText(text) {
121
+ return Text.prototype.setText.call(this, text);
122
+ }
123
+ _getContextFont() {
124
+ return Text.prototype._getContextFont.call(this);
125
+ }
126
+ _getTextSize(text) {
127
+ const dummyCanvas = this.dummyCanvas;
128
+ const _context = dummyCanvas.getContext('2d');
129
+ _context.save();
130
+ _context.font = this._getContextFont();
131
+ const metrics = _context.measureText(text);
132
+ _context.restore();
133
+ return {
134
+ width: metrics.width,
135
+ height: parseInt(`${this.fontSize()}`, 10),
136
+ };
137
+ }
138
+ _setTextData() {
139
+ const charArr = stringToArray(this.text());
140
+ const chars = [];
141
+ let width = 0;
142
+ for (let i = 0; i < charArr.length; i++) {
143
+ chars.push({
144
+ char: charArr[i],
145
+ width: this._getTextSize(charArr[i]).width,
146
+ });
147
+ width += chars[i].width;
148
+ }
149
+ const { height } = this._getTextSize(this.attrs.text);
150
+ this.textWidth = width;
151
+ this.textHeight = height;
152
+ this.glyphInfo = [];
153
+ if (!this.attrs.data) {
154
+ return null;
155
+ }
156
+ const letterSpacing = this.letterSpacing();
157
+ const align = this.align();
158
+ const kerningFunc = this.kerningFunc();
159
+ const textWidth = Math.max(this.textWidth + ((this.attrs.text || '').length - 1) * letterSpacing, 0);
160
+ let offset = 0;
161
+ if (align === 'center') {
162
+ offset = Math.max(0, this.pathLength / 2 - textWidth / 2);
163
+ }
164
+ if (align === 'right') {
165
+ offset = Math.max(0, this.pathLength - textWidth);
166
+ }
167
+ let offsetToGlyph = offset;
168
+ for (let i = 0; i < chars.length; i++) {
169
+ const charStartPoint = this._getPointAtLength(offsetToGlyph);
170
+ if (!charStartPoint)
171
+ return;
172
+ const char = chars[i].char;
173
+ let glyphWidth = chars[i].width + letterSpacing;
174
+ if (char === ' ' && align === 'justify') {
175
+ const numberOfSpaces = this.text().split(' ').length - 1;
176
+ glyphWidth += (this.pathLength - textWidth) / numberOfSpaces;
177
+ }
178
+ const charEndPoint = this._getPointAtLength(offsetToGlyph + glyphWidth);
179
+ if (!charEndPoint) {
180
+ return;
181
+ }
182
+ const width = Path.getLineLength(charStartPoint.x, charStartPoint.y, charEndPoint.x, charEndPoint.y);
183
+ let kern = 0;
184
+ if (kerningFunc) {
185
+ try {
186
+ kern = kerningFunc(chars[i - 1].char, char) * this.fontSize();
187
+ }
188
+ catch (e) {
189
+ kern = 0;
190
+ }
191
+ }
192
+ charStartPoint.x += kern;
193
+ charEndPoint.x += kern;
194
+ this.textWidth += kern;
195
+ const midpoint = Path.getPointOnLine(kern + width / 2.0, charStartPoint.x, charStartPoint.y, charEndPoint.x, charEndPoint.y);
196
+ const rotation = Math.atan2(charEndPoint.y - charStartPoint.y, charEndPoint.x - charStartPoint.x);
197
+ this.glyphInfo.push({
198
+ transposeX: midpoint.x,
199
+ transposeY: midpoint.y,
200
+ text: charArr[i],
201
+ rotation: rotation,
202
+ p0: charStartPoint,
203
+ p1: charEndPoint,
204
+ width: width,
205
+ });
206
+ offsetToGlyph += glyphWidth;
207
+ }
208
+ }
209
+ getSelfRect() {
210
+ if (!this.glyphInfo.length) {
211
+ return {
212
+ x: 0,
213
+ y: 0,
214
+ width: 0,
215
+ height: 0,
216
+ };
217
+ }
218
+ const points = [];
219
+ this.glyphInfo.forEach(function (info) {
220
+ points.push(info.p0.x);
221
+ points.push(info.p0.y);
222
+ points.push(info.p1.x);
223
+ points.push(info.p1.y);
224
+ });
225
+ let minX = points[0] || 0;
226
+ let maxX = points[0] || 0;
227
+ let minY = points[1] || 0;
228
+ let maxY = points[1] || 0;
229
+ let x, y;
230
+ for (let i = 0; i < points.length / 2; i++) {
231
+ x = points[i * 2];
232
+ y = points[i * 2 + 1];
233
+ minX = Math.min(minX, x);
234
+ maxX = Math.max(maxX, x);
235
+ minY = Math.min(minY, y);
236
+ maxY = Math.max(maxY, y);
237
+ }
238
+ const fontSize = this.fontSize();
239
+ return {
240
+ x: minX - fontSize / 2,
241
+ y: minY - fontSize / 2,
242
+ width: maxX - minX + fontSize,
243
+ height: maxY - minY + fontSize,
244
+ };
245
+ }
246
+ destroy() {
247
+ Util.releaseCanvas(this.dummyCanvas);
248
+ return super.destroy();
249
+ }
250
+ }
251
+ TextPath.prototype._fillFunc = _fillFunc;
252
+ TextPath.prototype._strokeFunc = _strokeFunc;
253
+ TextPath.prototype._fillFuncHit = _fillFunc;
254
+ TextPath.prototype._strokeFuncHit = _strokeFunc;
255
+ TextPath.prototype.className = 'TextPath';
256
+ TextPath.prototype._attrsAffectingSize = ['text', 'fontSize', 'data'];
257
+ _registerNode(TextPath);
258
+ Factory.addGetterSetter(TextPath, 'data');
259
+ Factory.addGetterSetter(TextPath, 'fontFamily', 'Arial');
260
+ Factory.addGetterSetter(TextPath, 'fontSize', 12, getNumberValidator());
261
+ Factory.addGetterSetter(TextPath, 'fontStyle', NORMAL);
262
+ Factory.addGetterSetter(TextPath, 'align', 'left');
263
+ Factory.addGetterSetter(TextPath, 'letterSpacing', 0, getNumberValidator());
264
+ Factory.addGetterSetter(TextPath, 'textBaseline', 'middle');
265
+ Factory.addGetterSetter(TextPath, 'fontVariant', NORMAL);
266
+ Factory.addGetterSetter(TextPath, 'text', EMPTY_STRING);
267
+ Factory.addGetterSetter(TextPath, 'textDecoration', '');
268
+ Factory.addGetterSetter(TextPath, 'kerningFunc', undefined);
@@ -0,0 +1,130 @@
1
+ import { Transform } from '../Util.ts';
2
+ import { Node } from '../Node.ts';
3
+ import { Rect } from './Rect.ts';
4
+ import { Group } from '../Group.ts';
5
+ import type { ContainerConfig } from '../Container.ts';
6
+ import type { GetSet, IRect, Vector2d } from '../types.ts';
7
+ export interface Box extends IRect {
8
+ rotation: number;
9
+ }
10
+ export interface TransformerConfig extends ContainerConfig {
11
+ resizeEnabled?: boolean;
12
+ rotateEnabled?: boolean;
13
+ rotateLineVisible?: boolean;
14
+ rotationSnaps?: Array<number>;
15
+ rotationSnapTolerance?: number;
16
+ rotateAnchorOffset?: number;
17
+ rotateAnchorCursor?: string;
18
+ borderEnabled?: boolean;
19
+ borderStroke?: string;
20
+ borderStrokeWidth?: number;
21
+ borderDash?: Array<number>;
22
+ anchorFill?: string;
23
+ anchorStroke?: string;
24
+ anchorStrokeWidth?: number;
25
+ anchorSize?: number;
26
+ anchorCornerRadius?: number;
27
+ keepRatio?: boolean;
28
+ shiftBehavior?: string;
29
+ centeredScaling?: boolean;
30
+ enabledAnchors?: Array<string>;
31
+ flipEnabled?: boolean;
32
+ node?: Rect;
33
+ ignoreStroke?: boolean;
34
+ boundBoxFunc?: (oldBox: Box, newBox: Box) => Box;
35
+ useSingleNodeRotation?: boolean;
36
+ shouldOverdrawWholeArea?: boolean;
37
+ anchorDragBoundFunc?: (oldPos: Vector2d, newPos: Vector2d, evt: any) => Vector2d;
38
+ anchorStyleFunc?: (anchor: Rect) => void;
39
+ }
40
+ export declare class Transformer extends Group {
41
+ _nodes: Array<Node>;
42
+ _movingAnchorName: string | null;
43
+ _transforming: boolean;
44
+ _anchorDragOffset: Vector2d;
45
+ sin: number;
46
+ cos: number;
47
+ _cursorChange: boolean;
48
+ static isTransforming: () => boolean;
49
+ constructor(config?: TransformerConfig);
50
+ attachTo(node: Node): this;
51
+ setNode(node: Node): this;
52
+ getNode(): Node<import("../Node.ts").NodeConfig>;
53
+ _getEventNamespace(): string;
54
+ setNodes(nodes?: Array<Node>): this;
55
+ _proxyDrag(node: Node): void;
56
+ getNodes(): Node<import("../Node.ts").NodeConfig>[];
57
+ getActiveAnchor(): string | null;
58
+ detach(): void;
59
+ _resetTransformCache(): void;
60
+ _getNodeRect(): any;
61
+ __getNodeShape(node: Node, rot?: number, relative?: Node): {
62
+ rotation: number;
63
+ x: number;
64
+ y: number;
65
+ width: number;
66
+ height: number;
67
+ };
68
+ __getNodeRect(): {
69
+ x: number;
70
+ y: number;
71
+ width: number;
72
+ height: number;
73
+ rotation: number;
74
+ };
75
+ getX(): any;
76
+ getY(): any;
77
+ getWidth(): any;
78
+ getHeight(): any;
79
+ _createElements(): void;
80
+ _createAnchor(name: any): void;
81
+ _createBack(): void;
82
+ _handleMouseDown(e: any): void;
83
+ _handleMouseMove(e: any): void;
84
+ _handleMouseUp(e: any): void;
85
+ getAbsoluteTransform(): Transform;
86
+ _removeEvents(e?: any): void;
87
+ _fitNodesInto(newAttrs: any, evt?: any): void;
88
+ forceUpdate(): void;
89
+ _batchChangeChild(selector: string, attrs: any): void;
90
+ update(): void;
91
+ isTransforming(): boolean;
92
+ stopTransform(): void;
93
+ destroy(): this;
94
+ toObject(): {
95
+ attrs: any;
96
+ className: string;
97
+ children?: Array<any>;
98
+ };
99
+ clone(obj?: any): this;
100
+ getClientRect(): IRect;
101
+ nodes: GetSet<Node[], this>;
102
+ enabledAnchors: GetSet<string[], this>;
103
+ rotationSnaps: GetSet<number[], this>;
104
+ anchorSize: GetSet<number, this>;
105
+ resizeEnabled: GetSet<boolean, this>;
106
+ rotateEnabled: GetSet<boolean, this>;
107
+ rotateLineVisible: GetSet<boolean, this>;
108
+ rotateAnchorOffset: GetSet<number, this>;
109
+ rotationSnapTolerance: GetSet<number, this>;
110
+ rotateAnchorCursor: GetSet<string, this>;
111
+ padding: GetSet<number, this>;
112
+ borderEnabled: GetSet<boolean, this>;
113
+ borderStroke: GetSet<string, this>;
114
+ borderStrokeWidth: GetSet<number, this>;
115
+ borderDash: GetSet<number[], this>;
116
+ anchorFill: GetSet<string, this>;
117
+ anchorStroke: GetSet<string, this>;
118
+ anchorCornerRadius: GetSet<number, this>;
119
+ anchorStrokeWidth: GetSet<number, this>;
120
+ keepRatio: GetSet<boolean, this>;
121
+ shiftBehavior: GetSet<string, this>;
122
+ centeredScaling: GetSet<boolean, this>;
123
+ flipEnabled: GetSet<boolean, this>;
124
+ ignoreStroke: GetSet<boolean, this>;
125
+ boundBoxFunc: GetSet<(oldBox: Box, newBox: Box) => Box, this>;
126
+ anchorDragBoundFunc: GetSet<(oldPos: Vector2d, newPos: Vector2d, e: MouseEvent) => Vector2d, this>;
127
+ anchorStyleFunc: GetSet<null | ((Node: Rect) => void), this>;
128
+ shouldOverdrawWholeArea: GetSet<boolean, this>;
129
+ useSingleNodeRotation: GetSet<boolean, this>;
130
+ }