-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathsceneController.js
274 lines (235 loc) · 6.08 KB
/
sceneController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const {Path, Color, Ellipse, Rectangle} = require("scenegraph");
/* --------------------------
drowParms = {
x:Number,
y:Number,
width:Number,
height:Number,
leftEdgeType:"none" or "arrow1" or "arrow2" or "arrow3" or "circle1" or "circle2" or "square1" or "square2" or "bar",
rightEdgeType:
lineType:"straight" or "curve" or "snake",
lineWidth:Number,
color:String
}
------------------------------ */
const lineOffset = {
none: 0,
arrow1: 0,
arrow2: 0,
arrow3: 0,
circle1: 8,
circle2: 8,
square1: 8,
square2: 8,
bar: 0
}
function drawConnector(parms){
const x = parms.x;
const y = parms.y;
const width = parms.width;
const height = parms.height;
const leftEdgeType = parms.leftEdgeType;
const rightEdgeType = parms.rightEdgeType;
const edgeScale = parms.edgeScale;
const lineType = parms.lineType;
const lineWidth = parms.lineWidth;
const color = parms.color;
// create leftEdge
let edgeX = x;
let edgeY = y;
const leftEdge = createEdge({
x: edgeX,
y: edgeY,
type: leftEdgeType,
lineWidth: lineWidth,
scale: edgeScale,
color: color
}, false);
if(lineType == "curve" && (leftEdgeType == "arrow1" || leftEdgeType == "arrow2" || leftEdgeType == "arrow3" || leftEdgeType == "bar")){
leftEdge.rotateAround(90, {x:0, y:0})
}
// create RightEdge
edgeX = x + width;
edgeY = y;
if(lineType == "curve" || lineType == "snake"){
edgeY = y + height;
}
const rightEdge = createEdge({
x: edgeX,
y: edgeY,
type: rightEdgeType,
lineWidth: lineWidth,
scale: edgeScale,
color: color
}, true);
// create line
let leftOffset = lineOffset[leftEdgeType] * edgeScale/100;
let rightOffset = lineOffset[rightEdgeType] * edgeScale/100;
const line = createLine({
sx: x,
sy: y,
ex: x + width,
ey: y + height,
type: lineType,
lineWidth: lineWidth,
color: color
}, leftOffset, rightOffset);
return [line, leftEdge, rightEdge];
}
function createLine(parms, leftOffset, rightOffset){
const sx = parms.sx;
const sy = parms.sy;
const ex = parms.ex;
const ey = parms.ey;
const type = parms.type;
const lineWidth = parms.lineWidth;
const color = parms.color;
let p0, p1, p2, p3;
let pathData = "";
switch(type){
case "straight":
p0 = (sx + leftOffset) + "," + sy;
p1 = (ex - rightOffset) + "," + sy;
pathData = `M ${p0} L ${p0} ${p1}`;
break;
case "curve":
p0 = sx + "," + (sy + leftOffset);
p1 = sx + "," + ey;
p2 = (ex - rightOffset) + "," + ey;
pathData = `M ${p0} L ${p0} ${p1} ${p2}`;
break;
case "snake":
const mx = (ex - sx)/2 + sx;
p0 = (sx + leftOffset) + "," + sy;
p1 = mx + "," + sy;
p2 = mx + "," + ey;
p3 = (ex - rightOffset) + "," + ey;
pathData = `M ${p0} L ${p0} ${p1} ${p2} ${p3}`;
break;
}
const path = new Path();
path.stroke = new Color(color);
path.strokeWidth = lineWidth;
path.pathData = pathData;
path.name = "line";
return path;
}
function createEdge(parms, isRight){
const x = parms.x;
const y = parms.y;
const type = parms.type;
const lineWidth = parms.lineWidth;
const color = parms.color;
const scale = parms.scale/100;
let p0,p1,p2,p3;
let pathData = "";
let stroke;
let fill;
let path;
switch(type){
case "arrow1":
if(isRight){
p0 = (-15*scale) + "," + (-8*scale);
p1 = "0,0";
p2 = (-15*scale) + "," + (8*scale);
p3 = (-12*scale) + ",0";
}else{
p0 = (15*scale) + "," + (-8*scale);
p1 = "0,0";
p2 = (15*scale) + "," + (8*scale);
p3 = (12*scale) + ",0";
}
pathData = `M ${p0} L ${p0} ${p1} ${p2} ${p3} Z`;
path = new Path();
path.pathData = pathData;
path.stroke = new Color(color);
path.fill = new Color(color);
path.moveInParentCoordinates(x,y);
break;
case "arrow2":
if(isRight){
p0 = (-15*scale) + "," + (-8*scale);
p1 = "0,0";
p2 = (-15*scale) + "," + (8*scale);
}else{
p0 = (15*scale) + "," + (-8*scale);
p1 = "0,0";
p2 = (15*scale) + "," + (8*scale);
}
pathData = `M ${p0} L ${p0} ${p1} ${p2} Z`;
path = new Path();
path.pathData = pathData;
path.stroke = new Color(color);
path.fill = new Color(color);
path.moveInParentCoordinates(x,y);
break;
case "arrow3":
if(isRight){
p0 = (-15*scale) + "," + (-10*scale);
p1 = "0,0";
p2 = (-15*scale) + "," + (10*scale);
}else{
p0 = (15*scale) + "," + (-10*scale);
p1 = "0,0";
p2 = (15*scale) + "," + (10*scale);
}
pathData = `M ${p0} L ${p0} ${p1} ${p2}`;
path = new Path();
path.pathData = pathData;
path.stroke = new Color(color);
path.moveInParentCoordinates(x,y);
break;
case "circle1":
path = new Ellipse();
path.radiusX = 8*scale;
path.radiusY = 8*scale;
path.stroke = new Color(color);
path.moveInParentCoordinates(x-8*scale, y-8*scale);
break;
case "circle2":
path = new Ellipse();
path.radiusX = 8*scale;
path.radiusY = 8*scale;
path.stroke = new Color(color);
path.fill = new Color(color);
path.moveInParentCoordinates(x-8*scale, y-8*scale);
break;
case "square1":
path = new Rectangle();
path.width = 16*scale;
path.height = 16*scale;
path.stroke = new Color(color);
path.moveInParentCoordinates(x-8*scale, y-8*scale);
break;
case "square2":
path = new Rectangle();
path.width = 16*scale;
path.height = 16*scale;
path.stroke = new Color(color);
path.fill = new Color(color);
path.moveInParentCoordinates(x-8*scale, y-8*scale);
break;
case "bar":
p0 = "0," + (-8*scale);
p1 = "0," + (8*scale);
pathData = `M ${p0} L ${p0} ${p1}`;
path = new Path();
path.pathData = pathData;
path.stroke = new Color(color);
path.moveInParentCoordinates(x,y);
break;
case "none":
path = new Path();
path.moveInParentCoordinates(x,y);
break;
}
path.strokeWidth = lineWidth;
if(isRight){
path.name = "rightEdge";
}else{
path.name = "leftEdge";
}
return path;
}
module.exports.drawConnector = drawConnector;
module.exports.lineOffset = lineOffset;