@nasser-sw/fabric 7.0.1-beta7 → 7.0.1-beta9
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/debug/konva-master/CHANGELOG.md +1475 -0
- package/debug/konva-master/LICENSE +22 -0
- package/debug/konva-master/README.md +209 -0
- package/debug/konva-master/gulpfile.mjs +110 -0
- package/debug/konva-master/package.json +139 -0
- package/debug/konva-master/release.sh +62 -0
- package/debug/konva-master/resources/doc-includes/ContainerParams.txt +6 -0
- package/debug/konva-master/resources/doc-includes/NodeParams.txt +20 -0
- package/debug/konva-master/resources/doc-includes/ShapeParams.txt +53 -0
- package/debug/konva-master/resources/jsdoc.conf.json +28 -0
- package/debug/konva-master/rollup.config.mjs +32 -0
- package/debug/konva-master/src/Animation.ts +237 -0
- package/debug/konva-master/src/BezierFunctions.ts +826 -0
- package/debug/konva-master/src/Canvas.ts +230 -0
- package/debug/konva-master/src/Container.ts +649 -0
- package/debug/konva-master/src/Context.ts +1017 -0
- package/debug/konva-master/src/Core.ts +5 -0
- package/debug/konva-master/src/DragAndDrop.ts +173 -0
- package/debug/konva-master/src/Factory.ts +246 -0
- package/debug/konva-master/src/FastLayer.ts +29 -0
- package/debug/konva-master/src/Global.ts +210 -0
- package/debug/konva-master/src/Group.ts +31 -0
- package/debug/konva-master/src/Layer.ts +546 -0
- package/debug/konva-master/src/Node.ts +3477 -0
- package/debug/konva-master/src/PointerEvents.ts +67 -0
- package/debug/konva-master/src/Shape.ts +2081 -0
- package/debug/konva-master/src/Stage.ts +1000 -0
- package/debug/konva-master/src/Tween.ts +811 -0
- package/debug/konva-master/src/Util.ts +1123 -0
- package/debug/konva-master/src/Validators.ts +210 -0
- package/debug/konva-master/src/_CoreInternals.ts +85 -0
- package/debug/konva-master/src/_FullInternals.ts +171 -0
- package/debug/konva-master/src/canvas-backend.ts +36 -0
- package/debug/konva-master/src/filters/Blur.ts +388 -0
- package/debug/konva-master/src/filters/Brighten.ts +48 -0
- package/debug/konva-master/src/filters/Brightness.ts +30 -0
- package/debug/konva-master/src/filters/Contrast.ts +75 -0
- package/debug/konva-master/src/filters/Emboss.ts +207 -0
- package/debug/konva-master/src/filters/Enhance.ts +154 -0
- package/debug/konva-master/src/filters/Grayscale.ts +25 -0
- package/debug/konva-master/src/filters/HSL.ts +108 -0
- package/debug/konva-master/src/filters/HSV.ts +106 -0
- package/debug/konva-master/src/filters/Invert.ts +23 -0
- package/debug/konva-master/src/filters/Kaleidoscope.ts +274 -0
- package/debug/konva-master/src/filters/Mask.ts +220 -0
- package/debug/konva-master/src/filters/Noise.ts +44 -0
- package/debug/konva-master/src/filters/Pixelate.ts +107 -0
- package/debug/konva-master/src/filters/Posterize.ts +46 -0
- package/debug/konva-master/src/filters/RGB.ts +82 -0
- package/debug/konva-master/src/filters/RGBA.ts +103 -0
- package/debug/konva-master/src/filters/Sepia.ts +27 -0
- package/debug/konva-master/src/filters/Solarize.ts +29 -0
- package/debug/konva-master/src/filters/Threshold.ts +44 -0
- package/debug/konva-master/src/index.ts +3 -0
- package/debug/konva-master/src/shapes/Arc.ts +176 -0
- package/debug/konva-master/src/shapes/Arrow.ts +231 -0
- package/debug/konva-master/src/shapes/Circle.ts +76 -0
- package/debug/konva-master/src/shapes/Ellipse.ts +121 -0
- package/debug/konva-master/src/shapes/Image.ts +319 -0
- package/debug/konva-master/src/shapes/Label.ts +386 -0
- package/debug/konva-master/src/shapes/Line.ts +364 -0
- package/debug/konva-master/src/shapes/Path.ts +1013 -0
- package/debug/konva-master/src/shapes/Rect.ts +79 -0
- package/debug/konva-master/src/shapes/RegularPolygon.ts +167 -0
- package/debug/konva-master/src/shapes/Ring.ts +94 -0
- package/debug/konva-master/src/shapes/Sprite.ts +370 -0
- package/debug/konva-master/src/shapes/Star.ts +125 -0
- package/debug/konva-master/src/shapes/Text.ts +1065 -0
- package/debug/konva-master/src/shapes/TextPath.ts +583 -0
- package/debug/konva-master/src/shapes/Transformer.ts +1889 -0
- package/debug/konva-master/src/shapes/Wedge.ts +129 -0
- package/debug/konva-master/src/skia-backend.ts +35 -0
- package/debug/konva-master/src/types.ts +84 -0
- package/debug/konva-master/tsconfig.json +31 -0
- package/debug/konva-master/tsconfig.test.json +7 -0
- package/dist/index.js +977 -29
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.min.mjs +1 -1
- package/dist/index.min.mjs.map +1 -1
- package/dist/index.mjs +977 -29
- package/dist/index.mjs.map +1 -1
- package/dist/index.node.cjs +977 -29
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +977 -29
- package/dist/index.node.mjs.map +1 -1
- package/dist/package.json.min.mjs +1 -1
- package/dist/package.json.mjs +1 -1
- package/dist/src/shapes/Line.d.ts +1 -0
- package/dist/src/shapes/Line.d.ts.map +1 -1
- package/dist/src/shapes/Line.min.mjs +1 -1
- package/dist/src/shapes/Line.min.mjs.map +1 -1
- package/dist/src/shapes/Line.mjs +63 -6
- package/dist/src/shapes/Line.mjs.map +1 -1
- package/dist/src/shapes/Text/Text.d.ts +19 -0
- package/dist/src/shapes/Text/Text.d.ts.map +1 -1
- package/dist/src/shapes/Text/Text.min.mjs +1 -1
- package/dist/src/shapes/Text/Text.min.mjs.map +1 -1
- package/dist/src/shapes/Text/Text.mjs +238 -4
- package/dist/src/shapes/Text/Text.mjs.map +1 -1
- package/dist/src/shapes/Textbox.d.ts +38 -1
- package/dist/src/shapes/Textbox.d.ts.map +1 -1
- package/dist/src/shapes/Textbox.min.mjs +1 -1
- package/dist/src/shapes/Textbox.min.mjs.map +1 -1
- package/dist/src/shapes/Textbox.mjs +497 -15
- package/dist/src/shapes/Textbox.mjs.map +1 -1
- package/dist/src/text/examples/arabicTextExample.d.ts +60 -0
- package/dist/src/text/examples/arabicTextExample.d.ts.map +1 -0
- package/dist/src/text/measure.d.ts +9 -0
- package/dist/src/text/measure.d.ts.map +1 -1
- package/dist/src/text/measure.min.mjs +1 -1
- package/dist/src/text/measure.min.mjs.map +1 -1
- package/dist/src/text/measure.mjs +175 -4
- package/dist/src/text/measure.mjs.map +1 -1
- package/dist/src/text/overlayEditor.d.ts.map +1 -1
- package/dist/src/text/overlayEditor.min.mjs +1 -1
- package/dist/src/text/overlayEditor.min.mjs.map +1 -1
- package/dist/src/text/overlayEditor.mjs +7 -0
- package/dist/src/text/overlayEditor.mjs.map +1 -1
- package/dist/src/text/scriptUtils.d.ts +142 -0
- package/dist/src/text/scriptUtils.d.ts.map +1 -0
- package/dist/src/text/scriptUtils.min.mjs +2 -0
- package/dist/src/text/scriptUtils.min.mjs.map +1 -0
- package/dist/src/text/scriptUtils.mjs +212 -0
- package/dist/src/text/scriptUtils.mjs.map +1 -0
- package/dist-extensions/src/shapes/Line.d.ts +1 -0
- package/dist-extensions/src/shapes/Line.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Text/Text.d.ts +19 -0
- package/dist-extensions/src/shapes/Text/Text.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Textbox.d.ts +38 -1
- package/dist-extensions/src/shapes/Textbox.d.ts.map +1 -1
- package/dist-extensions/src/text/measure.d.ts +9 -0
- package/dist-extensions/src/text/measure.d.ts.map +1 -1
- package/dist-extensions/src/text/overlayEditor.d.ts.map +1 -1
- package/dist-extensions/src/text/scriptUtils.d.ts +142 -0
- package/dist-extensions/src/text/scriptUtils.d.ts.map +1 -0
- package/fabric-test-editor.html +2401 -46
- package/fabric-test2.html +43 -0
- package/fonts/STV Bold.ttf +0 -0
- package/fonts/STV Light.ttf +0 -0
- package/fonts/STV Regular.ttf +0 -0
- package/package.json +1 -1
- package/src/shapes/Line.ts +132 -46
- package/src/shapes/Text/Text.ts +238 -5
- package/src/shapes/Textbox.ts +521 -11
- package/src/text/measure.ts +200 -50
- package/src/text/overlayEditor.ts +7 -0
|
@@ -0,0 +1,1123 @@
|
|
|
1
|
+
import { Konva } from './Global.ts';
|
|
2
|
+
import type { Context } from './Context.ts';
|
|
3
|
+
import type { IRect, RGB, Vector2d } from './types.ts';
|
|
4
|
+
|
|
5
|
+
const NODE_ERROR = `Konva.js unsupported environment.
|
|
6
|
+
|
|
7
|
+
Looks like you are trying to use Konva.js in Node.js environment. because "document" object is undefined.
|
|
8
|
+
|
|
9
|
+
To use Konva.js in Node.js environment, you need to use the "canvas-backend" or "skia-backend" module.
|
|
10
|
+
|
|
11
|
+
bash: npm install canvas
|
|
12
|
+
js: import "konva/canvas-backend";
|
|
13
|
+
|
|
14
|
+
or
|
|
15
|
+
|
|
16
|
+
bash: npm install skia-canvas
|
|
17
|
+
js: import "konva/skia-backend";
|
|
18
|
+
`;
|
|
19
|
+
const ensureBrowser = () => {
|
|
20
|
+
if (typeof document === 'undefined') {
|
|
21
|
+
throw new Error(NODE_ERROR);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/*
|
|
26
|
+
* Last updated November 2011
|
|
27
|
+
* By Simon Sarris
|
|
28
|
+
* www.simonsarris.com
|
|
29
|
+
* sarris@acm.org
|
|
30
|
+
*
|
|
31
|
+
* Free to use and distribute at will
|
|
32
|
+
* So long as you are nice to people, etc
|
|
33
|
+
*/
|
|
34
|
+
/*
|
|
35
|
+
* The usage of this class was inspired by some of the work done by a forked
|
|
36
|
+
* project, KineticJS-Ext by Wappworks, which is based on Simon's Transform
|
|
37
|
+
* class. Modified by Eric Rowell
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Transform constructor.
|
|
42
|
+
* In most of the cases you don't need to use it in your app. Because it is for internal usage in Konva core.
|
|
43
|
+
* But there is a documentation for that class in case you still want
|
|
44
|
+
* to make some manual calculations.
|
|
45
|
+
* @constructor
|
|
46
|
+
* @param {Array} [m] Optional six-element matrix
|
|
47
|
+
* @memberof Konva
|
|
48
|
+
*/
|
|
49
|
+
export class Transform {
|
|
50
|
+
m: Array<number>;
|
|
51
|
+
dirty = false;
|
|
52
|
+
constructor(m = [1, 0, 0, 1, 0, 0]) {
|
|
53
|
+
this.m = (m && m.slice()) || [1, 0, 0, 1, 0, 0];
|
|
54
|
+
}
|
|
55
|
+
reset() {
|
|
56
|
+
this.m[0] = 1;
|
|
57
|
+
this.m[1] = 0;
|
|
58
|
+
this.m[2] = 0;
|
|
59
|
+
this.m[3] = 1;
|
|
60
|
+
this.m[4] = 0;
|
|
61
|
+
this.m[5] = 0;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Copy Konva.Transform object
|
|
65
|
+
* @method
|
|
66
|
+
* @name Konva.Transform#copy
|
|
67
|
+
* @returns {Konva.Transform}
|
|
68
|
+
* @example
|
|
69
|
+
* const tr = shape.getTransform().copy()
|
|
70
|
+
*/
|
|
71
|
+
copy() {
|
|
72
|
+
return new Transform(this.m);
|
|
73
|
+
}
|
|
74
|
+
copyInto(tr: Transform) {
|
|
75
|
+
tr.m[0] = this.m[0];
|
|
76
|
+
tr.m[1] = this.m[1];
|
|
77
|
+
tr.m[2] = this.m[2];
|
|
78
|
+
tr.m[3] = this.m[3];
|
|
79
|
+
tr.m[4] = this.m[4];
|
|
80
|
+
tr.m[5] = this.m[5];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Transform point
|
|
84
|
+
* @method
|
|
85
|
+
* @name Konva.Transform#point
|
|
86
|
+
* @param {Object} point 2D point(x, y)
|
|
87
|
+
* @returns {Object} 2D point(x, y)
|
|
88
|
+
*/
|
|
89
|
+
point(point: Vector2d) {
|
|
90
|
+
const m = this.m;
|
|
91
|
+
return {
|
|
92
|
+
x: m[0] * point.x + m[2] * point.y + m[4],
|
|
93
|
+
y: m[1] * point.x + m[3] * point.y + m[5],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Apply translation
|
|
98
|
+
* @method
|
|
99
|
+
* @name Konva.Transform#translate
|
|
100
|
+
* @param {Number} x
|
|
101
|
+
* @param {Number} y
|
|
102
|
+
* @returns {Konva.Transform}
|
|
103
|
+
*/
|
|
104
|
+
translate(x: number, y: number) {
|
|
105
|
+
this.m[4] += this.m[0] * x + this.m[2] * y;
|
|
106
|
+
this.m[5] += this.m[1] * x + this.m[3] * y;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Apply scale
|
|
111
|
+
* @method
|
|
112
|
+
* @name Konva.Transform#scale
|
|
113
|
+
* @param {Number} sx
|
|
114
|
+
* @param {Number} sy
|
|
115
|
+
* @returns {Konva.Transform}
|
|
116
|
+
*/
|
|
117
|
+
scale(sx: number, sy: number) {
|
|
118
|
+
this.m[0] *= sx;
|
|
119
|
+
this.m[1] *= sx;
|
|
120
|
+
this.m[2] *= sy;
|
|
121
|
+
this.m[3] *= sy;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Apply rotation
|
|
126
|
+
* @method
|
|
127
|
+
* @name Konva.Transform#rotate
|
|
128
|
+
* @param {Number} rad Angle in radians
|
|
129
|
+
* @returns {Konva.Transform}
|
|
130
|
+
*/
|
|
131
|
+
rotate(rad: number) {
|
|
132
|
+
const c = Math.cos(rad);
|
|
133
|
+
const s = Math.sin(rad);
|
|
134
|
+
const m11 = this.m[0] * c + this.m[2] * s;
|
|
135
|
+
const m12 = this.m[1] * c + this.m[3] * s;
|
|
136
|
+
const m21 = this.m[0] * -s + this.m[2] * c;
|
|
137
|
+
const m22 = this.m[1] * -s + this.m[3] * c;
|
|
138
|
+
this.m[0] = m11;
|
|
139
|
+
this.m[1] = m12;
|
|
140
|
+
this.m[2] = m21;
|
|
141
|
+
this.m[3] = m22;
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Returns the translation
|
|
146
|
+
* @method
|
|
147
|
+
* @name Konva.Transform#getTranslation
|
|
148
|
+
* @returns {Object} 2D point(x, y)
|
|
149
|
+
*/
|
|
150
|
+
getTranslation() {
|
|
151
|
+
return {
|
|
152
|
+
x: this.m[4],
|
|
153
|
+
y: this.m[5],
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Apply skew
|
|
158
|
+
* @method
|
|
159
|
+
* @name Konva.Transform#skew
|
|
160
|
+
* @param {Number} sx
|
|
161
|
+
* @param {Number} sy
|
|
162
|
+
* @returns {Konva.Transform}
|
|
163
|
+
*/
|
|
164
|
+
skew(sx: number, sy: number) {
|
|
165
|
+
const m11 = this.m[0] + this.m[2] * sy;
|
|
166
|
+
const m12 = this.m[1] + this.m[3] * sy;
|
|
167
|
+
const m21 = this.m[2] + this.m[0] * sx;
|
|
168
|
+
const m22 = this.m[3] + this.m[1] * sx;
|
|
169
|
+
this.m[0] = m11;
|
|
170
|
+
this.m[1] = m12;
|
|
171
|
+
this.m[2] = m21;
|
|
172
|
+
this.m[3] = m22;
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Transform multiplication
|
|
177
|
+
* @method
|
|
178
|
+
* @name Konva.Transform#multiply
|
|
179
|
+
* @param {Konva.Transform} matrix
|
|
180
|
+
* @returns {Konva.Transform}
|
|
181
|
+
*/
|
|
182
|
+
multiply(matrix: Transform) {
|
|
183
|
+
const m11 = this.m[0] * matrix.m[0] + this.m[2] * matrix.m[1];
|
|
184
|
+
const m12 = this.m[1] * matrix.m[0] + this.m[3] * matrix.m[1];
|
|
185
|
+
|
|
186
|
+
const m21 = this.m[0] * matrix.m[2] + this.m[2] * matrix.m[3];
|
|
187
|
+
const m22 = this.m[1] * matrix.m[2] + this.m[3] * matrix.m[3];
|
|
188
|
+
|
|
189
|
+
const dx = this.m[0] * matrix.m[4] + this.m[2] * matrix.m[5] + this.m[4];
|
|
190
|
+
const dy = this.m[1] * matrix.m[4] + this.m[3] * matrix.m[5] + this.m[5];
|
|
191
|
+
|
|
192
|
+
this.m[0] = m11;
|
|
193
|
+
this.m[1] = m12;
|
|
194
|
+
this.m[2] = m21;
|
|
195
|
+
this.m[3] = m22;
|
|
196
|
+
this.m[4] = dx;
|
|
197
|
+
this.m[5] = dy;
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Invert the matrix
|
|
202
|
+
* @method
|
|
203
|
+
* @name Konva.Transform#invert
|
|
204
|
+
* @returns {Konva.Transform}
|
|
205
|
+
*/
|
|
206
|
+
invert() {
|
|
207
|
+
const d = 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
|
|
208
|
+
const m0 = this.m[3] * d;
|
|
209
|
+
const m1 = -this.m[1] * d;
|
|
210
|
+
const m2 = -this.m[2] * d;
|
|
211
|
+
const m3 = this.m[0] * d;
|
|
212
|
+
const m4 = d * (this.m[2] * this.m[5] - this.m[3] * this.m[4]);
|
|
213
|
+
const m5 = d * (this.m[1] * this.m[4] - this.m[0] * this.m[5]);
|
|
214
|
+
this.m[0] = m0;
|
|
215
|
+
this.m[1] = m1;
|
|
216
|
+
this.m[2] = m2;
|
|
217
|
+
this.m[3] = m3;
|
|
218
|
+
this.m[4] = m4;
|
|
219
|
+
this.m[5] = m5;
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* return matrix
|
|
224
|
+
* @method
|
|
225
|
+
* @name Konva.Transform#getMatrix
|
|
226
|
+
*/
|
|
227
|
+
getMatrix() {
|
|
228
|
+
return this.m;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* convert transformation matrix back into node's attributes
|
|
232
|
+
* @method
|
|
233
|
+
* @name Konva.Transform#decompose
|
|
234
|
+
* @returns {Konva.Transform}
|
|
235
|
+
*/
|
|
236
|
+
decompose() {
|
|
237
|
+
const a = this.m[0];
|
|
238
|
+
const b = this.m[1];
|
|
239
|
+
const c = this.m[2];
|
|
240
|
+
const d = this.m[3];
|
|
241
|
+
const e = this.m[4];
|
|
242
|
+
const f = this.m[5];
|
|
243
|
+
|
|
244
|
+
const delta = a * d - b * c;
|
|
245
|
+
|
|
246
|
+
const result = {
|
|
247
|
+
x: e,
|
|
248
|
+
y: f,
|
|
249
|
+
rotation: 0,
|
|
250
|
+
scaleX: 0,
|
|
251
|
+
scaleY: 0,
|
|
252
|
+
skewX: 0,
|
|
253
|
+
skewY: 0,
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// Apply the QR-like decomposition.
|
|
257
|
+
if (a != 0 || b != 0) {
|
|
258
|
+
const r = Math.sqrt(a * a + b * b);
|
|
259
|
+
result.rotation = b > 0 ? Math.acos(a / r) : -Math.acos(a / r);
|
|
260
|
+
result.scaleX = r;
|
|
261
|
+
result.scaleY = delta / r;
|
|
262
|
+
result.skewX = (a * c + b * d) / delta;
|
|
263
|
+
result.skewY = 0;
|
|
264
|
+
} else if (c != 0 || d != 0) {
|
|
265
|
+
const s = Math.sqrt(c * c + d * d);
|
|
266
|
+
result.rotation =
|
|
267
|
+
Math.PI / 2 - (d > 0 ? Math.acos(-c / s) : -Math.acos(c / s));
|
|
268
|
+
result.scaleX = delta / s;
|
|
269
|
+
result.scaleY = s;
|
|
270
|
+
result.skewX = 0;
|
|
271
|
+
result.skewY = (a * c + b * d) / delta;
|
|
272
|
+
} else {
|
|
273
|
+
// a = b = c = d = 0
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
result.rotation = Util._getRotation(result.rotation);
|
|
277
|
+
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// CONSTANTS
|
|
283
|
+
const OBJECT_ARRAY = '[object Array]',
|
|
284
|
+
OBJECT_NUMBER = '[object Number]',
|
|
285
|
+
OBJECT_STRING = '[object String]',
|
|
286
|
+
OBJECT_BOOLEAN = '[object Boolean]',
|
|
287
|
+
PI_OVER_DEG180 = Math.PI / 180,
|
|
288
|
+
DEG180_OVER_PI = 180 / Math.PI,
|
|
289
|
+
HASH = '#',
|
|
290
|
+
EMPTY_STRING = '',
|
|
291
|
+
ZERO = '0',
|
|
292
|
+
KONVA_WARNING = 'Konva warning: ',
|
|
293
|
+
KONVA_ERROR = 'Konva error: ',
|
|
294
|
+
RGB_PAREN = 'rgb(',
|
|
295
|
+
COLORS = {
|
|
296
|
+
aliceblue: [240, 248, 255],
|
|
297
|
+
antiquewhite: [250, 235, 215],
|
|
298
|
+
aqua: [0, 255, 255],
|
|
299
|
+
aquamarine: [127, 255, 212],
|
|
300
|
+
azure: [240, 255, 255],
|
|
301
|
+
beige: [245, 245, 220],
|
|
302
|
+
bisque: [255, 228, 196],
|
|
303
|
+
black: [0, 0, 0],
|
|
304
|
+
blanchedalmond: [255, 235, 205],
|
|
305
|
+
blue: [0, 0, 255],
|
|
306
|
+
blueviolet: [138, 43, 226],
|
|
307
|
+
brown: [165, 42, 42],
|
|
308
|
+
burlywood: [222, 184, 135],
|
|
309
|
+
cadetblue: [95, 158, 160],
|
|
310
|
+
chartreuse: [127, 255, 0],
|
|
311
|
+
chocolate: [210, 105, 30],
|
|
312
|
+
coral: [255, 127, 80],
|
|
313
|
+
cornflowerblue: [100, 149, 237],
|
|
314
|
+
cornsilk: [255, 248, 220],
|
|
315
|
+
crimson: [220, 20, 60],
|
|
316
|
+
cyan: [0, 255, 255],
|
|
317
|
+
darkblue: [0, 0, 139],
|
|
318
|
+
darkcyan: [0, 139, 139],
|
|
319
|
+
darkgoldenrod: [184, 132, 11],
|
|
320
|
+
darkgray: [169, 169, 169],
|
|
321
|
+
darkgreen: [0, 100, 0],
|
|
322
|
+
darkgrey: [169, 169, 169],
|
|
323
|
+
darkkhaki: [189, 183, 107],
|
|
324
|
+
darkmagenta: [139, 0, 139],
|
|
325
|
+
darkolivegreen: [85, 107, 47],
|
|
326
|
+
darkorange: [255, 140, 0],
|
|
327
|
+
darkorchid: [153, 50, 204],
|
|
328
|
+
darkred: [139, 0, 0],
|
|
329
|
+
darksalmon: [233, 150, 122],
|
|
330
|
+
darkseagreen: [143, 188, 143],
|
|
331
|
+
darkslateblue: [72, 61, 139],
|
|
332
|
+
darkslategray: [47, 79, 79],
|
|
333
|
+
darkslategrey: [47, 79, 79],
|
|
334
|
+
darkturquoise: [0, 206, 209],
|
|
335
|
+
darkviolet: [148, 0, 211],
|
|
336
|
+
deeppink: [255, 20, 147],
|
|
337
|
+
deepskyblue: [0, 191, 255],
|
|
338
|
+
dimgray: [105, 105, 105],
|
|
339
|
+
dimgrey: [105, 105, 105],
|
|
340
|
+
dodgerblue: [30, 144, 255],
|
|
341
|
+
firebrick: [178, 34, 34],
|
|
342
|
+
floralwhite: [255, 255, 240],
|
|
343
|
+
forestgreen: [34, 139, 34],
|
|
344
|
+
fuchsia: [255, 0, 255],
|
|
345
|
+
gainsboro: [220, 220, 220],
|
|
346
|
+
ghostwhite: [248, 248, 255],
|
|
347
|
+
gold: [255, 215, 0],
|
|
348
|
+
goldenrod: [218, 165, 32],
|
|
349
|
+
gray: [128, 128, 128],
|
|
350
|
+
green: [0, 128, 0],
|
|
351
|
+
greenyellow: [173, 255, 47],
|
|
352
|
+
grey: [128, 128, 128],
|
|
353
|
+
honeydew: [240, 255, 240],
|
|
354
|
+
hotpink: [255, 105, 180],
|
|
355
|
+
indianred: [205, 92, 92],
|
|
356
|
+
indigo: [75, 0, 130],
|
|
357
|
+
ivory: [255, 255, 240],
|
|
358
|
+
khaki: [240, 230, 140],
|
|
359
|
+
lavender: [230, 230, 250],
|
|
360
|
+
lavenderblush: [255, 240, 245],
|
|
361
|
+
lawngreen: [124, 252, 0],
|
|
362
|
+
lemonchiffon: [255, 250, 205],
|
|
363
|
+
lightblue: [173, 216, 230],
|
|
364
|
+
lightcoral: [240, 128, 128],
|
|
365
|
+
lightcyan: [224, 255, 255],
|
|
366
|
+
lightgoldenrodyellow: [250, 250, 210],
|
|
367
|
+
lightgray: [211, 211, 211],
|
|
368
|
+
lightgreen: [144, 238, 144],
|
|
369
|
+
lightgrey: [211, 211, 211],
|
|
370
|
+
lightpink: [255, 182, 193],
|
|
371
|
+
lightsalmon: [255, 160, 122],
|
|
372
|
+
lightseagreen: [32, 178, 170],
|
|
373
|
+
lightskyblue: [135, 206, 250],
|
|
374
|
+
lightslategray: [119, 136, 153],
|
|
375
|
+
lightslategrey: [119, 136, 153],
|
|
376
|
+
lightsteelblue: [176, 196, 222],
|
|
377
|
+
lightyellow: [255, 255, 224],
|
|
378
|
+
lime: [0, 255, 0],
|
|
379
|
+
limegreen: [50, 205, 50],
|
|
380
|
+
linen: [250, 240, 230],
|
|
381
|
+
magenta: [255, 0, 255],
|
|
382
|
+
maroon: [128, 0, 0],
|
|
383
|
+
mediumaquamarine: [102, 205, 170],
|
|
384
|
+
mediumblue: [0, 0, 205],
|
|
385
|
+
mediumorchid: [186, 85, 211],
|
|
386
|
+
mediumpurple: [147, 112, 219],
|
|
387
|
+
mediumseagreen: [60, 179, 113],
|
|
388
|
+
mediumslateblue: [123, 104, 238],
|
|
389
|
+
mediumspringgreen: [0, 250, 154],
|
|
390
|
+
mediumturquoise: [72, 209, 204],
|
|
391
|
+
mediumvioletred: [199, 21, 133],
|
|
392
|
+
midnightblue: [25, 25, 112],
|
|
393
|
+
mintcream: [245, 255, 250],
|
|
394
|
+
mistyrose: [255, 228, 225],
|
|
395
|
+
moccasin: [255, 228, 181],
|
|
396
|
+
navajowhite: [255, 222, 173],
|
|
397
|
+
navy: [0, 0, 128],
|
|
398
|
+
oldlace: [253, 245, 230],
|
|
399
|
+
olive: [128, 128, 0],
|
|
400
|
+
olivedrab: [107, 142, 35],
|
|
401
|
+
orange: [255, 165, 0],
|
|
402
|
+
orangered: [255, 69, 0],
|
|
403
|
+
orchid: [218, 112, 214],
|
|
404
|
+
palegoldenrod: [238, 232, 170],
|
|
405
|
+
palegreen: [152, 251, 152],
|
|
406
|
+
paleturquoise: [175, 238, 238],
|
|
407
|
+
palevioletred: [219, 112, 147],
|
|
408
|
+
papayawhip: [255, 239, 213],
|
|
409
|
+
peachpuff: [255, 218, 185],
|
|
410
|
+
peru: [205, 133, 63],
|
|
411
|
+
pink: [255, 192, 203],
|
|
412
|
+
plum: [221, 160, 203],
|
|
413
|
+
powderblue: [176, 224, 230],
|
|
414
|
+
purple: [128, 0, 128],
|
|
415
|
+
rebeccapurple: [102, 51, 153],
|
|
416
|
+
red: [255, 0, 0],
|
|
417
|
+
rosybrown: [188, 143, 143],
|
|
418
|
+
royalblue: [65, 105, 225],
|
|
419
|
+
saddlebrown: [139, 69, 19],
|
|
420
|
+
salmon: [250, 128, 114],
|
|
421
|
+
sandybrown: [244, 164, 96],
|
|
422
|
+
seagreen: [46, 139, 87],
|
|
423
|
+
seashell: [255, 245, 238],
|
|
424
|
+
sienna: [160, 82, 45],
|
|
425
|
+
silver: [192, 192, 192],
|
|
426
|
+
skyblue: [135, 206, 235],
|
|
427
|
+
slateblue: [106, 90, 205],
|
|
428
|
+
slategray: [119, 128, 144],
|
|
429
|
+
slategrey: [119, 128, 144],
|
|
430
|
+
snow: [255, 255, 250],
|
|
431
|
+
springgreen: [0, 255, 127],
|
|
432
|
+
steelblue: [70, 130, 180],
|
|
433
|
+
tan: [210, 180, 140],
|
|
434
|
+
teal: [0, 128, 128],
|
|
435
|
+
thistle: [216, 191, 216],
|
|
436
|
+
transparent: [255, 255, 255, 0],
|
|
437
|
+
tomato: [255, 99, 71],
|
|
438
|
+
turquoise: [64, 224, 208],
|
|
439
|
+
violet: [238, 130, 238],
|
|
440
|
+
wheat: [245, 222, 179],
|
|
441
|
+
white: [255, 255, 255],
|
|
442
|
+
whitesmoke: [245, 245, 245],
|
|
443
|
+
yellow: [255, 255, 0],
|
|
444
|
+
yellowgreen: [154, 205, 5],
|
|
445
|
+
},
|
|
446
|
+
RGB_REGEX = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
|
|
447
|
+
let animQueue: Array<Function> = [];
|
|
448
|
+
|
|
449
|
+
const req =
|
|
450
|
+
(typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame) ||
|
|
451
|
+
function (f) {
|
|
452
|
+
setTimeout(f, 60);
|
|
453
|
+
};
|
|
454
|
+
/**
|
|
455
|
+
* @namespace Util
|
|
456
|
+
* @memberof Konva
|
|
457
|
+
*/
|
|
458
|
+
export const Util = {
|
|
459
|
+
/*
|
|
460
|
+
* cherry-picked utilities from underscore.js
|
|
461
|
+
*/
|
|
462
|
+
_isElement(obj: any): obj is Element {
|
|
463
|
+
return !!(obj && obj.nodeType == 1);
|
|
464
|
+
},
|
|
465
|
+
_isFunction(obj: any) {
|
|
466
|
+
return !!(obj && obj.constructor && obj.call && obj.apply);
|
|
467
|
+
},
|
|
468
|
+
_isPlainObject(obj: any) {
|
|
469
|
+
return !!obj && obj.constructor === Object;
|
|
470
|
+
},
|
|
471
|
+
_isArray(obj: any): obj is Array<any> {
|
|
472
|
+
return Object.prototype.toString.call(obj) === OBJECT_ARRAY;
|
|
473
|
+
},
|
|
474
|
+
_isNumber(obj: any): obj is number {
|
|
475
|
+
return (
|
|
476
|
+
Object.prototype.toString.call(obj) === OBJECT_NUMBER &&
|
|
477
|
+
!isNaN(obj) &&
|
|
478
|
+
isFinite(obj)
|
|
479
|
+
);
|
|
480
|
+
},
|
|
481
|
+
_isString(obj: any): obj is string {
|
|
482
|
+
return Object.prototype.toString.call(obj) === OBJECT_STRING;
|
|
483
|
+
},
|
|
484
|
+
_isBoolean(obj: any): obj is boolean {
|
|
485
|
+
return Object.prototype.toString.call(obj) === OBJECT_BOOLEAN;
|
|
486
|
+
},
|
|
487
|
+
// arrays are objects too
|
|
488
|
+
isObject(val: any): val is object {
|
|
489
|
+
return val instanceof Object;
|
|
490
|
+
},
|
|
491
|
+
isValidSelector(selector: any) {
|
|
492
|
+
if (typeof selector !== 'string') {
|
|
493
|
+
return false;
|
|
494
|
+
}
|
|
495
|
+
const firstChar = selector[0];
|
|
496
|
+
return (
|
|
497
|
+
firstChar === '#' ||
|
|
498
|
+
firstChar === '.' ||
|
|
499
|
+
firstChar === firstChar.toUpperCase()
|
|
500
|
+
);
|
|
501
|
+
},
|
|
502
|
+
_sign(number: number) {
|
|
503
|
+
if (number === 0) {
|
|
504
|
+
// that is not what sign usually returns
|
|
505
|
+
// but that is what we need
|
|
506
|
+
return 1;
|
|
507
|
+
}
|
|
508
|
+
if (number > 0) {
|
|
509
|
+
return 1;
|
|
510
|
+
} else {
|
|
511
|
+
return -1;
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
|
|
515
|
+
requestAnimFrame(callback: Function) {
|
|
516
|
+
animQueue.push(callback);
|
|
517
|
+
if (animQueue.length === 1) {
|
|
518
|
+
req(function () {
|
|
519
|
+
const queue = animQueue;
|
|
520
|
+
animQueue = [];
|
|
521
|
+
queue.forEach(function (cb) {
|
|
522
|
+
cb();
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
createCanvasElement() {
|
|
528
|
+
ensureBrowser();
|
|
529
|
+
const canvas = document.createElement('canvas');
|
|
530
|
+
// on some environments canvas.style is readonly
|
|
531
|
+
try {
|
|
532
|
+
(canvas as any).style = canvas.style || {};
|
|
533
|
+
} catch (e) {}
|
|
534
|
+
return canvas;
|
|
535
|
+
},
|
|
536
|
+
createImageElement() {
|
|
537
|
+
ensureBrowser();
|
|
538
|
+
return document.createElement('img');
|
|
539
|
+
},
|
|
540
|
+
_isInDocument(el: any) {
|
|
541
|
+
while ((el = el.parentNode)) {
|
|
542
|
+
if (el == document) {
|
|
543
|
+
return true;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return false;
|
|
547
|
+
},
|
|
548
|
+
|
|
549
|
+
/*
|
|
550
|
+
* arg can be an image object or image data
|
|
551
|
+
*/
|
|
552
|
+
_urlToImage(url: string, callback: Function) {
|
|
553
|
+
// if arg is a string, then it's a data url
|
|
554
|
+
const imageObj = Util.createImageElement();
|
|
555
|
+
imageObj.onload = function () {
|
|
556
|
+
callback(imageObj);
|
|
557
|
+
};
|
|
558
|
+
imageObj.src = url;
|
|
559
|
+
},
|
|
560
|
+
_rgbToHex(r: number, g: number, b: number) {
|
|
561
|
+
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
|
|
562
|
+
},
|
|
563
|
+
_hexToRgb(hex: string): RGB {
|
|
564
|
+
hex = hex.replace(HASH, EMPTY_STRING);
|
|
565
|
+
const bigint = parseInt(hex, 16);
|
|
566
|
+
return {
|
|
567
|
+
r: (bigint >> 16) & 255,
|
|
568
|
+
g: (bigint >> 8) & 255,
|
|
569
|
+
b: bigint & 255,
|
|
570
|
+
};
|
|
571
|
+
},
|
|
572
|
+
/**
|
|
573
|
+
* return random hex color
|
|
574
|
+
* @method
|
|
575
|
+
* @memberof Konva.Util
|
|
576
|
+
* @example
|
|
577
|
+
* shape.fill(Konva.Util.getRandomColor());
|
|
578
|
+
*/
|
|
579
|
+
getRandomColor() {
|
|
580
|
+
let randColor = ((Math.random() * 0xffffff) << 0).toString(16);
|
|
581
|
+
while (randColor.length < 6) {
|
|
582
|
+
randColor = ZERO + randColor;
|
|
583
|
+
}
|
|
584
|
+
return HASH + randColor;
|
|
585
|
+
},
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* get RGB components of a color
|
|
589
|
+
* @method
|
|
590
|
+
* @memberof Konva.Util
|
|
591
|
+
* @param {String} color
|
|
592
|
+
* @example
|
|
593
|
+
* // each of the following examples return {r:0, g:0, b:255}
|
|
594
|
+
* var rgb = Konva.Util.getRGB('blue');
|
|
595
|
+
* var rgb = Konva.Util.getRGB('#0000ff');
|
|
596
|
+
* var rgb = Konva.Util.getRGB('rgb(0,0,255)');
|
|
597
|
+
*/
|
|
598
|
+
getRGB(color: string): RGB {
|
|
599
|
+
let rgb;
|
|
600
|
+
// color string
|
|
601
|
+
if (color in COLORS) {
|
|
602
|
+
rgb = COLORS[color as keyof typeof COLORS];
|
|
603
|
+
return {
|
|
604
|
+
r: rgb[0],
|
|
605
|
+
g: rgb[1],
|
|
606
|
+
b: rgb[2],
|
|
607
|
+
};
|
|
608
|
+
} else if (color[0] === HASH) {
|
|
609
|
+
// hex
|
|
610
|
+
return this._hexToRgb(color.substring(1));
|
|
611
|
+
} else if (color.substr(0, 4) === RGB_PAREN) {
|
|
612
|
+
// rgb string
|
|
613
|
+
rgb = RGB_REGEX.exec(color.replace(/ /g, '')) as RegExpExecArray;
|
|
614
|
+
return {
|
|
615
|
+
r: parseInt(rgb[1], 10),
|
|
616
|
+
g: parseInt(rgb[2], 10),
|
|
617
|
+
b: parseInt(rgb[3], 10),
|
|
618
|
+
};
|
|
619
|
+
} else {
|
|
620
|
+
// default
|
|
621
|
+
return {
|
|
622
|
+
r: 0,
|
|
623
|
+
g: 0,
|
|
624
|
+
b: 0,
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
},
|
|
628
|
+
// convert any color string to RGBA object
|
|
629
|
+
// from https://github.com/component/color-parser
|
|
630
|
+
colorToRGBA(str: string) {
|
|
631
|
+
str = str || 'black';
|
|
632
|
+
return (
|
|
633
|
+
Util._namedColorToRBA(str) ||
|
|
634
|
+
Util._hex3ColorToRGBA(str) ||
|
|
635
|
+
Util._hex4ColorToRGBA(str) ||
|
|
636
|
+
Util._hex6ColorToRGBA(str) ||
|
|
637
|
+
Util._hex8ColorToRGBA(str) ||
|
|
638
|
+
Util._rgbColorToRGBA(str) ||
|
|
639
|
+
Util._rgbaColorToRGBA(str) ||
|
|
640
|
+
Util._hslColorToRGBA(str)
|
|
641
|
+
);
|
|
642
|
+
},
|
|
643
|
+
// Parse named css color. Like "green"
|
|
644
|
+
_namedColorToRBA(str: string) {
|
|
645
|
+
const c = COLORS[str.toLowerCase() as keyof typeof COLORS];
|
|
646
|
+
if (!c) {
|
|
647
|
+
return null;
|
|
648
|
+
}
|
|
649
|
+
return {
|
|
650
|
+
r: c[0],
|
|
651
|
+
g: c[1],
|
|
652
|
+
b: c[2],
|
|
653
|
+
a: 1,
|
|
654
|
+
};
|
|
655
|
+
},
|
|
656
|
+
// Parse rgb(n, n, n)
|
|
657
|
+
_rgbColorToRGBA(str: string) {
|
|
658
|
+
if (str.indexOf('rgb(') === 0) {
|
|
659
|
+
str = str.match(/rgb\(([^)]+)\)/)![1];
|
|
660
|
+
const parts = str.split(/ *, */).map(Number);
|
|
661
|
+
return {
|
|
662
|
+
r: parts[0],
|
|
663
|
+
g: parts[1],
|
|
664
|
+
b: parts[2],
|
|
665
|
+
a: 1,
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
},
|
|
669
|
+
// Parse rgba(n, n, n, n)
|
|
670
|
+
_rgbaColorToRGBA(str: string) {
|
|
671
|
+
if (str.indexOf('rgba(') === 0) {
|
|
672
|
+
str = str.match(/rgba\(([^)]+)\)/)![1]!;
|
|
673
|
+
const parts = str.split(/ *, */).map((n, index) => {
|
|
674
|
+
if (n.slice(-1) === '%') {
|
|
675
|
+
return index === 3 ? parseInt(n) / 100 : (parseInt(n) / 100) * 255;
|
|
676
|
+
}
|
|
677
|
+
return Number(n);
|
|
678
|
+
});
|
|
679
|
+
return {
|
|
680
|
+
r: parts[0],
|
|
681
|
+
g: parts[1],
|
|
682
|
+
b: parts[2],
|
|
683
|
+
a: parts[3],
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
},
|
|
687
|
+
// Parse #nnnnnnnn
|
|
688
|
+
_hex8ColorToRGBA(str: string) {
|
|
689
|
+
if (str[0] === '#' && str.length === 9) {
|
|
690
|
+
return {
|
|
691
|
+
r: parseInt(str.slice(1, 3), 16),
|
|
692
|
+
g: parseInt(str.slice(3, 5), 16),
|
|
693
|
+
b: parseInt(str.slice(5, 7), 16),
|
|
694
|
+
a: parseInt(str.slice(7, 9), 16) / 0xff,
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
},
|
|
698
|
+
// Parse #nnnnnn
|
|
699
|
+
_hex6ColorToRGBA(str: string) {
|
|
700
|
+
if (str[0] === '#' && str.length === 7) {
|
|
701
|
+
return {
|
|
702
|
+
r: parseInt(str.slice(1, 3), 16),
|
|
703
|
+
g: parseInt(str.slice(3, 5), 16),
|
|
704
|
+
b: parseInt(str.slice(5, 7), 16),
|
|
705
|
+
a: 1,
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
},
|
|
709
|
+
// Parse #nnnn
|
|
710
|
+
_hex4ColorToRGBA(str: string) {
|
|
711
|
+
if (str[0] === '#' && str.length === 5) {
|
|
712
|
+
return {
|
|
713
|
+
r: parseInt(str[1] + str[1], 16),
|
|
714
|
+
g: parseInt(str[2] + str[2], 16),
|
|
715
|
+
b: parseInt(str[3] + str[3], 16),
|
|
716
|
+
a: parseInt(str[4] + str[4], 16) / 0xff,
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
},
|
|
720
|
+
// Parse #nnn
|
|
721
|
+
_hex3ColorToRGBA(str: string) {
|
|
722
|
+
if (str[0] === '#' && str.length === 4) {
|
|
723
|
+
return {
|
|
724
|
+
r: parseInt(str[1] + str[1], 16),
|
|
725
|
+
g: parseInt(str[2] + str[2], 16),
|
|
726
|
+
b: parseInt(str[3] + str[3], 16),
|
|
727
|
+
a: 1,
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
},
|
|
731
|
+
// Code adapted from https://github.com/Qix-/color-convert/blob/master/conversions.js#L244
|
|
732
|
+
_hslColorToRGBA(str: string) {
|
|
733
|
+
// Check hsl() format
|
|
734
|
+
if (/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.test(str)) {
|
|
735
|
+
// Extract h, s, l
|
|
736
|
+
const [_, ...hsl] = /hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.exec(str)!;
|
|
737
|
+
|
|
738
|
+
const h = Number(hsl[0]) / 360;
|
|
739
|
+
const s = Number(hsl[1]) / 100;
|
|
740
|
+
const l = Number(hsl[2]) / 100;
|
|
741
|
+
|
|
742
|
+
let t2;
|
|
743
|
+
let t3;
|
|
744
|
+
let val;
|
|
745
|
+
|
|
746
|
+
if (s === 0) {
|
|
747
|
+
val = l * 255;
|
|
748
|
+
return {
|
|
749
|
+
r: Math.round(val),
|
|
750
|
+
g: Math.round(val),
|
|
751
|
+
b: Math.round(val),
|
|
752
|
+
a: 1,
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
if (l < 0.5) {
|
|
757
|
+
t2 = l * (1 + s);
|
|
758
|
+
} else {
|
|
759
|
+
t2 = l + s - l * s;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const t1 = 2 * l - t2;
|
|
763
|
+
|
|
764
|
+
const rgb = [0, 0, 0];
|
|
765
|
+
for (let i = 0; i < 3; i++) {
|
|
766
|
+
t3 = h + (1 / 3) * -(i - 1);
|
|
767
|
+
if (t3 < 0) {
|
|
768
|
+
t3++;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
if (t3 > 1) {
|
|
772
|
+
t3--;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
if (6 * t3 < 1) {
|
|
776
|
+
val = t1 + (t2 - t1) * 6 * t3;
|
|
777
|
+
} else if (2 * t3 < 1) {
|
|
778
|
+
val = t2;
|
|
779
|
+
} else if (3 * t3 < 2) {
|
|
780
|
+
val = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
|
|
781
|
+
} else {
|
|
782
|
+
val = t1;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
rgb[i] = val * 255;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
return {
|
|
789
|
+
r: Math.round(rgb[0]),
|
|
790
|
+
g: Math.round(rgb[1]),
|
|
791
|
+
b: Math.round(rgb[2]),
|
|
792
|
+
a: 1,
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
},
|
|
796
|
+
/**
|
|
797
|
+
* check intersection of two client rectangles
|
|
798
|
+
* @method
|
|
799
|
+
* @memberof Konva.Util
|
|
800
|
+
* @param {Object} r1 - { x, y, width, height } client rectangle
|
|
801
|
+
* @param {Object} r2 - { x, y, width, height } client rectangle
|
|
802
|
+
* @example
|
|
803
|
+
* const overlapping = Konva.Util.haveIntersection(shape1.getClientRect(), shape2.getClientRect());
|
|
804
|
+
*/
|
|
805
|
+
haveIntersection(r1: IRect, r2: IRect) {
|
|
806
|
+
return !(
|
|
807
|
+
r2.x > r1.x + r1.width ||
|
|
808
|
+
r2.x + r2.width < r1.x ||
|
|
809
|
+
r2.y > r1.y + r1.height ||
|
|
810
|
+
r2.y + r2.height < r1.y
|
|
811
|
+
);
|
|
812
|
+
},
|
|
813
|
+
cloneObject<Any>(obj: Any): Any {
|
|
814
|
+
const retObj: any = {};
|
|
815
|
+
for (const key in obj) {
|
|
816
|
+
if (this._isPlainObject(obj[key])) {
|
|
817
|
+
retObj[key] = this.cloneObject(obj[key]);
|
|
818
|
+
} else if (this._isArray(obj[key])) {
|
|
819
|
+
retObj[key] = this.cloneArray(obj[key] as Array<any>);
|
|
820
|
+
} else {
|
|
821
|
+
retObj[key] = obj[key];
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return retObj;
|
|
825
|
+
},
|
|
826
|
+
cloneArray(arr: Array<any>) {
|
|
827
|
+
return arr.slice(0);
|
|
828
|
+
},
|
|
829
|
+
degToRad(deg: number) {
|
|
830
|
+
return deg * PI_OVER_DEG180;
|
|
831
|
+
},
|
|
832
|
+
radToDeg(rad: number) {
|
|
833
|
+
return rad * DEG180_OVER_PI;
|
|
834
|
+
},
|
|
835
|
+
_degToRad(deg: number) {
|
|
836
|
+
Util.warn(
|
|
837
|
+
'Util._degToRad is removed. Please use public Util.degToRad instead.'
|
|
838
|
+
);
|
|
839
|
+
return Util.degToRad(deg);
|
|
840
|
+
},
|
|
841
|
+
_radToDeg(rad: number) {
|
|
842
|
+
Util.warn(
|
|
843
|
+
'Util._radToDeg is removed. Please use public Util.radToDeg instead.'
|
|
844
|
+
);
|
|
845
|
+
return Util.radToDeg(rad);
|
|
846
|
+
},
|
|
847
|
+
_getRotation(radians: number) {
|
|
848
|
+
return Konva.angleDeg ? Util.radToDeg(radians) : radians;
|
|
849
|
+
},
|
|
850
|
+
_capitalize(str: string) {
|
|
851
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
852
|
+
},
|
|
853
|
+
throw(str: string) {
|
|
854
|
+
throw new Error(KONVA_ERROR + str);
|
|
855
|
+
},
|
|
856
|
+
error(str: string) {
|
|
857
|
+
console.error(KONVA_ERROR + str);
|
|
858
|
+
},
|
|
859
|
+
warn(str: string) {
|
|
860
|
+
if (!Konva.showWarnings) {
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
console.warn(KONVA_WARNING + str);
|
|
864
|
+
},
|
|
865
|
+
each(obj: object, func: Function) {
|
|
866
|
+
for (const key in obj) {
|
|
867
|
+
func(key, obj[key as keyof typeof obj]);
|
|
868
|
+
}
|
|
869
|
+
},
|
|
870
|
+
_inRange(val: number, left: number, right: number) {
|
|
871
|
+
return left <= val && val < right;
|
|
872
|
+
},
|
|
873
|
+
_getProjectionToSegment(x1, y1, x2, y2, x3, y3) {
|
|
874
|
+
let x, y, dist;
|
|
875
|
+
|
|
876
|
+
const pd2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
|
|
877
|
+
if (pd2 == 0) {
|
|
878
|
+
x = x1;
|
|
879
|
+
y = y1;
|
|
880
|
+
dist = (x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2);
|
|
881
|
+
} else {
|
|
882
|
+
const u = ((x3 - x1) * (x2 - x1) + (y3 - y1) * (y2 - y1)) / pd2;
|
|
883
|
+
if (u < 0) {
|
|
884
|
+
x = x1;
|
|
885
|
+
y = y1;
|
|
886
|
+
dist = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
|
|
887
|
+
} else if (u > 1.0) {
|
|
888
|
+
x = x2;
|
|
889
|
+
y = y2;
|
|
890
|
+
dist = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
|
|
891
|
+
} else {
|
|
892
|
+
x = x1 + u * (x2 - x1);
|
|
893
|
+
y = y1 + u * (y2 - y1);
|
|
894
|
+
dist = (x - x3) * (x - x3) + (y - y3) * (y - y3);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
return [x, y, dist];
|
|
898
|
+
},
|
|
899
|
+
// line as array of points.
|
|
900
|
+
// line might be closed
|
|
901
|
+
_getProjectionToLine(pt: Vector2d, line: Array<Vector2d>, isClosed: boolean) {
|
|
902
|
+
const pc = Util.cloneObject(pt);
|
|
903
|
+
let dist = Number.MAX_VALUE;
|
|
904
|
+
line.forEach(function (p1, i) {
|
|
905
|
+
if (!isClosed && i === line.length - 1) {
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
const p2 = line[(i + 1) % line.length];
|
|
909
|
+
const proj = Util._getProjectionToSegment(
|
|
910
|
+
p1.x,
|
|
911
|
+
p1.y,
|
|
912
|
+
p2.x,
|
|
913
|
+
p2.y,
|
|
914
|
+
pt.x,
|
|
915
|
+
pt.y
|
|
916
|
+
);
|
|
917
|
+
const px = proj[0],
|
|
918
|
+
py = proj[1],
|
|
919
|
+
pdist = proj[2];
|
|
920
|
+
if (pdist < dist) {
|
|
921
|
+
pc.x = px;
|
|
922
|
+
pc.y = py;
|
|
923
|
+
dist = pdist;
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
return pc;
|
|
927
|
+
},
|
|
928
|
+
_prepareArrayForTween(startArray, endArray, isClosed) {
|
|
929
|
+
const start: Vector2d[] = [],
|
|
930
|
+
end: Vector2d[] = [];
|
|
931
|
+
if (startArray.length > endArray.length) {
|
|
932
|
+
const temp = endArray;
|
|
933
|
+
endArray = startArray;
|
|
934
|
+
startArray = temp;
|
|
935
|
+
}
|
|
936
|
+
for (let n = 0; n < startArray.length; n += 2) {
|
|
937
|
+
start.push({
|
|
938
|
+
x: startArray[n],
|
|
939
|
+
y: startArray[n + 1],
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
for (let n = 0; n < endArray.length; n += 2) {
|
|
943
|
+
end.push({
|
|
944
|
+
x: endArray[n],
|
|
945
|
+
y: endArray[n + 1],
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
const newStart: number[] = [];
|
|
950
|
+
end.forEach(function (point) {
|
|
951
|
+
const pr = Util._getProjectionToLine(point, start, isClosed);
|
|
952
|
+
newStart.push(pr.x);
|
|
953
|
+
newStart.push(pr.y);
|
|
954
|
+
});
|
|
955
|
+
return newStart;
|
|
956
|
+
},
|
|
957
|
+
_prepareToStringify<T>(obj: any): T | null {
|
|
958
|
+
let desc;
|
|
959
|
+
|
|
960
|
+
obj.visitedByCircularReferenceRemoval = true;
|
|
961
|
+
|
|
962
|
+
for (const key in obj) {
|
|
963
|
+
if (
|
|
964
|
+
!(obj.hasOwnProperty(key) && obj[key] && typeof obj[key] == 'object')
|
|
965
|
+
) {
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
968
|
+
desc = Object.getOwnPropertyDescriptor(obj, key);
|
|
969
|
+
if (
|
|
970
|
+
obj[key].visitedByCircularReferenceRemoval ||
|
|
971
|
+
Util._isElement(obj[key])
|
|
972
|
+
) {
|
|
973
|
+
if (desc.configurable) {
|
|
974
|
+
delete obj[key];
|
|
975
|
+
} else {
|
|
976
|
+
return null;
|
|
977
|
+
}
|
|
978
|
+
} else if (Util._prepareToStringify(obj[key]) === null) {
|
|
979
|
+
if (desc.configurable) {
|
|
980
|
+
delete obj[key];
|
|
981
|
+
} else {
|
|
982
|
+
return null;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
delete obj.visitedByCircularReferenceRemoval;
|
|
988
|
+
|
|
989
|
+
return obj;
|
|
990
|
+
},
|
|
991
|
+
// very simplified version of Object.assign
|
|
992
|
+
_assign<T, U>(target: T, source: U) {
|
|
993
|
+
for (const key in source) {
|
|
994
|
+
(target as any)[key] = source[key];
|
|
995
|
+
}
|
|
996
|
+
return target as T & U;
|
|
997
|
+
},
|
|
998
|
+
_getFirstPointerId(evt) {
|
|
999
|
+
if (!evt.touches) {
|
|
1000
|
+
// try to use pointer id or fake id
|
|
1001
|
+
return evt.pointerId || 999;
|
|
1002
|
+
} else {
|
|
1003
|
+
return evt.changedTouches[0].identifier;
|
|
1004
|
+
}
|
|
1005
|
+
},
|
|
1006
|
+
releaseCanvas(...canvases: HTMLCanvasElement[]) {
|
|
1007
|
+
if (!Konva.releaseCanvasOnDestroy) return;
|
|
1008
|
+
|
|
1009
|
+
canvases.forEach((c) => {
|
|
1010
|
+
c.width = 0;
|
|
1011
|
+
c.height = 0;
|
|
1012
|
+
});
|
|
1013
|
+
},
|
|
1014
|
+
drawRoundedRectPath(
|
|
1015
|
+
context: Context,
|
|
1016
|
+
width: number,
|
|
1017
|
+
height: number,
|
|
1018
|
+
cornerRadius: number | number[]
|
|
1019
|
+
) {
|
|
1020
|
+
// if negative dimensions, abs width/height and move rectangle
|
|
1021
|
+
let xOrigin = width < 0 ? width : 0;
|
|
1022
|
+
let yOrigin = height < 0 ? height : 0;
|
|
1023
|
+
width = Math.abs(width);
|
|
1024
|
+
height = Math.abs(height);
|
|
1025
|
+
let topLeft = 0;
|
|
1026
|
+
let topRight = 0;
|
|
1027
|
+
let bottomLeft = 0;
|
|
1028
|
+
let bottomRight = 0;
|
|
1029
|
+
if (typeof cornerRadius === 'number') {
|
|
1030
|
+
topLeft =
|
|
1031
|
+
topRight =
|
|
1032
|
+
bottomLeft =
|
|
1033
|
+
bottomRight =
|
|
1034
|
+
Math.min(cornerRadius, width / 2, height / 2);
|
|
1035
|
+
} else {
|
|
1036
|
+
topLeft = Math.min(cornerRadius[0] || 0, width / 2, height / 2);
|
|
1037
|
+
topRight = Math.min(cornerRadius[1] || 0, width / 2, height / 2);
|
|
1038
|
+
bottomRight = Math.min(cornerRadius[2] || 0, width / 2, height / 2);
|
|
1039
|
+
bottomLeft = Math.min(cornerRadius[3] || 0, width / 2, height / 2);
|
|
1040
|
+
}
|
|
1041
|
+
context.moveTo(xOrigin + topLeft, yOrigin);
|
|
1042
|
+
context.lineTo(xOrigin + width - topRight, yOrigin);
|
|
1043
|
+
context.arc(
|
|
1044
|
+
xOrigin + width - topRight,
|
|
1045
|
+
yOrigin + topRight,
|
|
1046
|
+
topRight,
|
|
1047
|
+
(Math.PI * 3) / 2,
|
|
1048
|
+
0,
|
|
1049
|
+
false
|
|
1050
|
+
);
|
|
1051
|
+
context.lineTo(xOrigin + width, yOrigin + height - bottomRight);
|
|
1052
|
+
context.arc(
|
|
1053
|
+
xOrigin + width - bottomRight,
|
|
1054
|
+
yOrigin + height - bottomRight,
|
|
1055
|
+
bottomRight,
|
|
1056
|
+
0,
|
|
1057
|
+
Math.PI / 2,
|
|
1058
|
+
false
|
|
1059
|
+
);
|
|
1060
|
+
context.lineTo(xOrigin + bottomLeft, yOrigin + height);
|
|
1061
|
+
context.arc(
|
|
1062
|
+
xOrigin + bottomLeft,
|
|
1063
|
+
yOrigin + height - bottomLeft,
|
|
1064
|
+
bottomLeft,
|
|
1065
|
+
Math.PI / 2,
|
|
1066
|
+
Math.PI,
|
|
1067
|
+
false
|
|
1068
|
+
);
|
|
1069
|
+
context.lineTo(xOrigin, yOrigin + topLeft);
|
|
1070
|
+
context.arc(
|
|
1071
|
+
xOrigin + topLeft,
|
|
1072
|
+
yOrigin + topLeft,
|
|
1073
|
+
topLeft,
|
|
1074
|
+
Math.PI,
|
|
1075
|
+
(Math.PI * 3) / 2,
|
|
1076
|
+
false
|
|
1077
|
+
);
|
|
1078
|
+
},
|
|
1079
|
+
drawRoundedPolygonPath(
|
|
1080
|
+
context: Context,
|
|
1081
|
+
points: Vector2d[],
|
|
1082
|
+
sides: number,
|
|
1083
|
+
radius: number,
|
|
1084
|
+
cornerRadius: number | number[]
|
|
1085
|
+
) {
|
|
1086
|
+
radius = Math.abs(radius);
|
|
1087
|
+
for (let i = 0; i < sides; i++) {
|
|
1088
|
+
const prev = points[(i - 1 + sides) % sides];
|
|
1089
|
+
const curr = points[i];
|
|
1090
|
+
const next = points[(i + 1) % sides];
|
|
1091
|
+
const vec1 = { x: curr.x - prev.x, y: curr.y - prev.y };
|
|
1092
|
+
const vec2 = { x: next.x - curr.x, y: next.y - curr.y };
|
|
1093
|
+
const len1 = Math.hypot(vec1.x, vec1.y);
|
|
1094
|
+
const len2 = Math.hypot(vec2.x, vec2.y);
|
|
1095
|
+
let currCornerRadius;
|
|
1096
|
+
if (typeof cornerRadius === 'number') {
|
|
1097
|
+
currCornerRadius = cornerRadius;
|
|
1098
|
+
} else {
|
|
1099
|
+
currCornerRadius = i < cornerRadius.length ? cornerRadius[i] : 0;
|
|
1100
|
+
}
|
|
1101
|
+
const maxCornerRadius = radius * Math.cos(Math.PI / sides);
|
|
1102
|
+
// cornerRadius creates perfect circle at 1/2 radius
|
|
1103
|
+
currCornerRadius =
|
|
1104
|
+
maxCornerRadius * Math.min(1, (currCornerRadius / radius) * 2);
|
|
1105
|
+
const normalVec1 = { x: vec1.x / len1, y: vec1.y / len1 };
|
|
1106
|
+
const normalVec2 = { x: vec2.x / len2, y: vec2.y / len2 };
|
|
1107
|
+
const p1 = {
|
|
1108
|
+
x: curr.x - normalVec1.x * currCornerRadius,
|
|
1109
|
+
y: curr.y - normalVec1.y * currCornerRadius,
|
|
1110
|
+
};
|
|
1111
|
+
const p2 = {
|
|
1112
|
+
x: curr.x + normalVec2.x * currCornerRadius,
|
|
1113
|
+
y: curr.y + normalVec2.y * currCornerRadius,
|
|
1114
|
+
};
|
|
1115
|
+
if (i === 0) {
|
|
1116
|
+
context.moveTo(p1.x, p1.y);
|
|
1117
|
+
} else {
|
|
1118
|
+
context.lineTo(p1.x, p1.y);
|
|
1119
|
+
}
|
|
1120
|
+
context.arcTo(curr.x, curr.y, p2.x, p2.y, currCornerRadius);
|
|
1121
|
+
}
|
|
1122
|
+
},
|
|
1123
|
+
};
|