d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
1
|
import React, { Component } from "react"
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
2
|
import "./areaSelete.css"
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
3
4
5
6
7
|
export default class AreaSelete extends Component {
constructor() {
super();
this.state = {
id: new Date().getTime() + Math.floor(Math.random() * (10000 - 1)) + 1,
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
8
|
mode: "",
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
9
10
|
width: "100px",
height: "100px",
|
76d17fa3
Zhang Zhuo
更新bug -张卓
|
11
12
|
circleRadius: 5,//圆半径
circleBorderWidth: 2,//圆边线的半径宽
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
13
14
|
circleBorderColor: "#28B1D9",//圆边线的颜色
circleInColor: "#28B1D9",//圆里里面的颜色
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
15
|
circleSelectColor: "#FFF",
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
16
17
18
|
lineColor: "#28B1D9",//线的颜色
lineWidth: 2,//线的宽度
areaColor: "rgba(40, 177, 217, 0.2)",//选中区域颜色
|
e2460342
Zhang Zhuo
更改 -张卓
|
19
|
disabled: "false",//判断是否可以移动 false是可以移动
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
20
|
title: "",//文字的内容
|
76d17fa3
Zhang Zhuo
更新bug -张卓
|
21
22
23
|
titleColor: "#000",//文字的颜色
titleFont: "14px bold 黑体",//文字的字体
titleLineHeight: "",//文字的行高
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
24
25
26
|
titleBackgroundColor: "",//字的北京颜色支持rgba
titlePadding: "10px",//文字的padding
titleTextAlign: "center",
|
76d17fa3
Zhang Zhuo
更新bug -张卓
|
27
|
position: [10, 10],//文字的位置
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
28
29
30
31
32
|
serviceData: []
}
}
static getDerivedStateFromProps(props, state) {
var obj = {};
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
33
34
|
for (var key in props) {
if (props[key] && props[key] !== state[key]) {
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
35
36
37
|
obj[key] = props[key]
}
}
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
38
|
if (Object.keys(obj).length > 0) {
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
39
|
return obj;
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
40
|
} else {
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
41
42
43
|
return null
}
}
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
44
45
|
shouldComponentUpdate(nextProps) {
if (this.props.serviceData.length !== nextProps.serviceData) {
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
46
|
return true
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
47
|
} else {
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
48
49
50
51
52
|
return false
}
}
componentDidMount() {
this.init()
|
e2460342
Zhang Zhuo
更改 -张卓
|
53
|
window.addEventListener("resize", () => {
|
76d17fa3
Zhang Zhuo
更新bug -张卓
|
54
55
|
this.init()
})
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
}
init() {
this.setCanvasSize();//根据父盒子宽高大小设置画布的大小
}
setCanvasSize() {
let { id } = this.state;
var canvasEl = document.getElementById(id)
var parentEl = canvasEl.parentNode;
var parentWidth = parentEl.offsetWidth;//父标签的宽度
var parentHeight = parentEl.offsetHeight;//父标签的高度
this.setData({
width: parentWidth + "px",
height: parentHeight + "px"
})
}
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
71
72
73
74
75
76
77
78
|
removeTags(tagName, tagClass) {//删除标签
var tagElements = document.getElementsByTagName(tagName);
for (var m = tagElements.length - 1; m >= 0; m--) {//从大到小因为没次删除都是动态的,数组会越来越小
if (tagElements[m].className == tagClass) {
tagElements[m].parentNode.removeChild(tagElements[m]);
}
}
}
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
79
|
draw() {
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
80
|
let { id, serviceData, width, height, mode, disabled } = this.state;
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
81
82
83
|
var canvasEl = document.getElementById(id)
var ctx = canvasEl.getContext("2d");
ctx.clearRect(0, 0, parseFloat(width), parseFloat(height));
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
84
|
this.removeTags("input", id + "");
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
85
86
|
this.drawLine(ctx, serviceData);
this.drawCircleAll(ctx, serviceData);
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
87
|
this.drawWriting(ctx, serviceData)
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
88
89
90
|
// this.clickMove()//点击模式
// this.mouseMove()//鼠标移动模式
// this.touchMove()//手指触摸移动模式
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
91
92
93
94
95
96
|
if (disabled === "false") {
if (mode === 'click') {
this.isontouchend() ? this.touchMove() : this.clickMove()
} else {
this.isontouchend() ? this.touchMove() : this.mouseMove()
}
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
97
98
99
100
101
102
103
|
}
}
isontouchend() {//判断是否支持触屏
return "ontouchend" in document ? true : false;
}
drawCircleAll(ctx, data) {//画所有圆
for (var i = 0; i < data.length; i++) {
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
104
|
this.drawCircle(ctx, data[i][0], data[i][1], this.selectedIndex === i)
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
105
106
|
}
}
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
107
108
109
|
drawCircle(ctx, x, y, selected) {//画单个圆
let { circleRadius, circleInColor, circleBorderColor, circleBorderWidth, circleSelectColor } = this.state
ctx.fillStyle = selected ? circleSelectColor : circleInColor;//画里面
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
110
|
ctx.beginPath();
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
111
|
ctx.arc(x, y, parseInt(circleRadius), 0, Math.PI * 2, true);
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
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
|
ctx.closePath();
ctx.fill();
ctx.beginPath();//画外面的空心圆
ctx.arc(x, y, parseInt(circleRadius), 0, Math.PI * 2, true);
ctx.lineWidth = parseInt(circleBorderWidth);
ctx.strokeStyle = circleBorderColor;
ctx.stroke(); //画空心圆
ctx.closePath();
}
drawLine(ctx, data) {//画线
let { lineColor, lineWidth, areaColor } = this.state
ctx.beginPath();
for (var i = 0; i < data.length; i++) {
if (i === 0) {
ctx.moveTo(data[i][0], data[i][1]);
} else {
ctx.lineTo(data[i][0], data[i][1]);
}
}
ctx.lineWidth = parseInt(lineWidth);
ctx.strokeStyle = lineColor;
ctx.closePath();
ctx.fillStyle = areaColor;
ctx.fill();
ctx.stroke();
}
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
138
|
drawWriting(ctx, data) {//写文字
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
139
140
141
142
143
|
let { title, titleColor, titleFont, titleLineHeight, position, titlePadding } = this.state;
// ctx.font = titleFont || "18px bold 黑体";
// ctx.fillStyle = titleColor || "#fff";
// ctx.lineHeight = titleLineHeight || titleFont.split(" ")[0];
// ctx.textBaseline = "middle";
|
76d17fa3
Zhang Zhuo
更新bug -张卓
|
144
|
var lineHeight = parseInt(titleLineHeight || titleFont.split(" ")[0])
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
145
146
|
var x = 0;
var y = 99999999;
|
76d17fa3
Zhang Zhuo
更新bug -张卓
|
147
|
for (var i = 0; i < data.length; i++) {
|
e2460342
Zhang Zhuo
更改 -张卓
|
148
149
|
if (parseFloat(data[i][0]) > x) x = parseFloat(data[i][0]);
if (parseFloat(data[i][1]) < y) y = parseFloat(data[i][1]);
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
150
|
}
|
76d17fa3
Zhang Zhuo
更新bug -张卓
|
151
|
title.split(/\n/).forEach((item, index) => {
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
152
|
this.inputStyleCreate(item, x + (position[0] || 15), y + (position[1] || 15) + index * (lineHeight + 3 * parseInt(titlePadding)))
|
96108619
Zhang Zhuo
更新添加文字功能 -张卓
|
153
154
|
})
}
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
inputStyleCreate(title, x, y) {
let { titleColor, titleFont, titleTextAlign, titleLineHeight, titleBackgroundColor, titlePadding,disabled, id } = this.state;
var canvasEl = document.getElementById(id)
var parentEl = canvasEl.parentNode;
var input = document.createElement("input");
input.style.font = titleFont;
input.style.lineHeight = titleLineHeight;
input.style.color = titleColor;
input.style.backgroundColor = titleBackgroundColor || "rgba(40, 177, 217, 0.2)";
input.style.padding = titlePadding;
input.style.textAlign = titleTextAlign;
input.style.display = disabled==="true"?true:false;
input.style.position = "absolute";
input.style.border = "none";
input.style.left = x + "px";
input.style.top = y + "px";
input.style.zIndex = "100";
input.value = title;
input.className = id + "";
input.oninput = (e) => {
console.log(e.target.value)
}
parentEl.appendChild(input)
}
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
179
|
clickMove() {//鼠标点击移动
|
e2460342
Zhang Zhuo
更改 -张卓
|
180
|
let { id, serviceData } = this.state;
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
181
182
183
184
185
186
187
188
189
190
|
var canvasEl = document.getElementById(id)
canvasEl.onclick = (e) => {
var left = e.layerX
var top = e.layerY
if (this.selectedIndex || this.selectedIndex === 0) {//此时已经选中了点
serviceData[this.selectedIndex] = [left, top]
this.selectedIndex = null
this.setData({
serviceData: serviceData
})
|
e2460342
Zhang Zhuo
更改 -张卓
|
191
|
this.sendFather(serviceData)
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
192
193
194
195
196
197
198
199
200
|
} else {
this.selectBox(left, top)
this.setData({
serviceData: serviceData
})
}
}
}
mouseMove() {//鼠标移动
|
e2460342
Zhang Zhuo
更改 -张卓
|
201
202
|
let { id } = this.state;
var serviceDataw = ""
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
203
204
|
var canvasEl = document.getElementById(id)
canvasEl.onmousedown = (e) => {
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
205
|
var { serviceData } = this.state;
|
e2460342
Zhang Zhuo
更改 -张卓
|
206
|
serviceDataw = serviceData
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
207
208
209
210
211
|
var left = e.layerX
var top = e.layerY
if (this.selectBox(left, top)) {
canvasEl.onmousemove = (e) => {
serviceData[this.selectedIndex] = [e.layerX, e.layerY];
|
e2460342
Zhang Zhuo
更改 -张卓
|
212
|
serviceDataw = serviceData
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
213
214
215
216
217
218
219
220
221
222
|
this.setData({
serviceData: serviceData
})
};
}
}
canvasEl.onmouseup = (e) => {
canvasEl.onmousemove = null;
this.selectedIndex = null;
this.setData({
|
e2460342
Zhang Zhuo
更改 -张卓
|
223
|
serviceData: this.state.serviceData
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
224
225
226
227
|
})
var left = e.layerX
var top = e.layerY
if (this.selectBox(left, top)) {
|
e2460342
Zhang Zhuo
更改 -张卓
|
228
|
this.sendFather(this.state.serviceData)
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
229
230
231
232
|
}
};
}
touchMove() {//手指触摸移动
|
e2460342
Zhang Zhuo
更改 -张卓
|
233
|
let { id, serviceData } = this.state;
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
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
|
var canvasEl = document.getElementById(id)
var parentEl = canvasEl.parentNode;
var pingmuTop = parentEl.getBoundingClientRect().top; //画布距离屏幕上面的距离 这里要计算出触摸距离当前画布的距离好进行移动
var pingmuLeft = parentEl.getBoundingClientRect().left; //画布距离屏幕左面的距离
canvasEl.ontouchstart = (e) => {
var left = e.touches[0].pageX - pingmuLeft
var top = e.touches[0].pageY - pingmuTop
if (this.selectBox(left, top)) {
canvasEl.ontouchmove = (e) => {
serviceData[this.selectedIndex] = [e.touches[0].pageX - pingmuLeft, e.touches[0].pageY - pingmuTop];
this.setData({
serviceData: serviceData
})
}
}
}
canvasEl.ontouchend = (e) => {
this.selectedIndex = null
canvasEl.ontouchmove = null;
this.setData({
serviceData: serviceData
})
var left = e.changedTouches[0].pageX - pingmuLeft
var top = e.changedTouches[0].pageY - pingmuTop
if (this.selectBox(left, top)) {
|
e2460342
Zhang Zhuo
更改 -张卓
|
259
|
this.sendFather(serviceData)
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
260
261
262
263
264
265
266
267
268
269
|
}
};
}
selectBox(x, y) {//判断是否选中
let { serviceData, circleRadius, circleBorderWidth } = this.state;
let rangeMax = circleRadius + circleBorderWidth;
this.selectedIndex = null;//选中点后把下面传过去
for (var i = 0; i < serviceData.length; i++) {
var dianX = serviceData[i][0];
var dianY = serviceData[i][1];
|
e2460342
Zhang Zhuo
更改 -张卓
|
270
|
if ((((dianX - x) * (dianX - x)) + ((dianY - y) * (dianY - y))) < (rangeMax * rangeMax)) {
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
271
|
this.selectedIndex = i;//选中后返回下标然后返回真
|
e2460342
Zhang Zhuo
更改 -张卓
|
272
|
break;
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
273
274
275
276
|
}
}
if (i == serviceData.length) {
return false
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
277
278
|
} else {
|
e2460342
Zhang Zhuo
更改 -张卓
|
279
280
281
282
283
284
|
return true
}
}
sendFather(serviceData) {//向父亲发送里面的值
if (this.state.disabled !== "true") {
this.props.change(serviceData)
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
285
286
287
288
289
290
291
292
293
|
}
}
setData(data) {//设置值发生变化重新画
this.setState(data, () => {
this.draw()
})
}
render() {
let { id, width, height } = this.state;
|
42886855
Zhang Zhuo
更改保存下 -张卓
|
294
|
return (<canvas style={{ position: "absolute", top: 0, left: 0 }} id={id} width={width} height={height}></canvas>)
|
d47ac30f
Zhang Zhuo
更新组件区域选择 -张卓
|
295
296
|
}
}
|