I wanted to mess around with incorporating the 3d viewer into a larger application, but it looks like the example files (should be at src/example.js) have been yoinked for now?
I took a look at @chloesun’s cool hackathon project, but it looks like in that case, she was just taking speckle objects into her own ThreeJS environment, rather than using Speckle’s Viewer. From inspecting the Speckle Viewer code, it looks like the Viewer is a wrapper around a standard ThreeJS script.
I’m just running into issues with embedding the Viewer into a React component (I’m getting complaints about “this” being undefined --so trying to append a component to the container doesn’t work, but since it’s an error thrown in the built Speckle Viewer package, it’s hard to debug).
The viewer should be able to be injected into any frontend framework, in the same way you would use it in VanillaJS.
Only differences would be “framework-specific” setup that may be difficult for us to pin-point on each case.
@vwb thx for the super compact example! its pretty neat!
Not a react expert myself either, but in Vue, as far as I remember, you’d need to pass in the libraries to the transpileDependencies list in the vue.config.js file (the Vue configuration file for building, which is done via Webpack afaik)
If that was not included, the viewer will always fail to load. Maybe there’s a similar option in React to look into?
Here’s an example of what it looks like on the revit dash demo app:
And you can also find a slightly more complex viewer (vue based) in there too
This is quite similar to the viewer implementation we used in our frontend.
However, the Viewer has recently undergone some changes, and these examples are yet to be updated to the latest version, so some things may have changed that I’m unaware of.
Alex (our 3D Viewer team-member) is out on vacation so I’ll ping @dimitrie and @fabians to see if they can fill in the gaps here!
We haven’t rolled out the latest viewer refactor, that’s still under dev - and the integration with the frontend won’t change to be honest.
One thing to keep in mind are potential gotchas around an SPA’s component lifecycles and threejs (re)initialisation; you don’t want to create a new viewer instance every time you navigate back and forth to the page with the viewer.
@AlanRynne I’m pretty sure the viewer library is getting loaded; it’s more of an issue with what @vwb mentioned about making sure the HTML element is mounted. I’m refactoring as a React class based component to check on this (@dimitrie using a class based component should make it easier to be explicit on the component setup and tear down.)
This looks like a minimal example in React for properly mounting a threejs component
In general when using frameworks, it’s a good idea to wrap anything complicated in it’s own “component” (or whatever it’s called in your framework of choice ).
This way you have total control about how it interacts with your framework, and then you just use it as any other component.
I think the world of Speckle + React is rather unexplored (as you may have discovered, we’re mostly Vue fans here ) so do let us know what you make of it
I started a repo for making a generic React app with a Speckle viewer (in progress, there’s some messiness with setting a threejs canvas size inside a component).