Three.js如何在用户看不到3d空间时停止渲染
three.js how to stop render when the user can't see the 3d space?
我正在制作一个旋转的简单球体渲染。到目前为止,一切都很好,但我想基本上暂停渲染时,用户不再看dom元素,渲染器是一个孩子,因为元素是一个方式下的页面和宝贵的资源被使用在他们不需要的地方。这是我到目前为止写的-
window.addEventListener('scroll', () => {
let scrollPosition = document.body.scrollTop;
//element is almost about to be visible, time to start rendering
if (scrollPosition >= topOfElement - 200) {
if (everythingIsLoaded && !isRendering) {
render();
console.log('render has been started');
} else {
//wait until everythingIsLoaded is true
}
//element is not visible, stop rendering
} else {
//need to stop rendering here!
//this doesn't work, dunno how to do this
if (animationFrame) {
stopRendering(render);
console.log('render has been halted');
}
}
});
我的render()
函数是这样的-
const render = () => {
animationFrame = requestAnimationFrame( render );
sphere.rotation.y += 0.001;
renderer.render( scene, camera );
isRendering = true;
};
是否有办法停止渲染当用户不再能够看到webGL渲染区域?
我试着做这样的事情(正如你在上面的代码中看到的)-
var stopRendering = (af) => {
cancelAnimationFrame(af);
isRendering = false;
};
,但这似乎并没有真正停止渲染。
window.cancelAnimationFrame
接受window.requestAnimationFrame
返回的id,在您的示例中是名为animationFrame
的变量。它和setTimeout和clearTimeout是一样的
现在你传递给它一个完整的函数引用,render()
函数。在提供的代码中很难跟踪您对全局变量的使用,但您可能只想这样做:
if (animationFrame) {
stopRendering(animationFrame);
}
因为在你的情况下,你分配由requestAnimationFrame
返回的id到变量animationFrame
.
相关文章:
- JavaScript名称空间和对象创建
- 在javascript中使用命名空间
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- javascript中的命名空间,IDE中支持代码完成/内容辅助's
- 具有大型几何图形的基于沙发的空间查询
- 如何使用javascript命名空间
- JSDoc:模块和名称空间之间的关系是什么
- Highcharts 3d调整zindex错误
- js命名空间和变量范围
- html,js-如何限制元素"范围“-命名空间
- 动画制作/减缓元素填充移除元素留下的空间
- 转换自的JavaScript命名空间
- 命名空间与自调用函数
- 在IndexedDB中保留空间
- HighCharts:3D柱形图在选择时更改边框颜色
- 3d上的深度比例错误
- 3d数学:屏幕空间到世界空间
- JavaScript / ThreeJS - 围绕中心Y轴(在3D空间中)在圆圈中移动对象的方程式
- Three.js如何在用户看不到3d空间时停止渲染
- 如何在WebGL 3d空间中从鼠标点击坐标获取对象