Weird lerp behaviour on tab refocus. Camera incorrectly positioned on refocus

Hello again!

I’ve found this very odd behaviour, it happens when the browser tab refocuses (clicking another tab, waiting a couple of seconds, then going back to the playground / babylon rendering canvas)

It’s particular visible when using lerp to move the camera, but I’ve also had it happen on meshes, sprites etc. My speculation is that the deltaTime is somehow calculated incorrectly when the page is not in focus, however, I also tried adding an event listener for “visibilitychange” for tracking the tab focus, then using this to disable rendering and disable camera movements and also setting the camera position directly on refocus, so it might be related to internals somehow.

I hope you can help! Thanks in regards!

PG (Reproduce by clicking another tab, wait 2 seconds minimum, then click back to the playground tab. You can see the camera is somehow coming from the top now, where we should expect the same position):

Video of PG bug

That’s because you are using delta*0.75 for the interpolation factor, and when it becomes greater than 1 you get this artifact because the interpolation factor should be between 0 and 1.

You can clamp the value to 1:

1 Like

Nice, that’s good to know! Using your PG, the camera still seem to jump or “freeze” at a position when the browser tab i not focused.
Your solution seems closer to what I expected, however, I still expect the camera to be at the exact same position and “lerping” at the same speed when refocusing the tab, why is this not the case?

E.g. If I run the playground without ever changing tab, it’s all smooth, the camera never jumps.

It’s the lerp that limits the amount of changes because the factor must be between 0 and 1.

If you don’t lerp, it’s smooth even if the tab looses focus for more than 2s:

Ah I see, so the deltaTime is of cause greater than 1 when we refocus since time away from the page counts as time passed. I had the idea that sitting in another tab would just be “ignored” in the delta