简体   繁体   English

Three.js示例不在画布上显示

[英]Three.js example not displaying on canvas

i found an example of three.js and I am trying to implement it on a <canvas></canvas> element. 我找到了three.js的示例,并尝试在<canvas></canvas>元素上实现它。 I get a reference to the element but I dont get a visual. 我获得了对该元素的引用,但没有视觉效果。 I have my canvas element with the Id of "mycanvas". 我的画布元素带有“ mycanvas”的ID。

<canvas id="mycanvas" style="border: 5px solid white" width="600" height="500"></canvas>

an I use an onload function in the body called WebGLStart() which calls the script below. 我在称为WebGLStart()的主体中使用onload函数,该函数调用以下脚本。

   <script>
        function WebGLStart()
        {
            //Get the canvas and the context
            var canvas = document.getElementById('mycanvas');
            var canvas_context = canvas.getContext("webgl");
            console.log("Canvas: "+canvas.width);
            console.log("Canvas: "+canvas.height);

            var RENDER_DIST = 1000,
                FOV = 75;

            var WIDTH = canvas.width,
                HEIGHT= canvas.height;

            var scene = new THREE.Scene();


            var camera = new THREE.PerspectiveCamera(FOV, WIDTH / HEIGHT, 0.1, RENDER_DIST);

            camera.position.z = 100;

            scene.add(camera);

            renderer = new THREE.WebGLRenderer();
            renderer.setSize(WIDTH,HEIGHT); 
            console.log("R info: "+renderer.info);

            canvas.appendChild(renderer.domElement);

            init();
            loopRun();

            function init() 
            {
                var geometry = new THREE.SphereGeometry(50); 
                var material = new THREE.MeshBasicMaterial({color: 0xff0000}); 
                var sphere = new THREE.Mesh(geometry, material); 
                scene.add(sphere);
            }

            function loopRun() 
            {
                requestAnimationFrame(loopRun);
                renderer.render(scene, camera);
            }
        }
    </script>

Is there any reason why this would not display? 有什么原因不能显示? I get outputs on chromes prompt(canvas width and height) but no display. 我在chromes提示符下获得输出(画布的宽度和高度),但没有显示。 any ideas would be appreciated 任何想法,将不胜感激

Child elements of the canvas element are not visible. canvas元素的子元素不可见。

To solve this attach the renderer DOM element to some other DOM element, eg document body. 为了解决这个问题,请将渲染器DOM元素附加到其他DOM元素,例如文档主体。

if you change this line: 如果您更改此行:

canvas.appendChild(renderer.domElement);

to this: 对此:

document.body.appendChild(renderer.domElement);

You'll get the desired result. 您会得到理想的结果。

Tade0 was right in the above answer! Tade0在上面的答案中是对的! This is for people who like me had a <canvas></canvas> tags and now have to remove it. 这适用于像我这样具有<canvas></canvas>标记但现在必须将其删除的人。
The way I did it was first: 我的操作方式是:
Implement a style tag and call your class something unique: 实现一个样式标签,并给您的班级添加一些独特的名称:

<style>

    canvas_for_three { 
          width: 600px; 
          height: 500px;
          border: 1px solid white;
          position: absolute;
          left: 0px;
          top: 0px;
          z-index: -1;
    }
  </style>

Next remove your <canvas></canvas> tags and replace them with: 接下来,删除您的<canvas></canvas>标签,并将其替换为:

<div id="ctx" class="canvas_for_three">
</div>

From here then on in your onload function use the 从这里开始,然后在onload函数中使用
var canvas = document.getElementById('ctx');
So when your attaching the render to a DOM element it is now: 因此,当您将渲染附加到DOM元素时,它现在是:

canvas.appendChild(renderer.domElement); 

Where canvas is your newly created canvas. 画布是您新创建的画布。 This will replace the canvas tags. 这将替换canvas标签。

Now you should have replaced your canvas tags with divs and the image viewport should be in the same position as where you had your <canvas></canvas> tags 现在,您应该已经用div替换了画布标签,并且图像视口应该与您拥有<canvas></canvas>标签的位置相同

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

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