2017-06-04 129 views
0

我目前正在使用threejs和​​方法。我试着通过按住一个按钮将相机旋转到右侧。我的问题在于,通过释放按钮,旋转不会停止,每当我再次按下按钮时,结束都会加速。如何用cancelAnimationFrame停止旋转?

HTML:

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

的JavaScript:

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

function rotatestopRight(){ 
    cancelAnimationFrame(rotateRight); 
} 

我试图通过使上发布,其工作mainPivot.rotation.y-=Math.PI/180;解决这个问题,但它不是最好的解决办法,因为onmouseup事件很容易被绕过。任何想法如何解决?

回答

2

错误地使用它。​​返回您在拨打cancelAnimationFrame时使用的ID。

var id = requestAnimationFrame(); 

... 

cancelAnimationFrame(id); 
+0

谢谢!这有帮助! – PLAYCUBE