Check out this Code:
I have a dynamic "object" created in Canvas using Three.js, but it's currently appearing at the bottom of the page, just before the closing </body>
tag. How can I adjust its position so that it loads inside the "
<div id="container"></div>
" div instead?
Take a look at my code:
HTML:
<div id="container"></div>
JS
// revolutions per second
var angularSpeed = 0.2;
var lastTime = 0;
// function executed on each animation frame
function animate() {
// update
var time = (new Date()).getTime();
var timeDiff = time - lastTime;
var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;
cube.rotation.y += angleChange;
lastTime = time;
// render
renderer.render(scene, camera);
// request new frame
requestAnimationFrame(function () {
animate();
});
}
// renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("container").appendChild(renderer.domElement); // add renderer to container
// camera
var camera = new THREE.PerspectiveCamera(120, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 700;
// scene
var scene = new THREE.Scene();
// create cube (Length, Height, Width)
var cube = new THREE.Mesh(new THREE.CubeGeometry(400, 200, 200), new THREE.MeshNormalMaterial());
scene.add(cube);
// start animation
animate();
FIDDLE for reference: http://jsfiddle.net/6a7u4/1/
If you require more information or have any suggestions, feel free to let me know.
Please share your thoughts.