# Basic Example

Let's explore the most basic Speckle viewer example, step-by-step.

Before we can do anything, we'll need a HTML container for the viewer.

<!DOCTYPE html>
<html>
  <head>
    ...
  </head>

  <body>
    ...
    <div id="renderer">
    ...
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12

First thing we want to do is to create and initialize a viewer instance.

/** Get the HTML container */
const container = document.getElementById("renderer");

/** Create Viewer instance */
const viewer = new Viewer(container);
/** Initialise the viewer */
await viewer.init();
1
2
3
4
5
6
7

Next, let's add a camera controller so we have control over the camera.

/** Add the stock camera controller extension */
viewer.createExtension(CameraController);
1
2

Finally, let's load in some data from a Speckle model. It's easier to use the provided UrlHelper which knows how to parse various speckle URL formats

/** Create a loader for the speckle stream */
const urls = await UrlHelper.getResourceUrls(
  "https://app.speckle.systems/projects/24c98619ac/models/38639656b8"
);
for (const url of urls) {
  const loader = new SpeckleLoader(viewer.getWorldTree(), url, "");
  /** Load the speckle data */
  await viewer.loadObject(loader, true);
}
1
2
3
4
5
6
7
8
9

You can run the example live here (opens new window) or embedded below

Last Updated: 7/25/2024, 11:23:15 AM