konva 8.1.4
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/CHANGELOG.md +1140 -0
- package/LICENSE +22 -0
- package/README.md +189 -0
- package/konva.js +18090 -0
- package/konva.min.js +12 -0
- package/lib/Animation.d.ts +24 -0
- package/lib/Animation.js +141 -0
- package/lib/Canvas.d.ts +32 -0
- package/lib/Canvas.js +107 -0
- package/lib/Container.d.ts +47 -0
- package/lib/Container.js +318 -0
- package/lib/Context.d.ts +76 -0
- package/lib/Context.js +520 -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 +104 -0
- package/lib/Factory.d.ts +11 -0
- package/lib/Factory.js +117 -0
- package/lib/FastLayer.d.ts +4 -0
- package/lib/FastLayer.js +12 -0
- package/lib/Global.d.ts +30 -0
- package/lib/Global.js +53 -0
- package/lib/Group.d.ts +6 -0
- package/lib/Group.js +13 -0
- package/lib/Layer.d.ts +60 -0
- package/lib/Layer.js +301 -0
- package/lib/Node.d.ts +318 -0
- package/lib/Node.js +1381 -0
- package/lib/PointerEvents.d.ts +11 -0
- package/lib/PointerEvents.js +37 -0
- package/lib/Shape.d.ts +184 -0
- package/lib/Shape.js +515 -0
- package/lib/Stage.d.ts +85 -0
- package/lib/Stage.js +687 -0
- package/lib/Tween.d.ts +86 -0
- package/lib/Tween.js +516 -0
- package/lib/Util.d.ts +87 -0
- package/lib/Util.js +742 -0
- package/lib/Validators.d.ts +11 -0
- package/lib/Validators.js +171 -0
- package/lib/_CoreInternals.d.ts +144 -0
- package/lib/_CoreInternals.js +34 -0
- package/lib/_FullInternals.d.ts +188 -0
- package/lib/_FullInternals.js +78 -0
- package/lib/filters/Blur.d.ts +2 -0
- package/lib/filters/Blur.js +697 -0
- package/lib/filters/Brighten.d.ts +2 -0
- package/lib/filters/Brighten.js +12 -0
- package/lib/filters/Contrast.d.ts +2 -0
- package/lib/filters/Contrast.js +34 -0
- package/lib/filters/Emboss.d.ts +2 -0
- package/lib/filters/Emboss.js +102 -0
- package/lib/filters/Enhance.d.ts +2 -0
- package/lib/filters/Enhance.js +83 -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 +24 -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 +132 -0
- package/lib/filters/Mask.d.ts +2 -0
- package/lib/filters/Mask.js +140 -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 +39 -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 +25 -0
- package/lib/filters/Threshold.d.ts +2 -0
- package/lib/filters/Threshold.js +10 -0
- package/lib/index-node.d.ts +2 -0
- package/lib/index-node.js +18 -0
- package/lib/index-types.d.ts +179 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/shapes/Arc.d.ts +19 -0
- package/lib/shapes/Arc.js +35 -0
- package/lib/shapes/Arrow.d.ts +24 -0
- package/lib/shapes/Arrow.js +94 -0
- package/lib/shapes/Circle.d.ts +13 -0
- package/lib/shapes/Circle.js +33 -0
- package/lib/shapes/Ellipse.d.ts +16 -0
- package/lib/shapes/Ellipse.js +37 -0
- package/lib/shapes/Image.d.ts +23 -0
- package/lib/shapes/Image.js +100 -0
- package/lib/shapes/Label.d.ts +35 -0
- package/lib/shapes/Label.js +166 -0
- package/lib/shapes/Line.d.ts +28 -0
- package/lib/shapes/Line.js +155 -0
- package/lib/shapes/Path.d.ts +37 -0
- package/lib/shapes/Path.js +627 -0
- package/lib/shapes/Rect.d.ts +9 -0
- package/lib/shapes/Rect.js +42 -0
- package/lib/shapes/RegularPolygon.d.ts +22 -0
- package/lib/shapes/RegularPolygon.js +65 -0
- package/lib/shapes/Ring.d.ts +16 -0
- package/lib/shapes/Ring.js +33 -0
- package/lib/shapes/Sprite.d.ts +30 -0
- package/lib/shapes/Sprite.js +108 -0
- package/lib/shapes/Star.d.ts +17 -0
- package/lib/shapes/Star.js +38 -0
- package/lib/shapes/Text.d.ts +59 -0
- package/lib/shapes/Text.js +347 -0
- package/lib/shapes/TextPath.d.ts +54 -0
- package/lib/shapes/TextPath.js +343 -0
- package/lib/shapes/Transformer.d.ts +112 -0
- package/lib/shapes/Transformer.js +944 -0
- package/lib/shapes/Wedge.d.ts +17 -0
- package/lib/shapes/Wedge.js +38 -0
- package/lib/types.d.ts +50 -0
- package/lib/types.js +22 -0
- package/package.json +111 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { Factory } from '../Factory.js';
|
|
2
|
+
import { Shape } from '../Shape.js';
|
|
3
|
+
import { Group } from '../Group.js';
|
|
4
|
+
import { getNumberOrArrayOfNumbersValidator, getNumberValidator, } from '../Validators.js';
|
|
5
|
+
import { _registerNode } from '../Global.js';
|
|
6
|
+
var ATTR_CHANGE_LIST = [
|
|
7
|
+
'fontFamily',
|
|
8
|
+
'fontSize',
|
|
9
|
+
'fontStyle',
|
|
10
|
+
'padding',
|
|
11
|
+
'lineHeight',
|
|
12
|
+
'text',
|
|
13
|
+
'width',
|
|
14
|
+
'height',
|
|
15
|
+
], CHANGE_KONVA = 'Change.konva', NONE = 'none', UP = 'up', RIGHT = 'right', DOWN = 'down', LEFT = 'left', attrChangeListLen = ATTR_CHANGE_LIST.length;
|
|
16
|
+
export class Label extends Group {
|
|
17
|
+
constructor(config) {
|
|
18
|
+
super(config);
|
|
19
|
+
this.on('add.konva', function (evt) {
|
|
20
|
+
this._addListeners(evt.child);
|
|
21
|
+
this._sync();
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
getText() {
|
|
25
|
+
return this.find('Text')[0];
|
|
26
|
+
}
|
|
27
|
+
getTag() {
|
|
28
|
+
return this.find('Tag')[0];
|
|
29
|
+
}
|
|
30
|
+
_addListeners(text) {
|
|
31
|
+
var that = this, n;
|
|
32
|
+
var func = function () {
|
|
33
|
+
that._sync();
|
|
34
|
+
};
|
|
35
|
+
for (n = 0; n < attrChangeListLen; n++) {
|
|
36
|
+
text.on(ATTR_CHANGE_LIST[n] + CHANGE_KONVA, func);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
getWidth() {
|
|
40
|
+
return this.getText().width();
|
|
41
|
+
}
|
|
42
|
+
getHeight() {
|
|
43
|
+
return this.getText().height();
|
|
44
|
+
}
|
|
45
|
+
_sync() {
|
|
46
|
+
var text = this.getText(), tag = this.getTag(), width, height, pointerDirection, pointerWidth, x, y, pointerHeight;
|
|
47
|
+
if (text && tag) {
|
|
48
|
+
width = text.width();
|
|
49
|
+
height = text.height();
|
|
50
|
+
pointerDirection = tag.pointerDirection();
|
|
51
|
+
pointerWidth = tag.pointerWidth();
|
|
52
|
+
pointerHeight = tag.pointerHeight();
|
|
53
|
+
x = 0;
|
|
54
|
+
y = 0;
|
|
55
|
+
switch (pointerDirection) {
|
|
56
|
+
case UP:
|
|
57
|
+
x = width / 2;
|
|
58
|
+
y = -1 * pointerHeight;
|
|
59
|
+
break;
|
|
60
|
+
case RIGHT:
|
|
61
|
+
x = width + pointerWidth;
|
|
62
|
+
y = height / 2;
|
|
63
|
+
break;
|
|
64
|
+
case DOWN:
|
|
65
|
+
x = width / 2;
|
|
66
|
+
y = height + pointerHeight;
|
|
67
|
+
break;
|
|
68
|
+
case LEFT:
|
|
69
|
+
x = -1 * pointerWidth;
|
|
70
|
+
y = height / 2;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
tag.setAttrs({
|
|
74
|
+
x: -1 * x,
|
|
75
|
+
y: -1 * y,
|
|
76
|
+
width: width,
|
|
77
|
+
height: height,
|
|
78
|
+
});
|
|
79
|
+
text.setAttrs({
|
|
80
|
+
x: -1 * x,
|
|
81
|
+
y: -1 * y,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
Label.prototype.className = 'Label';
|
|
87
|
+
_registerNode(Label);
|
|
88
|
+
export class Tag extends Shape {
|
|
89
|
+
_sceneFunc(context) {
|
|
90
|
+
var width = this.width(), height = this.height(), pointerDirection = this.pointerDirection(), pointerWidth = this.pointerWidth(), pointerHeight = this.pointerHeight(), cornerRadius = this.cornerRadius();
|
|
91
|
+
let topLeft = 0;
|
|
92
|
+
let topRight = 0;
|
|
93
|
+
let bottomLeft = 0;
|
|
94
|
+
let bottomRight = 0;
|
|
95
|
+
if (typeof cornerRadius === 'number') {
|
|
96
|
+
topLeft = topRight = bottomLeft = bottomRight = Math.min(cornerRadius, width / 2, height / 2);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
topLeft = Math.min(cornerRadius[0] || 0, width / 2, height / 2);
|
|
100
|
+
topRight = Math.min(cornerRadius[1] || 0, width / 2, height / 2);
|
|
101
|
+
bottomRight = Math.min(cornerRadius[2] || 0, width / 2, height / 2);
|
|
102
|
+
bottomLeft = Math.min(cornerRadius[3] || 0, width / 2, height / 2);
|
|
103
|
+
}
|
|
104
|
+
context.beginPath();
|
|
105
|
+
context.moveTo(topLeft, 0);
|
|
106
|
+
if (pointerDirection === UP) {
|
|
107
|
+
context.lineTo((width - pointerWidth) / 2, 0);
|
|
108
|
+
context.lineTo(width / 2, -1 * pointerHeight);
|
|
109
|
+
context.lineTo((width + pointerWidth) / 2, 0);
|
|
110
|
+
}
|
|
111
|
+
context.lineTo(width - topRight, 0);
|
|
112
|
+
context.arc(width - topRight, topRight, topRight, (Math.PI * 3) / 2, 0, false);
|
|
113
|
+
if (pointerDirection === RIGHT) {
|
|
114
|
+
context.lineTo(width, (height - pointerHeight) / 2);
|
|
115
|
+
context.lineTo(width + pointerWidth, height / 2);
|
|
116
|
+
context.lineTo(width, (height + pointerHeight) / 2);
|
|
117
|
+
}
|
|
118
|
+
context.lineTo(width, height - bottomRight);
|
|
119
|
+
context.arc(width - bottomRight, height - bottomRight, bottomRight, 0, Math.PI / 2, false);
|
|
120
|
+
if (pointerDirection === DOWN) {
|
|
121
|
+
context.lineTo((width + pointerWidth) / 2, height);
|
|
122
|
+
context.lineTo(width / 2, height + pointerHeight);
|
|
123
|
+
context.lineTo((width - pointerWidth) / 2, height);
|
|
124
|
+
}
|
|
125
|
+
context.lineTo(bottomLeft, height);
|
|
126
|
+
context.arc(bottomLeft, height - bottomLeft, bottomLeft, Math.PI / 2, Math.PI, false);
|
|
127
|
+
if (pointerDirection === LEFT) {
|
|
128
|
+
context.lineTo(0, (height + pointerHeight) / 2);
|
|
129
|
+
context.lineTo(-1 * pointerWidth, height / 2);
|
|
130
|
+
context.lineTo(0, (height - pointerHeight) / 2);
|
|
131
|
+
}
|
|
132
|
+
context.lineTo(0, topLeft);
|
|
133
|
+
context.arc(topLeft, topLeft, topLeft, Math.PI, (Math.PI * 3) / 2, false);
|
|
134
|
+
context.closePath();
|
|
135
|
+
context.fillStrokeShape(this);
|
|
136
|
+
}
|
|
137
|
+
getSelfRect() {
|
|
138
|
+
var x = 0, y = 0, pointerWidth = this.pointerWidth(), pointerHeight = this.pointerHeight(), direction = this.pointerDirection(), width = this.width(), height = this.height();
|
|
139
|
+
if (direction === UP) {
|
|
140
|
+
y -= pointerHeight;
|
|
141
|
+
height += pointerHeight;
|
|
142
|
+
}
|
|
143
|
+
else if (direction === DOWN) {
|
|
144
|
+
height += pointerHeight;
|
|
145
|
+
}
|
|
146
|
+
else if (direction === LEFT) {
|
|
147
|
+
x -= pointerWidth * 1.5;
|
|
148
|
+
width += pointerWidth;
|
|
149
|
+
}
|
|
150
|
+
else if (direction === RIGHT) {
|
|
151
|
+
width += pointerWidth * 1.5;
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
x: x,
|
|
155
|
+
y: y,
|
|
156
|
+
width: width,
|
|
157
|
+
height: height,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
Tag.prototype.className = 'Tag';
|
|
162
|
+
_registerNode(Tag);
|
|
163
|
+
Factory.addGetterSetter(Tag, 'pointerDirection', NONE);
|
|
164
|
+
Factory.addGetterSetter(Tag, 'pointerWidth', 0, getNumberValidator());
|
|
165
|
+
Factory.addGetterSetter(Tag, 'pointerHeight', 0, getNumberValidator());
|
|
166
|
+
Factory.addGetterSetter(Tag, 'cornerRadius', 0, getNumberOrArrayOfNumbersValidator(4));
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Shape, ShapeConfig } from '../Shape';
|
|
2
|
+
import { GetSet } from '../types';
|
|
3
|
+
import { Context } from '../Context';
|
|
4
|
+
export interface LineConfig extends ShapeConfig {
|
|
5
|
+
points?: number[];
|
|
6
|
+
tension?: number;
|
|
7
|
+
closed?: boolean;
|
|
8
|
+
bezier?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare class Line<Config extends LineConfig = LineConfig> extends Shape<Config> {
|
|
11
|
+
constructor(config?: Config);
|
|
12
|
+
_sceneFunc(context: Context): void;
|
|
13
|
+
getTensionPoints(): any;
|
|
14
|
+
_getTensionPoints(): any[];
|
|
15
|
+
_getTensionPointsClosed(): any[];
|
|
16
|
+
getWidth(): number;
|
|
17
|
+
getHeight(): number;
|
|
18
|
+
getSelfRect(): {
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
};
|
|
24
|
+
closed: GetSet<boolean, this>;
|
|
25
|
+
bezier: GetSet<boolean, this>;
|
|
26
|
+
tension: GetSet<number, this>;
|
|
27
|
+
points: GetSet<number[], this>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Factory } from '../Factory.js';
|
|
2
|
+
import { Shape } from '../Shape.js';
|
|
3
|
+
import { getNumberValidator, getNumberArrayValidator } from '../Validators.js';
|
|
4
|
+
import { _registerNode } from '../Global.js';
|
|
5
|
+
function getControlPoints(x0, y0, x1, y1, x2, y2, t) {
|
|
6
|
+
var d01 = Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2)), d12 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)), fa = (t * d01) / (d01 + d12), fb = (t * d12) / (d01 + d12), p1x = x1 - fa * (x2 - x0), p1y = y1 - fa * (y2 - y0), p2x = x1 + fb * (x2 - x0), p2y = y1 + fb * (y2 - y0);
|
|
7
|
+
return [p1x, p1y, p2x, p2y];
|
|
8
|
+
}
|
|
9
|
+
function expandPoints(p, tension) {
|
|
10
|
+
var len = p.length, allPoints = [], n, cp;
|
|
11
|
+
for (n = 2; n < len - 2; n += 2) {
|
|
12
|
+
cp = getControlPoints(p[n - 2], p[n - 1], p[n], p[n + 1], p[n + 2], p[n + 3], tension);
|
|
13
|
+
if (isNaN(cp[0])) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
allPoints.push(cp[0]);
|
|
17
|
+
allPoints.push(cp[1]);
|
|
18
|
+
allPoints.push(p[n]);
|
|
19
|
+
allPoints.push(p[n + 1]);
|
|
20
|
+
allPoints.push(cp[2]);
|
|
21
|
+
allPoints.push(cp[3]);
|
|
22
|
+
}
|
|
23
|
+
return allPoints;
|
|
24
|
+
}
|
|
25
|
+
export class Line extends Shape {
|
|
26
|
+
constructor(config) {
|
|
27
|
+
super(config);
|
|
28
|
+
this.on('pointsChange.konva tensionChange.konva closedChange.konva bezierChange.konva', function () {
|
|
29
|
+
this._clearCache('tensionPoints');
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
_sceneFunc(context) {
|
|
33
|
+
var points = this.points(), length = points.length, tension = this.tension(), closed = this.closed(), bezier = this.bezier(), tp, len, n;
|
|
34
|
+
if (!length) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
context.beginPath();
|
|
38
|
+
context.moveTo(points[0], points[1]);
|
|
39
|
+
if (tension !== 0 && length > 4) {
|
|
40
|
+
tp = this.getTensionPoints();
|
|
41
|
+
len = tp.length;
|
|
42
|
+
n = closed ? 0 : 4;
|
|
43
|
+
if (!closed) {
|
|
44
|
+
context.quadraticCurveTo(tp[0], tp[1], tp[2], tp[3]);
|
|
45
|
+
}
|
|
46
|
+
while (n < len - 2) {
|
|
47
|
+
context.bezierCurveTo(tp[n++], tp[n++], tp[n++], tp[n++], tp[n++], tp[n++]);
|
|
48
|
+
}
|
|
49
|
+
if (!closed) {
|
|
50
|
+
context.quadraticCurveTo(tp[len - 2], tp[len - 1], points[length - 2], points[length - 1]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (bezier) {
|
|
54
|
+
n = 2;
|
|
55
|
+
while (n < length) {
|
|
56
|
+
context.bezierCurveTo(points[n++], points[n++], points[n++], points[n++], points[n++], points[n++]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
for (n = 2; n < length; n += 2) {
|
|
61
|
+
context.lineTo(points[n], points[n + 1]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (closed) {
|
|
65
|
+
context.closePath();
|
|
66
|
+
context.fillStrokeShape(this);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
context.strokeShape(this);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
getTensionPoints() {
|
|
73
|
+
return this._getCache('tensionPoints', this._getTensionPoints);
|
|
74
|
+
}
|
|
75
|
+
_getTensionPoints() {
|
|
76
|
+
if (this.closed()) {
|
|
77
|
+
return this._getTensionPointsClosed();
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return expandPoints(this.points(), this.tension());
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
_getTensionPointsClosed() {
|
|
84
|
+
var p = this.points(), len = p.length, tension = this.tension(), firstControlPoints = getControlPoints(p[len - 2], p[len - 1], p[0], p[1], p[2], p[3], tension), lastControlPoints = getControlPoints(p[len - 4], p[len - 3], p[len - 2], p[len - 1], p[0], p[1], tension), middle = expandPoints(p, tension), tp = [firstControlPoints[2], firstControlPoints[3]]
|
|
85
|
+
.concat(middle)
|
|
86
|
+
.concat([
|
|
87
|
+
lastControlPoints[0],
|
|
88
|
+
lastControlPoints[1],
|
|
89
|
+
p[len - 2],
|
|
90
|
+
p[len - 1],
|
|
91
|
+
lastControlPoints[2],
|
|
92
|
+
lastControlPoints[3],
|
|
93
|
+
firstControlPoints[0],
|
|
94
|
+
firstControlPoints[1],
|
|
95
|
+
p[0],
|
|
96
|
+
p[1],
|
|
97
|
+
]);
|
|
98
|
+
return tp;
|
|
99
|
+
}
|
|
100
|
+
getWidth() {
|
|
101
|
+
return this.getSelfRect().width;
|
|
102
|
+
}
|
|
103
|
+
getHeight() {
|
|
104
|
+
return this.getSelfRect().height;
|
|
105
|
+
}
|
|
106
|
+
getSelfRect() {
|
|
107
|
+
var points = this.points();
|
|
108
|
+
if (points.length < 4) {
|
|
109
|
+
return {
|
|
110
|
+
x: points[0] || 0,
|
|
111
|
+
y: points[1] || 0,
|
|
112
|
+
width: 0,
|
|
113
|
+
height: 0,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (this.tension() !== 0) {
|
|
117
|
+
points = [
|
|
118
|
+
points[0],
|
|
119
|
+
points[1],
|
|
120
|
+
...this._getTensionPoints(),
|
|
121
|
+
points[points.length - 2],
|
|
122
|
+
points[points.length - 1],
|
|
123
|
+
];
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
points = this.points();
|
|
127
|
+
}
|
|
128
|
+
var minX = points[0];
|
|
129
|
+
var maxX = points[0];
|
|
130
|
+
var minY = points[1];
|
|
131
|
+
var maxY = points[1];
|
|
132
|
+
var x, y;
|
|
133
|
+
for (var i = 0; i < points.length / 2; i++) {
|
|
134
|
+
x = points[i * 2];
|
|
135
|
+
y = points[i * 2 + 1];
|
|
136
|
+
minX = Math.min(minX, x);
|
|
137
|
+
maxX = Math.max(maxX, x);
|
|
138
|
+
minY = Math.min(minY, y);
|
|
139
|
+
maxY = Math.max(maxY, y);
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
x: minX,
|
|
143
|
+
y: minY,
|
|
144
|
+
width: maxX - minX,
|
|
145
|
+
height: maxY - minY,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
Line.prototype.className = 'Line';
|
|
150
|
+
Line.prototype._attrsAffectingSize = ['points', 'bezier', 'tension'];
|
|
151
|
+
_registerNode(Line);
|
|
152
|
+
Factory.addGetterSetter(Line, 'closed', false);
|
|
153
|
+
Factory.addGetterSetter(Line, 'bezier', false);
|
|
154
|
+
Factory.addGetterSetter(Line, 'tension', 0, getNumberValidator());
|
|
155
|
+
Factory.addGetterSetter(Line, 'points', [], getNumberArrayValidator());
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Shape, ShapeConfig } from '../Shape';
|
|
2
|
+
import { GetSet } from '../types';
|
|
3
|
+
export interface PathConfig extends ShapeConfig {
|
|
4
|
+
data?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class Path extends Shape<PathConfig> {
|
|
7
|
+
dataArray: any[];
|
|
8
|
+
pathLength: number;
|
|
9
|
+
constructor(config?: PathConfig);
|
|
10
|
+
_sceneFunc(context: any): void;
|
|
11
|
+
getSelfRect(): {
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
};
|
|
17
|
+
getLength(): number;
|
|
18
|
+
getPointAtLength(length: any): any;
|
|
19
|
+
data: GetSet<string, this>;
|
|
20
|
+
static getLineLength(x1: any, y1: any, x2: any, y2: any): number;
|
|
21
|
+
static getPointOnLine(dist: any, P1x: any, P1y: any, P2x: any, P2y: any, fromX?: any, fromY?: any): any;
|
|
22
|
+
static getPointOnCubicBezier(pct: any, P1x: any, P1y: any, P2x: any, P2y: any, P3x: any, P3y: any, P4x: any, P4y: any): {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
};
|
|
26
|
+
static getPointOnQuadraticBezier(pct: any, P1x: any, P1y: any, P2x: any, P2y: any, P3x: any, P3y: any): {
|
|
27
|
+
x: number;
|
|
28
|
+
y: number;
|
|
29
|
+
};
|
|
30
|
+
static getPointOnEllipticalArc(cx: any, cy: any, rx: any, ry: any, theta: any, psi: any): {
|
|
31
|
+
x: any;
|
|
32
|
+
y: any;
|
|
33
|
+
};
|
|
34
|
+
static parsePathData(data: any): any[];
|
|
35
|
+
static calcLength(x: any, y: any, cmd: any, points: any): any;
|
|
36
|
+
static convertEndpointToCenterParameterization(x1: any, y1: any, x2: any, y2: any, fa: any, fs: any, rx: any, ry: any, psiDeg: any): any[];
|
|
37
|
+
}
|
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
import { Factory } from '../Factory.js';
|
|
2
|
+
import { Shape } from '../Shape.js';
|
|
3
|
+
import { _registerNode } from '../Global.js';
|
|
4
|
+
export class Path extends Shape {
|
|
5
|
+
constructor(config) {
|
|
6
|
+
super(config);
|
|
7
|
+
this.dataArray = [];
|
|
8
|
+
this.pathLength = 0;
|
|
9
|
+
this.dataArray = Path.parsePathData(this.data());
|
|
10
|
+
this.pathLength = 0;
|
|
11
|
+
for (var i = 0; i < this.dataArray.length; ++i) {
|
|
12
|
+
this.pathLength += this.dataArray[i].pathLength;
|
|
13
|
+
}
|
|
14
|
+
this.on('dataChange.konva', function () {
|
|
15
|
+
this.dataArray = Path.parsePathData(this.data());
|
|
16
|
+
this.pathLength = 0;
|
|
17
|
+
for (var i = 0; i < this.dataArray.length; ++i) {
|
|
18
|
+
this.pathLength += this.dataArray[i].pathLength;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
_sceneFunc(context) {
|
|
23
|
+
var ca = this.dataArray;
|
|
24
|
+
context.beginPath();
|
|
25
|
+
var isClosed = false;
|
|
26
|
+
for (var n = 0; n < ca.length; n++) {
|
|
27
|
+
var c = ca[n].command;
|
|
28
|
+
var p = ca[n].points;
|
|
29
|
+
switch (c) {
|
|
30
|
+
case 'L':
|
|
31
|
+
context.lineTo(p[0], p[1]);
|
|
32
|
+
break;
|
|
33
|
+
case 'M':
|
|
34
|
+
context.moveTo(p[0], p[1]);
|
|
35
|
+
break;
|
|
36
|
+
case 'C':
|
|
37
|
+
context.bezierCurveTo(p[0], p[1], p[2], p[3], p[4], p[5]);
|
|
38
|
+
break;
|
|
39
|
+
case 'Q':
|
|
40
|
+
context.quadraticCurveTo(p[0], p[1], p[2], p[3]);
|
|
41
|
+
break;
|
|
42
|
+
case 'A':
|
|
43
|
+
var cx = p[0], cy = p[1], rx = p[2], ry = p[3], theta = p[4], dTheta = p[5], psi = p[6], fs = p[7];
|
|
44
|
+
var r = rx > ry ? rx : ry;
|
|
45
|
+
var scaleX = rx > ry ? 1 : rx / ry;
|
|
46
|
+
var scaleY = rx > ry ? ry / rx : 1;
|
|
47
|
+
context.translate(cx, cy);
|
|
48
|
+
context.rotate(psi);
|
|
49
|
+
context.scale(scaleX, scaleY);
|
|
50
|
+
context.arc(0, 0, r, theta, theta + dTheta, 1 - fs);
|
|
51
|
+
context.scale(1 / scaleX, 1 / scaleY);
|
|
52
|
+
context.rotate(-psi);
|
|
53
|
+
context.translate(-cx, -cy);
|
|
54
|
+
break;
|
|
55
|
+
case 'z':
|
|
56
|
+
isClosed = true;
|
|
57
|
+
context.closePath();
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!isClosed && !this.hasFill()) {
|
|
62
|
+
context.strokeShape(this);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
context.fillStrokeShape(this);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
getSelfRect() {
|
|
69
|
+
var points = [];
|
|
70
|
+
this.dataArray.forEach(function (data) {
|
|
71
|
+
if (data.command === 'A') {
|
|
72
|
+
var start = data.points[4];
|
|
73
|
+
var dTheta = data.points[5];
|
|
74
|
+
var end = data.points[4] + dTheta;
|
|
75
|
+
var inc = Math.PI / 180.0;
|
|
76
|
+
if (Math.abs(start - end) < inc) {
|
|
77
|
+
inc = Math.abs(start - end);
|
|
78
|
+
}
|
|
79
|
+
if (dTheta < 0) {
|
|
80
|
+
for (let t = start - inc; t > end; t -= inc) {
|
|
81
|
+
const point = Path.getPointOnEllipticalArc(data.points[0], data.points[1], data.points[2], data.points[3], t, 0);
|
|
82
|
+
points.push(point.x, point.y);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
for (let t = start + inc; t < end; t += inc) {
|
|
87
|
+
const point = Path.getPointOnEllipticalArc(data.points[0], data.points[1], data.points[2], data.points[3], t, 0);
|
|
88
|
+
points.push(point.x, point.y);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else if (data.command === 'C') {
|
|
93
|
+
for (let t = 0.0; t <= 1; t += 0.01) {
|
|
94
|
+
const point = Path.getPointOnCubicBezier(t, data.start.x, data.start.y, data.points[0], data.points[1], data.points[2], data.points[3], data.points[4], data.points[5]);
|
|
95
|
+
points.push(point.x, point.y);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
points = points.concat(data.points);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
var minX = points[0];
|
|
103
|
+
var maxX = points[0];
|
|
104
|
+
var minY = points[1];
|
|
105
|
+
var maxY = points[1];
|
|
106
|
+
var x, y;
|
|
107
|
+
for (var i = 0; i < points.length / 2; i++) {
|
|
108
|
+
x = points[i * 2];
|
|
109
|
+
y = points[i * 2 + 1];
|
|
110
|
+
if (!isNaN(x)) {
|
|
111
|
+
minX = Math.min(minX, x);
|
|
112
|
+
maxX = Math.max(maxX, x);
|
|
113
|
+
}
|
|
114
|
+
if (!isNaN(y)) {
|
|
115
|
+
minY = Math.min(minY, y);
|
|
116
|
+
maxY = Math.max(maxY, y);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
x: Math.round(minX),
|
|
121
|
+
y: Math.round(minY),
|
|
122
|
+
width: Math.round(maxX - minX),
|
|
123
|
+
height: Math.round(maxY - minY),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
getLength() {
|
|
127
|
+
return this.pathLength;
|
|
128
|
+
}
|
|
129
|
+
getPointAtLength(length) {
|
|
130
|
+
var point, i = 0, ii = this.dataArray.length;
|
|
131
|
+
if (!ii) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
while (i < ii && length > this.dataArray[i].pathLength) {
|
|
135
|
+
length -= this.dataArray[i].pathLength;
|
|
136
|
+
++i;
|
|
137
|
+
}
|
|
138
|
+
if (i === ii) {
|
|
139
|
+
point = this.dataArray[i - 1].points.slice(-2);
|
|
140
|
+
return {
|
|
141
|
+
x: point[0],
|
|
142
|
+
y: point[1],
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (length < 0.01) {
|
|
146
|
+
point = this.dataArray[i].points.slice(0, 2);
|
|
147
|
+
return {
|
|
148
|
+
x: point[0],
|
|
149
|
+
y: point[1],
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
var cp = this.dataArray[i];
|
|
153
|
+
var p = cp.points;
|
|
154
|
+
switch (cp.command) {
|
|
155
|
+
case 'L':
|
|
156
|
+
return Path.getPointOnLine(length, cp.start.x, cp.start.y, p[0], p[1]);
|
|
157
|
+
case 'C':
|
|
158
|
+
return Path.getPointOnCubicBezier(length / cp.pathLength, cp.start.x, cp.start.y, p[0], p[1], p[2], p[3], p[4], p[5]);
|
|
159
|
+
case 'Q':
|
|
160
|
+
return Path.getPointOnQuadraticBezier(length / cp.pathLength, cp.start.x, cp.start.y, p[0], p[1], p[2], p[3]);
|
|
161
|
+
case 'A':
|
|
162
|
+
var cx = p[0], cy = p[1], rx = p[2], ry = p[3], theta = p[4], dTheta = p[5], psi = p[6];
|
|
163
|
+
theta += (dTheta * length) / cp.pathLength;
|
|
164
|
+
return Path.getPointOnEllipticalArc(cx, cy, rx, ry, theta, psi);
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
static getLineLength(x1, y1, x2, y2) {
|
|
169
|
+
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
|
|
170
|
+
}
|
|
171
|
+
static getPointOnLine(dist, P1x, P1y, P2x, P2y, fromX, fromY) {
|
|
172
|
+
if (fromX === undefined) {
|
|
173
|
+
fromX = P1x;
|
|
174
|
+
}
|
|
175
|
+
if (fromY === undefined) {
|
|
176
|
+
fromY = P1y;
|
|
177
|
+
}
|
|
178
|
+
var m = (P2y - P1y) / (P2x - P1x + 0.00000001);
|
|
179
|
+
var run = Math.sqrt((dist * dist) / (1 + m * m));
|
|
180
|
+
if (P2x < P1x) {
|
|
181
|
+
run *= -1;
|
|
182
|
+
}
|
|
183
|
+
var rise = m * run;
|
|
184
|
+
var pt;
|
|
185
|
+
if (P2x === P1x) {
|
|
186
|
+
pt = {
|
|
187
|
+
x: fromX,
|
|
188
|
+
y: fromY + rise,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
else if ((fromY - P1y) / (fromX - P1x + 0.00000001) === m) {
|
|
192
|
+
pt = {
|
|
193
|
+
x: fromX + run,
|
|
194
|
+
y: fromY + rise,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
var ix, iy;
|
|
199
|
+
var len = this.getLineLength(P1x, P1y, P2x, P2y);
|
|
200
|
+
var u = (fromX - P1x) * (P2x - P1x) + (fromY - P1y) * (P2y - P1y);
|
|
201
|
+
u = u / (len * len);
|
|
202
|
+
ix = P1x + u * (P2x - P1x);
|
|
203
|
+
iy = P1y + u * (P2y - P1y);
|
|
204
|
+
var pRise = this.getLineLength(fromX, fromY, ix, iy);
|
|
205
|
+
var pRun = Math.sqrt(dist * dist - pRise * pRise);
|
|
206
|
+
run = Math.sqrt((pRun * pRun) / (1 + m * m));
|
|
207
|
+
if (P2x < P1x) {
|
|
208
|
+
run *= -1;
|
|
209
|
+
}
|
|
210
|
+
rise = m * run;
|
|
211
|
+
pt = {
|
|
212
|
+
x: ix + run,
|
|
213
|
+
y: iy + rise,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
return pt;
|
|
217
|
+
}
|
|
218
|
+
static getPointOnCubicBezier(pct, P1x, P1y, P2x, P2y, P3x, P3y, P4x, P4y) {
|
|
219
|
+
function CB1(t) {
|
|
220
|
+
return t * t * t;
|
|
221
|
+
}
|
|
222
|
+
function CB2(t) {
|
|
223
|
+
return 3 * t * t * (1 - t);
|
|
224
|
+
}
|
|
225
|
+
function CB3(t) {
|
|
226
|
+
return 3 * t * (1 - t) * (1 - t);
|
|
227
|
+
}
|
|
228
|
+
function CB4(t) {
|
|
229
|
+
return (1 - t) * (1 - t) * (1 - t);
|
|
230
|
+
}
|
|
231
|
+
var x = P4x * CB1(pct) + P3x * CB2(pct) + P2x * CB3(pct) + P1x * CB4(pct);
|
|
232
|
+
var y = P4y * CB1(pct) + P3y * CB2(pct) + P2y * CB3(pct) + P1y * CB4(pct);
|
|
233
|
+
return {
|
|
234
|
+
x: x,
|
|
235
|
+
y: y,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
static getPointOnQuadraticBezier(pct, P1x, P1y, P2x, P2y, P3x, P3y) {
|
|
239
|
+
function QB1(t) {
|
|
240
|
+
return t * t;
|
|
241
|
+
}
|
|
242
|
+
function QB2(t) {
|
|
243
|
+
return 2 * t * (1 - t);
|
|
244
|
+
}
|
|
245
|
+
function QB3(t) {
|
|
246
|
+
return (1 - t) * (1 - t);
|
|
247
|
+
}
|
|
248
|
+
var x = P3x * QB1(pct) + P2x * QB2(pct) + P1x * QB3(pct);
|
|
249
|
+
var y = P3y * QB1(pct) + P2y * QB2(pct) + P1y * QB3(pct);
|
|
250
|
+
return {
|
|
251
|
+
x: x,
|
|
252
|
+
y: y,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
static getPointOnEllipticalArc(cx, cy, rx, ry, theta, psi) {
|
|
256
|
+
var cosPsi = Math.cos(psi), sinPsi = Math.sin(psi);
|
|
257
|
+
var pt = {
|
|
258
|
+
x: rx * Math.cos(theta),
|
|
259
|
+
y: ry * Math.sin(theta),
|
|
260
|
+
};
|
|
261
|
+
return {
|
|
262
|
+
x: cx + (pt.x * cosPsi - pt.y * sinPsi),
|
|
263
|
+
y: cy + (pt.x * sinPsi + pt.y * cosPsi),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
static parsePathData(data) {
|
|
267
|
+
if (!data) {
|
|
268
|
+
return [];
|
|
269
|
+
}
|
|
270
|
+
var cs = data;
|
|
271
|
+
var cc = [
|
|
272
|
+
'm',
|
|
273
|
+
'M',
|
|
274
|
+
'l',
|
|
275
|
+
'L',
|
|
276
|
+
'v',
|
|
277
|
+
'V',
|
|
278
|
+
'h',
|
|
279
|
+
'H',
|
|
280
|
+
'z',
|
|
281
|
+
'Z',
|
|
282
|
+
'c',
|
|
283
|
+
'C',
|
|
284
|
+
'q',
|
|
285
|
+
'Q',
|
|
286
|
+
't',
|
|
287
|
+
'T',
|
|
288
|
+
's',
|
|
289
|
+
'S',
|
|
290
|
+
'a',
|
|
291
|
+
'A',
|
|
292
|
+
];
|
|
293
|
+
cs = cs.replace(new RegExp(' ', 'g'), ',');
|
|
294
|
+
for (var n = 0; n < cc.length; n++) {
|
|
295
|
+
cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
|
|
296
|
+
}
|
|
297
|
+
var arr = cs.split('|');
|
|
298
|
+
var ca = [];
|
|
299
|
+
var coords = [];
|
|
300
|
+
var cpx = 0;
|
|
301
|
+
var cpy = 0;
|
|
302
|
+
var re = /([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:e[-+]?\d+)?)/gi;
|
|
303
|
+
var match;
|
|
304
|
+
for (n = 1; n < arr.length; n++) {
|
|
305
|
+
var str = arr[n];
|
|
306
|
+
var c = str.charAt(0);
|
|
307
|
+
str = str.slice(1);
|
|
308
|
+
coords.length = 0;
|
|
309
|
+
while ((match = re.exec(str))) {
|
|
310
|
+
coords.push(match[0]);
|
|
311
|
+
}
|
|
312
|
+
var p = [];
|
|
313
|
+
for (var j = 0, jlen = coords.length; j < jlen; j++) {
|
|
314
|
+
if (coords[j] === '00') {
|
|
315
|
+
p.push(0, 0);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
var parsed = parseFloat(coords[j]);
|
|
319
|
+
if (!isNaN(parsed)) {
|
|
320
|
+
p.push(parsed);
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
p.push(0);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
while (p.length > 0) {
|
|
327
|
+
if (isNaN(p[0])) {
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
var cmd = null;
|
|
331
|
+
var points = [];
|
|
332
|
+
var startX = cpx, startY = cpy;
|
|
333
|
+
var prevCmd, ctlPtx, ctlPty;
|
|
334
|
+
var rx, ry, psi, fa, fs, x1, y1;
|
|
335
|
+
switch (c) {
|
|
336
|
+
case 'l':
|
|
337
|
+
cpx += p.shift();
|
|
338
|
+
cpy += p.shift();
|
|
339
|
+
cmd = 'L';
|
|
340
|
+
points.push(cpx, cpy);
|
|
341
|
+
break;
|
|
342
|
+
case 'L':
|
|
343
|
+
cpx = p.shift();
|
|
344
|
+
cpy = p.shift();
|
|
345
|
+
points.push(cpx, cpy);
|
|
346
|
+
break;
|
|
347
|
+
case 'm':
|
|
348
|
+
var dx = p.shift();
|
|
349
|
+
var dy = p.shift();
|
|
350
|
+
cpx += dx;
|
|
351
|
+
cpy += dy;
|
|
352
|
+
cmd = 'M';
|
|
353
|
+
if (ca.length > 2 && ca[ca.length - 1].command === 'z') {
|
|
354
|
+
for (var idx = ca.length - 2; idx >= 0; idx--) {
|
|
355
|
+
if (ca[idx].command === 'M') {
|
|
356
|
+
cpx = ca[idx].points[0] + dx;
|
|
357
|
+
cpy = ca[idx].points[1] + dy;
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
points.push(cpx, cpy);
|
|
363
|
+
c = 'l';
|
|
364
|
+
break;
|
|
365
|
+
case 'M':
|
|
366
|
+
cpx = p.shift();
|
|
367
|
+
cpy = p.shift();
|
|
368
|
+
cmd = 'M';
|
|
369
|
+
points.push(cpx, cpy);
|
|
370
|
+
c = 'L';
|
|
371
|
+
break;
|
|
372
|
+
case 'h':
|
|
373
|
+
cpx += p.shift();
|
|
374
|
+
cmd = 'L';
|
|
375
|
+
points.push(cpx, cpy);
|
|
376
|
+
break;
|
|
377
|
+
case 'H':
|
|
378
|
+
cpx = p.shift();
|
|
379
|
+
cmd = 'L';
|
|
380
|
+
points.push(cpx, cpy);
|
|
381
|
+
break;
|
|
382
|
+
case 'v':
|
|
383
|
+
cpy += p.shift();
|
|
384
|
+
cmd = 'L';
|
|
385
|
+
points.push(cpx, cpy);
|
|
386
|
+
break;
|
|
387
|
+
case 'V':
|
|
388
|
+
cpy = p.shift();
|
|
389
|
+
cmd = 'L';
|
|
390
|
+
points.push(cpx, cpy);
|
|
391
|
+
break;
|
|
392
|
+
case 'C':
|
|
393
|
+
points.push(p.shift(), p.shift(), p.shift(), p.shift());
|
|
394
|
+
cpx = p.shift();
|
|
395
|
+
cpy = p.shift();
|
|
396
|
+
points.push(cpx, cpy);
|
|
397
|
+
break;
|
|
398
|
+
case 'c':
|
|
399
|
+
points.push(cpx + p.shift(), cpy + p.shift(), cpx + p.shift(), cpy + p.shift());
|
|
400
|
+
cpx += p.shift();
|
|
401
|
+
cpy += p.shift();
|
|
402
|
+
cmd = 'C';
|
|
403
|
+
points.push(cpx, cpy);
|
|
404
|
+
break;
|
|
405
|
+
case 'S':
|
|
406
|
+
ctlPtx = cpx;
|
|
407
|
+
ctlPty = cpy;
|
|
408
|
+
prevCmd = ca[ca.length - 1];
|
|
409
|
+
if (prevCmd.command === 'C') {
|
|
410
|
+
ctlPtx = cpx + (cpx - prevCmd.points[2]);
|
|
411
|
+
ctlPty = cpy + (cpy - prevCmd.points[3]);
|
|
412
|
+
}
|
|
413
|
+
points.push(ctlPtx, ctlPty, p.shift(), p.shift());
|
|
414
|
+
cpx = p.shift();
|
|
415
|
+
cpy = p.shift();
|
|
416
|
+
cmd = 'C';
|
|
417
|
+
points.push(cpx, cpy);
|
|
418
|
+
break;
|
|
419
|
+
case 's':
|
|
420
|
+
ctlPtx = cpx;
|
|
421
|
+
ctlPty = cpy;
|
|
422
|
+
prevCmd = ca[ca.length - 1];
|
|
423
|
+
if (prevCmd.command === 'C') {
|
|
424
|
+
ctlPtx = cpx + (cpx - prevCmd.points[2]);
|
|
425
|
+
ctlPty = cpy + (cpy - prevCmd.points[3]);
|
|
426
|
+
}
|
|
427
|
+
points.push(ctlPtx, ctlPty, cpx + p.shift(), cpy + p.shift());
|
|
428
|
+
cpx += p.shift();
|
|
429
|
+
cpy += p.shift();
|
|
430
|
+
cmd = 'C';
|
|
431
|
+
points.push(cpx, cpy);
|
|
432
|
+
break;
|
|
433
|
+
case 'Q':
|
|
434
|
+
points.push(p.shift(), p.shift());
|
|
435
|
+
cpx = p.shift();
|
|
436
|
+
cpy = p.shift();
|
|
437
|
+
points.push(cpx, cpy);
|
|
438
|
+
break;
|
|
439
|
+
case 'q':
|
|
440
|
+
points.push(cpx + p.shift(), cpy + p.shift());
|
|
441
|
+
cpx += p.shift();
|
|
442
|
+
cpy += p.shift();
|
|
443
|
+
cmd = 'Q';
|
|
444
|
+
points.push(cpx, cpy);
|
|
445
|
+
break;
|
|
446
|
+
case 'T':
|
|
447
|
+
ctlPtx = cpx;
|
|
448
|
+
ctlPty = cpy;
|
|
449
|
+
prevCmd = ca[ca.length - 1];
|
|
450
|
+
if (prevCmd.command === 'Q') {
|
|
451
|
+
ctlPtx = cpx + (cpx - prevCmd.points[0]);
|
|
452
|
+
ctlPty = cpy + (cpy - prevCmd.points[1]);
|
|
453
|
+
}
|
|
454
|
+
cpx = p.shift();
|
|
455
|
+
cpy = p.shift();
|
|
456
|
+
cmd = 'Q';
|
|
457
|
+
points.push(ctlPtx, ctlPty, cpx, cpy);
|
|
458
|
+
break;
|
|
459
|
+
case 't':
|
|
460
|
+
ctlPtx = cpx;
|
|
461
|
+
ctlPty = cpy;
|
|
462
|
+
prevCmd = ca[ca.length - 1];
|
|
463
|
+
if (prevCmd.command === 'Q') {
|
|
464
|
+
ctlPtx = cpx + (cpx - prevCmd.points[0]);
|
|
465
|
+
ctlPty = cpy + (cpy - prevCmd.points[1]);
|
|
466
|
+
}
|
|
467
|
+
cpx += p.shift();
|
|
468
|
+
cpy += p.shift();
|
|
469
|
+
cmd = 'Q';
|
|
470
|
+
points.push(ctlPtx, ctlPty, cpx, cpy);
|
|
471
|
+
break;
|
|
472
|
+
case 'A':
|
|
473
|
+
rx = p.shift();
|
|
474
|
+
ry = p.shift();
|
|
475
|
+
psi = p.shift();
|
|
476
|
+
fa = p.shift();
|
|
477
|
+
fs = p.shift();
|
|
478
|
+
x1 = cpx;
|
|
479
|
+
y1 = cpy;
|
|
480
|
+
cpx = p.shift();
|
|
481
|
+
cpy = p.shift();
|
|
482
|
+
cmd = 'A';
|
|
483
|
+
points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
|
|
484
|
+
break;
|
|
485
|
+
case 'a':
|
|
486
|
+
rx = p.shift();
|
|
487
|
+
ry = p.shift();
|
|
488
|
+
psi = p.shift();
|
|
489
|
+
fa = p.shift();
|
|
490
|
+
fs = p.shift();
|
|
491
|
+
x1 = cpx;
|
|
492
|
+
y1 = cpy;
|
|
493
|
+
cpx += p.shift();
|
|
494
|
+
cpy += p.shift();
|
|
495
|
+
cmd = 'A';
|
|
496
|
+
points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
ca.push({
|
|
500
|
+
command: cmd || c,
|
|
501
|
+
points: points,
|
|
502
|
+
start: {
|
|
503
|
+
x: startX,
|
|
504
|
+
y: startY,
|
|
505
|
+
},
|
|
506
|
+
pathLength: this.calcLength(startX, startY, cmd || c, points),
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
if (c === 'z' || c === 'Z') {
|
|
510
|
+
ca.push({
|
|
511
|
+
command: 'z',
|
|
512
|
+
points: [],
|
|
513
|
+
start: undefined,
|
|
514
|
+
pathLength: 0,
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return ca;
|
|
519
|
+
}
|
|
520
|
+
static calcLength(x, y, cmd, points) {
|
|
521
|
+
var len, p1, p2, t;
|
|
522
|
+
var path = Path;
|
|
523
|
+
switch (cmd) {
|
|
524
|
+
case 'L':
|
|
525
|
+
return path.getLineLength(x, y, points[0], points[1]);
|
|
526
|
+
case 'C':
|
|
527
|
+
len = 0.0;
|
|
528
|
+
p1 = path.getPointOnCubicBezier(0, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
|
|
529
|
+
for (t = 0.01; t <= 1; t += 0.01) {
|
|
530
|
+
p2 = path.getPointOnCubicBezier(t, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
|
|
531
|
+
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
532
|
+
p1 = p2;
|
|
533
|
+
}
|
|
534
|
+
return len;
|
|
535
|
+
case 'Q':
|
|
536
|
+
len = 0.0;
|
|
537
|
+
p1 = path.getPointOnQuadraticBezier(0, x, y, points[0], points[1], points[2], points[3]);
|
|
538
|
+
for (t = 0.01; t <= 1; t += 0.01) {
|
|
539
|
+
p2 = path.getPointOnQuadraticBezier(t, x, y, points[0], points[1], points[2], points[3]);
|
|
540
|
+
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
541
|
+
p1 = p2;
|
|
542
|
+
}
|
|
543
|
+
return len;
|
|
544
|
+
case 'A':
|
|
545
|
+
len = 0.0;
|
|
546
|
+
var start = points[4];
|
|
547
|
+
var dTheta = points[5];
|
|
548
|
+
var end = points[4] + dTheta;
|
|
549
|
+
var inc = Math.PI / 180.0;
|
|
550
|
+
if (Math.abs(start - end) < inc) {
|
|
551
|
+
inc = Math.abs(start - end);
|
|
552
|
+
}
|
|
553
|
+
p1 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], start, 0);
|
|
554
|
+
if (dTheta < 0) {
|
|
555
|
+
for (t = start - inc; t > end; t -= inc) {
|
|
556
|
+
p2 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
|
|
557
|
+
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
558
|
+
p1 = p2;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
for (t = start + inc; t < end; t += inc) {
|
|
563
|
+
p2 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
|
|
564
|
+
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
565
|
+
p1 = p2;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
p2 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], end, 0);
|
|
569
|
+
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
570
|
+
return len;
|
|
571
|
+
}
|
|
572
|
+
return 0;
|
|
573
|
+
}
|
|
574
|
+
static convertEndpointToCenterParameterization(x1, y1, x2, y2, fa, fs, rx, ry, psiDeg) {
|
|
575
|
+
var psi = psiDeg * (Math.PI / 180.0);
|
|
576
|
+
var xp = (Math.cos(psi) * (x1 - x2)) / 2.0 + (Math.sin(psi) * (y1 - y2)) / 2.0;
|
|
577
|
+
var yp = (-1 * Math.sin(psi) * (x1 - x2)) / 2.0 +
|
|
578
|
+
(Math.cos(psi) * (y1 - y2)) / 2.0;
|
|
579
|
+
var lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
|
|
580
|
+
if (lambda > 1) {
|
|
581
|
+
rx *= Math.sqrt(lambda);
|
|
582
|
+
ry *= Math.sqrt(lambda);
|
|
583
|
+
}
|
|
584
|
+
var f = Math.sqrt((rx * rx * (ry * ry) - rx * rx * (yp * yp) - ry * ry * (xp * xp)) /
|
|
585
|
+
(rx * rx * (yp * yp) + ry * ry * (xp * xp)));
|
|
586
|
+
if (fa === fs) {
|
|
587
|
+
f *= -1;
|
|
588
|
+
}
|
|
589
|
+
if (isNaN(f)) {
|
|
590
|
+
f = 0;
|
|
591
|
+
}
|
|
592
|
+
var cxp = (f * rx * yp) / ry;
|
|
593
|
+
var cyp = (f * -ry * xp) / rx;
|
|
594
|
+
var cx = (x1 + x2) / 2.0 + Math.cos(psi) * cxp - Math.sin(psi) * cyp;
|
|
595
|
+
var cy = (y1 + y2) / 2.0 + Math.sin(psi) * cxp + Math.cos(psi) * cyp;
|
|
596
|
+
var vMag = function (v) {
|
|
597
|
+
return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
|
|
598
|
+
};
|
|
599
|
+
var vRatio = function (u, v) {
|
|
600
|
+
return (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v));
|
|
601
|
+
};
|
|
602
|
+
var vAngle = function (u, v) {
|
|
603
|
+
return (u[0] * v[1] < u[1] * v[0] ? -1 : 1) * Math.acos(vRatio(u, v));
|
|
604
|
+
};
|
|
605
|
+
var theta = vAngle([1, 0], [(xp - cxp) / rx, (yp - cyp) / ry]);
|
|
606
|
+
var u = [(xp - cxp) / rx, (yp - cyp) / ry];
|
|
607
|
+
var v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
|
|
608
|
+
var dTheta = vAngle(u, v);
|
|
609
|
+
if (vRatio(u, v) <= -1) {
|
|
610
|
+
dTheta = Math.PI;
|
|
611
|
+
}
|
|
612
|
+
if (vRatio(u, v) >= 1) {
|
|
613
|
+
dTheta = 0;
|
|
614
|
+
}
|
|
615
|
+
if (fs === 0 && dTheta > 0) {
|
|
616
|
+
dTheta = dTheta - 2 * Math.PI;
|
|
617
|
+
}
|
|
618
|
+
if (fs === 1 && dTheta < 0) {
|
|
619
|
+
dTheta = dTheta + 2 * Math.PI;
|
|
620
|
+
}
|
|
621
|
+
return [cx, cy, rx, ry, theta, dTheta, psi, fs];
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
Path.prototype.className = 'Path';
|
|
625
|
+
Path.prototype._attrsAffectingSize = ['data'];
|
|
626
|
+
_registerNode(Path);
|
|
627
|
+
Factory.addGetterSetter(Path, 'data');
|