Skip to content
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

requestAnimationFrame background handling #176

Closed
Rycochet opened this issue Jul 14, 2014 · 4 comments
Closed

requestAnimationFrame background handling #176

Rycochet opened this issue Jul 14, 2014 · 4 comments

Comments

@Rycochet
Copy link
Collaborator

I've got to make a really irritating little site (don't blame me - clients etc lol), which needs animation going pretty much all the time, including creating dynamic elements, animating them in, then animating them out again a short while later (too dynamic to just toggle sadly).

Sadly the downside to requestAnimationFrame is that when the animation is wanting to happen on a background window/tab it doesn't actually happen - resulting in me going back a while later to find hundreds of new elements that animate out almost immediately.

I've been thinking about what's best to do - and can easily check if the tab is active for starting things etc, but how about an option (possibly) to setTimeout the end time, and if the animation hasn't been called (or it's in the background etc) then call complete() and set the final values?

@julianshapiro
Copy link
Owner

I've solved this issue. Expect it in the upcoming update. It works automatically and has no performance drawbacks.

@julianshapiro
Copy link
Owner

This is now available in the latest version. Please update your copy of Velocity and let me know if there are any issues.

@Rycochet
Copy link
Collaborator Author

I like this way of doing it - going to have to adjust some of my code now :-P

@iwsfg
Copy link
Contributor

iwsfg commented Nov 27, 2015

Hmm..... Fix from 5508fea works fine only if tab was intialy active and later was moved onto background.

Howerver, if tab happens to be inactive at the moment when Velocity code is being executed
(when browser just got launched and it restored bunch of tabs, for instance), visibilitychange event wont fire until we enter the tab. So reassignment code never kicks in, leaving rAF variable point at the native requestAnimationFrame implementation and execution of every animation we start during this time window is being postponed.

Unless it was intended to work this way (I don't see the reasons for this though) I think it should be changed to something like

if (!Velocity.State.isMobile && document.hidden !== undefined) {
    var spoofTicker = function() {
        /* Reassign the rAF function (which the global tick() function uses) based on the tab's focus state. */

        if (document.hidden) {
            rAF = function(callback) { 
                /* The tick function needs a truthy first argument to pass its internal timestamp check. */
                return setTimeout(function() { callback(true) }, 16);
            };

            /* The rAF loop has been paused by the browser, so we manually restart the tick. */
            tick();
        } else {
            rAF = window.requestAnimationFrame || rAFPollyfill;
        }
    };

    document.addEventListener("visibilitychange", spoofTicker);
    spoofTicker();
}

but with a better name for the function than the one I came up with.

That's probably unnecessary, but here's a very basic fiddle you can observe problem at. (Just click the link with Mouse3 button or Ctrl+Mouse1, wait for 3-5 seconds after its finished loading and switch to it. You will see animation playing, even though it should be finished at that point.)

Rycochet pushed a commit that referenced this issue Aug 3, 2020
Support for the vh and vw CSS units. Closes #181.

Fixes bug where value operators leaked into subsequent properties.
Closes #182.

Errors in users’ callbacks won’t halt the execution of Velocity itself.
Closes #177.

Changed the “:animating” indicator class to just “animating” for
support with querySelectorAll.

On desktop browsers, animations no longer sprint to completion (they
remain properly timed) when a tab loses then regains focus. Closes #176.

Fixed bug where the stop command wouldn’t work when an effect had a
delay. Closes #159.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants