three.js:单击鼠标时停止自动旋转 [英] three.js: stop automatic rotation while mouse is clicked

查看:1818
本文介绍了three.js:单击鼠标时停止自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩three.js.我构建了一个旋转的多维数据集,您也可以在OrbitControls的帮助下旋转它.现在,我的目标是在单击鼠标时停止自动旋转,因此当用户想要自己旋转立方体时,不会分散用户的注意力.所以动画:

I am playing around with three.js. I built a rotating cube, which you also can rotate around with the help of OrbitControls. Now I am aiming to stop the automatic rotation when the mouse is clicked, so the user won't be distracted when he wants to rotate the cube by himself. So the animation:

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

    render();
    requestAnimationFrame( animate );
}

应该在鼠标按下时停止并在释放鼠标时继续.如何与OrbitControls结合使用?

should stop, when mouse is down and continue, when mouse is released. How can I achieve it in combination with OrbitControls?

完整代码:

Javascript:

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事件侦听器.

You can use the mousedown and mouseup event listeners.

类似这样的东西:

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 );
}

此处是可工作的数字笔.

这篇关于three.js:单击鼠标时停止自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