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.
@@ -1,43 +1,43 @@
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
43
  const mouse = new THREE.Vector2();
@@ -48,78 +48,88 @@ MeasureArea.prototype = {
48
48
  mouse.x = (canvasX / elRect.width) * 2.0 - 1.0;
49
49
  mouse.y = -(canvasY / elRect.height) * 2.0 + 1.0;
50
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
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;
55
55
  }
56
- return null
56
+ return null;
57
57
  },
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
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;
64
64
  },
65
65
  createLabel(name, text, position) {
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
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;
72
72
  },
73
73
  mousemove(e) {
74
- if (_this.isCompleted || _this.pointArray.length === 0) return
75
- const point =_this.getPosition(e)
74
+ if (_this.isCompleted || _this.pointArray.length === 0) return;
75
+ const point = _this.getPosition(e);
76
76
  if (point) {
77
- _this.pointArray.length === 1 ? _this.pointArray.push(point) : _this.pointArray.splice(_this.pointArray.length - 1, 1, point)
78
- 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;
79
81
  if (_this.tempPoints) {
80
- _this.tempPoints.position.set(point.x, point.y, point.z)
82
+ _this.tempPoints.position.set(point.x, point.y, point.z);
81
83
  } else {
82
- const geom = _this.createLabel('circle-tag', '', point)
83
- _this.tempPoints = geom
84
- _this.points.push(geom)
85
- _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);
86
88
  }
87
- const p1 = _this.pointArray[length - 2]
88
- const p2 = _this.pointArray[length - 1]
89
+ const p1 = _this.pointArray[length - 2];
90
+ const p2 = _this.pointArray[length - 1];
89
91
  if (_this.tempLine) {
90
- _this.tempLine.geometry.setFromPoints([p1, p2])
92
+ _this.tempLine.geometry.setFromPoints([p1, p2]);
91
93
  } else {
92
- _this.tempLine = _this.createLine(p1, p2)
93
- _this.polyline.push(_this.tempLine)
94
- _this.scene.add(_this.tempLine)
94
+ _this.tempLine = _this.createLine(p1, p2);
95
+ _this.polyline.push(_this.tempLine);
96
+ _this.scene.add(_this.tempLine);
95
97
  }
96
98
  if (_this.pointArray.length > 2) {
97
- const area = _this.calculateArea(_this.pointArray)
98
- _this.createPolygon(_this.pointArray)
99
+ const area = _this.calculateArea(_this.pointArray);
100
+ _this.createPolygon(_this.pointArray);
99
101
  if (_this.tempLabel) {
100
- _this.polygonMesh.geometry.computeBoundingSphere()
101
- _this.tempLabel.element.textContent = _this.numberToString(area) // + '㎡'
102
- _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
+ );
103
109
  } else {
104
- _this.polygonMesh.geometry.computeBoundingSphere()
105
- console.log(_this.polygonMesh.geometry)
106
- _this.tempLabel = _this.createLabel('measure-label', area, _this.polygonMesh.geometry.boundingSphere.center)
107
- _this.labels.push(_this.tempLabel)
108
- _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);
109
119
  }
110
120
  }
111
121
  if (_this.tipsLabel) {
112
- _this.tipsLabel.position.set(point.x + 0.1, point.y, point.z + 0.05)
113
- }
122
+ _this.tipsLabel.position.set(point.x + 0.1, point.y, point.z + 0.05);
123
+ }
114
124
  }
115
125
  },
116
126
  createTipsLabel(label, position) {
117
- const div = document.createElement('div')
118
- div.className = 'tips-label'
119
- div.textContent = label
120
- const tipsLabel = new CSS2DObject(div)
121
- tipsLabel.position.set(position.x + 0.1, position.y, position.z + 0.05)
122
- 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;
123
133
  },
124
134
  mousedown() {
125
135
  this.firstTime = new Date().getTime();
@@ -128,135 +138,142 @@ MeasureArea.prototype = {
128
138
  let lastTime = new Date().getTime();
129
139
  if (lastTime - this.firstTime < 300) {
130
140
  if (_this.isCompleted) {
131
- _this.renderer.domElement.addEventListener('mousemove', _this.mousemove)
141
+ _this.renderer.domElement.addEventListener('mousemove', _this.mousemove);
132
142
  }
133
- clearTimeout(_this.timer)
143
+ clearTimeout(_this.timer);
134
144
  _this.timer = setTimeout(() => {
135
- _this.isCompleted = false
136
- const point = _this.getPosition(e)
145
+ _this.isCompleted = false;
146
+ const point = _this.getPosition(e);
137
147
  if (point) {
138
- if(_this.tipsLabel) {
139
- _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);
140
150
  } else {
141
- _this.tipsLabel = _this.createTipsLabel('左击绘制右击结束', point)
142
- _this.scene.add(_this.tipsLabel)
151
+ _this.tipsLabel = _this.createTipsLabel('左击绘制右击结束', point);
152
+ _this.scene.add(_this.tipsLabel);
143
153
  }
144
154
  if (_this.tempPoints) {
145
- _this.tempPoints.position.set(point.x, point.y, point.z)
146
- _this.tempPoints = undefined
155
+ _this.tempPoints.position.set(point.x, point.y, point.z);
156
+ _this.tempPoints = undefined;
147
157
  } else {
148
- const geom = _this.createLabel('circle-tag', '', point)
149
- _this.points.push(geom)
150
- _this.scene.add(geom)
158
+ const geom = _this.createLabel('circle-tag', '', point);
159
+ _this.points.push(geom);
160
+ _this.scene.add(geom);
151
161
  }
152
- _this.tempLine = undefined
153
- _this.pointArray.push(point)
162
+ _this.tempLine = undefined;
163
+ _this.pointArray.push(point);
154
164
  }
155
- }, 100)
165
+ }, 100);
156
166
  }
