I've successfully created a 3D wireframe cube using Three.js examples, however, it's continuously rotating,
I'm looking to halt this animation.
When I remove the animate();
function at the end of the script, the Canvas fails to load.
// start animation
animate();
</script>
Trying to remove the animate();
function from :
// request new frame
requestAnimationFrame(function(){
animate();
});
The rotation stops but the shape distorts strangely, not resembling the cube. Moreover, with each refresh, the shape changes.
Below is my code:
Javascript:
<script defer="defer">
// revolutions per second
var angularSpeed = 0.2;
var lastTime = 0;
// this function runs on every 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 container = document.getElementById("container");
var renderer = new THREE.WebGLRenderer();
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);
// camera
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 700;
// scene
var scene = new THREE.Scene();
// cube Length, Height, Width
var cube = new THREE.Mesh(new THREE.CubeGeometry(400, 200, 200), new THREE.MeshBasicMaterial({
wireframe: true,
color: '#ff0000'
}));
cube.rotation.x = Math.PI * 0.1;
scene.add(cube);
// start animation
animate();
</script>
Fiddle link for reference: http://jsfiddle.net/UsEDt/1/
If you require additional information, feel free to ask.
Please advise on how to proceed.