仅当鼠标位于画布上时才允许鼠标控制three.js场景

时间:2012-12-02 15:49:43

标签: 3d three.js

three.js画布是一个500 x 500的正方形,在一个更大的网页内有其他内容,我需要鼠标控制相机(用于缩放和旋转对象),只有在鼠标在500 x内时才会发生500平方。

此外,我无法滚动网页,但是当我们将鼠标事件监听器与500 x 500画布隔离时,可能会修复此问题。

当前代码:

<!DOCTYPE html>
<html>
<head> 
   <style>
    div         { 
            position:relative;
            left:100px;           
            top:100px;
            background-color: #eeeeee;
            border:1px solid black;             
            width:500px;
            height:500px;
                }

    canvas      {
            width:500px;
            height:500px;
                }

   </style> 
</head>     
<body> 

    <script src="https://raw.github.com/mrdoob/three.js/master/build/three.min.js"></script>

    <script src="https://raw.github.com/mrdoob/three.js/master/examples/js/loaders/STLLoader.js"></script>

    <script src="https://raw.github.com/mrdoob/three.js/master/examples/js/controls/TrackballControls.js"></script>

    <script>
        var container, camera, scene, renderer, controls;

        init();
        animate();

        function init() {

            container = document.createElement( 'div' );
            document.body.appendChild( container );

            var width = container.clientWidth;
            var height = container.clientHeight;


            camera = new THREE.PerspectiveCamera( 10 , width / height , .1 , 10000 );

            camera.position.set( 0, 0, 10);

            scene = new THREE.Scene();

            controls = new THREE.TrackballControls( camera ); // mouse control
            controls.addEventListener( 'change', render );    // mouse control

            // object

            var loader = new THREE.STLLoader();
            loader.addEventListener( 'load', function ( event ) {

                var geometry = event.content;

                var material = new THREE.MeshLambertMaterial( { ambient: 0xff5533, color: 0xff5533 } );

                var mesh = new THREE.Mesh( geometry, material );

                scene.add( mesh );

            } );

            loader.load( 'slotted_disk.stl' ); // from https://raw.github.com/mrdoob/three.js/master/examples/models/stl/slotted_disk.stl


            // lights

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

            var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
            directionalLight.position = camera.position;
            scene.add( directionalLight );

            // renderer

            renderer = new THREE.WebGLRenderer( { antialias: true } );
            renderer.setSize( width , height );
            container.appendChild( renderer.domElement );

            window.addEventListener( 'resize', onWindowResize, false );


        }

        function addLight( x, y, z, color, intensity ) {

            var directionalLight = new THREE.DirectionalLight( color, intensity );
            directionalLight.position.set( x, y, z )
            scene.add( directionalLight );

        }

       function onWindowResize() {   

            camera.aspect = width / height;
            camera.updateProjectionMatrix();

            renderer.setSize( width, height );
        }

        function animate() {

            requestAnimationFrame( animate );
    controls.update();
            render();

        }

       function render() {

           camera.lookAt( scene.position );
           renderer.render( scene, camera );

       }

    </script>

</body>
</html>

2 个答案:

答案 0 :(得分:28)

大多数控件允许您指定添加事件侦听器的DOM节点。默认情况下,事件处理程序会添加到文档中,但这会将鼠标处理限制在容器元素中:

controls = new THREE.TrackballControls( camera , container);

答案 1 :(得分:4)

建议的答案对我不起作用,但这样做。希望它可以帮助别人!

controls = new THREE.TrackballControls(camera, renderer.domElement);