简体   繁体   English

RequestAnimationFrame 中的 forEach 循环

[英]forEach loop inside RequestAnimationFrame

I would like to structure a (THREEjs) project I'm working on like this我想构建一个(THREEjs)项目,我正在像这样工作

    class Blah
       scene    : new THREE.Scene()
       renderer : new THREE.WebGLRenderer()
       camera   : new THREE.PerspectiveCamera()

       render : ( renderables )->

          renderables.forEach ( renderable )->
             renderable()

          @renderer.render( @scene, @camera ) 

   foo = new Blah()

   animateMovie =->
       requestAnimationFrame( animateMovie )     
       foo.render([baz.update, bar.update])

The render method takes an array of functions to update. render方法需要一个函数数组来更新。 Would having a loop inside animateMovie , which gets called recursively using requestAnimationFrame , cause stack overflow, and is there any performance issues with doing something like this. animateMovie内部animateMovie会有一个循环,它使用requestAnimationFrame递归调用,导致堆栈溢出,并且执行此类操作是否存在任何性能问题。

The requestAnimationFrame() method tells the browser to run a callback function right before the next repaint happens. requestAnimationFrame()方法告诉浏览器在下一次重绘发生之前运行回调函数。

It's particularly useful when using JavaScript for animations and repeating UI updates.在将 JavaScript 用于动画和重复 UI 更新时,它特别有用。 Because it ties into the browser's repaint timing, it produces a smoother effect than using something like setInterval()因为它与浏览器的重绘时间有关,所以它比使用setInterval()东西产生更平滑的效果

The requestAnimationFrame() method only runs once. requestAnimationFrame()方法只运行一次。 You can make it loop over-and-over again using a technique called recursion.您可以使用称为递归的技术使其反复循环。

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

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