Visitar URL original
fix(simple camera): listen resize change on container element instead… · ThatOpen/engine_components@8b1d75d · GitHub
Skip to content

Commit 8b1d75d

Browse files
authored
fix(simple camera): listen resize change on container element instead of window (#694)
1 parent 134829b commit 8b1d75d

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

‎packages/core/src/core/Worlds/src/simple-camera.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export class SimpleCamera extends BaseCamera implements Updateable, Disposable {
3333
*/
3434
three: THREE.PerspectiveCamera | THREE.OrthographicCamera;
3535

36+
protected _resizeObserver: ResizeObserver | null = null;
37+
3638
private _allControls = new Map<string, CameraControls>();
3739

3840
/**
@@ -92,10 +94,9 @@ export class SimpleCamera extends BaseCamera implements Updateable, Disposable {
9294
super(components);
9395
this.three = this.setupCamera();
9496

95-
this.setupEvents(true);
96-
9797
this.worlds.onItemSet.add(({ value: world }) => {
9898
const controls = this.newCameraControls();
99+
this.setupEvents(true);
99100
this._allControls.set(world.uuid, controls);
100101
});
101102

@@ -105,6 +106,7 @@ export class SimpleCamera extends BaseCamera implements Updateable, Disposable {
105106
controls.dispose();
106107
this._allControls.delete(world.uuid);
107108
}
109+
this._resizeObserver?.disconnect();
108110
});
109111

110112
// this.onWorldChanged.add(({ action, world }) => {
@@ -225,10 +227,22 @@ export class SimpleCamera extends BaseCamera implements Updateable, Disposable {
225227
}
226228

227229
private setupEvents(active: boolean) {
230+
if (this._resizeObserver) {
231+
this._resizeObserver.disconnect();
232+
this._resizeObserver = null;
233+
}
234+
228235
if (active) {
229-
window.addEventListener("resize", this.updateAspect);
230-
} else {
231-
window.removeEventListener("resize", this.updateAspect);
236+
if (!this.currentWorld) {
237+
throw new Error("This camera needs a world to work!");
238+
}
239+
if (!this.currentWorld.renderer) {
240+
throw new Error("This camera needs a renderer to work!");
241+
}
242+
243+
const { domElement } = this.currentWorld.renderer.three;
244+
this._resizeObserver = new ResizeObserver(this.updateAspect);
245+
this._resizeObserver.observe(domElement);
232246
}
233247
}
234248

0 commit comments

Comments
 (0)