Canvas 3d JS Library

WebGL made easy!
  • rss
  • What is C3DL?
  • Download
  • Tutorials
    • Tutorial #1: WebGL Browsers
    • Tutorial #2: A simple scene
    • Tutorial #3: Callback
    • Tutorial #4: Models
    • Tutorial #5: Light effects
    • Tutorial #6: Picking
    • Tutorial #7: Materials
    • Tutorial #8: Particle Systems
    • Tutorial #9: Camera Basics
    • Tutorial #10: Advanced FreeCamera
    • Tutorial #11: OrbitCamera
    • Tutorial #12: Advanced Camera Functions
  • Development News
  • Documentation
  • Community
  • Resources
  • Contact
  • About

Namespaces and const

Andor Salga | 12 November, 2008 | 17:41
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 if they are close enough to be considered equal.  Tolerance is global which allows us to easily use it wherever necessary. However, it is likely that users will have their own variable named ‘tolerance’, so it makes sense to just pack our tolerance into the C3DL namespace. This is where the problem started. I couldn’t find the syntax to properly create a const within a namespace.  Hopefully I’m wrong and there is a way to do this, but from what it looks like I’m left with few alternatives.

I could remove the constness from the variable and then I would be able to place it in the C3DL namespace.  It would be up to us to make sure our library does not assign new values to variables which should stay constant such as ‘tolerance’, ‘far_clipping_plane’, ‘near_clipping_plane’, etc. Although if users start using our globals for their own needs, they may assume we made sure it is const and users can end up accidentally overriding it, possibly creating bugs.

We could also just ditch the javascript ‘namespacing’ idea and place C3DL_ in front of everything. That way our variables can stay constant and be unique.  The problem I have with this is that if used with functions, C3DL_popMatrix() is just too awkward.

Last idea I have is to use a hybrid naming system: place all our functions in the C3DL namespace but prepend symbolic constants with C3DL_.  This will allow us to keep the variables constant and unique by name as well as keep the functions unique by namespace.  The only problem I have with this is there is a slight inconsistency, but will users mind?  If we decide to use this method, our function calls will look something like C3DL.popMatrix() and constants would be C3DL_TOLERANCE.  I’m open to suggestions on other ways to fix this issue.
Categories
c3dl development
Comments rss
Comments rss

« DAE Scenegraph Portable Canvas v0.2! »

One response

For name spaces, my suggestions would be: Use c3dl as the

Jeremy Giberson | 13 November, 2008 | 20:18

For name spaces, my suggestions would be:

Use c3dl as the name space, not C3DL. The reasoning being that its faster to type with out worry for caps lock or shift key usage. And its going to be typed a lot when coding.
var c3dl = {};

For constants, just for symbolic reference of them being constant, I would place them under const in the name space.

// etc. capitalization can be all uppers if desired but I would stick with camel cased for multiwords
c3dl.const = {};
c3dl.const.tolerance = 0.0001;
c3dl.const.version = 1.0;
c3dl.const.farPlane = 300.0;
c3dl.const.nearPlane = 100.0;

Move all objects into c3dl, and helper functions into c3dl grouped under an appropriate name space.

var c3dl.model = function() { /* model stuff */ };
var c3dl.vector = { };
c3dl.vector.make = function (x,y,z) { };
c3dl.vector.cross = function (v1, v2) { };
// so on, and so forth

Notice the difference in the way model and vector were defined. model is a function definition because the intention is to use it as object factory. However vector is a grouping of related functions. The reason why vector should be a utility class is to standardize naming conventions for the vector functions. Right now, there are functions named with vector being the first word, vector being the last word and even vector being a middle and last word. Naming schemes should be consistent. Secondly, since you have an actually grouping object you can then generate shortcuts to the utility functions by creating a new variable and assigning to the helper class. So it actually lends to less typing & more clarity.
IE:

// using c3dl classes/functions
var m = new c3dl.model(/* params */);
var resultVector = c3dl.vector.cross(vec1, vec2);

// using shortcuts to make typing less burdensome
var v = c3dl.vector;
// lots of vector work follows
v.add(vec1, v.scale(v2, v.make(0.5, 0.25, 0.75));
// which was much easier to read since 3 function calls could drop the c3dl.vector prefix's.

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Videos

Demos

  • Asteroids-3D
  • RTS Prototype
  • Particle Systems Demo
  • Cross-Browser Orbiter
  • Mocap Demo With Spheres
  • Google Maps-3D

C3DL Development News

A spec change that keeps coming back to haunt me

At some point, the way firefox handles keyboard events changed. I’m not sure exactly when it happened, all I know is that it broke how I was dealing with keyboard interaction on almost every demo I’ve written (for example,the mocap demo and MotionView). When I wrote the demos, the keydown event would be fired once, [...]

Release 2.2

The 2.2 Release of the Canvas 3D Library includes a number of new features, updates to old features and fixes for several bugs along with the requisite changes to meet the evolving WebGL spec. Some of the things included (in no particular order) are: Better picking code. The ability to swap textures as a scene [...]

Tutorials

  • Tutorial #1: WebGL Browsers
  • Tutorial #2: A simple scene
  • Tutorial #3: Callback
  • Tutorial #4: Models
  • Tutorial #5: Light effects
  • Tutorial #6: Picking
  • Tutorial #7: Materials
  • Tutorial #8: Particle Systems
  • Tutorial #9: Camera Basics
    • Tutorial9-YawPitchRoll
  • Tutorial #10: Advanced FreeCamera
  • Tutorial #11: OrbitCamera
  • Tutorial #12: Advanced Camera Functions

Documentation

Archives

Archives

C3DL Development News

Recent Comments

  • June 2011
  • March 2011
  • October 2010
  • July 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • A spec change that keeps coming back to haunt me
  • Release 2.2
  • 2.1 Release and things to come
  • Level Up! An Open Web Game Jam
  • Site moved!
  • SceneCreator0.3
  • WWW2010 in Raleigh
  • Motionview
  • On the train to Mountainview
  • C3DL 2.0-WebGL and beyond
  • That depends on what... - peter
  • This application is ... - Haisens
  • I think that example... - peter
  • The above links are ... - Atash
  • Hi there, just wante... - Patrick H. Lauke
  • Firefox 4 was releas... - Cathy Leung
  • In order to access l... - peter
  • I am not able to dis... - preksha
  • "JavaScript can’t di... - Joe Hocking
  • I should point out t... - peter



Canvas 3d JS Library

©2007- 2010 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