requestAnimationFrame gebruiken met een doel FPS

By on

Combining setInterval and requestAnimationFrame to get a target framerate

In the beginning...

People who've worked with Javascript animations before will probably know the next piece of code:
var targetFps = 25;
var loopInterval = window.setInterval(renderFrame, 1000 / targetFps);

What this does is create a loop that will run 25 times per second, which will hopefully give you your target framerate of 25 frames per second.

And They said: Do not use setInterval!

But then the powers that be thought of a new api: requestAnimatonFrame. It's basically a function that tells the browser it needs to execute something which will result in a redraw of a certain element.

We were told to set it up in a function like this:

function renderLoop(){
    // Put another iteration of renderLoop in the animation queue
    window.requestAnimationFrame(renderLoop);
    
    // Actually calculate and draw a frame
    renderFrame();
}

Yes, you're reading it correctly: it's basically a function calling itself. This will give you the best possible framerate. But what if we could combine those 2: setInterval and requestAnimationFrame?

The best of both worlds

function renderLoop(){

    var targetFps = 25;	
    
    // Combine setInterval and requestAnimationFrame in order to get a desired fps
    setInterval(function() {
        window.requestAnimationFrame(renderFrame);
    }, 1000 / targetFps);
}

And now you have a working requestAnimationFrame setup with a target fps!

So, what do you think?

A requestAnimationFrame polyfill

Warning: you will probably want to use a fallback function. Current fallback functions default to setInterval itself, which is not what we want.

Here's a very basic one you can use:

window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
		  window.webkitRequestAnimationFrame ||
		  window.mozRequestAnimationFrame    || 
		  window.oRequestAnimationFrame      || 
		  window.msRequestAnimationFrame     || 
		  function(/* function */ callback, /* DOMElement */ element){

			// Since our requestAnimFrame is already in a loop in order to
			// control the preferred FPS, just call callback, not an interval
			callback();
		  };
})();

Comments

juliadiets.com

This is also a well known problem in the professional video industry when you want to playback a video at a different FPS then the device showing it refresh at. Many techniques has been used such as frame blending and complex re-timing re-building intermediate frames based on motion vectors, but with canvas these techniques are not available and the result will always be jerky video.

hlfrmxsx

01/01/1967

please visit giochi

Some really excellent information, Gladiolus I discovered this.

fashion victim

Excellent web site you have here.. It's difficult too find quality writimg like yours nowadays. I truly appreciate individuals like you! Take care!!

MuillNot

Why on earth should I do it? Leave him alone, he’s out of sorts today

MuillNot

Whatever the case I’ll always be subscribing to your entire supply and I hope you jot down ever again soon! I went to the show to kill time.

imitation youth jersey

Buut wanna remark that you have a very decent internet site, I like the layout it actually stands out.

check cheap

Thanks for sharing your thoughtts about crate training cheap. Regards

Johny Jackson

great points altogether, you simply won a new reader. What might you suggest in regards to your publish that you simply made a few days ago? Any positive?

ProFunding247

Quicker and Simpler than the SBA, http://ProFunding247.com can get your business a loan for $2K-350,000 With low-credit and no collateral.

Use our 1 minute form to See exactly how much you can get, No-Cost:

http://ProFunding247.com

If you've been established for at least 12 months you are already pre-qualified. Our Fast service means funding can be finished within 48 hours. Terms are specific for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds have no Restrictions, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://ProFunding247.com

Have a great day, The Pro Funding 24-7 Team

unsubscribe here - http://profunding247.com/i.php?url/r.php?url=skerit.com&id=e98 10685-B Hazelhurst Dr. # 21465 Houston, TX 77043

FastFunding365

Hi, letting you know that http://FastFunding365.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://FastFunding365.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://FastFunding365.com

Have a great day, The Fast Funding 365 Team

unsubscribe/remove - http://FastFunding365.com/r.php?url=skerit.com&id=e102

FastFundingAdvisors

Hi, letting you know that http://FastFundingAdvisors.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://FastFundingAdvisors.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://FastFundingAdvisors.com

Have a great day, The Fast Funding Advisors Team

unsubscribe/remove - http://FastFundingAdvisors.com/r.php?url=skerit.com&id=e102

GetaBusinessLoanNow

Faster and Easier than the SBA, http://GetaBusinessLoanNow.com can get your business a loan for $2K-350,000 With low-credit and no collateral.

Use our quick form to See exactly how much you can get, No-Cost:

http://GetaBusinessLoanNow.com

If you've been established for at least 1 year you are already pre-qualified. Our Fast service means funding can be completed within 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds have no Restrictions, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://GetaBusinessLoanNow.com

Have a great day, The Get a Business Loan Now Team

http://GetaBusinessLoanNow.com/r.php?url=skerit.com&id=e105

GetaBusinessFunded247

Hi, letting you know that http://GetaBusinessFunded-24-7.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://GetaBusinessFunded-24-7.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://GetaBusinessFunded-24-7.com

Have a great day, The Get a Business Funded 247 Team

unsubscribe/remove - http://GetaBusinessFunded-24-7.com/r.php?url=skerit.com&id=e104

BusinessCapitalAdvisor365

Faster and Easier than the SBA, http://BusinessCapitalAdvisor365.com can get your business a loan for $2K-350,000 With low-credit and no collateral.

Use our fast form to See exactly how much you can get, No-Cost:

