javascript - Three.js:单击鼠标时停止自动旋转

标签 javascript jquery three.js rotation mouseevent

我正在使用 Three.js。我构建了一个旋转立方体,您也可以在 OrbitControls 的帮助下旋转它。现在我的目标是在单击鼠标时停止自动旋转,这样用户在想要自己旋转立方体时就不会分心。所以动画:

function animate() {
    mesh.rotation.x += .015;
    mesh.rotation.y += .015;
    mesh.rotation.y += .015;

    render();
    requestAnimationFrame( animate );
}

当鼠标按下时应该停止,当鼠标释放时应该继续。如何与 OrbitControls 结合实现它?

完整代码:

Javascript:

var camera;
var scene;
var renderer;
var mesh;
var geometry;
var material1;
var material2;
var material3;
var material4;
var material5;
var material6;
var materials;
var meshFaceMaterial;
var controls;



init();
animate();

function init() {

    camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
    camera.position.z = 25;

    controls = new THREE.OrbitControls( camera );
    controls.addEventListener( 'change', render );

    scene = new THREE.Scene();

  light = new THREE.DirectionalLight( 0xffffff );
  light.position.set( 1, 1, 1 );
  scene.add( light );

  light = new THREE.DirectionalLight( 0xffffff );
  light.position.set( -1, -1, -1 );
  scene.add( light );

  light = new THREE.AmbientLight( 0x222222 );
  scene.add( light );

    geometry = new THREE.CubeGeometry( 10, 10, 10);
    material1 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub1.jpg') } );
    material2 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub2.jpg') } );
    material3 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub3.jpg') } );
    material4 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub4.jpg') } );
    material5 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub5.jpg') } );
    material6 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub6.jpg') } );

    materials = [material1, material3, material5, material6, material4, material2];

    meshFaceMaterial = new THREE.MeshFaceMaterial( materials );

    mesh = new THREE.Mesh(geometry, meshFaceMaterial );
    mesh.position.z = 0;
    scene.add( mesh );

    renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
document.getElementById('render').appendChild( renderer.domElement );   
    window.addEventListener( 'resize', onWindowResize, false );

    render();
}

function animate() {
    mesh.rotation.x += .015;
    mesh.rotation.y += .015;
    mesh.rotation.y += .015;

    render();
    requestAnimationFrame( animate );
}

function render() {
    renderer.render( scene, camera );
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight );
    render();
}

最佳答案

您可以使用 mousedownmouseup 事件监听器。

类似这样的事情:

var isMouseDown = false;

function init(){
    window.addEventListener('mousedown', onMouseDown);
    window.addEventListener('mouseup', onMouseUp);
}

function onMouseDown(){
    isMouseDown = true;
}

function onMouseUp(){
    isMouseDown = false;
}

function animate() {
    if(!isMouseDown){
        mesh.rotation.x += .015;
        mesh.rotation.y += .015;
        mesh.rotation.y += .015;
    }
    render();
    requestAnimationFrame( animate );
}

Here是一个可用的代码笔。

关于javascript - Three.js:单击鼠标时停止自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39449582/

相关文章:

javascript - 将纹理分配给 Three.js 中加载的 glb 模型

javascript - 在给定的时间间隔内创建平滑旋转 (+- x%)

asp.net - Javascript 在 ASP.NET 中找不到隐藏字段?

javascript - jquery 非常简单的插件,非常基础

javascript - 为菜单添加高度

jquery - 使用 jQuery 将表行切换到另一个表的克隆

javascript - 在 Three.js 中创建漏洞

javascript - 为什么这个 reddit 代码会耗尽 CPU 资源?

javascript - jquery 可拖动函数

jQuery - 将 "selected"应用于特定值的选项