-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should deltaTime account for throttling of unfocused tab? #4839
Comments
I'm thinking of this in terms of physics engine update, would the expectation be that the physics stops on unfocus? Also I'm not 100% sure the animation frame pausing on unfocus behaviour is consistent enough to not cause sync issues. From my experience (ie. may be wrong) unfocus event also fires when the window is not active but the tab is on top, in which case the animation frame do continue to execute, which again can cause sync issues. Need to test this bit to be sure if any implementation would be possible. |
I agree with this. Maybe the thing to do here is add to the documentation for deltaTime to communicate this unique case? From a quick look around the internet, it doesn't seem like there are ways to influence browser-level refresh throttling when tabs aren't foregrounded. (Unless you played audio file in the webpage, which should prevent the throttling.) I also found that there's events tied to "Page Visibility", which could wrap in p5 so that creators could subscribe to this event if they needed to do something special to account for the expected jump in |
I haven't come across the Page Visibility events before and a quick read through it I'm still not sure does it ties to the pausing of |
Fwiw, I use three.js as well, so out of curiosity I did a test on the three.js clock which has a p5 has events for window blur and focus which appears to be triggered by changing the tab as well: That could be used to pause the draw loop or offset the value of But it seems like a pause in rendering at any point kind of makes calculating I made an attempt to fix this issue: I don't know if this is the right approach, but it seems to fix things for the example I originally created: This isn't directly related to the unfocused tab issue, but the issue I first noticed with GIF animations getting delayed is also caused by using I'm not familiar with the thinking that went into the animation loop for p5, so I'm curious looking at the Would it make sense to continue calling draw just to update the |
@owenbmcc I wouldn't recommend tying it to blur and focus event as you mentioned switching focus while the window is still visible will fire the blur event as well and I find that potentially unusable especially since I can't pop the developer console out and monitor behaviour there. The page visibility API as mentioned above could be a solution but without trying it I don't know what its actual behaviour is like in relation to The implementation of the draw loop as you can imagine goes back a long time so I can't say much about it as I don't know that much. I think the idea of making |
@limzykenneth I agree that makes sense. I should have explained more clearly, the solution I came up with for the unfocused tab issue wouldn't pause the sketch completely if you open the console or focus another program. My approach was to set |
For what it's worth, as a user I expect
I would much prefer a warning in the documentation explaining that browsers may stop rendering animations when backgrounded, than p5 assuming that I want an artificially small/consistent |
@weslord I think that is a reasonable expectation as well. |
I feel differently about this but not enough to advocate for a change. In general, I do expect Bouncing ellipse tied to real-time This simple sketch breaks immediately upon tab switching. This wouldn't happen if we were doing the standard In my opinion, this makes the most instinctive uses of 'real time' very fragile to tab-switching. The useful connection between frame-by-frame rendering and 'real-time' is made unusable by the throttling. I am not suggesting that we do anything other than mention this in the documentation and get rid of all internal uses of |
Relates to #6785, need to test |
Hi! This came up while fixing a problem with duplicate GIF timing (see discussion in #4824 ). Wanted to ask a larger question about timing and
deltaTime
here before throwing a workaround into the GIF timing.My question is: Should a coder expect there to be unpredictable spikes in
deltaTime
when coming back to a sketch after switching tabs?This sketch illustrates this. If you click into the sketch, switch tabs, and come back after a few seconds, you will probably see a number that is nowhere near 1000 next to 'deltaTime' in the console (depends on how 'busy' your CPU currently is)
I know this mostly comes down to a combination of
window.performance.now
and browser throttling behavior with unfocused tab animation callbacks.This is less of a problem with
millis()
where a coder would expect the value to correlate to the time since the sketch started. But withdeltaTime
, the animation frame throttling makes the difference in 'real-time' between frames very unexpected even if it is true to the space between frames.Should we account for this in
deltaTime
in some way? My instinct is no, but this isn't clearcut for me because sketches that rely ondeltaTime
will be very fragile to tab switching.For me, this brings up larger questions about whether we should manually throttle unfocused sketches in a controlled way by default to make behavior predictable and consistent across browsers/hardware? What do others think? @limzykenneth @montoyamoraga @outofambit @akshay-99 @owenbmcc
Most appropriate sub-area of p5.js?
The text was updated successfully, but these errors were encountered: