Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript requestAnimationFrame循环是如何工作的?_Javascript_Three.js - Fatal编程技术网

Javascript requestAnimationFrame循环是如何工作的?

Javascript requestAnimationFrame循环是如何工作的?,javascript,three.js,Javascript,Three.js,我还在学习javascript&Three.js,在理解requestAnimationFrame函数如何工作时遇到了困难。有人能解释一下下面的代码是如何使用简单的单词的吗 (您可以检查完整代码) requestAnimationFrame()在完成重新绘制之前,将鼠标伸向浏览器并要求其执行一个函数 守则: function render(time) { time *= 0.001; // convert time to seconds cube.rotatio

我还在学习javascript&Three.js,在理解requestAnimationFrame函数如何工作时遇到了困难。有人能解释一下下面的代码是如何使用简单的单词的吗

(您可以检查完整代码)

requestAnimationFrame()在完成重新绘制之前,将鼠标伸向浏览器并要求其执行一个函数

守则:

  function render(time) {    
    time *= 0.001;  // convert time to seconds

    cube.rotation.x = time;
    cube.rotation.y = time;

    // render our scene
    renderer.render(scene, camera);

    requestAnimationFrame(render);
  }
  requestAnimationFrame(render);

  • 您设置了一个时间变量,该变量的值在每次调用render()时都会发生变化
  • 将立方体旋转x和y值设置为时间变量
  • 使用更新的属性渲染场景
  • 在渲染函数中调用requestAnimationFrame传入渲染。这将导致一个循环,该循环将使用更新的坐标在屏幕上不断重新绘制立方体
  • 在渲染函数外部调用requestAnimationFrame并传入渲染函数,以对立方体进行初始渲染

“您设置了一个时间变量,该变量在每次调用render()时的值都会发生变化。”时间由rAF传递,是一个时间戳(它通常是文档处于活动状态后经过的时间,如performance.now()),非常感谢!谢谢你对“时间”价值的解释,我认为这对我来说没有意义!
  function render(time) {    
    time *= 0.001;  // convert time to seconds

    cube.rotation.x = time;
    cube.rotation.y = time;

    // render our scene
    renderer.render(scene, camera);

    requestAnimationFrame(render);
  }
  requestAnimationFrame(render);