User Tools

Site Tools


Sidebar

General

Python Tutorials

WebGL Tutorials

Node.js Tutorials

Node.js in WebStorm


Martin Christen

View Martin Christen's profile on LinkedIn


Twitter

Timeline of @MartinChristen

MartinChristen avatar

On my way to the 3D city model workshop in Bonn where I‘m presenting our latest @igeoFHNW #AugmentedReality App… https://t.co/8ghq4GfZW5
About 5 days, 7 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

Teaching #Python to 11-13-year-old girls was great fun today at the Swiss national future day (#zukunftstag).… https://t.co/1lDmWdl02D
About 1 week, 2 days ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

RT @GeoPythonConf: Interested in joining GeoPython 2019? Register to the mailing list: https://t.co/ub3CU7L5WL #python #GeoPython #geospat…
About 2 weeks, 2 days ago by: Martin Christen (@MartinChristen)
webgl:tutorial04

WebGL Tutorial 4: Animation - using requestAnimationFrame

When running an a graphics application at 60 frames per second (FPS) a frame must be drawn within 16.67ms.

Quick redrawing frames is a key element of all 3d graphics engines. Using time events (for example setInterval) has many disadvantages, especially it is not synchronized with other animations (for example CSS, SVG, …) and it is not precise enough.

Luckily there is an API for this, the “requestAnimationFrame”, an example call is:

function renderloop(){
  window.requestAnimFrame(renderloop);
  render();
};

The big drawback of requestAnimationFrame: It is still vendor specific and doesn't run with all WebGL capable browsers.

“requestAnimationFrame” is available in:

  • Chrome Version 24+
  • Firefox Version 23+
  • Internet Explorer 10+
  • Opera 10+
  • (Safari: nightly builds only)

The following code created by Eric Möller, Paul Irish and Tino Zijdel is a cross browser solution with a fallback for all browsers:

(function() {
   var lastTime = 0;
   var vendors = ['ms', 'moz', 'webkit', 'o'];
   for(var x = 0; x < vendors.length &&  !window.requestAnimationFrame; ++x){
      window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
      window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] 
                          || window[vendors[x]+'CancelRequestAnimationFrame'];
   }
 
   if (!window.requestAnimationFrame)
       window.requestAnimationFrame = function(callback, element) {
           var currTime = new Date().getTime();
           var timeToCall = Math.max(0, 16 - (currTime - lastTime));
           var id = window.setTimeout(function() { 
                    callback(currTime + timeToCall); },timeToCall);
           lastTime = currTime + timeToCall;
           return id;
        };
 
   if (!window.cancelAnimationFrame)
      window.cancelAnimationFrame = function(id) {
         clearTimeout(id);
      };
}());

References
Paul Irish, 2011, "requestAnimationFrame for Smart Animating"
Erik Möller, 2011, requestAnimationFrame for smart(er) animating
Source Code: https://gist.github.com/paulirish/1579671
Mozilla docs: requestAnimationFrame API


webgl/tutorial04.txt · Last modified: 2013/09/03 15:32 by mchristen