为什么在javascript中没有识别mousedown事件(有三个)?

时间:2015-11-19 14:05:34

标签: javascript html three.js

我有一个简单的THREE.js,我想点击圆形对象,但是当我点击浏览器中的任何地方时,没有任何反应。我跟着this suggestion没有成功。 完整,完整的代码在此处:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Test</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                color: #808080;
                font-family:Monospace;
                font-size:13px;
                text-align:center;

                background-color: #ffffff;
                margin: 0px;
                overflow: hidden;
            }

            #info {
                position: absolute;
                top: 0px; width: 100%;
                padding: 5px;
            }

            a {

                color: #0080ff;
            }

        </style>
    </head>
    <body>

        <div id="container"></div>
        <script src="js/build/three.min.js"></script>

        <script>

            var container, stats;
            var camera, scene, renderer;
            var group1;
            var mouseX = 0, mouseY = 0;
            var map_width = 512;
            var map_height = 512; 

            var windowHalfX = window.innerWidth / 2;
            var windowHalfY = window.innerHeight / 2;

            init();
            animate();

            function createMesh(filename) {           
                var geometry = new THREE.SphereGeometry( 70, 40, 40 );
                geometry.addEventListener('onclick',  onDocumentMouseDown);
                var loader = new THREE.TextureLoader();
                var texture = loader.load(filename);
                var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: 0.5 } );
                var mesh = new THREE.Mesh( geometry, material );
                return mesh;
            }


            function init() {
                container = document.getElementById( 'container' );
                camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
                camera.position.z = 500;
                scene = new THREE.Scene();

                group1 = new THREE.Group();    
                var mesh = createMesh("textures/canvas1.png");
                group1.add( mesh );
                scene.add( group1 );                

                renderer = new THREE.WebGLRenderer({ alpha: true });
                renderer.setClearColor( 0xffffff, 0 );
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                container.appendChild( renderer.domElement );

                document.addEventListener( 'onclick',  onDocumentMouseDown, false);

            }

            function onDocumentMouseDown( event) {
                //event.preventDefault();
                console.log("test");
                window.alert("clicked");
            }

            function animate() {
                requestAnimationFrame( animate );
                render();
            }

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


        </script>

    </body>
</html>

要重现此示例,您只需要一些随机图像和THREE.js库。我没有在控制台中看到任何错误(与此问题相关),也没有在浏览器中看到任何其他控制台输出......

我尝试按照文档(THREE.js)中的描述向geometry.addEventListener个对象之一添加方法,但这也没有做任何事情。

1 个答案:

答案 0 :(得分:1)

document.addEventListener( 'click',  onDocumentMouseDown, false);