I have several web pages where there’s a Babylon canvas in the header / hero section of the page, with other content below it - for example: rocketclowns.com
For performance reasons, I am looking for a method to stop the Babylon render loop when the canvas is no longer in the browser viewport. What would be the best way to achieve this?
var observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
// If the canvas is visible, start the engine
engine.runRenderLoop(function () {
if (sceneToRender) {
sceneToRender.render()
}
});
} else {
// If the canvas is not visible, stop the engine
engine.stopRenderLoop();
}
});
}, { threshold: 0 }); // 0.2 means that engine runs when 20% of canvas is visible
observer.observe(canvas);