简体   繁体   English

为什么我测试时我的Three.js代码无效?

[英]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. 我正在尝试Three.js,我一步一步地按照教程。 In the code editor I'm using( Visual Studio Code 2019) everything seems normal, but when I test it, nothing appears on the page. 在我正在使用的代码编辑器(Visual Studio Code 2019)中,一切看起来都很正常,但是当我测试它时,页面上没有任何内容。

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. 我正在使用的编辑器,使用桌面作为查找我的代码的地方,因为它是一个.html文件,我可以运行它。 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 THREE.scene应该是THREE.Scene
  • {antialias = true} should be {antialias: true} {antialias = true}应为{antialias: true}
  • { color = 0xff0000 } should be { color: 0xff0000 } { color = 0xff0000 }应为{ color: 0xff0000 }

Live demo with your code: https://jsfiddle.net/so736vxj/ 使用您的代码进行现场演示: https//jsfiddle.net/so736vxj/

BTW: If you are using VSCode, I'm a bit irritated that no errors are highlighted. BTW:如果你正在使用VSCode,我有点恼火,没有突出显示任何错误。 Especially the last two syntax errors should be reported since it is no valid JavaScript. 特别是应该报告最后两个语法错误,因为它不是有效的JavaScript。

three.js R105

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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