简体   繁体   English

雄辩的Javascript:DOM动画片段

[英]Eloquent Javascript: DOM Animation snippet

I'm trying to understand everything that is happening in this little code example in Eloquent Javascript: The Document Object Model (Chapter 13) , but I'm not clear on where exactly the value for "time" is being passed into the animate() function before the function itself gets passed into requestAnimationFrame(). 我试图理解Eloquent Javascript:文档对象模型(第13章)中的这个小代码示例中发生的一切,但是我不清楚“时间”的值到底在哪里传递给了animate( )函数,然后将该函数本身传递到requestAnimationFrame()中。 What exactly am I missing here? 我在这里到底想念什么?

<p style="text-align: center">
  <img src="img/cat.png" style="position: relative">
</p>

<script>
  var cat = document.querySelector("img");
  var angle = 0, lastTime = null;
  function animate(time) {
    if (lastTime != null)
      angle += (time - lastTime) * 0.001;
    lastTime = time;
    cat.style.top = (Math.sin(angle) * 20) + "px";
    cat.style.left = (Math.cos(angle) * 200) + "px";
    requestAnimationFrame(animate);
  }
  requestAnimationFrame(animate);
</script>

When you execute this line: requestAnimationFrame(animate); 执行此行时: requestAnimationFrame(animate); , the function animate will be called inside requestAnimationFrame and will get the time variable passed as an argument. animate函数将在requestAnimationFrame内部调用,并将获得作为参数传递的time变量。 Something like this (narrowed and rough): 像这样(狭窄且粗糙):

function requestAnimationFrame(callback) {
    var time = getTime();
    callback(time); //Where callback is your `animate` function
};

Of course that requestAnimationFrame does not look at all like the function above, but this is just to illustrate where time comes from. 当然, requestAnimationFrame看起来根本不像上面的函数,但这只是为了说明time是从哪里来的。

As per the documentation: 根据文档:

The callback method is passed a single argument, a DOMHighResTimeStamp, which indicates the current time when callbacks queued by requestAnimationFrame begin to fire. 回调方法传递一个单独的参数DOMHighResTimeStamp,该参数指示由requestAnimationFrame排队的回调开始触发的当前时间。 Multiple callbacks in a single frame, therefore, each receive the same timestamp even though time has passed during the computation of every previous callback's workload. 因此,即使在计算每个先前回调的工作量的过程中经过了时间,单个帧中的多个回调也将获得相同的时间戳。 This timestamp is a decimal number, in milliseconds, but with a minimal precision of 1ms (1000 µs). 该时间戳是一个十进制数(以毫秒为单位),但最小精度为1ms(1000 µs)。

Read more here: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame 在此处阅读更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

It's not being passed in beforehand . 它没有预先传递

The line function animate(time) { doesn't call anything: instead, it creates a function named animate , which takes in a single argument called time . function animate(time) {不会调用任何东西:而是创建了一个名为animate的函数,该函数接受一个称为time参数。

The specification for requestAnimationFrame states that the callback always gets passed a single argument, which equals the number of milliseconds (or fractions thereof) since the page first loaded. requestAnimationFrame的规范指出,回调始终传递单个参数,该参数等于自页面首次加载以来的毫秒数(或其分数)。 So time doesn't get passed in before the function gets passed to requestAnimationFrame : instead, it gets passed in every time requestAnimationFrame sets up its call to the callback . 因此,在函数传递给requestAnimationFrame之前不会time :相反, 每次requestAnimationFrame设置其对callback的调用时,时间都会传递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM