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,111 +1,118 @@
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', this.mousemove, false)
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
- const mouse = new THREE.Vector2()
42
- mouse.x = (e.clientX / _this.width) * 2 - 1
43
- mouse.y = -(e.clientY / _this.height) * 2 + 1
44
- _this.raycaster.setFromCamera(mouse, this.camera)
45
- let intersects = _this.raycaster.intersectObjects(_this.scene.children, true)
46
- if(intersects.length > 0) {
47
- return intersects[0].point
41
+ const mouse = new THREE.Vector2();
42
+ const elRect = this.renderer.domElement.getBoundingClientRect();
43
+ const canvasX = e.clientX - elRect.left;
44
+ const canvasY = e.clientY - elRect.top;
45
+
46
+ mouse.x = (canvasX / elRect.width) * 2.0 - 1.0;
47
+ mouse.y = -(canvasY / elRect.height) * 2.0 + 1.0;
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;
48
53
  }
49
- return null
54
+ return null;
50
55
  },
51
- createLine(p1, p2, config = {color: 0xff0000}) {
52
- const lineMaterial = new THREE.LineBasicMaterial({ color: config.color, linewidth: 15 })
53
- const lineGeometry = new THREE.BufferGeometry().setFromPoints([p1, p2])
54
- const line = new THREE.Line(lineGeometry, lineMaterial)
55
- line.frustumCulled = false
56
- 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;
57
62
  },
58
63
  createLabel(name, text, position) {
59
- const div = document.createElement('div')
60
- div.className = name
61
- div.textContent = text
62
- const divLabel = new CSS2DObject(div)
63
- divLabel.position.set(position.x, position.y, position.z)
64
- 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;
65
70
  },
66
71
  mousemove(e) {
67
- if (_this.isCompleted || _this.pointArray.length === 0) return
68
- const point = _this.getPosition(e)
72
+ if (_this.isCompleted || _this.pointArray.length === 0) return;
73
+ const point = _this.getPosition(e);
69
74
  if (point) {
70
- _this.pointArray.length === 1 ? _this.pointArray.push(point) : _this.pointArray.splice(_this.pointArray.length - 1, 1, point)
71
- const length = _this.pointArray.length
72
- if (_this.tempPoints) {
73
- _this.tempPoints.position.set(point.x, point.y, point.z)
74
- } else {
75
- const geom = _this.createLabel('circle-tag', '', point)
76
- _this.tempPoints = geom
77
- _this.points.push(geom)
78
- _this.scene.add(geom)
79
- }
80
- const p1 = _this.pointArray[length - 2]
81
- const p2 = _this.pointArray[length - 1]
82
- const dist = p1.distanceTo(p2)
83
- const label = `${_this.numberToString(dist)}`
84
- const position = new THREE.Vector3((p1.x + p2.x) / 2, (p1.y + p2.y) / 2, (p1.z + p2.z) / 2)
85
- if (_this.tempLine) {
86
- _this.tempLine.geometry.setFromPoints([p1, p2])
87
- _this.tempLabel.element.textContent = label
88
- _this.tempLabel.position.set(position.x, position.y, position.z)
89
- } else {
90
- _this.tempLine = _this.createLine(p1, p2)
91
- _this.tempLabel = _this.createLabel('measure-label', label, position)
92
- _this.polyline.push(_this.tempLine)
93
- _this.labels.push(_this.tempLabel)
94
- _this.scene.add(_this.tempLine)
95
- _this.scene.add(_this.tempLabel)
96
- }
97
- if (_this.tipsLabel) {
98
- _this.tipsLabel.position.set(point.x + 0.1, point.y, point.z + 0.05)
99
- }
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
+ }
100
107
  }
101
108
  },
102
109
  createTipsLabel(label, position) {
103
- const div = document.createElement('div')
104
- div.className = 'tips-label'
105
- div.textContent = label
106
- const tipsLabel = new CSS2DObject(div)
107
- tipsLabel.position.set(position.x + 0.1, position.y, position.z + 0.05)
108
- 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;
109
116
  },
