无法从HTML文件中执行JS功能

时间:2022-06-19 16:03:58

For some reason with this code it will not call 'createScene();' in index.html. I'm probably overlooking something very simple as I'm new to JS but I haven't been able to find anything.

由于某些原因,这段代码不会调用'createScene();'在index.html中。我可能会忽略一些非常简单的事情,因为我是JS的新手,但我找不到任何东西。

index.html

<html>
<head>
    <title>Pallidity</title>

    <script type = "text/javascript" src="Javascript/scene.js"></script> 

    <style type ="text/css">
    BODY
    {
        Margin: 0;
    }

    canvas
    {
        width: 100%;
        height:100%;
    }
    </style>
</head>
<body>

<script type="text/javascript">
    createScene();
</script>


</body>

scene.js:

function createScene(){
       <script src="Libraries/three.min.js"></script>
        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
        var renderer = new THREE.WebGLRenderer();
        renderer.setSize( window.innerWidth, window.innerHeight );
        document.body.appendChild( renderer.domElement );
        //Bare minmum to render


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

        camera.position.z = 5;

        var render = function () {
            requestAnimationFrame( render );

            cube.rotation.x += 0.1;
            cube.rotation.y += 0.1;

            renderer.render(scene, camera);
        };

        render();
}

1 个解决方案

#1


You have an html element (a script tag) as the first line of you createScene function. This is not valid JavaScript, therefore that function essentially does not exist. That's why it is not defined.

您有一个html元素(脚本标记)作为createScene函数的第一行。这不是有效的JavaScript,因此该函数基本上不存在。这就是为什么它没有定义。

You probably want this line in your HTML file right before your scene.js script tag.

您可能希望在您的scene.js脚本标记之前的HTML文件中使用此行。

#1


You have an html element (a script tag) as the first line of you createScene function. This is not valid JavaScript, therefore that function essentially does not exist. That's why it is not defined.

您有一个html元素(脚本标记)作为createScene函数的第一行。这不是有效的JavaScript,因此该函数基本上不存在。这就是为什么它没有定义。

You probably want this line in your HTML file right before your scene.js script tag.

您可能希望在您的scene.js脚本标记之前的HTML文件中使用此行。