beginShape()
The `beginShape()` function in p5.js starts the definition of a custom shape using vertices. It's essential for creative coders who want to create unique and complex shapes beyond basic geometric forms. For example, in a kaleidoscope generator, you might use `beginShape()` to create intricate patterns that change dynamically, allowing for a visually stimulating art piece.
๐ p5.js Reference โ๐ง See The Whole Picture
Imagine a blank canvas where you want to create a unique design. `beginShape()` is like laying the foundation and deciding if you want to draw dots or lines. Each subsequent vertex you define adds to this design, and finally, when you call `endShape()`, your canvas is filled with the completed artwork.
๐ Visual Learner's Guide
The Big Picture:
Running this code will show a closed triangle on your canvas.
Visual Analogy:
Think about laying out a string on a table to create a shape - you place the string at different points and then connect them to form a complete figure.
The Numbers Decoded:
The coordinates (100, 100) mean 'start at 100 pixels from the left and 100 pixels down from the top.' Each subsequent vertex builds off this starting point.
Connection Map:
`beginShape()` sets up the drawing space, while `endShape()` completes it. The vertices are like steps in connecting dots to form a picture.
Common Gotcha:
Beginners often forget to call `endShape()`, leaving their shapes incomplete!
๐ How It Works
This diagram illustrates the flow from your code to the execution of the `beginShape()` function and its impact on the canvas.
๐ Syntax
๐ How to Read This
Read `beginShape()` like this: 'Start drawing a shape with the specified parameters that define the vertices of the shape. The parentheses contain all the information needed to construct that shape.'
beginShape(param1, param2, ...)
Example: beginShape(200, 150, 80)
๐ Different Ways to Call It
beginShape(value)
This form is used when you want to create a shape based on a single parameter, which could be a style constant like POINTS or LINES.
beginShape(POINTS);
โ This setup would create a shape consisting of points at specified vertices.
beginShape(v1, v2, v3)
Use this form to draw shapes with multiple vertices, where each vertex is defined by coordinates.
beginShape(); vertex(100, 100); vertex(150, 50); vertex(200, 100); endShape();
โ This will produce a triangle shape connecting the three vertices.
โฉ๏ธ What It Returns
Type: void
The function doesn't return anything; it directly affects the drawing on the canvas, similar to how asking an artist to draw doesn't return any object but results in a visible output.
Parameters
value
Number | String
required
This can be a constant like POINTS or LINES, which determines how to connect the vertices of the shape.
This controls the type of shape or how the vertices are connected. For example, POINTS will draw individual points, while LINES will connect vertices with lines.
Range: Common values include POINTS, LINES, TRIANGLES, etc.
POINTS โ Dots at each vertex.
LINES โ Lines connecting the vertices.
TRIANGLES โ Connected triangle shape.
๐ก How Parameters Work Together
The value parameter acts as a guide for how to connect the points you define later. Think of it like choosing a drawing tool - if you choose POINTS, you will be creating a series of dots; if you choose LINES, you will create a continuous line connecting the vertices.
๐ป Working Example
// Complete working example with comments
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
beginShape(); // Start defining a shape
vertex(100, 100);
vertex(150, 50);
vertex(200, 100);
endShape(CLOSE); // Close the shape
}
Try This Code โ
โ๏ธ How It Works
When `beginShape()` is executed, p5.js prepares to record the vertices you define. It modifies the state of the canvas to expect new shape data. Each vertex you add alters the shape being drawn, and it doesnโt finalize until `endShape()` is called, which triggers the drawing process.
๐ Learn Prompt Patterns
These patterns help you write better prompts for ANY function:
๐ญ Persona Pattern
Tell the AI to act as a specific type of creator
"Act as a creative coder making generative art. Use beginShape() to create a dynamic geometric pattern."
Why it works: Giving the AI a persona helps it understand the CONTEXT and style you want.
๐ Recipe Pattern
Ask for step-by-step instructions
"Give me step-by-step code to use beginShape() to create a star shape. Start simple, then add complexity."
Why it works: Breaking down into steps helps you understand each part and learn incrementally.
๐ Template Pattern
Provide a structure for the AI to fill in
"Use this template: setup creates canvas, draw uses beginShape() with specific coordinates to make a flower shape."
Why it works: Templates give the AI clear constraints, resulting in more predictable output.
๐ Refinement Pattern
Start simple and iterate
"Start with a basic beginShape() example. Then I'll ask you to add color, animation, and interaction."
Why it works: Building up gradually helps you understand each addition and not get overwhelmed.
๐ฏ Direct Prompts - Name the Function
Tell the AI exactly which function to use:
"Create a colorful polygon using beginShape() with five vertices."
๐ก Why this is good: This structure helps clarify the visual goal while guiding incremental progress.
๐ What you learn: Learn to define shapes with multiple vertices.
// Function to create a colorful polygon with five vertices.
"Generate a dynamic star shape using beginShape()."
๐ก Why this is good: Providing a template helps ensure a focused output.
๐ What you learn: Understanding how to manipulate vertex positions dynamically.
// Function that creates a star shape with dynamic vertices.
๐ฎ Indirect Prompts - Describe What You Want
Don't name the function - see if the AI figures out to use beginShape():
"Describe how to draw a complex shape with multiple points without naming beginShape()."
๐ก Why this is good: This encourages learners to think visually about the process.
๏ฟฝ What you learn: You learn to describe visual intent and let the AI determine the function.
// Code that uses beginShape() to create the defined shape.
๐ผ๏ธ See It In Real Sketches
How beginShape() is used in actual gallery sketches:
AI Kaleidoscope Generator
In this sketch, `beginShape()` is used to define the starting point for generating symmetrical patterns based on user input.
// Example code from the Kaleidoscope Generator
beginShape();
vertex(x1, y1);
vertex(x2, y2);
endShape(CLOSE);...
View Sketch โ
๐ Fun Function Combinations
Try asking the AI to combine beginShape() with these:
beginShape() + noise()
โ Generates organic, flowing shapes that change over time.
Try: "Combine noise() with beginShape() to create a flowing organic shape."
// Example of combining noise with beginShape()
function draw() {
beginShape();
for (let i = 0; i < 10; i++) {
vertex(i * 40, noise(i) * height);
}
endShape();
}
โ ๏ธ Common Mistakes & Fixes
โ Mistake: Forgetting to call endShape() after beginShape().
๐ค Why it's wrong: Without calling endShape(), the shape remains incomplete and won't render on the canvas.
โ Fix: Always include endShape() after defining the shape's vertices.
๐บ๏ธ Learning Path
๐ก Project Ideas
- Create a custom logo using beginShape()
- Develop a generative art piece that changes shape over time
๐ค Ask xelsed-alpha6 to Explain
"Explain what beginShape() does in p5.js"
Try this prompt in p5js.AI to see how the AI explains it!
Try in p5js.AI โ๐ก Prompt AI to Write Code Using beginShape()
"Create something using beginShape()"
๐ง Ask AI to Help Debug
"My beginShape() isn't working, help me debug"
โจ Creative Combinations with AI
"Combine beginShape() with other functions for interesting effects"
Ready to Try beginShape()?
Open p5js.AI and ask the AI to help you use beginShape() in your own creative project.
Start Coding with AI โ