Canvas 3d JS Library

where 3D is born!
  • rss
  • What is C3DL?
  • Download
  • Tutorials
    • Tutorial #1: Installing Canvas 3D Addon
    • Tutorial #2: A Scene and a Cube
    • Tutorial #3: Update Callback
    • Tutorial #4: Models 101
  • Development News
  • Demos
    • Typing Game V2.1
    • Typing Game V3 (0.3 Release)
    • Explorer
    • Flickr - Picking
    • Ricochet
    • FSOSS Pictures
    • Puzzler
  • Resources
  • Contact
  • About

Tutorial #2: A Scene and a Cube

Before we begin

This tutorial will demonstrate the basics of using the Canvas 3D JS Libary. Before you begin this tutorial, make sure you have a browser with the canvas 3d addon installed and enabled. Tutorial #1 describes how to do this. Download the canvas 3d JS api. It is recommended that you download the latest release as it will likely contain the latest changes and updates.

Where to put the api

The canvas3d JS API is a javascript library that will make it easier to create and manage 3d content using Canvas 3D.  To use it as part of your website, simply expand the zip file and place the entire folder on your website’s host where the rest of your site’s content sits.  If you wish to try it out locally simply unzip it some place on your local machine.

Your first app

Inside the canvas3d JS API folder, create a javascript file named Main.js with the following code. The explanation of the code can be found later in this tutorial:
function canvasMain(canvasName){
  var scn = new Scene();
  if(scn.createScene(canvasName)){
    var n= new Cube();
    var cam = new FreeCamera();
    cam.setPosition(new Array(5.0, 0.0, -5.0));
    cam.setLookAtPoint(new Array(-5.0, 0.0, 5.0));
    scn.addObjectToScene(n);
    scn.addCameraToScene(cam);
    scn.startScene();
  }
}
Next, create a web page containing the following code:
<html>
<head>
<title>Cavas3D test</title>
<script type="application/javascript">var SCRIPT_PATH = ''</script>
<script type="application/javascript" src="C3DAPI.js" ></script>
<script type="application/javascript"  src="Main.js"></script>
</head>
<body onload="canvasMain('canvas');">
<canvas id="canvas" style="border: 2px solid blue" width="500" height="500">
</canvas>
</body>
</html>
Opening the web page from a canvas 3d enabled browser should have the following result:
t2screenshot.png

3D Coordinates

Before we begin to look at the code it is important to look at the 3d coordinate system. open gl is a right handed coordinate system. Some 3d systems are left handed (directX for example). By right handed coordinate system, we mean that the positive x points to the right of the screen, positive y points to the top of the screen and positive z points out of the screen. It is called the right handed system because you can use your right hand to remember which way is positive z.

Code Explanation

//the function that will be called by the web page canvasName is the name
//of the canvas where the scene will show.
function canvasMain(canvasName){
  //create a new Scene object
  var scn = new Scene(); //check that it was successful
  if(scn.createScene(canvasName)){
    //create a cube. by default, this cube is centred at 0,0,0
    var n= new Cube();
    //create a camera
    var cam = new FreeCamera();
    //set the camera position (behind back right corner of cube)
    cam.setPosition(new Array(5.0, 0.0, -5.0));
    // make the camera look at the cube (look towards the front left corner)
    cam.setLookAtPoint(new Array(-5.0, 0.0, 5.0));
    //Add the object to the scene
    scn.addObjectToScene(n);
    //add the camera to the scene
    scn.addCameraToScene(cam);
    //start the scene
    scn.startScene();
  }
}
<html>
<head>
<title>Cavas3D test</title>

<!-- set up the script inside a web page. The next line
  of code is used to set up a search path for the api. For our simple
  project we are putting the html source inside the library directory
  and thus the search path is empty. For more complex projects, you
  may wish to separate the project code into a different directory.
  In that case, the SCRIPT_PATH should be set as a relative directory to
  the api-->
<script type="application/javascript">var SCRIPT_PATH = ''</script>

<!--Load the js library files -->
<script type="application/javascript" src="C3DAPI.js" ></script>

<!--Load our main file. If you called it something else, simply
  substitute your file name here-->
<script type="application/javascript" src="Main.js"></script>

</head>

<!-- when this page's body gets loaded, the canvasMain() function
  from Main.js will get called-->
<body onload="canvasMain('canvas');">

<!-- the actual canvas. Note the id of this canvas is "canvas" which is
  what we are passing to canvasMain()-->
<canvas id="canvas" style="border: 2px solid blue" width="500" height="500">
</canvas>
</body>
</html>

Search

Demos

  • Explorer
  • Flickr - Picking
  • FSOSS Pictures
  • Puzzler
  • Ricochet
  • Typing Game V2.1
  • Typing Game V3 (0.3 Release)

C3DL Development News

Portable Canvas v0.2!

Portable Canvas version 0.2 is available!!

It is now a usable browser, but restricted to pages on c3dl.org only. It can display canvas elements right out of the box, and doesn’t require anything else to be on your system (not even FF3!)*. It’s only been tested on Windows XP, but in theory it should be easy [...]

Namespaces and const

As our library expands with more functions, classes and global variables, the need for namespacing increases.  I started placing code in a C3DL namespace when I wrote the matrix stack operations.  However, yesterday I was looking in the constants.js file and saw the ‘tolerance’ variable.  It’s a const variable used when comparing floats to check [...]

Tutorials

  • Tutorial #1: Installing Canvas 3D Addon
  • Tutorial #2: A Scene and a Cube
  • Tutorial #3: Update Callback
  • Tutorial #4: Models 101

Archives

C3DL Development News

Recent Comments

  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • Portable Canvas v0.2!
  • Namespaces and const
  • DAE Scenegraph
  • The Matrix Stack
  • .obj to .dae
  • 0.5 Release and Other News!
  • Tracemonkey performance
  • More memory usage improvements
  • Canvas3D crashes in tracemonkey
  • Patched one hole in floating text
  • For name spaces, my... - Jeremy Giberson
  • Great! Really really... - Edson Mattos
  • Beautiful!... - Funtomas
  • Was no need to conta... - Andrew Smith
  • Andrew, have you con... - Funtomas
  • Thanks for posting t... - Andrew Smith
  • Vlad was in town and... - Cathy Leung
  • the upside down issu... - Cathy Leung
  • Got it! the addon is... - Bill Mill
  • Yes it does. The ad... - Cathy Leung



Canvas 3d JS Library

©2007- 2008 Canvas 3d JS Library

Disclaimer: This website is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Canada License.
The Canvas 3d JS Library and Demos found on this website are licenced under the MIT License

Creative Commons License