Drawing, Animation, and Color
Summary
This chapter expands your p5.js skills with drawing primitives, text rendering, and color theory. You will learn to use stroke() for outlines, rect() for rectangles, ellipse() for circles and ovals, and line() for connecting points. The chapter covers text display with text(), textSize(), and textAlign() functions, and explores shape rendering techniques. You will also develop a deeper understanding of color theory and how to apply it effectively in your simulations.
Concepts Covered
This chapter covers the following 10 concepts from the learning graph:
- stroke()
- rect()
- ellipse()
- line()
- text()
- textSize()
- textAlign()
- Drawing Primitives
- Shape Rendering
- Color Theory
Prerequisites
This chapter builds on concepts from:
Welcome to the Most Colorful Chapter!
Get ready to paint with code! This chapter is where your MicroSims burst into life with vibrant colors, elegant shapes, and expressive text. By the time you finish, you'll have the skills to create your own personal rainbow MicroSims—interactive visualizations that dance with color and respond to your every input.
In p5.js, everything you see on the canvas is built from simple building blocks called drawing primitives. Just as an artist might use basic strokes to create a masterpiece, you'll combine rectangles, ellipses, lines, and text to craft educational simulations that are both beautiful and functional.
The Joy of Color
Color isn't just decoration—it's communication. The right colors can highlight important data, guide attention, create mood, and make learning more engaging. This chapter gives you complete control over the rainbow!
Drawing Primitives: Your Visual Vocabulary
Drawing primitives are the fundamental shapes that form the foundation of all visual output in p5.js. Think of them as the alphabet of visual programming—simple on their own, but capable of creating infinite combinations when used together.
The core drawing primitives in p5.js include:
- Rectangles (
rect()) - Four-sided shapes with right angles - Ellipses (
ellipse()) - Circles and ovals - Lines (
line()) - Straight connections between two points - Points (
point()) - Single pixels - Triangles (
triangle()) - Three-sided shapes - Arcs (
arc()) - Portions of ellipses - Quadrilaterals (
quad()) - Four-sided shapes with any angles
For MicroSims, we use rectangles, ellipses, and lines most frequently. Let's master each one!
Rectangles with rect()
The rect() function draws rectangles—the workhorses of user interface design. Every button, slider background, control panel, and data display in our MicroSims uses rectangles.
Basic Rectangle Syntax
1 | |
| Parameter | Description |
|---|---|
x |
X-coordinate of the top-left corner |
y |
Y-coordinate of the top-left corner |
width |
Width of the rectangle in pixels |
height |
Height of the rectangle in pixels |
Rectangle Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Rounded Corners
Adding a fifth parameter creates rounded corners—perfect for modern, friendly-looking buttons:
1 2 | |
Rectangle Mode
By default, rect() uses the top-left corner as the reference point. You can change this with rectMode(CENTER) to draw rectangles from their center—useful for centering shapes easily.
Ellipses with ellipse()
The ellipse() function draws circles and ovals. While rectangles feel structured and mechanical, ellipses add organic, flowing elements to your designs.
Basic Ellipse Syntax
1 | |
| Parameter | Description |
|---|---|
x |
X-coordinate of the center |
y |
Y-coordinate of the center |
width |
Width (horizontal diameter) |
height |
Height (vertical diameter) |
Note that unlike rect(), the ellipse() function draws from the center by default!
Circle Shorthand
When width equals height, you get a perfect circle. p5.js also provides a convenient circle() function:
1 2 3 | |
Colorful Ellipse Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Lines with line()
The line() function draws straight lines between two points. Lines are essential for connecting elements, drawing axes, creating grids, and showing relationships.
Basic Line Syntax
1 | |
| Parameter | Description |
|---|---|
x1, y1 |
Starting point coordinates |
x2, y2 |
Ending point coordinates |
Line Style Control
Lines are styled using stroke() (color) and strokeWeight() (thickness):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
The stroke() Function: Outlining Your Shapes
The stroke() function sets the color for lines and shape outlines. Every shape you draw has two color components:
- Fill - The interior color (set with
fill()) - Stroke - The outline color (set with
stroke())
Stroke Syntax Options
1 2 3 4 5 6 | |
Controlling Stroke Appearance
1 2 | |
Stroke Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
| Function | Purpose |
|---|---|
stroke(color) |
Set outline color |
strokeWeight(n) |
Set line thickness to n pixels |
noStroke() |
Remove outlines |
noFill() |
Remove fill (transparent interior) |
Shape Rendering: How p5.js Draws
Understanding shape rendering helps you control exactly how your shapes appear. p5.js renders shapes in the order you write them—later shapes draw on top of earlier ones, like layers of paint.
The Rendering Pipeline
When you call a drawing function like rect() or ellipse(), p5.js:
- Checks the current fill color
- Checks the current stroke color and weight
- Calculates the shape geometry
- Renders the fill area
- Renders the stroke outline on top
This layered approach means you can create complex visuals by strategically ordering your drawing commands.
Diagram: Shape Rendering Order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
Rendering Modes
p5.js provides different modes that change how coordinates are interpreted:
1 2 3 4 5 6 | |
Text Display with text(), textSize(), and textAlign()
Text is essential for MicroSims—labels, values, instructions, and feedback all rely on clear text rendering. p5.js provides powerful text functions that give you complete control over typography.
The text() Function
1 2 | |
Setting Text Size with textSize()
1 | |
Aligning Text with textAlign()
1 2 | |
Horizontal alignment options:
LEFT- Text starts at x position (default)CENTER- Text is centered on x positionRIGHT- Text ends at x position
Vertical alignment options:
TOP- Text top at y positionCENTER- Text middle at y positionBOTTOM- Text bottom at y positionBASELINE- Text baseline at y position (default)
Text Example: Labeled Controls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
| Function | Purpose | Common Values |
|---|---|---|
text(str, x, y) |
Display text at position | Any string |
textSize(n) |
Set font size | 12-48 for most uses |
textAlign(h, v) |
Set alignment | LEFT, CENTER, RIGHT |
textFont(font) |
Set typeface | 'Georgia', 'Courier' |
Color Theory: The Science of Beautiful Palettes
Now we arrive at the heart of this colorful chapter: color theory. Understanding how colors work together transforms your MicroSims from functional to beautiful.
The Color Wheel
The color wheel is a circular arrangement of colors showing relationships between primary, secondary, and tertiary colors.
Diagram: Interactive Color Wheel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
Color Harmonies
Color harmonies are combinations of colors that are aesthetically pleasing. Here are the most useful harmonies for MicroSim design:
| Harmony Type | Description | Best Use |
|---|---|---|
| Complementary | Colors opposite on wheel | High contrast, emphasis |
| Analogous | Adjacent colors | Harmony, natural feel |
| Triadic | Three colors equally spaced | Vibrant, balanced |
| Split-Complementary | Base + two adjacent to complement | Contrast with less tension |
| Monochromatic | Single hue, varying brightness | Elegant, cohesive |
HSB Color Mode: The Rainbow's Best Friend
While RGB is intuitive for mixing light, HSB (Hue, Saturation, Brightness) is far superior for creating rainbow effects and color harmonies.
1 | |
| HSB Component | Range | Meaning |
|---|---|---|
| Hue | 0-360 | Position on color wheel (red=0, green=120, blue=240) |
| Saturation | 0-100 | Color intensity (0=gray, 100=vivid) |
| Brightness | 0-100 | Light level (0=black, 100=full brightness) |
Creating Rainbows with HSB
HSB makes rainbow generation trivially easy—just loop through hue values!
1 2 3 4 5 6 7 8 9 10 11 12 | |
This simple loop creates a full spectrum rainbow! The magic is that hue values map directly to positions on the color wheel.
MicroSim: Your Personal Rainbow Generator
Now let's put everything together! This MicroSim lets you create your own rainbow patterns with interactive controls for every aspect of the color.
Diagram: Personal Rainbow Generator MicroSim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
This MicroSim brings together everything in this chapter: rectangles for the control area, ellipses/arcs for the rainbow shape, lines for accents, text for labels, stroke for outlines, and color theory for beautiful palettes!
MicroSim: Shape Playground
Before creating complex simulations, it helps to experiment with drawing primitives in an interactive environment.
Diagram: Shape Playground MicroSim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
Combining Primitives: Building Complex Shapes
Real MicroSims combine multiple primitives to create meaningful visualizations. Here's how to think about composition:
Layered Construction
Build complex shapes from simple ones, drawing background elements first:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Creating Custom Shapes with beginShape()
For shapes beyond the primitives, use vertex-based drawing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Practical Application: MicroSim Control Panel
Let's apply everything to create a reusable control panel pattern:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
This pattern appears in nearly every MicroSim—a clean white control area with labeled sliders showing current values.
Key Takeaways
Congratulations! You've completed the most colorful chapter in this book. Here's what you've mastered:
-
Drawing Primitives are your visual vocabulary:
rect(),ellipse(),line(), and more. -
rect() draws rectangles from the top-left corner (or center with
rectMode(CENTER)), with optional rounded corners. -
ellipse() draws from the center by default, with width and height parameters for circles and ovals.
-
line() connects two points, styled with
stroke()andstrokeWeight(). -
stroke() sets outline color; use
noStroke()to remove outlines entirely. -
Shape Rendering follows painter's algorithm—draw order determines layering.
-
text() displays strings, with
textSize()for font size andtextAlign()for positioning. -
Color Theory helps you create harmonious palettes using complementary, analogous, and triadic relationships.
-
HSB Color Mode makes rainbows easy—just loop through hue values from 0 to 360!
-
Combining primitives with thoughtful layering creates professional-looking interfaces.
Challenge: What HSB values would you use to create a sunset palette?
A sunset palette uses warm hues in the red-orange-yellow range (0-60°), with varying saturation (60-100%) and brightness (70-100%). Try: fill(30, 90, 100) for orange, fill(0, 80, 90) for coral, and fill(50, 100, 100) for golden yellow!
Next Steps
You now have the skills to create visually stunning MicroSims with complete control over shapes, text, and color. In the next chapter, we'll add interactivity—sliders, buttons, and mouse events that let users explore simulations hands-on.
Go experiment with your new rainbow powers! Try creating:
- A color picker with HSB sliders
- A drawing app with shape stamps
- An animated rainbow that cycles through hues
- A mood board with complementary color pairs
- A text-based data display with styled labels
The canvas is your palette. Paint something wonderful!
References
For deeper exploration of color and design:
- p5.js Color Reference - Complete color function documentation
- Adobe Color - Interactive color wheel and palette generator
- Coolors - Fast palette generation tool
- Color Theory Fundamentals - Academic overview
- HSB vs RGB - Practical guide to HSB