shape|p5_color|p5_math|p5_transform|p5_event|p5_image|p5_typography|p5_io 🌱 Beginner

let()

The `let()` function is used to create and define variables in p5.js, allowing creative coders to store information and manipulate data easily. By using `let()`, artists can create dynamic visuals that respond to user input or change over time, making their artwork more interactive and engaging. For example, a coder might use `let()` to define the position and size of a shape, allowing it to move across the canvas based on mouse position.

📖 p5.js Reference →

🧠 See The Whole Picture

Imagine your code as a garden where `let()` plants variables. Each variable is like a seed that can grow into shapes, colors, or other elements of your artwork. When you define a variable with `let()`, you're preparing these seeds to influence what you'll create on your canvas.

📖 Visual Learner's Guide

The Big Picture: When you run this code, you'll see a circle appear on your canvas, perfectly centered. Visual Analogy: Think of `let()` like planting a seed in a garden. The seed (variable) will grow into a flower (a visual element) that you can manipulate later. The Numbers Decoded: In our example, `x=200` means the circle is 200 pixels from the LEFT edge of the canvas, `y=200` means it is 200 pixels from the TOP, and `d=100` means the circle's diameter is 100 pixels WIDE. Connection Map: Before the circle is drawn, the `fill()` function sets its color, and `stroke()` sets the outline color. Then, `ellipse()` uses these details to create the shape. Common Gotcha: Remember, the position you define with `let()` is the CENTER of the circle, not the corner, which can be a common mistake!

🔄 How It Works

This diagram illustrates how `let()` takes your code and grows variables that will ultimately affect what appears on the canvas.

📝 Syntax

📖 How to Read This

Read `let(x, y, d)` as: Create variables x and y at the position of the shape, and d as the size of the shape. The parentheses are like a delivery box - you hand the function the information it needs to create and control these variables.

let(param1, param2, ...)

Example: let(200, 150, 80)

🔀 Different Ways to Call It

let(value)

Use this form when you want to create a single variable with a specific value.

let(128) → This creates a variable with the value of 128, which could be used later in your code.
let(v1, v2, v3)

Use this form when you want to create multiple variables at once.

let(255, 100, 50) → This creates three variables, which could represent RGB color values for a shape.

â†Šī¸ What It Returns

Type: void

The `let()` function doesn't return anything; it simply sets up variables in your sketch. Think of it as preparing a canvas before you start painting.

đŸ“Ĩ What Goes In

You give `let()` a number to create a variable. Think of it as planting a seed in your coding garden. Each seed can grow into a different variable, which you can use later to control various aspects of your visual.

📤 What Happens

When you use `let()`, it plants the variable in your code, ready to be used. It won't show anything by itself, but it sets the stage for the visuals you will create later.

Parameters

value Number required

Number means any digit like 100, 3.14, or -50. Think of it as anything you could measure with a ruler or count.

This controls the value assigned to the variable. Typical values are any number, and at 0, the variable would hold no value. The maximum depends on the context but could be any reasonable number for your project.

Range: Any number based on your application

Examples: 0 → Visually, a value of 0 might represent no size or no color. 128 → A middle value like 128 can represent a medium brightness or size. 255 → The max value, like 255, could represent the brightest color or the largest size.

💡 How Parameters Work Together

Think of the parameters as ingredients in a recipe. If you mix different values together, you can create varying results in your artwork. For example, the size of a shape will change with different values, just like changing the amount of an ingredient changes a dish.

đŸ’ģ Working Example

// Complete working example with comments
function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  // Create a variable for the circle position and size
  let x = 200;
  let y = 200;
  let d = 100;
  // Draw the circle using the variables
  ellipse(x, y, d, d);
}
Try This Code →

âš™ī¸ How It Works

When p5.js executes `let()`, it reads the values you provide and creates variables in memory. This prepares the environment for your sketch, allowing you to reference these variables later in your code. The rendering pipeline uses these variables when drawing shapes or colors on the canvas.

🎓 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 `let()` to create a moving circle."

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 `let()` to create a bouncing ball. 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 `let()` with specific parameters to make a colorful square."

Why it works: Templates give the AI clear constraints, resulting in more predictable output.

🔄 Refinement Pattern

Start simple and iterate

"Start with a basic `let()` example. Then I'll ask you to add color, animation, 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:

Direct

"Use `let()` to create a series of circles that change size based on mouse position."

💡 Why this is good: This prompt encourages the AI to think creatively while applying the `let()` function.

📚 What you learn: How to use variables to create interactive visuals.

📝 Sample Output:
// 10-15 lines of COMPLETE working code
Direct

"Create a sketch using `let()` to define the color and position of a rectangle."

💡 Why this is good: Encourages a step-by-step approach to coding.

📚 What you learn: How to manipulate shapes and colors using variables.

📝 Sample Output:
// complete code

🔮 Indirect Prompts - Describe What You Want

Don't name the function - see if the AI figures out to use let():

Indirect

"Describe a visual where shapes change based on user input without naming `let()`."

💡 Why this is good: This teaches you can describe WHAT you want without knowing the function name.

īŋŊ What you learn: How to communicate visual intent effectively.

📝 Sample Output:
// Code showing AI chose `let()`

đŸ–ŧī¸ See It In Real Sketches

How let() is used in actual gallery sketches:

Dynamic Color Circles

In this sketch, `let()` is used to define the position and size of circles that change color as the mouse moves.

// Relevant code showing the function in context
let x = mouseX;
let y = mouseY;
let d = 50;
fill(255, 0, 0);
ellipse(x, y, d, d);...

🔀 Fun Function Combinations

Try asking the AI to combine let() with these:

let() + ellipse()

→ Create a circle that changes size based on mouse position.

Try: "Use `let()` to define the position and size of an ellipse that follows the mouse."

// 5-10 lines showing the combination
function draw() {
  let x = mouseX;
  let y = mouseY;
  let d = 50;
  ellipse(x, y, d, d);
}

âš ī¸ Common Mistakes & Fixes

❌ Mistake: Confusing the center position versus corner position when placing shapes.

🤔 Why it's wrong: Beginners often forget that the coordinates in functions like `ellipse()` place the shape's center, not the top-left corner.

✅ Fix: // Corrected code let x = 200; let y = 200; ellipse(x, y, 100, 100); // This draws a circle centered at (200, 200)

đŸ—ēī¸ Learning Path

💡 Project Ideas

  • Create an interactive color wheel using variables
  • Build a simple game with movable shapes using let()

🤖 Ask xelsed-alpha6 to Explain

Your prompt:

"Explain what let() 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 let()

Example prompt:

"Create something using let()"

🔧 Ask AI to Help Debug

When stuck, try:

"My let() isn't working, help me debug"

✨ Creative Combinations with AI

Ask AI to combine functions:

"Combine let() with other functions for interesting effects"

📚 Related Functions

Ready to Try let()?

Open p5js.AI and ask the AI to help you use let() in your own creative project.

Start Coding with AI →