-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
409 lines (386 loc) · 17.5 KB
/
index.html
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Title</title>
<style>
#container {
overflow: hidden;
}
</style>
</head>
<body>
<div id="button">定位</div>
<div id="svgTest" style="width: 50%;height:800px;position:relative;float:right;border:1px solid #999;"></div>
<script src="jquery-1.11.3.min.js"></script>
<script src="raphael.min.js"></script>
<script src="raphael.draggable1.js"></script>
<script>
$(function () {
var paper = Raphael('svgTest'), diceData = paper.set(), objArr;
var width = paper.width, height = paper.height;
var moveX = width / 2, moveY = height / 2;
moveX = 0;
// moveY = 0;
paper.zoomEnable();
$.ajax({
url: "",
type: "get",
data: {
"namelist": ""
},
success: function (res) {
if (res.state === "SUCCESS" && res.data.length) {
objArr = res.data;
$.ajax({
url: "",
type: "get",
data: {
"projectId": "888"
},
success: function (res) {
if (res.state === "SUCCESS" && res.data.length) {
var data = res.data;
$.each(data, function (a, item) {
var busList = item.busList, lineList = item.lineList, symboList = item.symboList;
if (busList && busList.length) {
$.each(busList, function (b, item2) {
drawMainLine(item2, "#4cae4c", 0.5)
})
}
if (lineList && lineList.length) {
$.each(lineList, function (b, item2) {
drawLine(item2, "#666", 0.5)
})
}
if (symboList && symboList.length) {
$.each(busList, function (b, item2) {
drawImg(item2)
})
}
})
}
}
})
}
}
})
$("#button").on("click", function () {
selectSvgImg("xxxxxxx")
})
//选中svg的图元
function selectSvgImg(symbolCode) {
diceData.forEach(function (item) {
var data = item.data("data");
if (data) {
if (data.symbolCode && data.symbolCode.indexOf(symbolCode) != -1) {
diceData.forEach(function (item3) {
var rect = item3.data("rect");
if (rect) {
rect.node.style.display = "none";
}
})
var rect2 = item.data("rect");
if (rect2) {
rect2.node.style.display = "block";
}
//缩放svg
zoomSvg(4)
var x2 = -(item.attrs.x - paper._scale.w / 2 - paper._scale.x);
var y2 = -(item.attrs.y - paper._scale.h / 2 - paper._scale.y);
diceData.forEach(function (item2) {
var mytransform = item2.data("mytransform");
var str = "T" + x2 + "," + y2;
for (var i = 0; i < mytransform.length; i++) {
if (mytransform[i][0] != "T") {
var arr = mytransform[i];
for (var j = 0; j < arr.length; j++) {
if (j === 0 || j === arr.length - 1) {
str += arr[j]
} else {
str += arr[j] + ","
}
}
}
}
item2.transform(str);
})
}
}
});
}
//缩放svg
function zoomSvg(zoom) {
var $svgTest = $(paper.canvas).parent()[0], scale = paper._scale;
var cw = paper.canvas.width.baseVal.value, ch = paper.canvas.height.baseVal.value;
var w = scale.w, h = scale.h, x = scale.x, y = scale.y;
var cx = cw / 2;
var cy = ch / 2;
var py, py, nx, ny;
var posv = 1 / scale.zoom;
px = cx * posv;
py = cy * posv;
if (zoom >= scale.maxZoom) {
scale.zoom = scale.maxZoom
} else if (zoom <= scale.minZoom) {
scale.zoom = scale.minZoom
} else {
scale.zoom = zoom;
}
posv = 1 / scale.zoom;
w = cw * posv;
h = ch * posv;
nx = cx * posv;
ny = cy * posv;
var dx = px - nx;
var dy = py - ny;
x += dx;
y += dy;
scale.w = w;
scale.h = h;
scale.x = x;
scale.y = y;
paper.setViewBox(scale.x, scale.y, scale.w, scale.h, false);
paper._scale = scale;
}
//画布整体拖拽 - 拖拽所有元素,以达到拖拽画布整体的效果
paper.canvas.onmousedown = function (evt) {
var e = evt || window.event;
var x1 = e.clientX;
var y1 = e.clientY;
var x2 = 0;
var y2 = 0;
diceData.forEach(function (item) {
item.data("mytransform", item.transform())
})
document.onmousemove = function (evt) {
var e = evt || window.event;
var x2 = e.clientX - x1;
var y2 = e.clientY - y2;
var scale = 1 / paper._scale.zoom;
diceData.forEach(function (item) {
item.transform(item.data("mytransform") + "T" + x2 * scale + "," + y2 * scale)
})
}
document.onmouseup = function (evt) {
diceData.forEach(function (item) {
item.data("mytransform", item.transform())
})
document.onmousemove = null;
}
document.ondragstart = function () {
return false;
}
}
//画主接线
function drawMainLine(item, color, width) {
var lineCoordinateS = item.lineCoordinateS,
lineCoordinateE = item.lineCoordinateE;
var p1 = lineCoordinateS.split(","), p3 = lineCoordinateE.split(","), x = 0, y = 0;
if (parseFloat(p1[0]) > parseFloat(p3[0])) {
x = moveX + parseFloat(p3[0]);
y = moveY + (-parseFloat(p3[1])) - width * 2;
} else {
x = moveX + parseFloat(p1[0]);
y = moveY + (-parseFloat(p1[1])) - width * 2;
}
var w = Math.abs(parseFloat(p1[0]) - parseFloat(p3[0])),
h = width * 5;
var rect = paper.rect(x, y, w, h).attr({
"stroke": color,
"stroke-width": width,
"title": "主线"
});
rect.data("mytransform", rect.transform())
diceData.push(rect)
}
//画线条
function drawLine(item, color, width) {
var lineCoordinateS = item.lineCoordinateS,
inflectionPoint = item.inflectionPoint,
lineCoordinateE = item.lineCoordinateE;
var p1 = lineCoordinateS.split(","), p3 = lineCoordinateE.split(",");
var path = "M";
if (p1 && p1.length) {
path += (moveX + parseFloat(p1[0]) + "," + (moveY + (-parseFloat(p1[1]))))
}
if (inflectionPoint) {
var arr = inflectionPoint.split(";");
for (var i = 0; i < arr.length; i++) {
var p2 = arr[i].split(",");
path += "L" + (moveX + parseFloat(p2[0]) + "," + (moveY + (-parseFloat(p2[1]))))
}
}
if (p3 && p3.length) {
path += "L" + (moveX + parseFloat(p3[0]) + "," + (moveY + (-parseFloat(p3[1]))))
}
// path += "Z"; //闭合,变成三角形
if (!color) {
color = "#8c8c8c"
}
if (!width) {
width = 1
}
line.data("mytransform", line.transform());
diceData.push(line)
}
//根据属性获取img路径
function getImgObj(name) {
for (var i = 0; i < objArr.length; i++) {
if (name === objArr[i].name) {
return objArr[i]
}
}
}
//画图元
function drawImg(item) {
var symbolCoordinate = item.symbolCoordinate,
symbolRatio = item.symbolRatio,
symbolRotation = item.symbolRotation;
var p = symbolCoordinate.split(",");
if (!objArr.length) {
return
}
var obj = getImgObj(item.symbolName);
if (!obj) {
return
}
var p2 = obj.xOffset, p3 = obj.yOffset;
var w = obj.width, h = obj.height,
x = x1 = moveX + parseFloat(p[0]), y = y1 = moveY + (-parseFloat[p[1]]);
x = x - parseFloat(p2);
y = y - parseFloat(p3);
symbolRotation = parseFloat(symbolRotation);
symbolRotation = symbolRotation < 0 ? Math.ceil(symbolRotation) : Math.floor(symbolRotation);
if (isNaN(symbolRotation)) {
symbolRotation = 0
}
symbolRatio = parseFloat(symbolRatio);
symbolRatio = symbolRatio < 0 ? Math.ceil(symbolRatio) : Math.floor(symbolRatio);
if (isNaN(symbolRatio)) {
symbolRatio = 1
}
var img = paper.image("/formal/svg/" + item.symbolName + ".svg", x, y, w, h)
.attr({
"cursor": "pointer"
})
.rotate(-symbolRotation, x1, y1)
.scale(symbolRatio)
.data("data", {
schId: item.schId,
symbolCode: item.symbolCode,
symbolLevel: item.symbolLevel,
symbolName: item.symbolName,
symbolMirror: item.symbolMirror,
symbolRotation: -symbolRotation,
symbolRatio: symbolRatio,
w: w,
h: h,
x: x,
y: y,
x1: x1,
y1: y1,
p2: p2,
p3: p3
});
diceData.push(img);
rect = paper.rect(x, y, w, h)
.attr({
"stroke": "#00f",
"stroke-width": 0.2
})
.rotate(-symbolRotation, x1, y1)
.scale(symbolRatio)
.data("data", {
symbolRotation: -symbolRotation,
symbolRatio: symbolRatio,
w: w + 0.2 * 2,
h: h + 0.2 * 2,
x: x,
y: y,
x1: x1,
y1: y1,
p2: p2,
p3: p3
});
diceData.push(rect);
img.click(function (e) {
var rect = this.data("rect");
if (rect.node.style.display === "none") {
diceData.forEach(function (item) {
var rect2 = this.data("rect");
if (rect2) {
rect2.node.style.display = "none"
}
})
rect.node.style.display = "block"
} else {
rect.node.style.display = "none"
}
})
img.hover(function () {
this.attr({
opacity: "0.5"
})
}, function () {
this.attr({
opacity: "1"
})
})
var symbolMirror = item.symbolMirror;
//平移镜像翻转
translateScale(img, w, h, p2, p3, symbolMirror, symbolRotation, item);
translateScale(rect, w, h, p2, p3, symbolMirror, symbolRotation);
rect.node.style.display = "none";
img.data("rect", rect)
img.data("mytransform", img.transform())
rect.data("mytransform", rect.transform())
}
//平移镜像翻转
function translateScale(img, w, h, p2, p3, symbolMirror, symbolRotation, item) {
if (symbolMirror) {
var s = symbolMirror.split(","), s1 = parseFloat(s[0]), s2 = parseFloat(s[1]);
s1 = s1 < 0 ? Math.ceil(s1) : Math.floor(s1);
s2 = s2 < 0 ? Math.ceil(s2) : Math.floor(s2);
if (s1 === 0 && s2 === -1) {
if (symbolRotation == "270") {
img.translate(0, -(h - 2 * p3)).scale(1, -1) //x轴翻转 y负方向平移 向下翻转
} else if (symbolRotation == "90") {
img.translate(0, (h - 2 * p3)).scale(1, -1) //x轴翻转 y负方向平移 向下翻转
} else if (symbolRotation == "180" || symbolRotation == "0") {
img.translate(w - 2 * p2, 0).scale(-1, 1) //y轴翻转 x正方向平移 向右翻转
}
}
if (s1 === 0 && s2 === 1) {
if (symbolRotation == "270") {
img.translate(0, h - 2 * p3).scale(1, -1) //x轴翻转 y正方向平移 向上翻转
} else if (symbolRotation == "90") {
img.translate(0, -(h - 2 * p3)).scale(1, -1) //x轴翻转 y正方向平移 向上翻转
} else if (symbolRotation == "180" || symbolRotation == "0") {
img.translate(w - 2 * p2, 0).scale(-1, 1) //y轴翻转 x负方向平移 向左翻转
}
}
if (s1 === 1 && s2 === 0) {
if (symbolRotation == "270") {
img.translate(-(w - 2 * p2), 0).scale(-1, 1) //y轴翻转 x正方向平移 向右翻转
} else if (symbolRotation == "90") {
img.translate(w - 2 * p2, 0).scale(-1, 1)//y轴翻转 x正方向平移 向右翻转
} else if (symbolRotation == "180" || symbolRotation == "0") {
img.translate(0, -(h - 2 * p3)).scale(1, -1) //x轴翻转 y正方向平移 向上翻转
}
}
if (s1 === -1 && s2 === 0) {
if (symbolRotation == "270") {
img.translate(w - 2 * p2, 0).scale(-1, 1) //y轴翻转 x负方向平移 向左翻转
} else if (symbolRotation == "90") {
img.translate(-(w - 2 * p2), 0).scale(-1, 1) //y轴翻转 x负方向平移 向左翻转
} else if (symbolRotation == "180" || symbolRotation == "0") {
img.translate(0, -(h - 2 * p3)).scale(1, -1) //x轴翻转 y负方向平移 向下翻转
}
}
}
}
})
</script>
</body>
</html>