157
167
  },
158
- rightClick (e) {
159
- if(_this.tipsLabel) {
160
- _this.scene.remove(_this.tipsLabel)
161
- _this.tipsLabel = undefined
168
+ rightClick(e) {
169
+ _this.preventContextMenu(e);
170
+ if (_this.tipsLabel) {
171
+ _this.scene.remove(_this.tipsLabel);
172
+ _this.tipsLabel = undefined;
162
173
  }
163
- clearTimeout(_this.timer)
164
- const point = _this.getPosition(e)
174
+ clearTimeout(_this.timer);
175
+ const point = _this.getPosition(e);
165
176
  if (point) {
166
- _this.isCompleted = true
177
+ _this.isCompleted = true;
167
178
  if (_this.tempPoints) {
168
- _this.tempPoints.position.set(point.x, point.y, point.z)
169
- _this.tempPoints = undefined
179
+ _this.tempPoints.position.set(point.x, point.y, point.z);
180
+ _this.tempPoints = undefined;
170
181
  }
171
- _this.tempLine = undefined
172
- _this.tempLabel = undefined
173
- _this.polygonMesh = undefined
174
- _this.pointArray.splice(0)
175
- _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);
176
187
  }
177
188
  },
178
189
  close() {
179
- this.renderer.domElement.removeEventListener('mouseup', this.click)
180
- this.renderer.domElement.removeEventListener('mousedown', this.mousedown)
181
- this.renderer.domElement.removeEventListener('mousemove', this.mousemove)
182
- this.renderer.domElement.removeEventListener('contextmenu', this.rightClick)
183
- this.remove(this.points)
184
- this.remove(this.polyline)
185
- this.remove(this.labels)
186
- this.remove(this.polygons)
187
- this.pointArray.splice(0)
188
- this.points.splice(0)
189
- this.polyline.splice(0)
190
- this.labels.splice(0)
191
- this.tempPoints = undefined
192
- this.tempLabel = undefined
193
- this.tempLine = undefined
194
- this.scene.remove(this.tipsLabel)
195
- this.tipsLabel = undefined
196
- 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';
197
208
  },
198
209
  remove(array) {
199
210
  for (let index = 0; index < array.length; index++) {
200
- const element = array[index]
211
+ const element = array[index];
201
212
  if (element.geometry) {
202
- element.geometry.dispose()
213
+ element.geometry.dispose();
203
214
  }
204
- this.scene.remove(element)
215
+ this.scene.remove(element);
205
216
  }
206
217
  },
207
218
  calculateArea(points) {
208
- let area = 0
219
+ let area = 0;
209
220
  for (let i = 0, j = 1, k = 2; k < points.length; j++, k++) {
210
- const a = points[i].distanceTo(points[j])
211
- const b = points[j].distanceTo(points[k])
212
- const c = points[k].distanceTo(points[i])
213
- const p = (a + b + c) / 2
214
- 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));
215
226
  }
216
- return area
227
+ return area;
217
228
  },
218
229
  numberToString(num) {
219
230
  if (num < 0.0001) {
220
- return num.toString()
231
+ return num.toString();
221
232
  }
222
- let fractionDigits = 2
233
+ let fractionDigits = 2;
223
234
  if (num < 0.01) {
224
- fractionDigits = 4
235
+ fractionDigits = 4;
225
236
  } else if (num < 0.1) {
226
- fractionDigits = 3
237
+ fractionDigits = 3;
227
238
  }
228
- return num.toFixed(fractionDigits)
239
+ return num.toFixed(fractionDigits);
229
240
  },
230
241
  createPolygon(points) {
231
- const length = points.length
232
- const faces = []
242
+ const length = points.length;
243
+ const faces = [];
233
244
  for (let i = 0; i < length - 2; i++) {
234
- faces.push(points[0].x, points[0].y, points[0].z)
235
- faces.push(points[i+1].x, points[i+1].y, points[i+1].z)
236
- 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);
237
248
  }
238
249
  if (faces.length > 3) {
239
250
  if (_this.polygonMesh) {
240
- _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
+ );
241
255
  } else {
242
- const geom = new THREE.BufferGeometry()
243
- 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));
244
258
  const material = new THREE.MeshBasicMaterial({
245
259
  color: 0xffffff,
246
260
  transparent: true,
247
261
  opacity: 0.5,
248
- side: THREE.DoubleSide
249
- })
250
- const mesh = new THREE.Mesh(geom, material)
251
- mesh.frustumCulled = false
252
- mesh.name = 'polygonMesh'
253
- _this.polygonMesh = mesh
254
- _this.scene.add(mesh)
255
- _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);
256
270
  }
257
271
  }
258
- }
259
- }
272
+ },
273
+ preventContextMenu(event) {
274
+ event.preventDefault();
275
+ },
276
+ };
260
277
  export default {
261
- MeasureArea
262
- }
278
+ MeasureArea,
279
+ };