This is the basic HTML file that can be used as a boilerplate when starting a project. This boilerplate uses orbit controls with damping (camera that can move around an object with deceleration effect) and creates a spinning cube.
<!DOCTYPE html>
<html>
<head>
<title>Three.js Boilerplate</title>
<!--This is important to get a correct canvas size on mobile-->
<meta name='viewport' content='width=device-width, user-scalable=no'/>
<style>
body{
margin:0;
overflow:hidden;
}
/*
Next 2 paragraphs are a good practice.
In IE/Edge you have to provide the cursor images.
*/
canvas{
cursor:grab;
cursor:-webkit-grab;
cursor:-moz-grab;
}
canvas:active{
cursor:grabbing;
cursor:-webkit-grabbing;
cursor:-moz-grabbing;
}
</style>
</head>
<body>
<script src='three.js/build/three.js'></script>
<script src='three.js/examples/js/controls/OrbitControls.js'></script>
<script>
var scene, renderer, camera, controls, cube;
init();
function init () {
renderer = new THREE.WebGLRenderer();
//this is to get the correct pixel detail on portable devices
renderer.setPixelRatio( window.devicePixelRatio );
//and this sets the canvas' size.
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
70, //FOV
window.innerWidth / window.innerHeight, //aspect
1, //near clipping plane
100 //far clipping plane
);
camera.position.set( 1, 3, 5 );
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.rotateSpeed = .07;
controls.enableDamping = true;
controls.dampingFactor = .05;
window.addEventListener( 'resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}, false );
cube = new THREE.Mesh(
new THREE.BoxGeometry( 1, 1, 1 ),
new THREE.MeshBasicMaterial()
);
scene.add( cube );
animate();
}
function animate () {
requestAnimationFrame( animate );
controls.update();
renderer.render( scene, camera );
cube.rotation.x += 0.01;
}
</script>
</body>
</html>