110
117
  mousedown() {
111
118
  this.firstTime = new Date().getTime();
@@ -114,92 +121,96 @@ MeasureDistance.prototype = {
114
121
  let lastTime = new Date().getTime();
115
122
  if (lastTime - this.firstTime < 300) {
116
123
  if (_this.isCompleted) {
117
- _this.renderer.domElement.addEventListener('mousemove', _this.mousemove)
124
+ _this.renderer.domElement.addEventListener('mousemove', _this.mousemove);
118
125
  }
119
- clearTimeout(_this.timer)
126
+ clearTimeout(_this.timer);
120
127
  _this.timer = setTimeout(() => {
121
- _this.isCompleted = false
122
- const point = _this.getPosition(e)
128
+ _this.isCompleted = false;
129
+ const point = _this.getPosition(e);
123
130
  if (point) {
124
- if(_this.tipsLabel) {
125
- _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);
126
133
  } else {
127
- _this.tipsLabel = _this.createTipsLabel('左击绘制右击结束', point)
128
- _this.scene.add(_this.tipsLabel)
134
+ _this.tipsLabel = _this.createTipsLabel('左击绘制右击结束', point);
135
+ _this.scene.add(_this.tipsLabel);
129
136
  }
130
137
  if (_this.tempPoints) {
131
- _this.tempPoints.position.set(point.x, point.y, point.z)
132
- _this.tempPoints = undefined
138
+ _this.tempPoints.position.set(point.x, point.y, point.z);
139
+ _this.tempPoints = undefined;
133
140
  } else {
134
- const geom = _this.createLabel('circle-tag', '', point)
135
- _this.points.push(geom)
136
- _this.scene.add(geom)
141
+ const geom = _this.createLabel('circle-tag', '', point);
142
+ _this.points.push(geom);
143
+ _this.scene.add(geom);
137
144
  }
138
- _this.tempLine = undefined
139
- _this.tempLabel = undefined
140
- _this.pointArray.push(point)
145
+ _this.tempLine = undefined;
146
+ _this.tempLabel = undefined;
147
+ _this.pointArray.push(point);
141
148
  }
142
- })
149
+ });
143
150
  }
144
151
  },
145
152
  rightClick(e) {
146
- if(_this.tipsLabel) {
147
- _this.scene.remove(_this.tipsLabel)
148
- _this.tipsLabel = undefined
153
+ _this.preventContextMenu(e);
154
+ if (_this.tipsLabel) {
155
+ _this.scene.remove(_this.tipsLabel);
156
+ _this.tipsLabel = undefined;
149
157
  }
150
- clearTimeout(_this.timer)
151
- const point = _this.getPosition(e)
158
+ clearTimeout(_this.timer);
159
+ const point = _this.getPosition(e);
152
160
  if (point) {
153
- _this.isCompleted = true
154
- _this.tempPoints = undefined
155
- _this.tempLine = undefined
156
- _this.tempLabel = undefined
157
- _this.pointArray.splice(0)
158
- _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);
159
167
  }
160
168
  },
161
169
  close() {
162
- this.renderer.domElement.removeEventListener('mousedown', this.mousedown)
163
- this.renderer.domElement.removeEventListener('mouseup', this.click)
164
- this.renderer.domElement.removeEventListener('mousemove', this.mousemove)
165
- this.renderer.domElement.removeEventListener('contextmenu', this.rightClick)
166
- this.remove(this.points)
167
- this.remove(this.polyline)
168
- this.remove(this.labels)
169
- this.pointArray.splice(0)
170
- this.points.splice(0)
171
- this.polyline.splice(0)
172
- this.labels.splice(0)
173
- this.tempPoints = undefined
174
- this.tempLabel = undefined
175
- this.tempLine = undefined
176
- this.scene.remove(this.tipsLabel)
177
- this.tipsLabel = undefined
178
- this.renderer.domElement.style.cursor = 'pointer'
179
- 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;
180
188
  },
181
189
  remove(array) {
182
190
  for (let index = 0; index < array.length; index++) {
183
- const element = array[index]
191
+ const element = array[index];
184
192
  if (element.geometry) {
185
- element.geometry.dispose()
193
+ element.geometry.dispose();
186
194
  }
187
- this.scene.remove(element)
195
+ this.scene.remove(element);
188
196
  }
189
197
  },
190
198
  numberToString(num) {
191
199
  if (num < 0.0001) {
192
- return num.toString()
200
+ return num.toString();
193
201
  }
194
- let fractionDigits = 2
202
+ let fractionDigits = 2;
195
203
  if (num < 0.01) {
196
- fractionDigits = 4
204
+ fractionDigits = 4;
197
205
  } else if (num < 0.1) {
198
- fractionDigits = 3
206
+ fractionDigits = 3;
199
207
  }
200
- return num.toFixed(fractionDigits)
201
- }
202
- }
208
+ return num.toFixed(fractionDigits);
209
+ },
210
+ preventContextMenu(event) {
211
+ event.preventDefault();
212
+ },
213
+ };
203
214
  export default {
204
- MeasureDistance
205
- }
215
+ MeasureDistance,
216
+ };