0

I have a simple THREE.js and I want to click on the round object, but when I click anywhere in the browser nothing happens. I followed this suggestion without success. The complete, full code is here:

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

To reproduce this example you just need some random image and the THREE.js library. I do neither see any error in the console (related to this problem), nor any other console output during clicking on the browser...

I tried to add a method to one of the THREE.js objects as described in the documentation (geometry.addEventListener), but this also does not do anything.

Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

1
document.addEventListener( 'click',  onDocumentMouseDown, false);
kaigorodov
  • 877
  • 9
  • 15
  • Thanks that works! But I checked the documentation which does not seem to contain the solution: http://www.w3schools.com/jsref/dom_obj_event.asp – Alex Nov 19 '15 at 14:26
  • Is this the wrong documentation? If so, why? Where to find a list of 'real' events...? – Alex Nov 19 '15 at 14:27
  • 1
    **onclick** - is an event name which you should place in DOM. In example `` . In Javascript it's just **click**. – kaigorodov Nov 19 '15 at 14:28