Tutorial & Videos

The main concept behind 3D is the existence of another dimension beyond the x and y axes. This is the dimension of depth, and it is usually labeled z. The canvas element does not have a built-in z dimension, but it isn’t too difficult to create one with JavaScript. And it’s actually less complex than a lot of the stuff you did in the previous chapters! The z axis To begin with, you must decide which direction the z-axis will go: in or out. If you recall back to the description of the canvas coordinate system in Chapter 3, it is in some ways opposite to most other commonly used coordinate systems. The y axis goes down instead of up, and angles are measured clockwise instead of counterclockwise. If an object’s z position increases, is it going away from you or toward you on the z-axis? Neither way is necessarily more correct than the other. In fact, this subject has been addressed enough times that there are even names to describe the two methods: left-hand system and right-hand system. Take either hand, and point the fingers in the direction of the positive x-axis and curl them toward the positive y-axis. The direction your thumb points, either toward you or away from you, is the direction that the positive z-axis points for that coordinate system. So, if you take your right-hand, point it away from you following the positive x-axis, and then curl the fingers to the ground, toward the positive y-axis, your thumb will point in front of you—the positive z-axis for the right-handed coordinate system. So in code, this means the z-axis increases as it goes away from the viewer, and decreases as it goes toward the viewer, as shown in Figure 15-1.

Figure 15-1. Right-hand coordinate system
If you try it with your left hand, you get the opposite result—your thumb is pointing behind you. Figure 15-2
shows the left-hand coordinate system.

Figure 15-2. Left-hand coordinate system
We use the right-hand coordinate system (refer to Figure 15-1) for the examples here, so as an object’s z position increases, it moves further in front of us. But that is just the preference of this book, there’s no reason that you couldn’t make a left-hand system.
The next step in creating a third (z) dimension is to figure out how to simulate perspective.

Comments on: "The third dimension and perspective" (4)

  1. greetings, really good site, and a fairly good understand! at least one for my book marks.

  2. Sweet post, nice blog design and style, keep up the great work

  3. Bookmarked, I enjoy your site! 🙂

  4. Major follower of this website, numerous your blogposts have seriously helped me out. Looking forward to upgrades!

Leave a comment