0
点赞
收藏
分享

微信扫一扫

requestAnimationFrame.js---封装动画Frame51

//主要是区分浏览器的不同,如果浏览器不支持requestNextAnimationFrame()方法,只能使用setTimeout()来代替。这种封装方式称为polyfill method,多平台智能适配方法,主要思想是利用不同平台的优缺点,当不满足时再用替代方法;个合集思想,而不是交集思想。
window.requestNextAnimationFrame =
   (function () {
      var originalWebkitRequestAnimationFrame = undefined,
          wrapper = undefined,
          callback = undefined,
          geckoVersion = 0,
          userAgent = navigator.userAgent,
          index = 0,
          self = this;

      // Workaround for Chrome 10 bug where Chrome
      // does not pass the time to the animation function
      
      if (window.webkitRequestAnimationFrame) {
         // Define the wrapper

         wrapper = function (time) {
           if (time === undefined) {//chrome10版本的BUG,现在应该已经修复了,此值一直是undefined
              time = +new Date();
           }
           self.callback(time);
         };

         // Make the switch
          
         originalWebkitRequestAnimationFrame = window.webkitRequestAnimationFrame;    

         window.webkitRequestAnimationFrame = function (callback, element) {
            self.callback = callback;

            // Browser calls the wrapper and wrapper calls the callback
            
            originalWebkitRequestAnimationFrame(wrapper, element);
         }
      }

      // Workaround for Gecko 2.0, which has a bug in
      // mozRequestAnimationFrame() that restricts animations
      // to 30-40 fps.

      if (window.mozRequestAnimationFrame) {
         // Check the Gecko version. Gecko is used by browsers
         // other than Firefox. Gecko 2.0 corresponds to
         // Firefox 4.0.
         
         index = userAgent.indexOf('rv:');

         if (userAgent.indexOf('Gecko') != -1) {
            geckoVersion = userAgent.substr(index + 3, 3);

            if (geckoVersion === '2.0') {
               // Forces the return statement to fall through
               // to the setTimeout() function.

               window.mozRequestAnimationFrame = undefined;
            }
         }
      }
      
      return window.requestAnimationFrame   ||//w3c对象
         window.webkitRequestAnimationFrame ||//chrome对象
         window.mozRequestAnimationFrame    ||//firefox对象
         window.oRequestAnimationFrame      ||
         window.msRequestAnimationFrame     ||//window对象

         function (callback, element) {//为了解决firefox 4.0版本帧速率过慢的bug,在后续版本中已改正
            var start,
                finish;

            window.setTimeout( function () {
               start = +new Date();
               callback(start);
               finish = +new Date();

               self.timeout = 1000 / 60 - (finish - start);

            }, self.timeout);
         };
      }
   )
();

举报

相关推荐

0 条评论