konva 10.0.10 → 10.0.12
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 +20 -11
- package/konva.min.js +3 -3
- package/lib/Animation.d.ts +24 -0
- package/lib/Animation.js +142 -0
- package/lib/BezierFunctions.d.ts +6 -0
- package/lib/BezierFunctions.js +784 -0
- package/lib/Canvas.d.ts +33 -0
- package/lib/Canvas.js +107 -0
- package/lib/Container.d.ts +54 -0
- package/lib/Container.js +316 -0
- package/lib/Context.d.ts +95 -0
- package/lib/Context.js +561 -0
- package/lib/Core.d.ts +3 -0
- package/lib/Core.js +3 -0
- package/lib/DragAndDrop.d.ts +17 -0
- package/lib/DragAndDrop.js +110 -0
- package/lib/Factory.d.ts +22 -0
- package/lib/Factory.js +123 -0
- package/lib/FastLayer.d.ts +4 -0
- package/lib/FastLayer.js +12 -0
- package/lib/Global.d.ts +37 -0
- package/lib/Global.js +66 -0
- package/lib/Group.d.ts +9 -0
- package/lib/Group.js +13 -0
- package/lib/Layer.d.ts +62 -0
- package/lib/Layer.js +305 -0
- package/lib/Node.d.ts +323 -0
- package/lib/Node.js +1532 -0
- package/lib/PointerEvents.d.ts +11 -0
- package/lib/PointerEvents.js +37 -0
- package/lib/Shape.d.ts +196 -0
- package/lib/Shape.js +555 -0
- package/lib/Stage.d.ts +86 -0
- package/lib/Stage.js +715 -0
- package/lib/Tween.d.ts +90 -0
- package/lib/Tween.js +531 -0
- package/lib/Util.d.ts +137 -0
- package/lib/Util.js +918 -0
- package/lib/Validators.d.ts +11 -0
- package/lib/Validators.js +178 -0
- package/lib/_CoreInternals.d.ts +224 -0
- package/lib/_CoreInternals.js +34 -0
- package/lib/_FullInternals.d.ts +306 -0
- package/lib/_FullInternals.js +80 -0
- package/lib/canvas-backend.d.ts +2 -0
- package/lib/canvas-backend.js +27 -0
- package/lib/filters/Blur.d.ts +2 -0
- package/lib/filters/Blur.js +235 -0
- package/lib/filters/Brighten.d.ts +2 -0
- package/lib/filters/Brighten.js +12 -0
- package/lib/filters/Brightness.d.ts +2 -0
- package/lib/filters/Brightness.js +8 -0
- package/lib/filters/Contrast.d.ts +2 -0
- package/lib/filters/Contrast.js +35 -0
- package/lib/filters/Emboss.d.ts +2 -0
- package/lib/filters/Emboss.js +101 -0
- package/lib/filters/Enhance.d.ts +2 -0
- package/lib/filters/Enhance.js +84 -0
- package/lib/filters/Grayscale.d.ts +2 -0
- package/lib/filters/Grayscale.js +9 -0
- package/lib/filters/HSL.d.ts +2 -0
- package/lib/filters/HSL.js +24 -0
- package/lib/filters/HSV.d.ts +2 -0
- package/lib/filters/HSV.js +23 -0
- package/lib/filters/Invert.d.ts +2 -0
- package/lib/filters/Invert.js +8 -0
- package/lib/filters/Kaleidoscope.d.ts +2 -0
- package/lib/filters/Kaleidoscope.js +133 -0
- package/lib/filters/Mask.d.ts +2 -0
- package/lib/filters/Mask.js +151 -0
- package/lib/filters/Noise.d.ts +2 -0
- package/lib/filters/Noise.js +12 -0
- package/lib/filters/Pixelate.d.ts +2 -0
- package/lib/filters/Pixelate.js +60 -0
- package/lib/filters/Posterize.d.ts +2 -0
- package/lib/filters/Posterize.js +10 -0
- package/lib/filters/RGB.d.ts +2 -0
- package/lib/filters/RGB.js +38 -0
- package/lib/filters/RGBA.d.ts +2 -0
- package/lib/filters/RGBA.js +49 -0
- package/lib/filters/Sepia.d.ts +2 -0
- package/lib/filters/Sepia.js +11 -0
- package/lib/filters/Solarize.d.ts +2 -0
- package/lib/filters/Solarize.js +14 -0
- package/lib/filters/Threshold.d.ts +2 -0
- package/lib/filters/Threshold.js +10 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/shapes/Arc.d.ts +27 -0
- package/lib/shapes/Arc.js +60 -0
- package/lib/shapes/Arrow.d.ts +27 -0
- package/lib/shapes/Arrow.js +99 -0
- package/lib/shapes/Circle.d.ts +15 -0
- package/lib/shapes/Circle.js +33 -0
- package/lib/shapes/Ellipse.d.ts +18 -0
- package/lib/shapes/Ellipse.js +37 -0
- package/lib/shapes/Image.d.ts +29 -0
- package/lib/shapes/Image.js +131 -0
- package/lib/shapes/Label.d.ts +39 -0
- package/lib/shapes/Label.js +173 -0
- package/lib/shapes/Line.d.ts +29 -0
- package/lib/shapes/Line.js +156 -0
- package/lib/shapes/Path.d.ts +50 -0
- package/lib/shapes/Path.js +670 -0
- package/lib/shapes/Rect.d.ts +11 -0
- package/lib/shapes/Rect.js +22 -0
- package/lib/shapes/RegularPolygon.d.ts +26 -0
- package/lib/shapes/RegularPolygon.js +72 -0
- package/lib/shapes/Ring.d.ts +17 -0
- package/lib/shapes/Ring.js +33 -0
- package/lib/shapes/Sprite.d.ts +32 -0
- package/lib/shapes/Sprite.js +108 -0
- package/lib/shapes/Star.d.ts +19 -0
- package/lib/shapes/Star.js +38 -0
- package/lib/shapes/Text.d.ts +90 -0
- package/lib/shapes/Text.js +461 -0
- package/lib/shapes/TextPath.d.ts +72 -0
- package/lib/shapes/TextPath.js +268 -0
- package/lib/shapes/Transformer.d.ts +130 -0
- package/lib/shapes/Transformer.js +1009 -0
- package/lib/shapes/Wedge.d.ts +19 -0
- package/lib/shapes/Wedge.js +38 -0
- package/lib/skia-backend.d.ts +2 -0
- package/lib/skia-backend.js +24 -0
- package/lib/types.d.ts +56 -0
- package/lib/types.js +21 -0
- package/package.json +1 -1
package/lib/Context.js
ADDED
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
import { Util } from "./Util.js";
|
|
2
|
+
import { Konva } from "./Global.js";
|
|
3
|
+
function simplifyArray(arr) {
|
|
4
|
+
const retArr = [], len = arr.length, util = Util;
|
|
5
|
+
for (let n = 0; n < len; n++) {
|
|
6
|
+
let val = arr[n];
|
|
7
|
+
if (util._isNumber(val)) {
|
|
8
|
+
val = Math.round(val * 1000) / 1000;
|
|
9
|
+
}
|
|
10
|
+
else if (!util._isString(val)) {
|
|
11
|
+
val = val + '';
|
|
12
|
+
}
|
|
13
|
+
retArr.push(val);
|
|
14
|
+
}
|
|
15
|
+
return retArr;
|
|
16
|
+
}
|
|
17
|
+
const COMMA = ',', OPEN_PAREN = '(', CLOSE_PAREN = ')', OPEN_PAREN_BRACKET = '([', CLOSE_BRACKET_PAREN = '])', SEMICOLON = ';', DOUBLE_PAREN = '()', EQUALS = '=', CONTEXT_METHODS = [
|
|
18
|
+
'arc',
|
|
19
|
+
'arcTo',
|
|
20
|
+
'beginPath',
|
|
21
|
+
'bezierCurveTo',
|
|
22
|
+
'clearRect',
|
|
23
|
+
'clip',
|
|
24
|
+
'closePath',
|
|
25
|
+
'createLinearGradient',
|
|
26
|
+
'createPattern',
|
|
27
|
+
'createRadialGradient',
|
|
28
|
+
'drawImage',
|
|
29
|
+
'ellipse',
|
|
30
|
+
'fill',
|
|
31
|
+
'fillText',
|
|
32
|
+
'getImageData',
|
|
33
|
+
'createImageData',
|
|
34
|
+
'lineTo',
|
|
35
|
+
'moveTo',
|
|
36
|
+
'putImageData',
|
|
37
|
+
'quadraticCurveTo',
|
|
38
|
+
'rect',
|
|
39
|
+
'roundRect',
|
|
40
|
+
'restore',
|
|
41
|
+
'rotate',
|
|
42
|
+
'save',
|
|
43
|
+
'scale',
|
|
44
|
+
'setLineDash',
|
|
45
|
+
'setTransform',
|
|
46
|
+
'stroke',
|
|
47
|
+
'strokeText',
|
|
48
|
+
'transform',
|
|
49
|
+
'translate',
|
|
50
|
+
];
|
|
51
|
+
const CONTEXT_PROPERTIES = [
|
|
52
|
+
'fillStyle',
|
|
53
|
+
'strokeStyle',
|
|
54
|
+
'shadowColor',
|
|
55
|
+
'shadowBlur',
|
|
56
|
+
'shadowOffsetX',
|
|
57
|
+
'shadowOffsetY',
|
|
58
|
+
'letterSpacing',
|
|
59
|
+
'lineCap',
|
|
60
|
+
'lineDashOffset',
|
|
61
|
+
'lineJoin',
|
|
62
|
+
'lineWidth',
|
|
63
|
+
'miterLimit',
|
|
64
|
+
'direction',
|
|
65
|
+
'font',
|
|
66
|
+
'textAlign',
|
|
67
|
+
'textBaseline',
|
|
68
|
+
'globalAlpha',
|
|
69
|
+
'globalCompositeOperation',
|
|
70
|
+
'imageSmoothingEnabled',
|
|
71
|
+
'filter',
|
|
72
|
+
];
|
|
73
|
+
const traceArrMax = 100;
|
|
74
|
+
let _cssFiltersSupported = null;
|
|
75
|
+
export function isCSSFiltersSupported() {
|
|
76
|
+
if (_cssFiltersSupported !== null) {
|
|
77
|
+
return _cssFiltersSupported;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const canvas = Util.createCanvasElement();
|
|
81
|
+
const ctx = canvas.getContext('2d');
|
|
82
|
+
if (!ctx) {
|
|
83
|
+
_cssFiltersSupported = false;
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
return !!ctx && 'filter' in ctx;
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
_cssFiltersSupported = false;
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export class Context {
|
|
94
|
+
constructor(canvas) {
|
|
95
|
+
this.canvas = canvas;
|
|
96
|
+
if (Konva.enableTrace) {
|
|
97
|
+
this.traceArr = [];
|
|
98
|
+
this._enableTrace();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
fillShape(shape) {
|
|
102
|
+
if (shape.fillEnabled()) {
|
|
103
|
+
this._fill(shape);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
_fill(shape) {
|
|
107
|
+
}
|
|
108
|
+
strokeShape(shape) {
|
|
109
|
+
if (shape.hasStroke()) {
|
|
110
|
+
this._stroke(shape);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
_stroke(shape) {
|
|
114
|
+
}
|
|
115
|
+
fillStrokeShape(shape) {
|
|
116
|
+
if (shape.attrs.fillAfterStrokeEnabled) {
|
|
117
|
+
this.strokeShape(shape);
|
|
118
|
+
this.fillShape(shape);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
this.fillShape(shape);
|
|
122
|
+
this.strokeShape(shape);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
getTrace(relaxed, rounded) {
|
|
126
|
+
let traceArr = this.traceArr, len = traceArr.length, str = '', n, trace, method, args;
|
|
127
|
+
for (n = 0; n < len; n++) {
|
|
128
|
+
trace = traceArr[n];
|
|
129
|
+
method = trace.method;
|
|
130
|
+
if (method) {
|
|
131
|
+
args = trace.args;
|
|
132
|
+
str += method;
|
|
133
|
+
if (relaxed) {
|
|
134
|
+
str += DOUBLE_PAREN;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
if (Util._isArray(args[0])) {
|
|
138
|
+
str += OPEN_PAREN_BRACKET + args.join(COMMA) + CLOSE_BRACKET_PAREN;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
if (rounded) {
|
|
142
|
+
args = args.map((a) => typeof a === 'number' ? Math.floor(a) : a);
|
|
143
|
+
}
|
|
144
|
+
str += OPEN_PAREN + args.join(COMMA) + CLOSE_PAREN;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
str += trace.property;
|
|
150
|
+
if (!relaxed) {
|
|
151
|
+
str += EQUALS + trace.val;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
str += SEMICOLON;
|
|
155
|
+
}
|
|
156
|
+
return str;
|
|
157
|
+
}
|
|
158
|
+
clearTrace() {
|
|
159
|
+
this.traceArr = [];
|
|
160
|
+
}
|
|
161
|
+
_trace(str) {
|
|
162
|
+
let traceArr = this.traceArr, len;
|
|
163
|
+
traceArr.push(str);
|
|
164
|
+
len = traceArr.length;
|
|
165
|
+
if (len >= traceArrMax) {
|
|
166
|
+
traceArr.shift();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
reset() {
|
|
170
|
+
const pixelRatio = this.getCanvas().getPixelRatio();
|
|
171
|
+
this.setTransform(1 * pixelRatio, 0, 0, 1 * pixelRatio, 0, 0);
|
|
172
|
+
}
|
|
173
|
+
getCanvas() {
|
|
174
|
+
return this.canvas;
|
|
175
|
+
}
|
|
176
|
+
clear(bounds) {
|
|
177
|
+
const canvas = this.getCanvas();
|
|
178
|
+
if (bounds) {
|
|
179
|
+
this.clearRect(bounds.x || 0, bounds.y || 0, bounds.width || 0, bounds.height || 0);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
this.clearRect(0, 0, canvas.getWidth() / canvas.pixelRatio, canvas.getHeight() / canvas.pixelRatio);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
_applyLineCap(shape) {
|
|
186
|
+
const lineCap = shape.attrs.lineCap;
|
|
187
|
+
if (lineCap) {
|
|
188
|
+
this.setAttr('lineCap', lineCap);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
_applyOpacity(shape) {
|
|
192
|
+
const absOpacity = shape.getAbsoluteOpacity();
|
|
193
|
+
if (absOpacity !== 1) {
|
|
194
|
+
this.setAttr('globalAlpha', absOpacity);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
_applyLineJoin(shape) {
|
|
198
|
+
const lineJoin = shape.attrs.lineJoin;
|
|
199
|
+
if (lineJoin) {
|
|
200
|
+
this.setAttr('lineJoin', lineJoin);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
_applyMiterLimit(shape) {
|
|
204
|
+
const miterLimit = shape.attrs.miterLimit;
|
|
205
|
+
if (miterLimit != null) {
|
|
206
|
+
this.setAttr('miterLimit', miterLimit);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
setAttr(attr, val) {
|
|
210
|
+
this._context[attr] = val;
|
|
211
|
+
}
|
|
212
|
+
arc(x, y, radius, startAngle, endAngle, counterClockwise) {
|
|
213
|
+
this._context.arc(x, y, radius, startAngle, endAngle, counterClockwise);
|
|
214
|
+
}
|
|
215
|
+
arcTo(x1, y1, x2, y2, radius) {
|
|
216
|
+
this._context.arcTo(x1, y1, x2, y2, radius);
|
|
217
|
+
}
|
|
218
|
+
beginPath() {
|
|
219
|
+
this._context.beginPath();
|
|
220
|
+
}
|
|
221
|
+
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
222
|
+
this._context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
|
|
223
|
+
}
|
|
224
|
+
clearRect(x, y, width, height) {
|
|
225
|
+
this._context.clearRect(x, y, width, height);
|
|
226
|
+
}
|
|
227
|
+
clip(...args) {
|
|
228
|
+
this._context.clip.apply(this._context, args);
|
|
229
|
+
}
|
|
230
|
+
closePath() {
|
|
231
|
+
this._context.closePath();
|
|
232
|
+
}
|
|
233
|
+
createImageData(width, height) {
|
|
234
|
+
const a = arguments;
|
|
235
|
+
if (a.length === 2) {
|
|
236
|
+
return this._context.createImageData(width, height);
|
|
237
|
+
}
|
|
238
|
+
else if (a.length === 1) {
|
|
239
|
+
return this._context.createImageData(width);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
createLinearGradient(x0, y0, x1, y1) {
|
|
243
|
+
return this._context.createLinearGradient(x0, y0, x1, y1);
|
|
244
|
+
}
|
|
245
|
+
createPattern(image, repetition) {
|
|
246
|
+
return this._context.createPattern(image, repetition);
|
|
247
|
+
}
|
|
248
|
+
createRadialGradient(x0, y0, r0, x1, y1, r1) {
|
|
249
|
+
return this._context.createRadialGradient(x0, y0, r0, x1, y1, r1);
|
|
250
|
+
}
|
|
251
|
+
drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
|
|
252
|
+
const a = arguments, _context = this._context;
|
|
253
|
+
if (a.length === 3) {
|
|
254
|
+
_context.drawImage(image, sx, sy);
|
|
255
|
+
}
|
|
256
|
+
else if (a.length === 5) {
|
|
257
|
+
_context.drawImage(image, sx, sy, sWidth, sHeight);
|
|
258
|
+
}
|
|
259
|
+
else if (a.length === 9) {
|
|
260
|
+
_context.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
|
|
264
|
+
this._context.ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise);
|
|
265
|
+
}
|
|
266
|
+
isPointInPath(x, y, path, fillRule) {
|
|
267
|
+
if (path) {
|
|
268
|
+
return this._context.isPointInPath(path, x, y, fillRule);
|
|
269
|
+
}
|
|
270
|
+
return this._context.isPointInPath(x, y, fillRule);
|
|
271
|
+
}
|
|
272
|
+
fill(...args) {
|
|
273
|
+
this._context.fill.apply(this._context, args);
|
|
274
|
+
}
|
|
275
|
+
fillRect(x, y, width, height) {
|
|
276
|
+
this._context.fillRect(x, y, width, height);
|
|
277
|
+
}
|
|
278
|
+
strokeRect(x, y, width, height) {
|
|
279
|
+
this._context.strokeRect(x, y, width, height);
|
|
280
|
+
}
|
|
281
|
+
fillText(text, x, y, maxWidth) {
|
|
282
|
+
if (maxWidth) {
|
|
283
|
+
this._context.fillText(text, x, y, maxWidth);
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
this._context.fillText(text, x, y);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
measureText(text) {
|
|
290
|
+
return this._context.measureText(text);
|
|
291
|
+
}
|
|
292
|
+
getImageData(sx, sy, sw, sh) {
|
|
293
|
+
return this._context.getImageData(sx, sy, sw, sh);
|
|
294
|
+
}
|
|
295
|
+
lineTo(x, y) {
|
|
296
|
+
this._context.lineTo(x, y);
|
|
297
|
+
}
|
|
298
|
+
moveTo(x, y) {
|
|
299
|
+
this._context.moveTo(x, y);
|
|
300
|
+
}
|
|
301
|
+
rect(x, y, width, height) {
|
|
302
|
+
this._context.rect(x, y, width, height);
|
|
303
|
+
}
|
|
304
|
+
roundRect(x, y, width, height, radii) {
|
|
305
|
+
this._context.roundRect(x, y, width, height, radii);
|
|
306
|
+
}
|
|
307
|
+
putImageData(imageData, dx, dy) {
|
|
308
|
+
this._context.putImageData(imageData, dx, dy);
|
|
309
|
+
}
|
|
310
|
+
quadraticCurveTo(cpx, cpy, x, y) {
|
|
311
|
+
this._context.quadraticCurveTo(cpx, cpy, x, y);
|
|
312
|
+
}
|
|
313
|
+
restore() {
|
|
314
|
+
this._context.restore();
|
|
315
|
+
}
|
|
316
|
+
rotate(angle) {
|
|
317
|
+
this._context.rotate(angle);
|
|
318
|
+
}
|
|
319
|
+
save() {
|
|
320
|
+
this._context.save();
|
|
321
|
+
}
|
|
322
|
+
scale(x, y) {
|
|
323
|
+
this._context.scale(x, y);
|
|
324
|
+
}
|
|
325
|
+
setLineDash(segments) {
|
|
326
|
+
if (this._context.setLineDash) {
|
|
327
|
+
this._context.setLineDash(segments);
|
|
328
|
+
}
|
|
329
|
+
else if ('mozDash' in this._context) {
|
|
330
|
+
this._context['mozDash'] = segments;
|
|
331
|
+
}
|
|
332
|
+
else if ('webkitLineDash' in this._context) {
|
|
333
|
+
this._context['webkitLineDash'] = segments;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
getLineDash() {
|
|
337
|
+
return this._context.getLineDash();
|
|
338
|
+
}
|
|
339
|
+
setTransform(a, b, c, d, e, f) {
|
|
340
|
+
this._context.setTransform(a, b, c, d, e, f);
|
|
341
|
+
}
|
|
342
|
+
stroke(path2d) {
|
|
343
|
+
if (path2d) {
|
|
344
|
+
this._context.stroke(path2d);
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
this._context.stroke();
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
strokeText(text, x, y, maxWidth) {
|
|
351
|
+
this._context.strokeText(text, x, y, maxWidth);
|
|
352
|
+
}
|
|
353
|
+
transform(a, b, c, d, e, f) {
|
|
354
|
+
this._context.transform(a, b, c, d, e, f);
|
|
355
|
+
}
|
|
356
|
+
translate(x, y) {
|
|
357
|
+
this._context.translate(x, y);
|
|
358
|
+
}
|
|
359
|
+
_enableTrace() {
|
|
360
|
+
let that = this, len = CONTEXT_METHODS.length, origSetter = this.setAttr, n, args;
|
|
361
|
+
const func = function (methodName) {
|
|
362
|
+
let origMethod = that[methodName], ret;
|
|
363
|
+
that[methodName] = function () {
|
|
364
|
+
args = simplifyArray(Array.prototype.slice.call(arguments, 0));
|
|
365
|
+
ret = origMethod.apply(that, arguments);
|
|
366
|
+
that._trace({
|
|
367
|
+
method: methodName,
|
|
368
|
+
args: args,
|
|
369
|
+
});
|
|
370
|
+
return ret;
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
for (n = 0; n < len; n++) {
|
|
374
|
+
func(CONTEXT_METHODS[n]);
|
|
375
|
+
}
|
|
376
|
+
that.setAttr = function () {
|
|
377
|
+
origSetter.apply(that, arguments);
|
|
378
|
+
const prop = arguments[0];
|
|
379
|
+
let val = arguments[1];
|
|
380
|
+
if (prop === 'shadowOffsetX' ||
|
|
381
|
+
prop === 'shadowOffsetY' ||
|
|
382
|
+
prop === 'shadowBlur') {
|
|
383
|
+
val = val / this.canvas.getPixelRatio();
|
|
384
|
+
}
|
|
385
|
+
that._trace({
|
|
386
|
+
property: prop,
|
|
387
|
+
val: val,
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
_applyGlobalCompositeOperation(node) {
|
|
392
|
+
const op = node.attrs.globalCompositeOperation;
|
|
393
|
+
const def = !op || op === 'source-over';
|
|
394
|
+
if (!def) {
|
|
395
|
+
this.setAttr('globalCompositeOperation', op);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
CONTEXT_PROPERTIES.forEach(function (prop) {
|
|
400
|
+
Object.defineProperty(Context.prototype, prop, {
|
|
401
|
+
get() {
|
|
402
|
+
return this._context[prop];
|
|
403
|
+
},
|
|
404
|
+
set(val) {
|
|
405
|
+
this._context[prop] = val;
|
|
406
|
+
},
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
export class SceneContext extends Context {
|
|
410
|
+
constructor(canvas, { willReadFrequently = false } = {}) {
|
|
411
|
+
super(canvas);
|
|
412
|
+
this._context = canvas._canvas.getContext('2d', {
|
|
413
|
+
willReadFrequently,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
_fillColor(shape) {
|
|
417
|
+
const fill = shape.fill();
|
|
418
|
+
this.setAttr('fillStyle', fill);
|
|
419
|
+
shape._fillFunc(this);
|
|
420
|
+
}
|
|
421
|
+
_fillPattern(shape) {
|
|
422
|
+
this.setAttr('fillStyle', shape._getFillPattern());
|
|
423
|
+
shape._fillFunc(this);
|
|
424
|
+
}
|
|
425
|
+
_fillLinearGradient(shape) {
|
|
426
|
+
const grd = shape._getLinearGradient();
|
|
427
|
+
if (grd) {
|
|
428
|
+
this.setAttr('fillStyle', grd);
|
|
429
|
+
shape._fillFunc(this);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
_fillRadialGradient(shape) {
|
|
433
|
+
const grd = shape._getRadialGradient();
|
|
434
|
+
if (grd) {
|
|
435
|
+
this.setAttr('fillStyle', grd);
|
|
436
|
+
shape._fillFunc(this);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
_fill(shape) {
|
|
440
|
+
const hasColor = shape.fill(), fillPriority = shape.getFillPriority();
|
|
441
|
+
if (hasColor && fillPriority === 'color') {
|
|
442
|
+
this._fillColor(shape);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
const hasPattern = shape.getFillPatternImage();
|
|
446
|
+
if (hasPattern && fillPriority === 'pattern') {
|
|
447
|
+
this._fillPattern(shape);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const hasLinearGradient = shape.getFillLinearGradientColorStops();
|
|
451
|
+
if (hasLinearGradient && fillPriority === 'linear-gradient') {
|
|
452
|
+
this._fillLinearGradient(shape);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
const hasRadialGradient = shape.getFillRadialGradientColorStops();
|
|
456
|
+
if (hasRadialGradient && fillPriority === 'radial-gradient') {
|
|
457
|
+
this._fillRadialGradient(shape);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (hasColor) {
|
|
461
|
+
this._fillColor(shape);
|
|
462
|
+
}
|
|
463
|
+
else if (hasPattern) {
|
|
464
|
+
this._fillPattern(shape);
|
|
465
|
+
}
|
|
466
|
+
else if (hasLinearGradient) {
|
|
467
|
+
this._fillLinearGradient(shape);
|
|
468
|
+
}
|
|
469
|
+
else if (hasRadialGradient) {
|
|
470
|
+
this._fillRadialGradient(shape);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
_strokeLinearGradient(shape) {
|
|
474
|
+
const start = shape.getStrokeLinearGradientStartPoint(), end = shape.getStrokeLinearGradientEndPoint(), colorStops = shape.getStrokeLinearGradientColorStops(), grd = this.createLinearGradient(start.x, start.y, end.x, end.y);
|
|
475
|
+
if (colorStops) {
|
|
476
|
+
for (let n = 0; n < colorStops.length; n += 2) {
|
|
477
|
+
grd.addColorStop(colorStops[n], colorStops[n + 1]);
|
|
478
|
+
}
|
|
479
|
+
this.setAttr('strokeStyle', grd);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
_stroke(shape) {
|
|
483
|
+
const dash = shape.dash(), strokeScaleEnabled = shape.getStrokeScaleEnabled();
|
|
484
|
+
if (shape.hasStroke()) {
|
|
485
|
+
if (!strokeScaleEnabled) {
|
|
486
|
+
this.save();
|
|
487
|
+
const pixelRatio = this.getCanvas().getPixelRatio();
|
|
488
|
+
this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
|
|
489
|
+
}
|
|
490
|
+
this._applyLineCap(shape);
|
|
491
|
+
if (dash && shape.dashEnabled()) {
|
|
492
|
+
this.setLineDash(dash);
|
|
493
|
+
this.setAttr('lineDashOffset', shape.dashOffset());
|
|
494
|
+
}
|
|
495
|
+
this.setAttr('lineWidth', shape.strokeWidth());
|
|
496
|
+
if (!shape.getShadowForStrokeEnabled()) {
|
|
497
|
+
this.setAttr('shadowColor', 'rgba(0,0,0,0)');
|
|
498
|
+
}
|
|
499
|
+
const hasLinearGradient = shape.getStrokeLinearGradientColorStops();
|
|
500
|
+
if (hasLinearGradient) {
|
|
501
|
+
this._strokeLinearGradient(shape);
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
this.setAttr('strokeStyle', shape.stroke());
|
|
505
|
+
}
|
|
506
|
+
shape._strokeFunc(this);
|
|
507
|
+
if (!strokeScaleEnabled) {
|
|
508
|
+
this.restore();
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
_applyShadow(shape) {
|
|
513
|
+
var _a, _b, _c;
|
|
514
|
+
const color = (_a = shape.getShadowRGBA()) !== null && _a !== void 0 ? _a : 'black', blur = (_b = shape.getShadowBlur()) !== null && _b !== void 0 ? _b : 5, offset = (_c = shape.getShadowOffset()) !== null && _c !== void 0 ? _c : {
|
|
515
|
+
x: 0,
|
|
516
|
+
y: 0,
|
|
517
|
+
}, scale = shape.getAbsoluteScale(), ratio = this.canvas.getPixelRatio(), scaleX = scale.x * ratio, scaleY = scale.y * ratio;
|
|
518
|
+
this.setAttr('shadowColor', color);
|
|
519
|
+
this.setAttr('shadowBlur', blur * Math.min(Math.abs(scaleX), Math.abs(scaleY)));
|
|
520
|
+
this.setAttr('shadowOffsetX', offset.x * scaleX);
|
|
521
|
+
this.setAttr('shadowOffsetY', offset.y * scaleY);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
export class HitContext extends Context {
|
|
525
|
+
constructor(canvas) {
|
|
526
|
+
super(canvas);
|
|
527
|
+
this._context = canvas._canvas.getContext('2d', {
|
|
528
|
+
willReadFrequently: true,
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
_fill(shape) {
|
|
532
|
+
this.save();
|
|
533
|
+
this.setAttr('fillStyle', shape.colorKey);
|
|
534
|
+
shape._fillFuncHit(this);
|
|
535
|
+
this.restore();
|
|
536
|
+
}
|
|
537
|
+
strokeShape(shape) {
|
|
538
|
+
if (shape.hasHitStroke()) {
|
|
539
|
+
this._stroke(shape);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
_stroke(shape) {
|
|
543
|
+
if (shape.hasHitStroke()) {
|
|
544
|
+
const strokeScaleEnabled = shape.getStrokeScaleEnabled();
|
|
545
|
+
if (!strokeScaleEnabled) {
|
|
546
|
+
this.save();
|
|
547
|
+
const pixelRatio = this.getCanvas().getPixelRatio();
|
|
548
|
+
this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
|
|
549
|
+
}
|
|
550
|
+
this._applyLineCap(shape);
|
|
551
|
+
const hitStrokeWidth = shape.hitStrokeWidth();
|
|
552
|
+
const strokeWidth = hitStrokeWidth === 'auto' ? shape.strokeWidth() : hitStrokeWidth;
|
|
553
|
+
this.setAttr('lineWidth', strokeWidth);
|
|
554
|
+
this.setAttr('strokeStyle', shape.colorKey);
|
|
555
|
+
shape._strokeFuncHit(this);
|
|
556
|
+
if (!strokeScaleEnabled) {
|
|
557
|
+
this.restore();
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
package/lib/Core.d.ts
ADDED
package/lib/Core.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Node } from './Node.ts';
|
|
2
|
+
import type { Vector2d } from './types.ts';
|
|
3
|
+
export declare const DD: {
|
|
4
|
+
readonly isDragging: boolean;
|
|
5
|
+
justDragged: boolean;
|
|
6
|
+
readonly node: Node<import("./Node.ts").NodeConfig> | undefined;
|
|
7
|
+
_dragElements: Map<number, {
|
|
8
|
+
node: Node;
|
|
9
|
+
startPointerPos: Vector2d;
|
|
10
|
+
offset: Vector2d;
|
|
11
|
+
pointerId?: number;
|
|
12
|
+
dragStatus: "ready" | "dragging" | "stopped";
|
|
13
|
+
}>;
|
|
14
|
+
_drag(evt: any): void;
|
|
15
|
+
_endDragBefore(evt?: any): void;
|
|
16
|
+
_endDragAfter(evt: any): void;
|
|
17
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Konva } from "./Global.js";
|
|
2
|
+
import { Util } from "./Util.js";
|
|
3
|
+
export const DD = {
|
|
4
|
+
get isDragging() {
|
|
5
|
+
let flag = false;
|
|
6
|
+
DD._dragElements.forEach((elem) => {
|
|
7
|
+
if (elem.dragStatus === 'dragging') {
|
|
8
|
+
flag = true;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
return flag;
|
|
12
|
+
},
|
|
13
|
+
justDragged: false,
|
|
14
|
+
get node() {
|
|
15
|
+
let node;
|
|
16
|
+
DD._dragElements.forEach((elem) => {
|
|
17
|
+
node = elem.node;
|
|
18
|
+
});
|
|
19
|
+
return node;
|
|
20
|
+
},
|
|
21
|
+
_dragElements: new Map(),
|
|
22
|
+
_drag(evt) {
|
|
23
|
+
const nodesToFireEvents = [];
|
|
24
|
+
DD._dragElements.forEach((elem, key) => {
|
|
25
|
+
const { node } = elem;
|
|
26
|
+
const stage = node.getStage();
|
|
27
|
+
stage.setPointersPositions(evt);
|
|
28
|
+
if (elem.pointerId === undefined) {
|
|
29
|
+
elem.pointerId = Util._getFirstPointerId(evt);
|
|
30
|
+
}
|
|
31
|
+
const pos = stage._changedPointerPositions.find((pos) => pos.id === elem.pointerId);
|
|
32
|
+
if (!pos) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (elem.dragStatus !== 'dragging') {
|
|
36
|
+
const dragDistance = node.dragDistance();
|
|
37
|
+
const distance = Math.max(Math.abs(pos.x - elem.startPointerPos.x), Math.abs(pos.y - elem.startPointerPos.y));
|
|
38
|
+
if (distance < dragDistance) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
node.startDrag({ evt });
|
|
42
|
+
if (!node.isDragging()) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
node._setDragPosition(evt, elem);
|
|
47
|
+
nodesToFireEvents.push(node);
|
|
48
|
+
});
|
|
49
|
+
nodesToFireEvents.forEach((node) => {
|
|
50
|
+
node.fire('dragmove', {
|
|
51
|
+
type: 'dragmove',
|
|
52
|
+
target: node,
|
|
53
|
+
evt: evt,
|
|
54
|
+
}, true);
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
_endDragBefore(evt) {
|
|
58
|
+
const drawNodes = [];
|
|
59
|
+
DD._dragElements.forEach((elem) => {
|
|
60
|
+
const { node } = elem;
|
|
61
|
+
const stage = node.getStage();
|
|
62
|
+
if (evt) {
|
|
63
|
+
stage.setPointersPositions(evt);
|
|
64
|
+
}
|
|
65
|
+
const pos = stage._changedPointerPositions.find((pos) => pos.id === elem.pointerId);
|
|
66
|
+
if (!pos) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (elem.dragStatus === 'dragging' || elem.dragStatus === 'stopped') {
|
|
70
|
+
DD.justDragged = true;
|
|
71
|
+
Konva._mouseListenClick = false;
|
|
72
|
+
Konva._touchListenClick = false;
|
|
73
|
+
Konva._pointerListenClick = false;
|
|
74
|
+
elem.dragStatus = 'stopped';
|
|
75
|
+
}
|
|
76
|
+
const drawNode = elem.node.getLayer() ||
|
|
77
|
+
(elem.node instanceof Konva['Stage'] && elem.node);
|
|
78
|
+
if (drawNode && drawNodes.indexOf(drawNode) === -1) {
|
|
79
|
+
drawNodes.push(drawNode);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
drawNodes.forEach((drawNode) => {
|
|
83
|
+
drawNode.draw();
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
_endDragAfter(evt) {
|
|
87
|
+
DD._dragElements.forEach((elem, key) => {
|
|
88
|
+
if (elem.dragStatus === 'stopped') {
|
|
89
|
+
elem.node.fire('dragend', {
|
|
90
|
+
type: 'dragend',
|
|
91
|
+
target: elem.node,
|
|
92
|
+
evt: evt,
|
|
93
|
+
}, true);
|
|
94
|
+
}
|
|
95
|
+
if (elem.dragStatus !== 'dragging') {
|
|
96
|
+
DD._dragElements.delete(key);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
if (Konva.isBrowser) {
|
|
102
|
+
window.addEventListener('mouseup', DD._endDragBefore, true);
|
|
103
|
+
window.addEventListener('touchend', DD._endDragBefore, true);
|
|
104
|
+
window.addEventListener('touchcancel', DD._endDragBefore, true);
|
|
105
|
+
window.addEventListener('mousemove', DD._drag);
|
|
106
|
+
window.addEventListener('touchmove', DD._drag);
|
|
107
|
+
window.addEventListener('mouseup', DD._endDragAfter, false);
|
|
108
|
+
window.addEventListener('touchend', DD._endDragAfter, false);
|
|
109
|
+
window.addEventListener('touchcancel', DD._endDragAfter, false);
|
|
110
|
+
}
|