How to display a three.js scene in a modal popup

HI,
I have a main scene for a project and a secondary scene that shows information and a drawing about the main scene.

Does anyone know if there is a way or can point me to info on how to display the second scene in a modal/popup?

Thanks

You can create another canvas and make a new scene but this will duplicate all your resources. It’s also possible to use a full screen canvas and then manage several views with scissor tests. However, this is also very complicated because of various overlaps.

Thanks for the reply!

I already have the second scene created and using scissor add it to a separate div in the main body of the page.

I then created a modal containing the second scene and this is where I have the issue.

When the scene is within a non modal div all displays perfectly, but when the div is included within the modal, the scene does not display.

Hope that makes sense!?

@Picklehead

I’m not sure I understand the structure of your page. Is your <canvas> a child element of the modal <div>? If not, its z-index might be too low, so the <canvas> is hidden under the modal <div>.

– Pavel

HI, the canvas is a child of the modal

If your modal parent is set to display none originally the internals of three js sets the canvas size to 0w 0h by default, you’ll see this by inspecting the canvas element, you need to resize the three js canvas when the modal is set to display block or anything other than “none”. There’s a number of ways to do this, the observer api is one but you can also just set the renderer size and camera aspect on the click of opening the modal

HI and Thanks!

I will try a canvas resize and see if that fixes the issue.

The canvas did have size on inspection.

I tried a resize regardless and did see the other scene for a split second and then it disappeared.

@Picklehead

Maybe it’s about time to share some minimal and online editable example of the problem.

– Pavel

Hi,
Have sorted the issue.

Very quick and dirty example using the Three.js example from Multiple Canvases Multiple Scenes - three.js manual (threejs.org)

Fiddle link Three.Js Multiple Scenes - JSFiddle - Code Playground

1 Like

@Picklehead

Good.

So, what was the problem?

– Pavel

1 Like

The issue was position of the canvas element within the code of the Popup. As per the example and all works perfectly.

1 Like

adding this one for react + three users:

(hover the version 8 link) it takes care of everything with a single, share canvas. a scene in that case is truly an isolated entity where everything works as expected, from interaction, events, controls, etc. and it’s trivial to set up.