http://BusinessCapitalAdvisor365.com

If you've been in business for at least 12 months you are already pre-qualified. Our Fast service means funding can be completed within 48hrs. Terms are specific for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds have no Restrictions, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://BusinessCapitalAdvisor365.com

Have a great day, The Business Capital Advisor 365 Team

http://BusinessCapitalAdvisor365.com/r.php?url=skerit.com&id=e105

ProFunder247

Faster and Simpler than the SBA, http://ProFunder-247.com can get your business a loan for $2K-350,000 With low-credit and without collateral.

Use our quick form to Find Out exactly how much you can get, No-Cost:

http://ProFunder-247.com

If you've been in business for at least 12 months you are already pre-qualified. Our Fast service means funding can be finished within 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds are also Non-Restrictive, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://ProFunder-247.com

Have a great day, The Pro Funder 247 Team

http://ProFunder-247.com/r.php?url=skerit.com&id=e107

BusinessFunding365

Hi, letting you know that http://Business-Funding-365.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://Business-Funding-365.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://Business-Funding-365.com

Have a great day, The Business Funding 365 Team

unsubscribe/remove - http://Business-Funding-365.com/r.php?url=skerit.com&id=e108

FastFundingAdvisor

Faster and Simpler than the SBA, http://Fast-Funding-Advisor.com can get your business a loan for $2K-350,000 With low-credit and without collateral.

Use our quick form to Find Out exactly how much you can get, No-Cost:

http://Fast-Funding-Advisor.com

If you've been in business for at least 1 year you are already pre-qualified. Our Quick service means funding can be completed within 48 hours. Terms are specific for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds are also Non-Restrictive, allowing you to use the full amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://Fast-Funding-Advisor.com

Have a great day, The Fast Funding Advisor Team

remove here - http://Fast-Funding-Advisor.com/r.php?url=skerit.com&id=e109

FastFundingAdvisors

Hi, letting you know that http://Fast-Funding-Advisors.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://Fast-Funding-Advisors.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://Fast-Funding-Advisors.com

Have a great day, The Fast Funding Advisors Team

unsubscribe/remove - http://Fast-Funding-Advisors.com/r.php?url=skerit.com&id=e110

ProFunding247

Quicker and Simpler than the SBA, http://ProFunding247.biz can get your business a loan for $2K-350,000 With low-credit and no collateral.

Use our 1 minute form to See exactly how much you can get, No-Cost:

http://ProFunding247.biz

If you've been in business for at least 1 year you are already pre-qualified. Our Fast service means funding can be finished within 48 hours. Terms are specific for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds are also Non-Restrictive, allowing you to use the whole amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://ProFunding247.biz

Have a great day, The Pro Funding 247 Team

remove here - http://ProFunding247.biz/r.php?url=skerit.com&id=e111

minecraft

Right here is the right site for everyone who hopes to understand this topic. You realize a whole lot its almost hard to argue with you (not that I actually would want to…HaHa). You definitely put a new spin on a topic that has been written about for ages. Excellent stuff, just wonderful!

minecraft

always i used to read smaller posts that as well clear their motive, and that is also happening with this paragraph which I am reading here.

minecraft

I don't know if it's just me or if perhaps everyone else experiencing issues with your website. It appears as if some of the written text on your content are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them too? This could be a issue with my internet browser because I've had this happen previously. Many thanks

GetaBusinessFunded247

Hi, letting you know that http://GetaBusinessFunded-24-7.com can find your business a SBA or private loan for $2,000 - $350K Without high credit or collateral.

Find Out how much you qualify for by clicking here:

http://GetaBusinessFunded-24-7.com

Minimum requirements include your company being established for at least a year and with current gross revenue of at least 120K. Eligibility and funding can be completed in as fast as 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get on various terms.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. These funds are Non-Restrictive, allowing you to spend the full amount in any way you require including business debt consolidation, hiring, marketing, or Absolutely Any Other expense.

If you need fast and easy business funding take a look at these programs now as there is limited availability:

http://GetaBusinessFunded-24-7.com

Have a great day, The Get a Business Funded 247 Team

unsubscribe/remove - http://GetaBusinessFunded-24-7.com/r.php?url=skerit.com&id=e114

minecraft

Nice respond in return of this question with firm arguments and telling the whole thing on the topic of that.

minecraft

Hi there to every body, it's my first visit of this blog; this web site includes remarkable and actually excellent stuff designed for readers.

minecraft

Hello, this weekend is fastidious for me, since this moment i am reading this enormous informative article here at my home.

FastFunding365

Quicker and Easier than the SBA, http://FastFunding365.com can get your business a loan for $2K-350,000 With low-credit and without collateral.

Use our fast form to Find Out exactly how much you can get, No-Cost:

http://FastFunding365.com

If you've been in business for at least 12 months you are already pre-qualified. Our Quick service means funding can be completed within 48 hours. Terms are personalized for each business so I suggest applying to find out exactly how much you can get.

This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds have no Restrictions, allowing you to use the full amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense.

There are limited SBA and private funds available so please apply now if interested:

http://FastFunding365.com

Have a great day, The Fast Funding 365 Team

remove here - http://FastFunding365.com/r.php?url=skerit.com&id=e115

minecraft

Thanks , I've recently been looking for information about this subject for ages and yours is the greatest I've found out till now. However, what about the conclusion? Are you positive concerning the source?

Jelle De Loecker