# Multiple scenes on different canvas using single engine

**URL:** <https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608>\
**Category:** Bugs\
**Created:** [December 14, 2020, 3:00am UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608 "2020-12-14T03:00:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![morterra](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/morterra/32/25383_2.png) [@morterra](https://forum.babylonjs.com/u/morterra)\
**Post date:** [December 14, 2020, 3:00am UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/1 "2020-12-14T03:00:39Z")

</div>

Hey guys,

I recently had an issues while creating two unique scenes, on two unique canvases, using the same Babylon engine, following the documentation here:

> **[Using Multiple Scenes | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/scene/multiScenes)**
>
> Learn about using multiple scenes at the same time in Babylon.js.

  

> **[Using Multiple Canvases | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/scene/multiCanvas)**
>
> Learn about using multiple canvases at the same time in Babylon.js.

And so I followed those and it seemed like they were incomplete because it wouldn’t work as described for me. So I looked through many different playgrounds and noticed they don’t work for me as they were built to work.

I’m wondering if there is a bug causing these issues, and is subsequently causing issues for what I’m trying to do. Here are the PGs with bugs:  
(the spheres don’t appear for me): [New feature: Multi canvases rendering](https://forum.babylonjs.com/t/new-feature-multi-canvases-rendering/6426/30)

(the second canvas just re-renders the first canvas): [New feature: Multi canvases rendering](https://forum.babylonjs.com/t/new-feature-multi-canvases-rendering/6426/45)

I’m basically trying to do something like this:

> **[Babylon.js Playground](https://playground.babylonjs.com/#Z8QL4V%234)**

But with one engine.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 14, 2020, 11:03am UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/2 "2020-12-14T11:03:03Z")

</div>

The canvas of the engine is not resized to match the size of the current view canvas, that’s why it does not work.

@Deltakosh I can see the code to resize the engine is:

```auto
        const dimsChanged =
            canvas.width !== canvas.clientWidth ||
            canvas.height !== canvas.clientHeight;
        if (canvas.clientWidth && canvas.clientHeight && dimsChanged) {
            canvas.width = canvas.clientWidth;
            canvas.height = canvas.clientHeight;
            parent.width = canvas.clientWidth;
            parent.height = canvas.clientHeight;
            this.resize();
        }

```

Note that the current code does not work because width === clientWidth and height === clientHeight after creation time (maybe the browser implementation of canvas creation changed since this code has been done?).

It’s already something that had been changed from:

```auto
        if (canvas.clientWidth && canvas.clientHeight) {
            canvas.width = canvas.clientWidth;
            canvas.height = canvas.clientHeight;
            parent.width = canvas.clientWidth;
            parent.height = canvas.clientHeight;
            this.resize();
        }

```

I guess this one was not ok because it resized the engine for each view/each frame.

I wonder if the right code would not be:

```auto
        const dimsChanged =
            canvas.clientWidth !== parent.width ||
            canvas.clientHeight !== parent.height;
        if (canvas.clientWidth && canvas.clientHeight && dimsChanged) {
            canvas.width = canvas.clientWidth;
            canvas.height = canvas.clientHeight;
            parent.width = canvas.clientWidth;
            parent.height = canvas.clientHeight;
            this.resize(true);
        }

```

As we render in the canvas of the engine (parent), we only need to resize it if it is not already the size of the current view canvas.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [December 14, 2020, 11:06pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/3 "2020-12-14T23:06:40Z")

</div>

I tend to agree. We should try

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 15, 2020, 11:24am UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/4 "2020-12-15T11:24:15Z")

</div>

PR is here:

> <https://github.com/BabylonJS/Babylon.js/pull/9605>

Note that the size of the canvas should be set after calling `registerView` and not before, because the function is setting the canvas size with the default canvas size.

This PG should work after the PR is merged: [https://playground.babylonjs.com/#LEIZ83#37](https://playground.babylonjs.com/#LEIZ83#37)

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [July 19, 2021, 12:27am UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/5 "2021-07-19T00:27:43Z")

</div>

@Evgeni_Popov I’m having trouble with this in 4.2.0, although my problem is a bit different.

If I have multiple canvas views on the one page, but each canvas is a different size (inside a different sized container) then the resizing causes flickering that shows the different sized canvases in the one view.

If all canvases are the same size then it’s fine, but that’s not my use case.

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [July 19, 2021, 12:45am UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/6 "2021-07-19T00:45:29Z")

</div>

@Evgeni_Popov works fine in 5.0.0-alpha.31 so Ignore my comment. 🙂 Thanks.

---

<div class="post-metadata">

**Author:** ![sim\_isc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sim_isc/32/18342_2.png) [@sim\_isc](https://forum.babylonjs.com/u/sim_isc)\
**Post date:** [September 1, 2021, 1:25pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/7 "2021-09-01T13:25:46Z")

</div>

Hi,  
I have a scenario similar to “inteja” (canvas of different size, flickering on each others after resizing) and, equally, moving to 5.0.0-alpha.40 the issue is perfectly fixed.  
The point is that moving to alpha version the performance seems to drastically decrease compared to 4.2 version… May I ask help in order to investigate the issue on 4.2 version? Do you think that I need to open a new related topic?

PS this is my first comment about babylonjs…great framework, great community ! thanks all!

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 1, 2021, 2:19pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/8 "2021-09-01T14:19:34Z")

</div>

We will not address the issue in 4.2 as there are way too many things we would need to back port.

Nevertheless I am really scared about the perf regression you are seeing and I would like to address it ASAP. Would you have a repro for it ? Version 5 needs to be at least as fast if not faster than 4.2.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 1, 2021, 2:20pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/9 "2021-09-01T14:20:36Z")

</div>

@sim_isc thanks a lot in advance for your help investigating the perf issue 🙂

cc @Deltakosh FYI

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [September 1, 2021, 3:36pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/10 "2021-09-01T15:36:19Z")

</div>

Agree we need to address that ASAP 🙂

---

<div class="post-metadata">

**Author:** ![sim\_isc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sim_isc/32/18342_2.png) [@sim\_isc](https://forum.babylonjs.com/u/sim_isc)\
**Post date:** [September 1, 2021, 3:47pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/11 "2021-09-01T15:47:15Z")

</div>

I see, no problem I’ll wait for release of 5.  
Sure, I need some time in order to isolate and set up properly the part of code related to babylonjs, I’ll write back ASAP.  
of course you are welcome!

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 1, 2021, 3:55pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/12 "2021-09-01T15:55:03Z")

</div>

Thanks a ton for your help, can not wait to fix those perf regressions.

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [September 1, 2021, 10:07pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/13 "2021-09-01T22:07:27Z")

</div>

FWIW I’ve been using 5.0.0-alpha.31 & 5.0.0-alpha.35 and haven’t noticed any performance regressions in our use cases. I know every application is different though.

---

<div class="post-metadata">

**Author:** ![sim\_isc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sim_isc/32/18342_2.png) [@sim\_isc](https://forum.babylonjs.com/u/sim_isc)\
**Post date:** [September 2, 2021, 9:28am UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/14 "2021-09-02T09:28:51Z")

</div>

Sure, i completely agree… I did more tests on the main application and I confirm the performance degradation, but, in the meantime, I make a simple test application related only on babylonjs code and no performances degradation is present!  
So the point seems to be related on some specific aspects of my application…

In any case, I write some details about my scenario, maybe they could be usefull…

First thing, when I speak about “performance decrease”, I mean an empiric evaluation, I just refer to “CPU usage” of google chrome, in one case (v 4.2) it stays stable in the range of 15-20% in the other it stays fixed to 98-100% (v 4.1 and 5alpha). So I haven’t made any fps measurament for now, but the difference seems to be not marginal and constant in time.

The application is written in angular 12 and is based on a dynamic dashboards structure, with the possibility to insert some widget related to different tasks; the babylonjs related widgets import some .stl file with “SceneLoader.ImportMesh” representing some tools machines and other stuff; after we move some components based on different factors and conditions (external info based on webstomp and other).I’ve attached “application example.png” just to give an idea of the concept.

Related to these widgets there is a dedicated angular service that handles the single babylonjs engine and all the related points as rendering and camera handling as described in the documentation of multi canvas and multi scenes.

The application is based on nx workspace, with the babylonjs code located in a dedicated lib, in attachment the package.json file.  
Some critical package beyond babylonjs are:  
angular-gridster2: for handling the dashboards structure  
ng-dynamic-component: for handling the dynamic creation of the component inside the widget of every dashboards

Now the test! Off course, the code of the application is identical apart from babylonjs version.

- File App\_v42: Application with v 4.2 and very good performance
- File App\_1\_v5: Application with v 5 in exactly same condition and poor performance (CPU usage)
- File App\_2\_v5: Application with v 5 with “less stressful” condition and quite poor performance (CPU usage)
- File App\_3\_v5: Application with v 5 with “less stressful” condition and quite poor performance (CPU usage)
- File Test\_v5: Test application with v 5 of babylonjs; the application reproduce the code of the lib related to babylonjs, with static defined canvas; so WITHOUT handling the dashboards structure and dynamic creation of the widgets. The performances are good with no significant difference compared to V4.2.

So I see that is not easy to understand the motivation of the performances change, it seems related to some specific aspects of the application…if you think that it is worth investigating the point more, I can gladly prepare a small test application with the dashboarding and widget modules integrated with the babylonjs code, in any case it could be interesting for someone…

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 2, 2021, 1:50pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/17 "2021-09-02T13:50:01Z")

</div>

w000000t, this definitely looks like a huge issue ☹

Could you share some perf capture of both so we could see what is eating the CPU ?

---

<div class="post-metadata">

**Author:** ![sim\_isc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sim_isc/32/18342_2.png) [@sim\_isc](https://forum.babylonjs.com/u/sim_isc)\
**Post date:** [September 3, 2021, 1:35pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/18 "2021-09-03T13:35:59Z")

</div>

Here a direct comparison between the two performances snap. It seems that the call “engine.setSize” is repeated lot of times in V5, and of course it takes lot of time…

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/a/6a2cd9a9cf79b257511fc29c7b18707c54fb89b2.png)

Made the same analysis on test application based on V5 and with static canvas. In this case the performance are good and, as for V4.2, the call “engine.setSize” is not repeated lot of times. For some reasons, using the dynamic dashboard approach (gridster + ngdynamic) force the lib to call setSize lot of times.

I compare the part of code that check for rising and we have:

V 42:

```auto
const dimsChanged =
            canvas.width !== canvas.clientWidth ||
            canvas.height !== canvas.clientHeight;
        if (canvas.clientWidth && canvas.clientHeight && dimsChanged) {
            canvas.width = canvas.clientWidth;
            canvas.height = canvas.clientHeight;
            parent.width = canvas.clientWidth;
            parent.height = canvas.clientHeight;
            this.resize();
        }

```

V 5:

```auto
 var width = Math.floor(canvas.clientWidth / this._hardwareScalingLevel);
        var height = Math.floor(canvas.clientHeight / this._hardwareScalingLevel);
        var dimsChanged = width !== canvas.width || parent.width !== canvas.width ||
            height !== canvas.height || parent.height !== canvas.height;
        if (canvas.clientWidth && canvas.clientHeight && dimsChanged) {
            canvas.width = width;
            canvas.height = height;
            this.setSize(width, height);
        }

```

Shouldn’t we update the new values of height and width of parent even in V5? ex:

```auto
var width = Math.floor(canvas.clientWidth / this._hardwareScalingLevel);
        var height = Math.floor(canvas.clientHeight / this._hardwareScalingLevel);
        var dimsChanged = width !== canvas.width || parent.width !== canvas.width ||
            height !== canvas.height || parent.height !== canvas.height;
        if (canvas.clientWidth && canvas.clientHeight && dimsChanged) {
            canvas.width = width;
            canvas.height = height;
            parent.width = canvas.width ;
            parent.height = canvas.height ;
            this.setSize(width, height);
        }

```

And so I understand why the test application doesn’t have performance problems… the canvas were all equal! as soon as I change the canvas sizes, forcing them different from each other, the performance problem was present event in the test application with static canvas…

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 3, 2021, 2:33pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/19 "2021-09-03T14:33:35Z")

</div>

Unfortunately, it looks like it was a bug before as we were considering all renders being identical in sizes.

Now, despite being dead slow, this at least renders in a consistent and expected way. I wonder how we could work around this ?

Maybe @Deltakosh has an idea ? but not sure there is an easy fix here

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 3, 2021, 2:42pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/20 "2021-09-03T14:42:00Z")

