drawCube(x, y, col)
This function demonstrates isometric 3D drawing: creating the illusion of depth using 2D quadrilaterals and shading. The translate(0, camY) line is the key to camera following—every cube shifts by the same amount, making the view scroll smoothly. lerpColor is used to darken faces, teaching color interpolation as a tool for visual depth.
🔬 These two quad() calls create the top and left faces. What if you swap the fill colors—put the bright color on the left face and the dark shade on the top? How does the 3D effect flip?
// Top Face
fill(col);
quad(x, y, x + 30, y - 15, x + 60, y, x + 30, y + 15);
// Left Face
fill(lerpColor(col, color(0), 0.2));
quad(x, y, x + 30, y + 15, x + 30, y + 45, x, y + 30);
var drawCube = function(x, y, col) {
push(); // Use push() and pop() for 2D drawing state
translate(0, camY); // Move everything based on camera
// Top Face
fill(col);
quad(x, y, x + 30, y - 15, x + 60, y, x + 30, y + 15);
// Left Face
fill(lerpColor(col, color(0), 0.2));
quad(x, y, x + 30, y + 15, x + 30, y + 45, x, y + 30);
// Right Face
fill(lerpColor(col, color(0), 0.4));
quad(x + 30, y + 15, x + 60, y, x + 60, y + 30, x + 30, y + 45);
pop(); // Restore drawing state
};
Line-by-line explanation (9 lines)
🔧 Subcomponents:
translate(0, camY);
Offsets all cube drawing by the camera's Y position so the canvas scrolls up as towers grow
quad(x, y, x + 30, y - 15, x + 60, y, x + 30, y + 15);
Draws the top-facing surface of the cube in the original color for maximum brightness
fill(lerpColor(col, color(0), 0.2)); quad(x, y, x + 30, y + 15, x + 30, y + 45, x, y + 30);
Draws the left side slightly darkened with lerpColor to create depth perception
fill(lerpColor(col, color(0), 0.4)); quad(x + 30, y + 15, x + 60, y, x + 60, y + 30, x + 30, y + 45);
Draws the right side darker than the left to enhance the 3D isometric illusion
push();- Saves the current drawing state so transformations don't affect other code
translate(0, camY);- Shifts all drawing down by camY pixels—this creates the illusion that the camera is moving up as the tower grows
fill(col);- Sets the fill color to the passed-in color (col) for the top face
quad(x, y, x + 30, y - 15, x + 60, y, x + 30, y + 15);- Draws a quadrilateral (4-sided polygon) for the top of the cube in bright color—the four corners create a diamond shape
fill(lerpColor(col, color(0), 0.2));- lerpColor interpolates between the original color and black at 20%—darkening it slightly for the left face
quad(x, y, x + 30, y + 15, x + 30, y + 45, x, y + 30);- Draws the left side of the cube using the darker shade to make it recede visually
fill(lerpColor(col, color(0), 0.4));- Darkens the color to 40% toward black, making the right face darker than the left for extra depth
quad(x + 30, y + 15, x + 60, y, x + 60, y + 30, x + 30, y + 45);- Draws the right side of the cube in the darkest shade
pop();- Restores the drawing state so the translate(0, camY) doesn't affect any code outside this function