Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right usage of requestAnimationFrame / cancelAnimationFrame [duplicate]

While working with the requestAnimationFrame API, I encountered a problem. It might be because the structure I use is wrong, but it seems logical to me.

What I basically do is a very easy background-position manipulation for an infinite falling effect:

( This version is simplified, but demonstrative )

cache.mechanism.snowFall = function(){

    cache.snow.position.y ++;
    if( cache.snow.position.y > cache.viewport.height ){ 
        cache.snow.position.y -= cache.viewport.height; 
    }
    cache.snow.elem.style.backgroundPosition = "0px " + cache.snow.position.y + "px";

    requestAnimationFrame( cache.mechanism.snowFall );

};

Actually, it moves the background position 1px lower with every animation frame. ( Resets when neccessary, to avoid high paint times, and FPS loss )

I initialize it by calling:

cache.mechanism.snowFall();

So, it runs fine, with a very high FPS, but it is not possible to stop it.

cancelAnimationFrame( cache.mechanism.snowFall );

-does nothing, and returns undefined.

like image 566
István Pálinkás Avatar asked Dec 18 '25 17:12

István Pálinkás


1 Answers

You should send the id of the requestAnimationFrame you want to cancel into cancelAnimationFrame. That id is a return value of the original requestAnimationFrame.

So request one animation loop:

var id = requestAnimationFrame(cache.mechanism.snowFall);

And to cancel that request:

cancelAnimationFrame(id);

Alternatively, since requestAnimationFrame must be called to keep the loop running, you could use a flag to stop the animation:

// Set an external flag to allow animations to continue
var continueAnimating = true;

function animate()
{
    if(continueAnimating)
    {
        // when continueAnimating is false, this new
        // request will not occur and animation stops
        requestAnimationFrame(animate); 
    }
}

// To turn off animation
continueAnimating = false;
like image 51
markE Avatar answered Dec 20 '25 10:12

markE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!