Capítulo 841 de 859
Most three.js examples render continuously via a requestAnimationFrame loop, which wastes power/battery for scenes that aren't animating — rendering once and then only in response to actual changes (input, loaded data, resize) is often the better choice for non-game apps.
OrbitControls change event: fires whenever the controls move the camera, giving a natural hook to re-render without a continuous loop.enableDamping on, controls.update() (needed every frame while damping settles) itself fires another change event — calling render directly from change would recurse forever, so you instead request a frame only if one isn't already pending.requestRenderIfNotRequested: a guarded wrapper around requestAnimationFrame(render) using a boolean flag, ensuring multiple rapid change events collapse into a single upcoming render rather than queuing many.onChange callback can simply call the same "request a render" function so adjusting a control re-renders once, on demand.let renderRequested = false;
function render() {
renderRequested = false;
renderer.render(scene, camera);
}
render();
function requestRenderIfNotRequested() {
if (!renderRequested) {
renderRequested = true;
requestAnimationFrame(render);
}
}
controls.addEventListener("change", requestRenderIfNotRequested);
window.addEventListener("resize", requestRenderIfNotRequested);
OrbitControls' change event (and window resize) to trigger a render rather than looping continuously.enableDamping enabled, guard against infinite recursion by only requesting a new frame if one isn't already pending.change event drives this whole pattern.