12

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>
4

2 回答 2

29

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

controls = new THREE.TrackballControls( camera , container);
于 2012-12-02T16:26:19.300 回答
4

建议的答案对我不起作用,但这确实有效。希望它可以帮助某人!

controls = new THREE.TrackballControls(camera, renderer.domElement);
于 2014-05-15T13:10:41.913 回答