Jump to content

How break from requestAnimationFrame() method


bruserker
 Share

Recommended Posts

I am programming an animated odometer which I intend to use on a game of mine, but I am having some trouble on designing a good animation loop. I am using Pixi and right now I am debugging it on the browser.

As soon as the html page is loaded, an initialization method is executed, which creates a container and a renderer which shall load the relevant images, as well as loading the actual and desired/new values of the odometer. Afterwards, the difference between these 2 values is calculated, and if it is different than zero, it should move some tiles on a specific frequency and update this difference of values based on the same frequency.

Here is how the method which I call on window.requestAnimatedFrame() method:

function update(){

if(differenceOfValues != 0){

moveTiles(frequency);

updateDifferenceOfValues(frequency);

}

else { //difference is now zero, so the animation should stop

    renderer.render(container);

    window.requestAnimationFrame(update); //updates before cancelling the animation

  if (requestId) { //my failed attempt on cancelling the animation :(
     window.cancelAnimationFrame(requestId);
     requestId = undefined;
   }

}

    requestId = window.requestAnimationFrame(update);

      renderer.render(container);

      window.requestAnimationFrame(update);

}

 

All the calculations and movements are performed correctly, however my main problem is on the update() method. In other words, I can't figure out a way of breaking out from it. I would like to snap out of it completely, so I could trigger some button events, like changing the desired/new value while the odometer is still moving, or setting the difference of values to zero, so the tiles position remain on their place, for instance (I can't click buttons or interact with the browser page because the program gets closed inside the update() method, even when there aren't any calculations to perform.)

Anyway, is there a way to "break" this loop execution?

 

Link to comment
Share on other sites

Easiest way to break the loop is to not call `requestAnimationFrame` again, based on some exit criteria.

function update() {
  console.log( performance.now() )
  
  var exit = Math.random() > 0.9

  if ( !exit ) {
    requestAnimationFrame( update )
  }
}

update()

We have a simple exit condition that fires about 10% of the time, if it is false then we continue to call the function again when the animationFrame is ready, otherwise we simply do not call it and the loop finishes.

You can use cancelAnimationFrame if you like but it will involve creating an additional variable to keep track. requestAnimationFrame returns an id which can be used by cancelAnimationFrame.

var raf = null

function update() {
  console.log( performance.now() )

  var exit = Math.random() > .9

  raf = requestAnimationFrame( update )

  if ( exit ) {
    cancelAnimationFrame( raf )
  }
}

update()

In this terse example using cancelAnimationFrame makes no sense but you might find a use case for it.

In your example you are calling requestAnimationFrame more than once anyway, which will probably result in some unexpected behaviour.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...