-
Notifications
You must be signed in to change notification settings - Fork 0
/
55-绘图雏形-06-legend.html
215 lines (159 loc) · 4.54 KB
/
55-绘图雏形-06-legend.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
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="svg270/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="svg270/themes/icon.css">
<script type="text/javascript" src="svg270/jquery.min.js"></script>
<script type="text/javascript" src="svg270/jquery.easyui.min.js"></script>
<script src="svg270/svg.min.js"></script>
<script src="svg270/svg.panzoom.min.js"></script>
<script src="svg270/svg.draggable.min.js"></script>
<link rel="stylesheet" type="text/css" href="svg270/svg.select.css">
<script src="svg270/svg.select.js"></script>
<script src="svg270/svg.resize.js"></script>
</head>
<style>
ul li{
margin-bottom: 20px;
cursor: pointer;
}
</style>
<body>
<div style="border-bottom: 1px solid gray;">我是一个画布视窗</div>
底图透明度:<div id="id_image_opacity" style="width:200px"></div>
<div id="drawing" style="float:left;">
</div>
<div style="float: left;margin-left: 10px;">
<a href="#">我是图例库</a>
<div style="width:100px;height:500px;border:1px solid green;">
<ul id="legend-list">
<li value="0" draggable="true">设备1</li>
<li value="1" draggable="true">设备2</li>
<li value="#" >管线</li>
<li value="2" draggable="true">仪表</li>
<li value="3" draggable="true">阀门</li>
<li value="4" draggable="true">设备1-备注</li>
</ul>
</div>
</div>
</body>
<script>
// 这里是整个视窗和最外层的<svg>是一致的
var drawimgWidth = 1000;
var drawingHeight = 600;
$("#drawing").css({
border: "2px solid rosybrown",
width: drawimgWidth + "px",
height: drawingHeight + "px",
});
var draw = SVG('drawing').size(drawimgWidth, drawingHeight)
// pan and zoom
draw.panZoom({ zoomMin: 0.01, zoomMax: 100 });;
//这是底图的测试图片
var imageWidth = 800;
var imageHeight = 600;
var image = draw.image('svg270/1.jpeg', imageWidth, imageHeight);
//灰度调整区域
// var imgArea = draw.group().style({ cursor: 'pointer', fill: '#fff' }).style("fill-opacity", 0.3);
var imgArea = draw.group().style({ fill: '#fff' }).style("fill-opacity", 0.3);
imgArea.add(image);
imgArea.rect(imageWidth, imageHeight);
//图例加入拖拽
// setTimeout(function(){
// $("g").each(function(){
// this.instance.draggable();
// })
// },1000)
btn();
loadAllLegends();
function btn() {
//底图透明度按钮
$('#id_image_opacity').slider({
mode: 'h', //声明滚动条类型。可用值有:'h'(水平)、'v'(垂直)。
min: 0,
max: 1,
step: 0.01,
value:0.3,
tipFormatter: function (value) {
return value + '%';
},
onChange: function (newValue, oldValue) {
imgArea.style("fill-opacity", newValue);
console.log(newValue);
}
});
}
var currentDragLegendText = null;
/**
* 加载图例
*/
function loadAllLegends(){
var arr = ["sb1","sb2","yb","fm","sb1-note"];
$("#legend-list li").each(function(){
var _this = this;
var v = $(this).attr("value");
if(v=="#"){
return;
}
var url = "./svg270/legend/"+arr[parseInt(v)]+".svg";
loadLegendByUrl(url,function(text){
// console.log(text);
$(_this).data("svgData",text);
})
$(this).click(function(){
// var v= $(_this).data("svgData");
// console.log(v);
})
//拖拽时,离开容器范围时触发
$(this).on("dragstart",function(e){
var v= $(_this).data("svgData");
currentDragLegendText = v;
}).on("dragover",function(e){
return false;
})
})
//放置图例时的触发事件
$("#drawing").on("drop",function(e){
var x = e.offsetX;
var y = e.offsetY;
console.log(x+"---"+y);
// console.log(currentDragLegendText);
if(!currentDragLegendText){
return;
}
// console.log(currentDragLegendText);
draw.svg(currentDragLegendText);
var a = SVG.get("layer1").attr("id",createUUID()).translate(x,y);
//记录血的教训:必须指定 fill 颜色;不然 fill=none 时 ,移动时必须选中边框才能移动
a.draggable();
a.click(function(){
this.attr("stroke",getRandomColor());
})
}).on("dragover",function(e){
return false;
})
}
/**
* 加载图例
*/
function loadLegendByUrl(url,callback){
$.post(url, {}, function (data) {
if($.isFunction(callback)){
callback(data);
}
}, "text");
}
function createUUID(){
var x= 13*Math.floor(Math.random()*1000)+"-"+7*Math.floor(Math.random()*100);
return "id_"+x;
}
/**
* 生成随机颜色
*/
function getRandomColor(){
var x= Math.floor(Math.random()*9)+""+Math.floor(Math.random()*9)+""+Math.floor(Math.random()*9);
return "#"+x;
}
</script>
</html>