fl-web-component 1.3.1 → 1.3.2
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/README.md +3 -0
- package/dist/fl-web-component.common.js +49 -32
- package/dist/fl-web-component.common.js.map +1 -1
- package/dist/fl-web-component.css +1 -1
- package/package.json +1 -1
- package/packages/components/com-flcanvas/index.vue +6 -1
- package/src/utils/threejs/measure-angle.js +175 -172
- package/src/utils/threejs/measure-area.js +185 -168
- package/src/utils/threejs/measure-distance.js +152 -146
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import * as THREE from 'three'
|
|
2
|
-
import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer'
|
|
3
|
-
var _this = null
|
|
4
|
-
var MeasureDistance = function(renderer, scene, camera, width, height) {
|
|
5
|
-
this.renderer = renderer
|
|
6
|
-
this.scene = scene
|
|
7
|
-
this.camera = camera
|
|
8
|
-
this.pointArray = [] // 保存当前操作所添加的点
|
|
9
|
-
this.raycaster = new THREE.Raycaster()
|
|
10
|
-
this.points = [] // 保存页面中所添加的点
|
|
11
|
-
this.polyline = [] //保存页面中所添加的直线
|
|
12
|
-
this.labels = [] // 保存页面中所添加的文本
|
|
13
|
-
this.tempPoints = undefined
|
|
14
|
-
this.tempLine = undefined
|
|
15
|
-
this.tempLabel = undefined
|
|
16
|
-
this.tipsLabel = undefined
|
|
17
|
-
this.isCompleted = false
|
|
18
|
-
this.timer = null
|
|
19
|
-
this.width = width
|
|
20
|
-
this.height = height
|
|
21
|
-
this.firstTime = 0
|
|
22
|
-
}
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer';
|
|
3
|
+
var _this = null;
|
|
4
|
+
var MeasureDistance = function (renderer, scene, camera, width, height) {
|
|
5
|
+
this.renderer = renderer;
|
|
6
|
+
this.scene = scene;
|
|
7
|
+
this.camera = camera;
|
|
8
|
+
this.pointArray = []; // 保存当前操作所添加的点
|
|
9
|
+
this.raycaster = new THREE.Raycaster();
|
|
10
|
+
this.points = []; // 保存页面中所添加的点
|
|
11
|
+
this.polyline = []; //保存页面中所添加的直线
|
|
12
|
+
this.labels = []; // 保存页面中所添加的文本
|
|
13
|
+
this.tempPoints = undefined;
|
|
14
|
+
this.tempLine = undefined;
|
|
15
|
+
this.tempLabel = undefined;
|
|
16
|
+
this.tipsLabel = undefined;
|
|
17
|
+
this.isCompleted = false;
|
|
18
|
+
this.timer = null;
|
|
19
|
+
this.width = width;
|
|
20
|
+
this.height = height;
|
|
21
|
+
this.firstTime = 0;
|
|
22
|
+
};
|
|
23
23
|
|
|
24
24
|
MeasureDistance.prototype = {
|
|
25
25
|
start() {
|
|
26
|
-
_this = this
|
|
27
|
-
this.renderer.domElement.style.cursor = 'crosshair'
|
|
28
|
-
this.renderer.domElement.addEventListener('mousedown', this.mousedown, false)
|
|
29
|
-
this.renderer.domElement.addEventListener('mouseup', this.click, false)
|
|
30
|
-
this.renderer.domElement.addEventListener('mousemove',
|
|
31
|
-
this.renderer.domElement.addEventListener('contextmenu', this.rightClick, false)
|
|
26
|
+
_this = this;
|
|
27
|
+
this.renderer.domElement.style.cursor = 'crosshair';
|
|
28
|
+
this.renderer.domElement.addEventListener('mousedown', this.mousedown, false);
|
|
29
|
+
this.renderer.domElement.addEventListener('mouseup', this.click, false);
|
|
30
|
+
this.renderer.domElement.addEventListener('mousemove', this.mousemove, false);
|
|
31
|
+
this.renderer.domElement.addEventListener('contextmenu', this.rightClick, false);
|
|
32
32
|
},
|
|
33
33
|
updateParams(width, height) {
|
|
34
|
-
this.camera.aspect = width / height
|
|
35
|
-
this.camera.updateProjectionMatrix()
|
|
36
|
-
this.renderer.setSize(width, height, true)
|
|
37
|
-
this.width = width
|
|
38
|
-
this.height = height
|
|
34
|
+
this.camera.aspect = width / height;
|
|
35
|
+
this.camera.updateProjectionMatrix();
|
|
36
|
+
this.renderer.setSize(width, height, true);
|
|
37
|
+
this.width = width;
|
|
38
|
+
this.height = height;
|
|
39
39
|
},
|
|
40
40
|
getPosition(e) {
|
|
41
41
|
const mouse = new THREE.Vector2();
|
|
@@ -46,71 +46,73 @@ MeasureDistance.prototype = {
|
|
|
46
46
|
mouse.x = (canvasX / elRect.width) * 2.0 - 1.0;
|
|
47
47
|
mouse.y = -(canvasY / elRect.height) * 2.0 + 1.0;
|
|
48
48
|
|
|
49
|
-
_this.raycaster.setFromCamera(mouse, this.camera)
|
|
50
|
-
let intersects = _this.raycaster.intersectObjects(_this.scene.children, true)
|
|
51
|
-
if(intersects.length > 0) {
|
|
52
|
-
return intersects[0].point
|
|
49
|
+
_this.raycaster.setFromCamera(mouse, this.camera);
|
|
50
|
+
let intersects = _this.raycaster.intersectObjects(_this.scene.children, true);
|
|
51
|
+
if (intersects.length > 0) {
|
|
52
|
+
return intersects[0].point;
|
|
53
53
|
}
|
|
54
|
-
return null
|
|
54
|
+
return null;
|
|
55
55
|
},
|
|
56
|
-
createLine(p1, p2, config = {color: 0xff0000}) {
|
|
57
|
-
const lineMaterial = new THREE.LineBasicMaterial({ color: config.color, linewidth: 15 })
|
|
58
|
-
const lineGeometry = new THREE.BufferGeometry().setFromPoints([p1, p2])
|
|
59
|
-
const line = new THREE.Line(lineGeometry, lineMaterial)
|
|
60
|
-
line.frustumCulled = false
|
|
61
|
-
return line
|
|
56
|
+
createLine(p1, p2, config = { color: 0xff0000 }) {
|
|
57
|
+
const lineMaterial = new THREE.LineBasicMaterial({ color: config.color, linewidth: 15 });
|
|
58
|
+
const lineGeometry = new THREE.BufferGeometry().setFromPoints([p1, p2]);
|
|
59
|
+
const line = new THREE.Line(lineGeometry, lineMaterial);
|
|
60
|
+
line.frustumCulled = false;
|
|
61
|
+
return line;
|
|
62
62
|
},
|
|
63
63
|
createLabel(name, text, position) {
|
|
64
|
-
const div = document.createElement('div')
|
|
65
|
-
div.className = name
|
|
66
|
-
div.textContent = text
|
|
67
|
-
const divLabel = new CSS2DObject(div)
|
|
68
|
-
divLabel.position.set(position.x, position.y, position.z)
|
|
69
|
-
return divLabel
|
|
64
|
+
const div = document.createElement('div');
|
|
65
|
+
div.className = name;
|
|
66
|
+
div.textContent = text;
|
|
67
|
+
const divLabel = new CSS2DObject(div);
|
|
68
|
+
divLabel.position.set(position.x, position.y, position.z);
|
|
69
|
+
return divLabel;
|
|
70
70
|
},
|
|
71
71
|
mousemove(e) {
|
|
72
|
-
if (_this.isCompleted || _this.pointArray.length === 0) return
|
|
73
|
-
const point = _this.getPosition(e)
|
|
72
|
+
if (_this.isCompleted || _this.pointArray.length === 0) return;
|
|
73
|
+
const point = _this.getPosition(e);
|
|
74
74
|
if (point) {
|
|
75
|
-
_this.pointArray.length === 1
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
_this.
|
|
94
|
-
|
|
95
|
-
_this.
|
|
96
|
-
|
|
97
|
-
_this.
|
|
98
|
-
_this.
|
|
99
|
-
_this.
|
|
100
|
-
_this.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
75
|
+
_this.pointArray.length === 1
|
|
76
|
+
? _this.pointArray.push(point)
|
|
77
|
+
: _this.pointArray.splice(_this.pointArray.length - 1, 1, point);
|
|
78
|
+
const length = _this.pointArray.length;
|
|
79
|
+
if (_this.tempPoints) {
|
|
80
|
+
_this.tempPoints.position.set(point.x, point.y, point.z);
|
|
81
|
+
} else {
|
|
82
|
+
const geom = _this.createLabel('circle-tag', '', point);
|
|
83
|
+
_this.tempPoints = geom;
|
|
84
|
+
_this.points.push(geom);
|
|
85
|
+
_this.scene.add(geom);
|
|
86
|
+
}
|
|
87
|
+
const p1 = _this.pointArray[length - 2];
|
|
88
|
+
const p2 = _this.pointArray[length - 1];
|
|
89
|
+
const dist = p1.distanceTo(p2);
|
|
90
|
+
const label = `${_this.numberToString(dist)}`;
|
|
91
|
+
const position = new THREE.Vector3((p1.x + p2.x) / 2, (p1.y + p2.y) / 2, (p1.z + p2.z) / 2);
|
|
92
|
+
if (_this.tempLine) {
|
|
93
|
+
_this.tempLine.geometry.setFromPoints([p1, p2]);
|
|
94
|
+
_this.tempLabel.element.textContent = label;
|
|
95
|
+
_this.tempLabel.position.set(position.x, position.y, position.z);
|
|
96
|
+
} else {
|
|
97
|
+
_this.tempLine = _this.createLine(p1, p2);
|
|
98
|
+
_this.tempLabel = _this.createLabel('measure-label', label, position);
|
|
99
|
+
_this.polyline.push(_this.tempLine);
|
|
100
|
+
_this.labels.push(_this.tempLabel);
|
|
101
|
+
_this.scene.add(_this.tempLine);
|
|
102
|
+
_this.scene.add(_this.tempLabel);
|
|
103
|
+
}
|
|
104
|
+
if (_this.tipsLabel) {
|
|
105
|
+
_this.tipsLabel.position.set(point.x + 0.1, point.y, point.z + 0.05);
|
|
106
|
+
}
|
|
105
107
|
}
|
|
106
108
|
},
|
|
107
109
|
createTipsLabel(label, position) {
|
|
108
|
-
const div = document.createElement('div')
|
|
109
|
-
div.className = 'tips-label'
|
|
110
|
-
div.textContent = label
|
|
111
|
-
const tipsLabel = new CSS2DObject(div)
|
|
112
|
-
tipsLabel.position.set(position.x + 0.1, position.y, position.z + 0.05)
|
|
113
|
-
return tipsLabel
|
|
110
|
+
const div = document.createElement('div');
|
|
111
|
+
div.className = 'tips-label';
|
|
112
|
+
div.textContent = label;
|
|
113
|
+
const tipsLabel = new CSS2DObject(div);
|
|
114
|
+
tipsLabel.position.set(position.x + 0.1, position.y, position.z + 0.05);
|
|
115
|
+
return tipsLabel;
|
|
114
116
|
},
|
|
115
117
|
mousedown() {
|
|
116
118
|
this.firstTime = new Date().getTime();
|
|
@@ -119,92 +121,96 @@ MeasureDistance.prototype = {
|
|
|
119
121
|
let lastTime = new Date().getTime();
|
|
120
122
|
if (lastTime - this.firstTime < 300) {
|
|
121
123
|
if (_this.isCompleted) {
|
|
122
|
-
_this.renderer.domElement.addEventListener('mousemove', _this.mousemove)
|
|
124
|
+
_this.renderer.domElement.addEventListener('mousemove', _this.mousemove);
|
|
123
125
|
}
|
|
124
|
-
clearTimeout(_this.timer)
|
|
126
|
+
clearTimeout(_this.timer);
|
|
125
127
|
_this.timer = setTimeout(() => {
|
|
126
|
-
_this.isCompleted = false
|
|
127
|
-
const point = _this.getPosition(e)
|
|
128
|
+
_this.isCompleted = false;
|
|
129
|
+
const point = _this.getPosition(e);
|
|
128
130
|
if (point) {
|
|
129
|
-
if(_this.tipsLabel) {
|
|
130
|
-
_this.tipsLabel.position.set(point.x + 0.1, point.y, point.z + 0.05)
|
|
131
|
+
if (_this.tipsLabel) {
|
|
132
|
+
_this.tipsLabel.position.set(point.x + 0.1, point.y, point.z + 0.05);
|
|
131
133
|
} else {
|
|
132
|
-
_this.tipsLabel = _this.createTipsLabel('左击绘制右击结束', point)
|
|
133
|
-
_this.scene.add(_this.tipsLabel)
|
|
134
|
+
_this.tipsLabel = _this.createTipsLabel('左击绘制右击结束', point);
|
|
135
|
+
_this.scene.add(_this.tipsLabel);
|
|
134
136
|
}
|
|
135
137
|
if (_this.tempPoints) {
|
|
136
|
-
_this.tempPoints.position.set(point.x, point.y, point.z)
|
|
137
|
-
_this.tempPoints = undefined
|
|
138
|
+
_this.tempPoints.position.set(point.x, point.y, point.z);
|
|
139
|
+
_this.tempPoints = undefined;
|
|
138
140
|
} else {
|
|
139
|
-
const geom = _this.createLabel('circle-tag', '',
|
|
140
|
-
_this.points.push(geom)
|
|
141
|
-
_this.scene.add(geom)
|
|
141
|
+
const geom = _this.createLabel('circle-tag', '', point);
|
|
142
|
+
_this.points.push(geom);
|
|
143
|
+
_this.scene.add(geom);
|
|
142
144
|
}
|
|
143
|
-
_this.tempLine = undefined
|
|
144
|
-
_this.tempLabel = undefined
|
|
145
|
-
_this.pointArray.push(point)
|
|
145
|
+
_this.tempLine = undefined;
|
|
146
|
+
_this.tempLabel = undefined;
|
|
147
|
+
_this.pointArray.push(point);
|
|
146
148
|
}
|
|
147
|
-
})
|
|
149
|
+
});
|
|
148
150
|
}
|
|
149
151
|
},
|
|
150
152
|
rightClick(e) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
_this.tipsLabel
|
|
153
|
+
_this.preventContextMenu(e);
|
|
154
|
+
if (_this.tipsLabel) {
|
|
155
|
+
_this.scene.remove(_this.tipsLabel);
|
|
156
|
+
_this.tipsLabel = undefined;
|
|
154
157
|
}
|
|
155
|
-
clearTimeout(_this.timer)
|
|
156
|
-
const point = _this.getPosition(e)
|
|
158
|
+
clearTimeout(_this.timer);
|
|
159
|
+
const point = _this.getPosition(e);
|
|
157
160
|
if (point) {
|
|
158
|
-
_this.isCompleted = true
|
|
159
|
-
_this.tempPoints = undefined
|
|
160
|
-
_this.tempLine = undefined
|
|
161
|
-
_this.tempLabel = undefined
|
|
162
|
-
_this.pointArray.splice(0)
|
|
163
|
-
_this.renderer.domElement.removeEventListener('mousemove', _this.mousemove)
|
|
161
|
+
_this.isCompleted = true;
|
|
162
|
+
_this.tempPoints = undefined;
|
|
163
|
+
_this.tempLine = undefined;
|
|
164
|
+
_this.tempLabel = undefined;
|
|
165
|
+
_this.pointArray.splice(0);
|
|
166
|
+
_this.renderer.domElement.removeEventListener('mousemove', _this.mousemove);
|
|
164
167
|
}
|
|
165
168
|
},
|
|
166
169
|
close() {
|
|
167
|
-
this.renderer.domElement.removeEventListener('mousedown', this.mousedown)
|
|
168
|
-
this.renderer.domElement.removeEventListener('mouseup', this.click)
|
|
169
|
-
this.renderer.domElement.removeEventListener('mousemove',
|
|
170
|
-
this.renderer.domElement.removeEventListener('contextmenu', this.rightClick)
|
|
171
|
-
this.remove(this.points)
|
|
172
|
-
this.remove(this.polyline)
|
|
173
|
-
this.remove(this.labels)
|
|
174
|
-
this.pointArray.splice(0)
|
|
175
|
-
this.points.splice(0)
|
|
176
|
-
this.polyline.splice(0)
|
|
177
|
-
this.labels.splice(0)
|
|
178
|
-
this.tempPoints = undefined
|
|
179
|
-
this.tempLabel = undefined
|
|
180
|
-
this.tempLine = undefined
|
|
181
|
-
this.scene.remove(this.tipsLabel)
|
|
182
|
-
this.tipsLabel = undefined
|
|
183
|
-
this.renderer.domElement.style.cursor = 'pointer'
|
|
184
|
-
this.firstTime = 0
|
|
170
|
+
this.renderer.domElement.removeEventListener('mousedown', this.mousedown);
|
|
171
|
+
this.renderer.domElement.removeEventListener('mouseup', this.click);
|
|
172
|
+
this.renderer.domElement.removeEventListener('mousemove', this.mousemove);
|
|
173
|
+
this.renderer.domElement.removeEventListener('contextmenu', this.rightClick);
|
|
174
|
+
this.remove(this.points);
|
|
175
|
+
this.remove(this.polyline);
|
|
176
|
+
this.remove(this.labels);
|
|
177
|
+
this.pointArray.splice(0);
|
|
178
|
+
this.points.splice(0);
|
|
179
|
+
this.polyline.splice(0);
|
|
180
|
+
this.labels.splice(0);
|
|
181
|
+
this.tempPoints = undefined;
|
|
182
|
+
this.tempLabel = undefined;
|
|
183
|
+
this.tempLine = undefined;
|
|
184
|
+
this.scene.remove(this.tipsLabel);
|
|
185
|
+
this.tipsLabel = undefined;
|
|
186
|
+
this.renderer.domElement.style.cursor = 'pointer';
|
|
187
|
+
this.firstTime = 0;
|
|
185
188
|
},
|
|
186
189
|
remove(array) {
|
|
187
190
|
for (let index = 0; index < array.length; index++) {
|
|
188
|
-
const element = array[index]
|
|
191
|
+
const element = array[index];
|
|
189
192
|
if (element.geometry) {
|
|
190
|
-
element.geometry.dispose()
|
|
193
|
+
element.geometry.dispose();
|
|
191
194
|
}
|
|
192
|
-
this.scene.remove(element)
|
|
195
|
+
this.scene.remove(element);
|
|
193
196
|
}
|
|
194
197
|
},
|
|
195
198
|
numberToString(num) {
|
|
196
199
|
if (num < 0.0001) {
|
|
197
|
-
return num.toString()
|
|
200
|
+
return num.toString();
|
|
198
201
|
}
|
|
199
|
-
let fractionDigits = 2
|
|
202
|
+
let fractionDigits = 2;
|
|
200
203
|
if (num < 0.01) {
|
|
201
|
-
fractionDigits = 4
|
|
204
|
+
fractionDigits = 4;
|
|
202
205
|
} else if (num < 0.1) {
|
|
203
|
-
fractionDigits = 3
|
|
206
|
+
fractionDigits = 3;
|
|
204
207
|
}
|
|
205
|
-
return num.toFixed(fractionDigits)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
+
return num.toFixed(fractionDigits);
|
|
209
|
+
},
|
|
210
|
+
preventContextMenu(event) {
|
|
211
|
+
event.preventDefault();
|
|
212
|
+
},
|
|
213
|
+
};
|
|
208
214
|
export default {
|
|
209
|
-
MeasureDistance
|
|
210
|
-
}
|
|
215
|
+
MeasureDistance,
|
|
216
|
+
};
|