Single-object viewer POC (#14)

This commit implements a Sketchfab-style single-object viewer as a minimal
proof-of-concept.

### `@storyteller/studio-web`
This is a new package to house web components for interfacing with the
`@storyteller/studio` WASM library. It currently includes a single component,
`<sts-viewer />`, which is exported from `@storyteller/studio-web/viewer`.

### `react-frontend`
This is a new application intended as a proof-of-concept and testbed for React
interop with `@storyteller/studio-web`.

Web components are currently a little bit fiddly to use in a React application.
`react-frontend/src/app/studio-viewer.tsx` demonstrates how a React-idiomatic
component can be written as a thin wrapper around a web component, and
`react-frontend/src/app/app.tsx` demonstrates that wrapper component in use.
This commit is contained in:
Danny McGee
2023-12-07 15:46:32 -05:00
committed by GitHub
parent 155272a5ba
commit cba70a62ae
70 changed files with 71793 additions and 1157 deletions
+1
View File
@@ -0,0 +1 @@
export * from "./lib/viewer.element";
+60
View File
@@ -0,0 +1,60 @@
import * as studio from "@storyteller/studio";
import { LitElement, PropertyValues, html, unsafeCSS } from "lit";
import { customElement, property } from "lit/decorators.js";
import styles from "./viewer.element.scss?inline";
import { ViewportSize } from "@storyteller/studio";
@customElement("sts-viewer")
export class ViewerElement extends LitElement {
static override styles = unsafeCSS(styles);
@property() objectId?: string;
@property() skyboxId?: string;
protected override createRenderRoot(): Element | ShadowRoot {
return this;
}
protected override updated(changes: PropertyValues<this>): void {
if (!studio.isInitialized()) {
this.#startViewer();
} else {
if (changes.has("objectId") && !!this.objectId)
studio.loadObject(this.objectId);
if (changes.has("skyboxId") && !!this.skyboxId)
studio.setSkybox(this.skyboxId);
}
}
#startViewer(): void {
if (!this.objectId) return;
if (!this.skyboxId) return;
const width = window.innerWidth;
const height = window.innerHeight;
try {
studio.startViewer(
this.objectId,
this.skyboxId,
"#studio_canvas",
"Storyteller Studio",
new ViewportSize(width, height),
);
} catch (err) {
if (!(err as Error).message?.startsWith("Using exceptions for control flow"))
throw err;
}
}
protected override render = () => html`
<canvas id="studio_canvas" alt="Storyteller Studio"></canvas>
`;
}
declare global {
export interface HTMLElementTagNameMap {
"sts-viewer": ViewerElement;
}
}