Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30638

Re: Animated vector field...

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Animated vector field...
Date 2016-06-06 21:55 +0200
Organization PointedEars Software (PES)
Message-ID <2375647.gXbDYIEuU1@PointedEars.de> (permalink)
References <nitnvj$mko$1@gioia.aioe.org> <nito4t$mo7$1@gioia.aioe.org> <1650761.fOrR8oP7AL@PointedEars.de> <nj24lg$13n0$1@gioia.aioe.org>

Show all headers | View raw


Chris M. Thomasson wrote:

> I am thinking of using:
> ___________________________________________________
> // Request animation workaround
> var g_request_animation_frame = window.requestAnimationFrame;
> 
> function prv_request_animation_frame(cb) {
>     if (!g_request_animation_frame) {
>         g_request_animation_frame =
>             window.webkitRequestAnimationFrame ||
>             window.mozRequestAnimationFrame ||
>             window.oRequestAnimationFrame ||
>             window.msRequestAnimationFrame;

If you call g_request_animation_frame(), you will call one of these methods 
as functions.  In strict mode, “this” will be undefined.  In non-strict 
mode, “this” will refer to the global object; that might work because of 
internal wrappers.  But you should always call object’s methods as such, 
i.e. in this case you should call them as methods of the object referred to 
by “window” explicitly.  The least you should do is to call them using 
Function.prototype.call/.apply(), but with host objects, all bets are off.

I recommend that you define at least

        g_request_animation_frame = window.webkitRequestAnimationFrame
          ? function (callback) {
              window.webkitRequestAnimationFrame(callback);
            }
          : window.mozRequestAnimationFrame
              ? function (callback) {
                  window.mozRequestAnimationFrame(callback);
                }
              : window.oRequestAnimationFrame
                  ? function (callback) {
                      window.oRequestAnimationFrame(callback);
                    }
                  : window.msRequestAnimationFrame
                      ? function (callback) {
                          window.msRequestAnimationFrame(callback);
                        }
                      : null;

instead.  An alternative that needs to be thoroughly tested:

       g_request_animation_frame =
         window.requestAnimationFrame
         || window.webkitRequestAnimationFrame
         || window.mozRequestAnimationFrame
         || window.oRequestAnimationFrame
         || window.msRequestAnimationFrame;

       var fpCall = Function.prototype.call;

       var native_request_animation_frame = function (callback) {
         fpCall.call(g_request_animation_frame, window, callback);
       };

It is not a good idea to access host object’s methods in a type-converting 
test; see implementations of isHostMethod() for an alternative.

However, you should check if attempting to call prefixed methods makes 
sense.  For example, Opera has switched to Blink; if they do not have 
window.oRequestAnimationFrame() now, they are not going to have it later, 
but, at most, window.webkitRequestAnimationFrame().

>             g_request_animation_frame = function (callback) {
>                 var cur_time = new Date().getTime();
>                 var time_addend = Math.max(0, 16 - (cur_time -
>                 last_time));

It is a good idea to consider that more time may have elapsed, but what are 
you getting at with the Math.max(…)?
 
>                 var id = window.setTimeout(function () {
>                     callback(cur_time + time_addend);
>                 }, time_addend);
> 
>                 last_time = cur_time + time_addend;
> 
>                 return id;
>             };
>         }
>     }
> 
>     g_request_animation_frame(cb);
> }
> […]

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Animated vector field... "Chris M. Thomasson" <nospam@nospam.no> - 2016-06-03 22:12 -0700
  Re: Animated vector field... "Chris M. Thomasson" <nospam@nospam.no> - 2016-06-03 22:15 -0700
    Re: Animated vector field... Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-04 13:45 +0200
      Re: Animated vector field... "Chris M. Thomasson" <nospam@nospam.no> - 2016-06-05 14:13 -0700
        Re: Animated vector field... "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-05 21:33 -0700
        Re: Animated vector field... Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-06 21:55 +0200
  Re: Animated vector field... Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2016-06-04 10:47 +0100
    Re: Animated vector field... "Chris M. Thomasson" <nospam@nospam.no> - 2016-06-05 14:16 -0700
  Re: Animated vector field... chris.m.thomasson.1@gmail.com - 2016-06-05 21:37 -0700
  Re: Animated vector field... "Chris M. Thomasson" <nospam@nospam.com> - 2016-06-09 16:02 -0700

csiph-web