</div>

Discussed with @Deltakosh offline 🙂

I will add a callback you can set to override our default checks so that you could override the default behavior.

But did you not see any issue with the rendering being stretched in 4.2 ?

---

<div class="post-metadata">

**Author:** ![sim\_isc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sim_isc/32/18342_2.png) [@sim\_isc](https://forum.babylonjs.com/u/sim_isc)\
**Post date:** [September 3, 2021, 2:53pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/21 "2021-09-03T14:53:44Z")

</div>

> [@sebavan](#):
>
> I will add a callback you can set to override our default checks so that you could override the default behavior.

Thank you very much.

> [@sebavan](#):
>
> But did you not see any issue with the rendering being stretched in 4.2 ?

yes, I confirm and the callback idea seems very good. I could handle the moment when the canvas are stretched (edit mode, window/widget resizing, etc…) with the default checks and then, in static condition, I force back the checks related to not equal canvas increasing performance.

I mean, I hope it will work but it looks promising

PS I’m going crazy with the “new user” limits for uploading and replying… 😆 😆 😆  
ok, now I got trust level 😅

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 3, 2021, 7:37pm UTC](https://forum.babylonjs.com/t/multiple-scenes-on-different-canvas-using-single-engine/16608/22 "2021-09-03T19:37:26Z")

</div>

I changed the code to allow you after you call register view to override the resize check function.

You ll be able to do:

```auto
var view = engine.registerView(targetCanvas);
view.customResize = (canvas) => {
    const dimsChanged =
            canvas.width !== canvas.clientWidth ||
            canvas.height !== canvas.clientHeight;
        if (canvas.clientWidth && canvas.clientHeight && dimsChanged) {
            canvas.width = canvas.clientWidth;
            canvas.height = canvas.clientHeight;
            const parent = engine.getRenderingCanvas();
            parent.width = canvas.clientWidth;
            parent.height = canvas.clientHeight;
            engine.resize();
        }
};

```
