将div.js动画放入div中

时间:2013-07-06 22:01:54

标签: html5-canvas three.js

这是three.js动画代码示例:

<script defer="defer">
  var angularSpeed = 0.2; 
  var lastTime = 0;
  function animate(){
    var time = (new Date()).getTime();
    var timeDiff = time - lastTime;
    var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;
    plane.rotation.z += angleChange;
    lastTime = time;
    renderer.render(scene, camera);
    requestAnimationFrame(function(){
        animate();
    });
  }
  var renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);
  var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
  camera.position.y = -450;
  camera.position.z = 400;
  camera.rotation.x = 45 * (Math.PI / 180);
  var scene = new THREE.Scene();
  var plane = new THREE.Mesh(new THREE.PlaneGeometry(300, 300), new THREE.MeshNormalMaterial());
  plane.overdraw = true;
  scene.add(plane);
  animate();
 </script>

我想通过div id将这个动画代码绑定到某个特定的div。因此,动画将显示在div内部。这将允许我使用css轻松操纵动画位置。我记得这样的事情:

HTML:

<canvas id="myCanvas" width="578" height="200"></canvas>

javascript:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
// do stuff here

但我还没有办法用three.js做这样的事情。正如您在代码示例中所看到的,我没有可以引用的canvas元素。有什么想法吗?

3 个答案:

答案 0 :(得分:21)

您可以使用以下模式:

<div id="canvas">

#canvas {
    background-color: #000;
    width: 200px;
    height: 200px;
    border: 1px solid black;
    margin: 100px;
    padding: 0px;
    position: static; /* fixed or static */
    top: 100px;
    left: 100px;
}

container = document.getElementById( 'canvas' );
document.body.appendChild( container );

renderer = new THREE.WebGLRenderer();
renderer.setSize( 200, 200 );
container.appendChild( renderer.domElement );

小提琴:http://jsfiddle.net/fek9ddg5/1/

另见THREE.js Ray Intersect fails by adding div

three.js r.69

答案 1 :(得分:1)

如果您不想对渲染器大小进行硬编码,那么下面的代码可以帮助您。

我正在动态计算画布的高度和宽度,然后相应地设置渲染器的大小和相机的位置。

CSS

            #canvas {
             background-color: #FFF;
             width: 200px;
             height:200px;
             border: 1px solid black;
              }

HTML

<div id="canvas"/>

的JavaScript

         //Add Renderer
         renderer = new THREE.CanvasRenderer();
         var container = document.getElementById('canvas');
         var w = container.offsetWidth;
         var h = container.offsetHeight;
         renderer.setSize(w, h);
         container.appendChild(renderer.domElement);

        //Add Camera
        camera = new THREE.PerspectiveCamera( 75, w / h, 2, 1000 );
        camera.position.z = 400;

        //Create Scene with geometry, material-> mesh
        scene = new THREE.Scene();
        geometry = new THREE.IcosahedronGeometry( 200, 3 );
        material = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, wireframeLinewidth: 1 } );
        mesh = new THREE.Mesh( geometry, material );
        scene.add( mesh );

答案 2 :(得分:0)

这是WestLangley显示的模式的另一种变体:

HTML:

<div class="relative h-256 w-256">
    <div id="scene"></div>
</div>

CSS:

#scene {
    overflow: hidden;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.relative {
    position: relative;
}

.h-256 {
    height: 256px;
}

.w-256 {
    width: 256px;
}

JavaScript:

this.mount = document.querySelector('#scene');

this.width = document.querySelector('#scene').offsetWidth;
this.height = document.querySelector('#scene').offsetHeight;

...

this.renderer = new THREE.WebGLRenderer({ alpha: true });

this.renderer.setPixelRatio(window.devicePixelRatio);

this.renderer.setSize(this.width, this.height);

this.mount.appendChild(this.renderer.domElement);

Three.JS在<canvas>容器中呈现div#scene元素,该元素的位置绝对是要填充包装器。大小由包装器div的尺寸控制。