简体   繁体   中英

Why my Three.js code isn't working when i test it?

I'm trying out Three.js, I followed a tutorial step-by-step. In the code editor I'm using( Visual Studio Code 2019) everything seems normal, but when I test it, nothing appears on the page.

the editor I'm using, used the desktop as the place to locate my code, since it is a .html file I could run it. When I did that, the only thing that appeared was the navbar I programmed, nothing else

This is the entire three.js code:
<script src="three.js-dev/build/three.min.js"> </script>
        <script>
            var scene = new THREE.scene();
            var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight);

            var renderer = new THREE.WebGLRenderer({antialias = true});
            renderer.setSize( window.innerWidth, window.innerHeight);
            $('body').append( renderer.domElement);

            var geometry = new THREE.BoxGeometry( 1, 1, 1 );
            var material = new THREE.MeshBasicMaterial ( { color = 0xff0000 });
            var cube = new THREE.Mesh( geometry, material) ;
            scene.add( cube );

            cube.position.z = -5;
            var animate = function () {

             cube.rotation.x += 0.01;

                renderer.render(scene, camera);
                requestAnimationFrame( animate );
            };
            animate();

and this the code before it:

<html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <script 
        src="https://code.jquery.com/jquery-2.1.4.js"
        integrity="sha256-siFczlgw4jULnUICcdm9gjQPZkw/YPDqhQ9+nAOScE4="
        crossorigin="anonymous"></script>
        </head>
        <body>
        <div id="navbar"><span>Three.js Tut</span></div>

I expected a red cube rotating, but nothing appeared. ¿Is there something I'm doing wrong?

You have a few errors in your code:

  • THREE.scene should be THREE.Scene
  • {antialias = true} should be {antialias: true}
  • { color = 0xff0000 } should be { color: 0xff0000 }

Live demo with your code: https://jsfiddle.net/so736vxj/

BTW: If you are using VSCode, I'm a bit irritated that no errors are highlighted. Especially the last two syntax errors should be reported since it is no valid JavaScript.

three.js R105

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM