Skip to content

Commit 79341b7

Browse files
committed
better component
1 parent 90c394b commit 79341b7

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/SweetHome3dWebcomponent.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ export class SweetHome3dWebcomponent extends BaseCustomWebComponentConstructorAp
2929
roundsPerMinute: Number
3030
}
3131

32-
private _viewerCanvas: HTMLCanvasElement;
3332

3433
public url: string;
3534
public level: string;
3635
public camera: string;
3736
public roundsPerMinute: number = 0;
3837

38+
#viewerCanvas: HTMLCanvasElement;
39+
#ready: boolean;
40+
#viewer: any;
41+
3942
constructor() {
4043
super();
4144
this._restoreCachedInititalValues()
4245

43-
this._viewerCanvas = this._getDomElement<HTMLCanvasElement>('viewerCanvas');
46+
this.#viewerCanvas = this._getDomElement<HTMLCanvasElement>('viewerCanvas');
4447
}
4548

4649
async ready() {
@@ -62,25 +65,40 @@ export class SweetHome3dWebcomponent extends BaseCustomWebComponentConstructorAp
6265
const importFiles = jsFiles.map(x => new URL(prefix + 'lib/' + x, import.meta.url).toString());
6366
for (const f of importFiles)
6467
await LazyLoader.LoadJavascript(f);
65-
//await LazyLoader.LoadJavascripts(...importFiles);
66-
67-
const options = {
68-
roundsPerMinute: this.roundsPerMinute, // Rotation speed of the animation launched once home is loaded in rounds per minute, no animation if missing or equal to 0
69-
navigationPanel: "none", // Displayed navigation arrows, "none" or "default" for default one or an HTML string containing elements with data-simulated-key
70-
level: this.level, // Uncomment to select the displayed level, default level if missing */
71-
camera: this.camera, // Uncomment to select a camera, default camera if missing */
72-
activateCameraSwitchKey: true // Switch between top view / virtual visit with space bar if not false or missing */
73-
};
74-
75-
//@ts-ignore
76-
viewHome(this._viewerCanvas, // Id of the canvas
77-
this.url, // URL or relative URL of the home to display
78-
err => console.error(err), // Callback called in case of error
79-
(part, info, percentage) => { }, // Callback called while loading
80-
options);
68+
69+
this.#ready = true;
70+
this.createViewer();
71+
72+
}
73+
74+
private createViewer() {
75+
if (this.#ready) {
76+
const options = {
77+
roundsPerMinute: this.roundsPerMinute, // Rotation speed of the animation launched once home is loaded in rounds per minute, no animation if missing or equal to 0
78+
navigationPanel: "none", // Displayed navigation arrows, "none" or "default" for default one or an HTML string containing elements with data-simulated-key
79+
level: this.level, // Uncomment to select the displayed level, default level if missing */
80+
camera: this.camera, // Uncomment to select a camera, default camera if missing */
81+
activateCameraSwitchKey: true // Switch between top view / virtual visit with space bar if not false or missing */
82+
};
83+
84+
if (this.isConnected) {
85+
//@ts-ignore
86+
this.#viewer = viewHome(this.#viewerCanvas, // Id of the canvas
87+
this.url, // URL or relative URL of the home to display
88+
err => console.error(err), // Callback called in case of error
89+
(part, info, percentage) => { }, // Callback called while loading
90+
options);
91+
}
92+
}
8193
}
8294

95+
connectedCallback() {
96+
this.createViewer();
97+
}
8398

99+
disconnectedCaallback() {
100+
this.#viewer?.dispose();
101+
}
84102
}
85103

86104
customElements.define(SweetHome3dWebcomponent.is, SweetHome3dWebcomponent);

0 commit comments

Comments
 (0)