80

I'm using Three.js with the WebGL renderer to make a game which fullscreens when a play link is clicked. For animation, I use requestAnimationFrame.

I initiate it like this:

self.animate = function()
{
    self.camera.lookAt(self.scene.position);

    self.renderer.render(self.scene, self.camera);

    if (self.willAnimate)
        window.requestAnimationFrame(self.animate, self.renderer.domElement);
}

self.startAnimating = function()
{
    self.willAnimate = true;
    self.animate();
}

self.stopAnimating = function()
{
    self.willAnimate = false;
}

When I want to, I call the startAnimating method, and yes, it does work as intended. But, when I call the stopAnimating function, things break! There are no reported errors, though...

The setup is basically like this:

  • There is a play link on the page
  • Once the user clicks the link, a renderer's domElement should fullscreen, and it does
  • The startAnimating method is called and the renderer starts rendering stuff
  • Once escape is clicked, I register an fullscreenchange event and execute the stopAnimating method
  • The page tries to exit fullscreen, it does, but the entire document is completely blank

I'm pretty sure my other code is OK, and that I'm somehow stopping requestAnimationFrame in a wrong way. My explanation probably sucked, so I uploaded the code to my website, you can see it happening here: http://banehq.com/Placeholdername/main.html.

Here is the version where I don't try to call the animation methods, and fullscreening in and out works: http://banehq.com/Correct/Placeholdername/main.html.

Once play is clicked the first time, the game initializes and it's start method is executed. Once the fullscreen exits, the game's stop method is executed. Every other time that play has been clicked, the game only executes it's start method, because there is no need for it to be initialized again.

Here's how it looks:

var playLinkHasBeenClicked = function()
{
    if (!started)
    {
        started = true;

        game = new Game(container); //"container" is an empty div
    }

    game.start();
}

And here's how the start and stop methods look like:

self.start = function()
{
    self.container.appendChild(game.renderer.domElement); //Add the renderer's domElement to an empty div
    THREEx.FullScreen.request(self.container);  //Request fullscreen on the div
    self.renderer.setSize(screen.width, screen.height); //Adjust screensize

    self.startAnimating();
}

self.stop = function()
{
    self.container.removeChild(game.renderer.domElement); //Remove the renderer from the div
    self.renderer.setSize(0, 0); //I guess this isn't needed, but welp

    self.stopAnimating();
}

The only difference between this and the working version is that startAnimating and stopAnimating method calls in start and stop methods are commented out.

6 Answers 6

145

One way to start/stop is like this

var requestId;

function loop(time) {
    requestId = undefined;

    ...
    // do stuff
    ...

    start();
}

function start() {
    if (!requestId) {
       requestId = window.requestAnimationFrame(loop);
    }
}

function stop() {
    if (requestId) {
       window.cancelAnimationFrame(requestId);
       requestId = undefined;
    }
}

Working example:

const timeElem = document.querySelector("#time");
var requestId;

function loop(time) {
    requestId = undefined;
    
    doStuff(time)
    start();
}

function start() {
    if (!requestId) {
       requestId = window.requestAnimationFrame(loop);
    }
}

function stop() {
    if (requestId) {
       window.cancelAnimationFrame(requestId);
       requestId = undefined;
    }
}

function doStuff(time) {
  timeElem.textContent = (time * 0.001).toFixed(2);
}
  

document.querySelector("#start").addEventListener('click', function() {
  start();
});

document.querySelector("#stop").addEventListener('click', function() {
  stop();
});
<button id="start">start</button>
<button id="stop">stop</button>
<div id="time"></div>

9
  • 7
    should this be cancelAnimationFrame instead of cancelRequestAnimationFrame?
    – leech
    Mar 7, 2014 at 5:28
  • 8
    changed, although in my defense it was originally called cancelRequestAnimationFrame. You can verify this by opening a console in Chrome and typing webkitCancel and you'll see webkitCancelRequestAnimationFrame still in there as of Chrome 33
    – gman
    Mar 7, 2014 at 16:03
  • Documentation for both functions can be found on MDN at: developer.mozilla.org/en-US/docs/Web/API/window/… (thanks for the awesome info!)
    – user2493235
    Apr 30, 2016 at 14:47
  • 1
    On thing I found out, you cannot stop the loop from inside the function: Here a demo. Only from outside :-)
    – Legends
    Feb 22, 2017 at 18:59
  • 3
    Actually you can cancel the loop from inside the function, like this: setTimeout(function(){window.cancelAnimationFrame(requestId);},0);
    – Legends
    Feb 22, 2017 at 19:19
24

Stopping is as simple as not calling requestAnimationFrame anymore, and restarting is to call it it again. ex)

        var pause = false;
        function loop(){
                //... your stuff;
                if(pause) return;
                window.requestionAnimationFrame(loop);
        }
       loop(); //to start it off
       pause = true; //to stop it
       loop(); //to restart it
4
  • Yes. That's actually the simplest way. Aug 11, 2016 at 17:01
  • 5
    But it's not efficient as you would have to perform a check inside each animation frame that way. I found cancelAnimationFrame() performing more efficient on heavy animation loops. Aug 30, 2016 at 7:54
  • 1
    I don't get why this solution by user3363398 is less efficient than gman's. In gmans solution there's instead a function call inside the loop and that function call in turn then performs the check. Isn't that heavier? One check versus function call + check.
    – Oortone
    Jun 4, 2021 at 7:37
  • I quite like this way because you don't assign and track an ID and a single pause Boolean is very intuitive. Performance wise its an assignment, vs a check (at best) that's write vs read. Its not going to make a measurable difference. Sep 22 at 3:01
6
var myAnim //your requestId
function anim()
       {
       //bla bla bla
       //it's important to update the requestId each time you're calling reuestAnimationFrame
       myAnim=requestAnimationFrame(anim)
       }

Let's start it

myAnim=requestAnimationFrame(anim)

Let's stop it

//the cancelation uses the last requestId
cancelAnimationFrame(myAnim)

Reference

2

I played around with the tutorial of a 2D Breakout Game where they also used requestAnimationFrame and I stopped it with a simple return. The return statement ends function execution if the value of return is omitted.

if(!lives) {
    alert("GAME OVER");
    return;
}

// looping the draw()
requestAnimationFrame(draw);
1
  • This would fall into the "just don't initiate the next cycle" approach to stopping a animation frame loop that @user3363398 proposed, the alternative approach being to use cancelAnimationFrame(). Nov 18, 2018 at 15:13
1

I would suggest having a look at the requestAnimationFrame polyfill gibhub page. There are discussions about how this is implemented.

1

So, after doing some more testing, I've found out that it was, indeed, my other code that posed a problem, not the animation stopping (it was a simple recursion after all). The problem was in dynamically adding and removing the renderer's domElement from the page. After I've stopped doing that, for there was really no reason to do so, and included it once where the initialization was happening, everything started working fine.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.