three-base 1.0.71 → 1.0.74
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/lib/ThreeBase.d.ts +1 -1
- package/lib/ThreeBase.js +8 -14
- package/lib/ThreeBase.ts +9 -14
- package/package.json +1 -1
package/lib/ThreeBase.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
16
16
|
controls: OrbitControls;
|
|
17
17
|
isRendererContextLost: boolean;
|
|
18
18
|
setContainer(div: HTMLDivElement | null): Promise<void>;
|
|
19
|
-
|
|
19
|
+
setsize(width: number, height: number): void;
|
|
20
20
|
controlsPosition: Transition;
|
|
21
21
|
controlsCenter: Transition;
|
|
22
22
|
init(): void;
|
package/lib/ThreeBase.js
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
5
|
* @Last Modified time: 2023-03-16 14:31:32
|
|
6
6
|
*/
|
|
7
|
-
import elementResizeEvent from "element-resize-event";
|
|
8
7
|
import { round } from "lodash";
|
|
9
8
|
import { AxesHelper, EventDispatcher, GridHelper, LinearFilter, Mesh, MOUSE, NoBlending, NormalBlending, PerspectiveCamera, RGBAFormat, Scene, TOUCH, Vector2, Vector3, WebGLRenderer, WebGLRenderTarget } from "three";
|
|
10
9
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
@@ -76,13 +75,6 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
76
75
|
for (let c of canvas)
|
|
77
76
|
c.remove();
|
|
78
77
|
div.append(this.renderer.domElement);
|
|
79
|
-
// 监听容器的尺寸的改变
|
|
80
|
-
elementResizeEvent(div, () => {
|
|
81
|
-
let rect = window.getComputedStyle(div);
|
|
82
|
-
let width = parseInt(rect.width);
|
|
83
|
-
let height = parseInt(rect.height);
|
|
84
|
-
this.resize(width, height);
|
|
85
|
-
});
|
|
86
78
|
// 如果上下文被销毁了, 就恢复上下文
|
|
87
79
|
if (this.isRendererContextLost) {
|
|
88
80
|
this.renderer.forceContextRestore();
|
|
@@ -90,17 +82,14 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
90
82
|
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
91
83
|
this.renderer.domElement.style.opacity = "1";
|
|
92
84
|
}
|
|
93
|
-
|
|
94
|
-
let width = parseInt(rect.width);
|
|
95
|
-
let height = parseInt(rect.height);
|
|
96
|
-
this.resize(width, height);
|
|
85
|
+
this.setsize(div.clientWidth, div.clientHeight);
|
|
97
86
|
// 开启时时渲染
|
|
98
87
|
threeUtils.removeEventListener("requestAnimationFrame", this.renderBindThis);
|
|
99
88
|
threeUtils.addEventListener("requestAnimationFrame", this.renderBindThis);
|
|
100
89
|
}
|
|
101
90
|
}
|
|
102
91
|
// 修改尺寸
|
|
103
|
-
|
|
92
|
+
setsize(width, height) {
|
|
104
93
|
if (this.camera instanceof PerspectiveCamera) {
|
|
105
94
|
this.camera.aspect = width / height;
|
|
106
95
|
this.camera.updateProjectionMatrix();
|
|
@@ -235,7 +224,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
235
224
|
this.renderer.forceContextLoss();
|
|
236
225
|
// 标记上下文被销毁了
|
|
237
226
|
this.isRendererContextLost = true;
|
|
238
|
-
this.
|
|
227
|
+
this.setsize(100, 100);
|
|
239
228
|
this.renderer.domElement.style.opacity = "0";
|
|
240
229
|
}
|
|
241
230
|
// 渲染场景
|
|
@@ -247,6 +236,11 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
247
236
|
this.stopRender();
|
|
248
237
|
return;
|
|
249
238
|
}
|
|
239
|
+
//判断尺寸发生改变时重置尺寸
|
|
240
|
+
const parentNode = this.renderer.domElement.parentNode;
|
|
241
|
+
if (parentNode.clientWidth !== this.size.x || parentNode.clientHeight !== this.size.y) {
|
|
242
|
+
this.setsize(parentNode.clientWidth, parentNode.clientHeight);
|
|
243
|
+
}
|
|
250
244
|
// 分发鼠标事件
|
|
251
245
|
this.threeMouse.dispatchEvent();
|
|
252
246
|
// 在不自动渲染时, needRender=false 就跳过渲染
|
package/lib/ThreeBase.ts
CHANGED
|
@@ -92,14 +92,6 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
92
92
|
for (let c of canvas) c.remove();
|
|
93
93
|
div.append(this.renderer.domElement);
|
|
94
94
|
|
|
95
|
-
// 监听容器的尺寸的改变
|
|
96
|
-
elementResizeEvent(div, () => {
|
|
97
|
-
let rect = window.getComputedStyle(div);
|
|
98
|
-
let width = parseInt(rect.width);
|
|
99
|
-
let height = parseInt(rect.height);
|
|
100
|
-
this.resize(width, height);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
95
|
// 如果上下文被销毁了, 就恢复上下文
|
|
104
96
|
if (this.isRendererContextLost) {
|
|
105
97
|
this.renderer.forceContextRestore();
|
|
@@ -108,10 +100,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
108
100
|
this.renderer.domElement.style.opacity = "1";
|
|
109
101
|
}
|
|
110
102
|
|
|
111
|
-
|
|
112
|
-
let width = parseInt(rect.width);
|
|
113
|
-
let height = parseInt(rect.height);
|
|
114
|
-
this.resize(width, height);
|
|
103
|
+
this.setsize(div.clientWidth, div.clientHeight);
|
|
115
104
|
|
|
116
105
|
// 开启时时渲染
|
|
117
106
|
threeUtils.removeEventListener("requestAnimationFrame", this.renderBindThis);
|
|
@@ -120,7 +109,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
120
109
|
}
|
|
121
110
|
|
|
122
111
|
// 修改尺寸
|
|
123
|
-
|
|
112
|
+
setsize(width: number, height: number) {
|
|
124
113
|
if (this.camera instanceof PerspectiveCamera) {
|
|
125
114
|
this.camera.aspect = width / height;
|
|
126
115
|
this.camera.updateProjectionMatrix();
|
|
@@ -279,7 +268,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
279
268
|
this.renderer.forceContextLoss();
|
|
280
269
|
// 标记上下文被销毁了
|
|
281
270
|
this.isRendererContextLost = true;
|
|
282
|
-
this.
|
|
271
|
+
this.setsize(100, 100);
|
|
283
272
|
this.renderer.domElement.style.opacity = "0";
|
|
284
273
|
}
|
|
285
274
|
|
|
@@ -295,6 +284,12 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
295
284
|
return;
|
|
296
285
|
}
|
|
297
286
|
|
|
287
|
+
//判断尺寸发生改变时重置尺寸
|
|
288
|
+
const parentNode = this.renderer.domElement.parentNode as HTMLDivElement;
|
|
289
|
+
if (parentNode.clientWidth !== this.size.x || parentNode.clientHeight !== this.size.y) {
|
|
290
|
+
this.setsize(parentNode.clientWidth, parentNode.clientHeight);
|
|
291
|
+
}
|
|
292
|
+
|
|
298
293
|
// 分发鼠标事件
|
|
299
294
|
this.threeMouse.dispatchEvent();
|
|
300
295
|
|