fl-web-component 1.3.0 → 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.
@@ -1,120 +1,135 @@
1
- import * as THREE from 'three'
2
- import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer'
3
- var _this = null
4
- var MeasureArea = 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.polygonMesh = undefined
20
- this.polygons = []
21
- this.width = width
22
- this.height = height
23
- this.firstTime = 0
24
- }
1
+ import * as THREE from 'three';
2
+ import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer';
3
+ var _this = null;
4
+ var MeasureArea = 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.polygonMesh = undefined;
20
+ this.polygons = [];
21
+ this.width = width;
22
+ this.height = height;
23
+ this.firstTime = 0;
24
+ };
25
25
 
26
26
  MeasureArea.prototype = {
27
27
  start() {
28
- _this = this
29
- this.renderer.domElement.style.cursor = 'crosshair'
30
- this.renderer.domElement.addEventListener('mouseup', this.click, false)
31
- this.renderer.domElement.addEventListener('mousedown', this.mousedown, false)
32
- this.renderer.domElement.addEventListener('mousemove', this.mousemove, false)
33
- this.renderer.domElement.addEventListener('contextmenu', this.rightClick, false)
28
+ _this = this;
29
+ this.renderer.domElement.style.cursor = 'crosshair';
30
+ this.renderer.domElement.addEventListener('mouseup', this.click, false);
31
+ this.renderer.domElement.addEventListener('mousedown', this.mousedown, false);
32
+ this.renderer.domElement.addEventListener('mousemove', this.mousemove, false);
33
+ this.renderer.domElement.addEventListener('contextmenu', this.rightClick, false);
34
34
  },
35
35
  updateParams(width, height) {
36
- this.camera.aspect = width / height
37
- this.camera.updateProjectionMatrix()
38
- this.renderer.setSize(width, height, true)
39
- this.width = width
40
- this.height = height
36
+ this.camera.aspect = width / height;
37
+ this.camera.updateProjectionMatrix();
38
+ this.renderer.setSize(width, height, true);
39
+ this.width = width;
40
+ this.height = height;
41
41
  },
42
42
  getPosition(e) {
43
- const mouse = new THREE.Vector2()
44
- mouse.x = (e.clientX / _this.width) * 2 - 1
45
- mouse.y = -(e.clientY / _this.height) * 2 + 1
46
- _this.raycaster.setFromCamera(mouse, _this.camera)
47
- let intersects =_this.raycaster.intersectObjects(_this.scene.children, true)
48
- if(intersects.length > 0) {
49
- return intersects[0].point
43
+ const mouse = new THREE.Vector2();
44
+ const elRect = this.renderer.domElement.getBoundingClientRect();
45
+ const canvasX = e.clientX - elRect.left;
46
+ const canvasY = e.clientY - elRect.top;
47
+
48
+ mouse.x = (canvasX / elRect.width) * 2.0 - 1.0;
49
+ mouse.y = -(canvasY / elRect.height) * 2.0 + 1.0;
50
+
51
+ _this.raycaster.setFromCamera(mouse, _this.camera);
52
+ let intersects = _this.raycaster.intersectObjects(_this.scene.children, true);
53
+ if (intersects.length > 0) {
54
+ return intersects[0].point;
50
55
  }
51
- return null
56
+ return null;
52
57
  },
53
- createLine(p1, p2, config = {color:0xff0000}) {
54
- const lineMaterial = new THREE.LineBasicMaterial({ color: config.color, linewidth: 20 })
55
- const lineGeometry = new THREE.BufferGeometry().setFromPoints([p1, p2])
56
- const line = new THREE.Line(lineGeometry, lineMaterial)
57
- line.frustumCulled = false
58
- return line
58
+ createLine(p1, p2, config = { color: 0xff0000 }) {
59
+ const lineMaterial = new THREE.LineBasicMaterial({ color: config.color, linewidth: 20 });
60
+ const lineGeometry = new THREE.BufferGeometry().setFromPoints([p1, p2]);
61
+ const line = new THREE.Line(lineGeometry, lineMaterial);
62
+ line.frustumCulled = false;
63
+ return line;
59
64
  },
60
65
  createLabel(name, text, position) {
61
- const div = document.createElement('div')
62
- div.className = name
63
- div.textContent = text
64
- const divLabel = new CSS2DObject(div)
65
- divLabel.position.set(position.x, position.y, position.z)
66
- return divLabel
66
+ const div = document.createElement('div');
67
+ div.className = name;
68
+ div.textContent = text;
69
+ const divLabel = new CSS2DObject(div);
70
+ divLabel.position.set(position.x, position.y, position.z);
71
+ return divLabel;
67
72
  },
68
73
  mousemove(e) {
69
- if (_this.isCompleted || _this.pointArray.length === 0) return
70
- const point =_this.getPosition(e)
74
+ if (_this.isCompleted || _this.pointArray.length === 0) return;
75
+ const point = _this.getPosition(e);
71
76
  if (point) {
72
- _this.pointArray.length === 1 ? _this.pointArray.push(point) : _this.pointArray.splice(_this.pointArray.length - 1, 1, point)
73
- const length = _this.pointArray.length
77
+ _this.pointArray.length === 1
78
+ ? _this.pointArray.push(point)
79
+ : _this.pointArray.splice(_this.pointArray.length - 1, 1, point);
80
+ const length = _this.pointArray.length;
74
81
  if (_this.tempPoints) {
75
- _this.tempPoints.position.set(point.x, point.y, point.z)
82
+ _this.tempPoints.position.set(point.x, point.y, point.z);
76
83
  } else {
77
- const geom = _this.createLabel('circle-tag', '', point)
78
- _this.tempPoints = geom
79
- _this.points.push(geom)
80
- _this.scene.add(geom)
84
+ const geom = _this.createLabel('circle-tag', '', point);
85
+ _this.tempPoints = geom;
86
+ _this.points.push(geom);
87
+ _this.scene.add(geom);
81
88
  }
82
- const p1 = _this.pointArray[length - 2]
83
- const p2 = _this.pointArray[length - 1]
89
+ const p1 = _this.pointArray[length - 2];
90
+ const p2 = _this.pointArray[length - 1];
84
91
  if (_this.tempLine) {
85
- _this.tempLine.geometry.setFromPoints([p1, p2])
92
+ _this.tempLine.geometry.setFromPoints([p1, p2]);
86
93
  } else {
87
- _this.tempLine = _this.createLine(p1, p2)
88
- _this.polyline.push(_this.tempLine)
89
- _this.scene.add(_this.tempLine)
94
+ _this.tempLine = _this.createLine(p1, p2);
95
+ _this.polyline.push(_this.tempLine);
96
+ _this.scene.add(_this.tempLine);
90
97
  }
91
98
  if (_this.pointArray.length > 2) {
92
- const area = _this.calculateArea(_this.pointArray)
93
- _this.createPolygon(_this.pointArray)
99
+ const area = _this.calculateArea(_this.pointArray);
100
+ _this.createPolygon(_this.pointArray);
94
101
  if (_this.tempLabel) {
95
- _this.polygonMesh.geometry.computeBoundingSphere()
96
- _this.tempLabel.element.textContent = _this.numberToString(area) // + '㎡'
97
- _this.tempLabel.position.set(_this.polygonMesh.geometry.boundingSphere.center.x, _this.polygonMesh.geometry.boundingSphere.center.y, _this.polygonMesh.geometry.boundingSphere.center.z)
102
+ _this.polygonMesh.geometry.computeBoundingSphere();
103
+ _this.tempLabel.element.textContent = _this.numberToString(area); // + '㎡'
104
+ _this.tempLabel.position.set(
105
+ _this.polygonMesh.geometry.boundingSphere.center.x,
106
+ _this.polygonMesh.geometry.boundingSphere.center.y,
107
+ _this.polygonMesh.geometry.boundingSphere.center.z
108
+ );
98
109
  } else {
99
- _this.polygonMesh.geometry.computeBoundingSphere()
100
- console.log(_this.polygonMesh.geometry)
101
- _this.tempLabel = _this.createLabel('measure-label', area, _this.polygonMesh.geometry.boundingSphere.center)
102
- _this.labels.push(_this.tempLabel)
103
- _this.scene.add(_this.tempLabel)
110
+ _this.polygonMesh.geometry.computeBoundingSphere();
111
+ console.log(_this.polygonMesh.geometry);
112
+ _this.tempLabel = _this.createLabel(
113
+ 'measure-label',
114
+ area,
115
+ _this.polygonMesh.geometry.boundingSphere.center
116
+ );
117
+ _this.labels.push(_this.tempLabel);
118
+ _this.scene.add(_this.tempLabel);
104
119
  }
105
120
  }
106
121
  if (_this.tipsLabel) {
107
- _this.tipsLabel.position.set(point.x + 0.1, point.y, point.z + 0.05)
108
- }
122
+ _this.tipsLabel.position.set(point.x + 0.1, point.y, point.z + 0.05);
123
+ }
109
124
  }
110
125
  },
111
126
  createTipsLabel(label, position) {
112
- const div = document.createElement('div')
113
- div.className = 'tips-label'
114
- div.textContent = label
115
- const tipsLabel = new CSS2DObject(div)
116
- tipsLabel.position.set(position.x + 0.1, position.y, position.z + 0.05)
117
- return tipsLabel
127
+ const div = document.createElement('div');
128
+ div.className = 'tips-label';
129
+ div.textContent = label;
130
+ const tipsLabel = new CSS2DObject(div);
131
+ tipsLabel.position.set(position.x + 0.1, position.y, position.z + 0.05);
132
+ return tipsLabel;
118
133
  },
119
134
  mousedown() {
120
135
  this.firstTime = new Date().getTime();
@@ -123,135 +138,142 @@ MeasureArea.prototype = {
123
138
  let lastTime = new Date().getTime();
124
139
  if (lastTime - this.firstTime < 300) {
125
140
  if (_this.isCompleted) {
126
- _this.renderer.domElement.addEventListener('mousemove', _this.mousemove)
141
+ _this.renderer.domElement.addEventListener('mousemove', _this.mousemove);
127
142
  }
128
- clearTimeout(_this.timer)
143
+ clearTimeout(_this.timer);
129
144
  _this.timer = setTimeout(() => {
130
- _this.isCompleted = false
131
- const point = _this.getPosition(e)
145
+ _this.isCompleted = false;
146
+ const point = _this.getPosition(e);
132
147
  if (point) {
133
- if(_this.tipsLabel) {
134
- _this.tipsLabel.position.set(point.x + 0.01, point.y, point.z + 0.05)
148
+ if (_this.tipsLabel) {
149
+ _this.tipsLabel.position.set(point.x + 0.01, point.y, point.z + 0.05);
135
150
  } else {
136
- _this.tipsLabel = _this.createTipsLabel('左击绘制右击结束', point)
137
- _this.scene.add(_this.tipsLabel)
151
+ _this.tipsLabel = _this.createTipsLabel('左击绘制右击结束', point);
152
+ _this.scene.add(_this.tipsLabel);
138
153
  }
139
154
  if (_this.tempPoints) {
140
- _this.tempPoints.position.set(point.x, point.y, point.z)
141
- _this.tempPoints = undefined
155
+ _this.tempPoints.position.set(point.x, point.y, point.z);
156
+ _this.tempPoints = undefined;
142
157
  } else {
143
- const geom = _this.createLabel('circle-tag', '', point)
144
- _this.points.push(geom)
145
- _this.scene.add(geom)
158
+ const geom = _this.createLabel('circle-tag', '', point);
159
+ _this.points.push(geom);
160
+ _this.scene.add(geom);
146
161
  }
147
- _this.tempLine = undefined
148
- _this.pointArray.push(point)
162
+ _this.tempLine = undefined;
163
+ _this.pointArray.push(point);
149
164
  }
150
- }, 100)
165
+ }, 100);
151
166
  }
152
167
  },
153
- rightClick (e) {
154
- if(_this.tipsLabel) {
155
- _this.scene.remove(_this.tipsLabel)
156
- _this.tipsLabel = undefined
168
+ rightClick(e) {
169
+ _this.preventContextMenu(e);
170
+ if (_this.tipsLabel) {
171
+ _this.scene.remove(_this.tipsLabel);
172
+ _this.tipsLabel = undefined;
157
173
  }
158
- clearTimeout(_this.timer)
159
- const point = _this.getPosition(e)
174
+ clearTimeout(_this.timer);
175
+ const point = _this.getPosition(e);
160
176
  if (point) {
161
- _this.isCompleted = true
177
+ _this.isCompleted = true;
162
178
  if (_this.tempPoints) {
163
- _this.tempPoints.position.set(point.x, point.y, point.z)
164
- _this.tempPoints = undefined
179
+ _this.tempPoints.position.set(point.x, point.y, point.z);
180
+ _this.tempPoints = undefined;
165
181
  }
166
- _this.tempLine = undefined
167
- _this.tempLabel = undefined
168
- _this.polygonMesh = undefined
169
- _this.pointArray.splice(0)
170
- _this.renderer.domElement.removeEventListener('mousemove', _this.mousemove)
182
+ _this.tempLine = undefined;
183
+ _this.tempLabel = undefined;
184
+ _this.polygonMesh = undefined;
185
+ _this.pointArray.splice(0);
186
+ _this.renderer.domElement.removeEventListener('mousemove', _this.mousemove);
171
187
  }
172
188
  },
173
189
  close() {
174
- this.renderer.domElement.removeEventListener('mouseup', this.click)
175
- this.renderer.domElement.removeEventListener('mousedown', this.mousedown)
176
- this.renderer.domElement.removeEventListener('mousemove', this.mousemove)
177
- this.renderer.domElement.removeEventListener('contextmenu', this.rightClick)
178
- this.remove(this.points)
179
- this.remove(this.polyline)
180
- this.remove(this.labels)
181
- this.remove(this.polygons)
182
- this.pointArray.splice(0)
183
- this.points.splice(0)
184
- this.polyline.splice(0)
185
- this.labels.splice(0)
186
- this.tempPoints = undefined
187
- this.tempLabel = undefined
188
- this.tempLine = undefined
189
- this.scene.remove(this.tipsLabel)
190
- this.tipsLabel = undefined
191
- this.renderer.domElement.style.cursor = 'pointer'
190
+ this.renderer.domElement.removeEventListener('mouseup', this.click);
191
+ this.renderer.domElement.removeEventListener('mousedown', this.mousedown);
192
+ this.renderer.domElement.removeEventListener('mousemove', this.mousemove);
193
+ this.renderer.domElement.removeEventListener('contextmenu', this.rightClick);
194
+ this.remove(this.points);
195
+ this.remove(this.polyline);
196
+ this.remove(this.labels);
197
+ this.remove(this.polygons);
198
+ this.pointArray.splice(0);
199
+ this.points.splice(0);
200
+ this.polyline.splice(0);
201
+ this.labels.splice(0);
202
+ this.tempPoints = undefined;
203
+ this.tempLabel = undefined;
204
+ this.tempLine = undefined;
205
+ this.scene.remove(this.tipsLabel);
206
+ this.tipsLabel = undefined;
207
+ this.renderer.domElement.style.cursor = 'pointer';
192
208
  },
193
209
  remove(array) {
194
210
  for (let index = 0; index < array.length; index++) {
195
- const element = array[index]
211
+ const element = array[index];
196
212
  if (element.geometry) {
197
- element.geometry.dispose()
213
+ element.geometry.dispose();
198
214
  }
199
- this.scene.remove(element)
215
+ this.scene.remove(element);
200
216
  }
201
217
  },
202
218
  calculateArea(points) {
203
- let area = 0
219
+ let area = 0;
204
220
  for (let i = 0, j = 1, k = 2; k < points.length; j++, k++) {
205
- const a = points[i].distanceTo(points[j])
206
- const b = points[j].distanceTo(points[k])
207
- const c = points[k].distanceTo(points[i])
208
- const p = (a + b + c) / 2
209
- area += Math.sqrt(p * (p - a) * (p - b) * (p - c))
221
+ const a = points[i].distanceTo(points[j]);
222
+ const b = points[j].distanceTo(points[k]);
223
+ const c = points[k].distanceTo(points[i]);
224
+ const p = (a + b + c) / 2;
225
+ area += Math.sqrt(p * (p - a) * (p - b) * (p - c));
210
226
  }
211
- return area
227
+ return area;
212
228
  },
213
229
  numberToString(num) {
214
230
  if (num < 0.0001) {
215
- return num.toString()
231
+ return num.toString();
216
232
  }
217
- let fractionDigits = 2
233
+ let fractionDigits = 2;
218
234
  if (num < 0.01) {
219
- fractionDigits = 4
235
+ fractionDigits = 4;
220
236
  } else if (num < 0.1) {
221
- fractionDigits = 3
237
+ fractionDigits = 3;
222
238
  }
223
- return num.toFixed(fractionDigits)
239
+ return num.toFixed(fractionDigits);
224
240
  },
225
241
  createPolygon(points) {
226
- const length = points.length
227
- const faces = []
242
+ const length = points.length;
243
+ const faces = [];
228
244
  for (let i = 0; i < length - 2; i++) {
229
- faces.push(points[0].x, points[0].y, points[0].z)
230
- faces.push(points[i+1].x, points[i+1].y, points[i+1].z)
231
- faces.push(points[i+2].x, points[i+2].y, points[i+2].z)
245
+ faces.push(points[0].x, points[0].y, points[0].z);
246
+ faces.push(points[i + 1].x, points[i + 1].y, points[i + 1].z);
247
+ faces.push(points[i + 2].x, points[i + 2].y, points[i + 2].z);
232
248
  }
233
249
  if (faces.length > 3) {
234
250
  if (_this.polygonMesh) {
235
- _this.polygonMesh.geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(faces), 3 ))
251
+ _this.polygonMesh.geometry.setAttribute(
252
+ 'position',
253
+ new THREE.BufferAttribute(new Float32Array(faces), 3)
254
+ );
236
255
  } else {
237
- const geom = new THREE.BufferGeometry()
238
- geom.setAttribute('position', new THREE.BufferAttribute(new Float32Array(faces), 3 ))
256
+ const geom = new THREE.BufferGeometry();
257
+ geom.setAttribute('position', new THREE.BufferAttribute(new Float32Array(faces), 3));
239
258
  const material = new THREE.MeshBasicMaterial({
240
259
  color: 0xffffff,
241
260
  transparent: true,
242
261
  opacity: 0.5,
243
- side: THREE.DoubleSide
244
- })
245
- const mesh = new THREE.Mesh(geom, material)
246
- mesh.frustumCulled = false
247
- mesh.name = 'polygonMesh'
248
- _this.polygonMesh = mesh
249
- _this.scene.add(mesh)
250
- _this.polygons.push(mesh)
262
+ side: THREE.DoubleSide,
263
+ });
264
+ const mesh = new THREE.Mesh(geom, material);
265
+ mesh.frustumCulled = false;
266
+ mesh.name = 'polygonMesh';
267
+ _this.polygonMesh = mesh;
268
+ _this.scene.add(mesh);
269
+ _this.polygons.push(mesh);
251
270
  }
252
271
  }
253
- }
254
- }
272
+ },
273
+ preventContextMenu(event) {
274
+ event.preventDefault();
275
+ },
276
+ };
255
277
  export default {
256
- MeasureArea
257
- }
278
+ MeasureArea,
279
+ };