简体   繁体   English

如何使用cancelAnimationFrame停止旋转?

[英]How to stop the rotation with cancelAnimationFrame?

I am working currently with threejs and the requestAnimationFrame method. 我目前正在使用threejs和requestAnimationFrame方法。 I try to rotate a camera to the right by holding a button. 我尝试按住按钮向右旋转相机。 My problem here is, that by releasing the button, the rotation doesn't stop end gets accelerated everytime I press the button again. 我的问题是,每次松开按钮,松开按钮,旋转不会停止,最终加速。

HTML: HTML:

<button id="right" onmousedown="rotateRight()" onmouseup="rotatestopRight()">Right</button> 

JavaScript: JavaScript:

function rotateRight(){
    mainPivot.add(camera);
    mainPivot.rotation.y+=Math.PI/180;
    requestAnimationFrame( rotateRight );
}

function rotatestopRight(){
    cancelAnimationFrame( rotateRight );
}

I tried to solve this by making mainPivot.rotation.y-=Math.PI/180; 我试图通过制作mainPivot.rotation.y-=Math.PI/180;来解决这个问题mainPivot.rotation.y-=Math.PI/180; on release, which worked, but it's not the best solution, since onmouseup event can easily be bypassed. 发行时有效,但这不是最佳解决方案,因为可以轻松绕过onmouseup事件。 Any idea how to get around? 任何想法如何解决?

You are using it incorrectly. 您使用不正确。 requestAnimationFrame returns an id which you use when you call cancelAnimationFrame . requestAnimationFrame返回您在调用cancelAnimationFrame时使用的ID。

var id = requestAnimationFrame();

...

cancelAnimationFrame( id );

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

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