Threejs canvas不会调整为tag div

时间:2014-12-31 15:38:19

标签: javascript html css canvas three.js

我已将我的标签画布放入div中,用于Bootstrap布局,但画布比div容器大,并且它不会将自身调整为div大小。我无法理解是什么原因。 实例:http://www.felpone.netsons.org/web-threejs/

<div class="row">


<div class="col-sm-8" style="background-color:lavenderblush;" id="ThreeJS" data-toggle="context" data-target="#context-menu"></div>



<div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div>



</div>



#ThreeJS canvas {

position: static!important;
padding: 1px!important;
margin: 1px!important;
background-color: rgba(188, 188, 188, 0.44)!important;

 }

这是我的threejs代码:

var SCREEN_WIDTH = window.innerWidth-5, SCREEN_HEIGHT = window.innerHeight-5;

    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.01, FAR = 20000;
    camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
    var r = 4, phi = Math.PI/4, theta = Math.PI/4;
    camera.position.set(r*Math.cos(phi)*Math.sin(theta),r*Math.sin(phi), r*Math.cos(phi)*Math.cos(theta));

    // RENDERER
    if ( Detector.webgl )
            renderer = new THREE.WebGLRenderer( {antialias:true} );
    else
            renderer = new THREE.CanvasRenderer(); 
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    renderer.autoClear = false;

    // CONTAINER
    container = document.getElementById( 'ThreeJS' );
    container.appendChild( renderer.domElement );

    // EVENTS
    THREEx.WindowResize(renderer, camera);
    THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });

    // CONTROLS
    controls = new THREE.OrbitControls( camera, renderer.domElement );
    controls.addEventListener( 'change', render );
    controls.target = new THREE.Vector3(0, 0, 0);

1 个答案:

答案 0 :(得分:2)

您可以在(window.innerWidth-5)/(window.innerHeight-5)设置渲染器大小(=画布),几乎是整页。首先在containerSCREEN_WIDTH之前定义SCREEN_HEIGHT,然后将其替换为:

var containerStyle = getComputedStyle(container,null);
var SCREEN_HEIGHT = parseInt(containerStyle.getPropertyValue('height')),
    SCREEN_WIDTH = parseInt(containerStyle.getPropertyValue('width'));

THREEx.WindowResize函数中相同:找到定义高度和宽度的位置,并用这些值替换它们。