javascript - 三.js点击动作(requestAnimationFrame)
问题描述
我希望相机在单击按钮时不断旋转 + = 事实证明,有一个触发器持续 2 秒(单击时)并结束。
let btn = document.querySelector('.btn--left');
btn.addEventListener('click',function(event) {
camera.position.x+=(3*Math.sin(angle/6));
camera.position.z+=(3*Math.cos(angle/6));
});
let angle = 0;
const animate = function () {
requestAnimationFrame( animate );
angle-=Math.PI/180*2;
renderer.render( scene, camera );
controls.update();
camera.updateProjectionMatrix();
};
animate();
解决方案
如果我理解正确你想要这个:https ://codepen.io/adelriosantiago/pen/QWEpvLg?editors=1010
基本上,有一个shouldRotate
变量指示相机何时应该旋转。默认情况下是false
.
在里面animate
我们检查 if shouldRotate === true
,如果这成立,那么旋转就会发生。
if (shouldRotate) {
angle -= Math.PI/180*2;
camera.position.x+=(3*Math.sin(angle/6));
camera.position.z+=(3*Math.cos(angle/6));
}
单击按钮时,setTimeout
设置为 2 秒。当计时器结束时,它会做shouldRotate = false
。像这样:
btn.addEventListener('click',function(event) {
shouldRotate = true;
setTimeout(() => {
shouldRotate = false;
}, 2000)
});
推荐阅读
- typescript - 用 Mocha 延迟测试 Cloud Functions 的内容
- python - 根据 Python 中另一列的条目创建新列
- javascript - Angular 中带有 hostlistener 的活动类
- javascript - 如何将来自后端的有效负载响应重新格式化为所需的格式?
- r - 如何在 knitr_kable 对象上加上括号
- java - 第二个 Scanner 对象的输入与类型不匹配..?
- woocommerce - 我将购物车和结帐页面合并为一个现在迷你购物车不更新金额
- node.js - Passport 未根据要求公开 login() 函数
- java - 没有@EnableAsync,@Async 是否可以工作
- google-cloud-firestore - 您可以在 Firestore 中隐藏集合组查询的父 ID 信息吗?