I am looking to initiate an animated rotation of an object by clicking a button. I have a basic understanding that the render function operates as an infinite loop and that adding 0.1 to cylinder.rotation.x continuously rotates the object. My goal is to trigger this rotation with a button click, rather than having it run automatically. Here is an example of what I have attempted so far:
<html>
<head>
<title>3D Cube</title>
<style>
canvas { width: 100%;
height: 100% }
</style>
</head>
<body>
<script src="three.js"></script>
<script>
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var cylindergeometry = new THREE.CylinderGeometry(0.1, 0.1, 2, 50, false);
var cylindermaterial = new THREE.MeshLambertMaterial({wireframe: true, color: 0x000000});
var cylinder = new THREE.Mesh(cylindergeometry, cylindermaterial);
cylinder.position.set(0,0,0);
scene.add(cylinder);
camera.position.z = 5;
var render = function () {
requestAnimationFrame(render);
cylinder.rotation.x = 0.1;
renderer.render(scene, camera);
};
render();
var btn = document.createElement("BUTTON");
var t = document.createTextNode("CLICK ME");
btn.appendChild(t);
document.body.appendChild(btn);
btn.onclick=function(){
// start animation
// change cylinder.rotation.x = 0.1; to cylinder.rotation.x += 0.1;
};
</script>
</body>
</html>