<script>

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera ( 40,( window.innerWidth)/(window.innerHeight), 0.1, 1000 );

var renderer1 = new THREE.WebGLRenderer();
renderer1.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer1.domElement );

var geometry = new THREE.SphereGeometry(0.5, 32, 32);
var materialGreen = new THREE.MeshBasicMaterial( { color: 0x0ad818, wireframe: true, wireframeLinewidth: 3 } );
var materialRed = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true, wireframeLinewidth: 3 } );

var ball01 = new THREE.Mesh(geometry, materialGreen);
scene.add(ball01);
ball01.position.set( -0.75,0, 0 )

var ball02 = new THREE.Mesh(geometry, materialRed);
scene.add(ball02);
ball02.position.set( 0.75,0, 0 )

var timer = 0;

var render = function () {
requestAnimationFrame( render );

timer = timer + 0.004;
camera.position.x = math.cos( timer ) * 2;
camera.position.z = math.sin( timer ) * 2;
ball02.rotation.y += 0.001;
ball01.rotation.x += 0.001;
camera.lookAt( scene.position );

renderer1.render(scene, camera);
};
render();

/<script>

Click Image for Full Page view

Script tags




three.js goes in between script tags :

Did not know if this page is necessary but it is here for the newbie just in case.

Three.js is JavaScript and so must be placed between script tags (refer to html tutorial for more info on script tags if you need to).

The remainder of the code we will be writing will all go between these script tags.

Again the location of JavaScript within a web page can be argued but for this tutorial it is arbitrary.

For clarity we shall place our script tags in between the body tags of our web page / note that for further clarity we are also deleting the Hello! paragraph as it is not needed.

Copy the following code and paste it into your editor.


        <script>
                    // the rest of our code will go here
        </script> 
         

Once done your code should look like the following.



<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
	    <title>Three.js Tutorial</title>

<script 
src="https://ajax.googleapis.com/ajax/libs/threejs/r84/three.min.js">
</script>	    

	        <style>
		    body { margin: 0; }
		    canvas { width: 100%; height: 100% }
		</style>
   </head>

       <body>
       
	 	<script>
                          // the rest of our code will go here
              </script> 
              
	</body>
</html>
          

notepad5

We are now ready to start on our animation.

An image of a three.js Torus Knot

Share links
Google+ share link
Twitter share link
LinkedIn share link
FaceBook share link