Skip to content

Commit 8ad1651

Browse files
authored
Fix breaking viewHelper while changing camera (#469)
* Make `viewHelperDiv` a class property * Remove the existing ViewHelperDiv if it already exists
1 parent 7161608 commit 8ad1651

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

packages/base/src/3dview/mainview.tsx

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -362,29 +362,38 @@ export class MainView extends React.Component<IProps, IStates> {
362362

363363
this._transformControls.enabled = false;
364364
this._transformControls.visible = false;
365+
this._createViewHelper();
366+
}
367+
};
365368

366-
// ViewHelper setup
367-
this._viewHelper = new ViewHelper(
368-
this._camera,
369-
this._renderer.domElement
370-
);
371-
this._viewHelper.center = this._controls.target;
369+
private _createViewHelper() {
370+
// Remove the existing ViewHelperDiv if it already exists
371+
if (
372+
this._viewHelperDiv &&
373+
this.divRef.current?.contains(this._viewHelperDiv)
374+
) {
375+
this.divRef.current.removeChild(this._viewHelperDiv);
376+
}
372377

373-
const viewHelperDiv = document.createElement('div');
374-
viewHelperDiv.id = 'viewHelper';
375-
viewHelperDiv.style.position = 'absolute';
376-
viewHelperDiv.style.right = '0px';
377-
viewHelperDiv.style.bottom = '0px';
378-
viewHelperDiv.style.height = '128px';
379-
viewHelperDiv.style.width = '128px';
378+
// Create new ViewHelper
379+
this._viewHelper = new ViewHelper(this._camera, this._renderer.domElement);
380+
this._viewHelper.center = this._controls.target;
380381

381-
this.divRef.current.appendChild(viewHelperDiv);
382+
const viewHelperDiv = document.createElement('div');
383+
viewHelperDiv.style.position = 'absolute';
384+
viewHelperDiv.style.right = '0px';
385+
viewHelperDiv.style.bottom = '0px';
386+
viewHelperDiv.style.height = '128px';
387+
viewHelperDiv.style.width = '128px';
382388

383-
viewHelperDiv.addEventListener('pointerup', event =>
384-
this._viewHelper.handleClick(event)
385-
);
386-
}
387-
};
389+
this._viewHelperDiv = viewHelperDiv;
390+
391+
this.divRef.current?.appendChild(this._viewHelperDiv);
392+
393+
this._viewHelperDiv.addEventListener('pointerup', event =>
394+
this._viewHelper.handleClick(event)
395+
);
396+
}
388397

389398
animate = (): void => {
390399
this._requestID = window.requestAnimationFrame(this.animate);
@@ -1312,6 +1321,7 @@ export class MainView extends React.Component<IProps, IStates> {
13121321
}
13131322

13141323
this._camera.add(this._cameraLight);
1324+
this._createViewHelper();
13151325

13161326
this._scene.add(this._camera);
13171327
this._controls.object = this._camera;
@@ -1476,6 +1486,7 @@ export class MainView extends React.Component<IProps, IStates> {
14761486
private _pointer3D: IPointer | null = null;
14771487
private _clock: THREE.Clock;
14781488
private _viewHelper: ViewHelper;
1489+
private _viewHelperDiv: HTMLDivElement | null = null;
14791490
private _collaboratorPointers: IDict<IPointer>;
14801491
private _pointerGeometry: THREE.SphereGeometry;
14811492
private _contextMenu: ContextMenu;

0 commit comments

Comments
 (0)