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!
To get us started, try moving the slider in the MicroSim below. Note that as you move the slider the color swatch chances and it will show you three numbers. These are the red, green and blue color values that make up the current color.
Drawing: Rainbow Color Picker
Run the Rainbow Color Picker Fullscreen
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 | |
You can even control the roundness of each individual corner by adding for numbers after the height!
1 | |
At Apple computer, Steve Jobs was famous for his instance that boxes on the Macintosh user interface had the ability to have smooth rounded corners. Research has proven that users prefer rounded corners to sharp corners. We encourage you to use this feature for ascetically pleasing designs.
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. Just be warned, if you make this
change the rest of the rect() is your drawing will also do this until you change it back to the rectMode(CORNER) default value.
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 | |
Using circle() makes your code a little easier to read, so it is preferred.
Colorful Circle Examples
Here is an example of drawing file colored circles on a black background
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 | |
Edit this drawing using the p5.js editor
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.
Named Colors
In addition to setting a color with an RGB or HSB value, P5.js also allow you to set the color using named colors.