将three.js容器定位为html div

时间:2012-09-25 13:00:52

标签: javascript css html5 three.js

我有一个article容器,里面有三个aside个标签。第三个aside元素id='menuPuzzleType'包含一个Three.js脚本,该脚本生成两个立方体网格物体,并在Canvas渲染器中渲染它们。

<article id="popup">

    <aside id='close'><img src="img/close.png" onClick="aFunction()" /></aside>
    <aside id='msgPuzzleType'>Please choose the puzzle type you want to play</aside>
    <aside id='menuPuzzleType'>
        <script>
        function animate() {
            requestAnimationFrame( animate );
            render();
        }

        function render() {
            cube.rotation.y -= 0.03;
            cube2.rotation.y -=0.03;
            renderer2.render(scene2, camera2);
        }
        var container2 = document.getElementById('menuPuzzleType');

        //renderer
        var renderer2 = new THREE.CanvasRenderer();
        renderer2.setSize(400,400);//Was 100,100
        container2.appendChild(renderer2.domElement);

        //Scene
        var scene2 = new THREE.Scene();

        //Camera
        var camera2 = new THREE.PerspectiveCamera(50,50/50,1,1000);
        camera2.position.z = 240;//normal value is 100
        camera2.position.y=60;
        scene2.add(camera2);

        //Axes
        var axes2= new THREE.AxisHelper();

        //Add texture for the cube
        //Use image as texture
        var img2 = new THREE.MeshBasicMaterial({ //CHANGED to MeshBasicMaterial
            map:THREE.ImageUtils.loadTexture('img/2d.png') 
        });
        img2.map.needsUpdate = true; //ADDED

        //Add Cube
        var cube = new THREE.Mesh(new THREE.CubeGeometry(40,40,40),img2);
        cube.position.x =- 60;
        scene2.add(cube);

        var img3 = new THREE.MeshBasicMaterial({ //CHANGED to MeshBasicMaterial
            map:THREE.ImageUtils.loadTexture('img/3d.png') 
        });

        img3.map.needsUpdate = true; //ADDED

        var cube2 = new THREE.Mesh(new THREE.CubeGeometry(40,40,40),img3);
        cube2.position.x = 70; cube.position.y = 60; cube2.position.y=60;
        scene2.add(cube2);
        renderer2.render(scene2,camera2);

        animate();
        </script>
    </aside>
</article>

我需要将两个多维数据集放入article容器标记中。

这是我的css代码:

body{ overflow:hidden; }

article {
    border:solid 1px #00CC33;
    width:420px;
    height:200px;
    font-family: baskerville; 
    font-size: 16px; 
    margin-top: 50px;

    /*For IE9 compatibility*/
    behavior: url(border-radius.htc);
    border-radius: 8px;
    position:relative;
}

#popup {
    bottom:50%;
    position:relative;
    margin: 0px; 
    left:30%;
}

#msgPuzzleType{ margin-left:60px; }

img{
    behavior: url(border-radius.htc);
    border-radius: 8px;
    position:relative;
}

#menuPuzzleType{
    background-color:#999999;
    border:solid 1px #3300CC;
    height:200px;
}

如何将两个多维数据集移动到article容器标记中?

2 个答案:

答案 0 :(得分:2)

解决问题的“Three.js”方法是:

renderer2.setSize( 400, 150 );

您还需要硬连线PerspectiveCamera的宽高比,因此您需要使其与画布尺寸相匹配。

var camera2 = new THREE.PerspectiveCamera( 50, 400/150, 1, 1000 );

这将使画布更小,因此它适合。然后,您可以将相机移近,以便您的对象具有您想要的大小。

答案 1 :(得分:1)

使用您提供的链接,我更改了以下内容以将多维数据集放入文章中:

canvas {
    position: absolute;
    top: -80px;
}

enter image description here