math 🌱 Beginner

mult()

The `mult()` function in p5.js is used to multiply numbers together, allowing creative coders to generate visual effects based on mathematical operations. For instance, you might use `mult()` to create dynamic scaling in visual art pieces by multiplying coordinates or sizes to achieve interesting transformations. This function connects to the broader theme of creative coding by enabling artists to manipulate shapes and colors mathematically, making it possible to create generative art that responds to user input or other variables.

📖 p5.js Reference →

🧠 See The Whole Picture

Imagine a conveyor belt where numbers are placed. As each number is added to the belt, they are multiplied together as they pass through a machine called `mult()`. The final output is a single number that represents all the inputs combined, which you can then use to influence your creative project.

📖 Visual Learner's Guide

The Big Picture:

When you run this code, you'll see a text on the canvas that shows the result of multiplying two numbers.

Visual Analogy:

Like stacking blocks, each number is a block that combines to create a bigger structure (the final number).

The Numbers Decoded:

In `mult(200, 150)`, 200 and 150 are like two towers; when multiplied, they create a large area that we can visualize as text size.

Connection Map:

This function connects to `textSize()`, which uses the output from `mult()` to determine how big the text appears on the screen.

Common Gotcha:

Ensure that you're multiplying numbers; multiplying by 0 will always yield 0, which may not produce the visual you expect!

🔄 How It Works

This diagram shows the flow of data from your code to the `mult()` function and then to the canvas where the visual result is produced.

📝 Syntax

📖 How to Read This

Read `mult(x, y)` as: 'Multiply x and y together.' The parentheses are like a delivery box - you hand the function the numbers it needs for multiplication.

mult(param1, param2, ...)

Example: mult(200, 150, 80)

🔀 Different Ways to Call It

mult(value)

This form is used when you want to multiply a single value by itself, useful for squaring numbers.

mult(128) → This produces 16384, as it multiplies 128 by itself.
mult(v1, v2, v3)

Use this form to multiply multiple values together, which is helpful for scaling and transformations.

mult(255, 100, 50) → This produces 1275000, multiplying all three values.

â†Šī¸ What It Returns

Type: Number

The `mult()` function returns a number, which represents the product of the input values. Think of it as a calculator that gives you the answer after performing the multiplication.

đŸ“Ĩ What Goes In

You give `mult()` two or more numbers. Each number is like a weight that influences the final result, determining how large or small the outcome will be based on the multiplication of those weights.

📤 What Happens

The `mult()` function takes your numbers and calculates the result, giving you a new number that can be used for further calculations or visual transformations, like scaling shapes or adjusting colors in your artwork.

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 parameter controls the numerical values that you want to multiply together. Typical values can range from small integers to large numbers. At 0, the output will always be 0 (multiplicative identity), and at maximum values, the product can become very large, depending on the input.

Range: Any positive number or negative number.

Examples: 0 → The result is 0, which means nothing is visually produced. 128 → This is simply a number; no visual output. 255 → This can be part of a larger multiplication producing a significant numerical output.

💡 How Parameters Work Together

Think of it like mixing ingredients in a recipe. Each number you input adds a different flavor to the final dish, and the more you add, the more complex the outcome becomes.

đŸ’ģ Working Example

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

function draw() {
  background(220);
  let result = mult(200, 150);
  textSize(result / 100);
  text('The result is: ' + result, 10, 200);
}
Try This Code →

âš™ī¸ How It Works

When p5.js executes `mult()`, it takes the values you provide, performs the multiplication operation, and returns the result. This happens in a single step, where the rendering pipeline uses the output for further visual manipulations, like setting text size or scaling shapes.

🎓 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 `mult()` to create a dynamic scaling effect based on user input."

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 `mult()` to create an effect where the size of a circle grows based on mouse position."

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 `mult()` with specific parameters to make a circle grow."

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

🔄 Refinement Pattern

Start simple and iterate.

"Start with a basic `mult()` 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 `mult()` to create a dynamic visual where shapes change size based on mouse position."

💡 Why this is good: This prompt clearly communicates the desired outcome and allows for creative expression.

📚 What you learn: Learn how to use multiplication to create interactive visuals.

📝 Sample Output:
// Full working code example
Direct

"Show me how to use `mult()` to adjust the size of a rectangle based on mouse coordinates."

💡 Why this is good: It breaks down the process into manageable steps, making it easier to grasp.

📚 What you learn: Understanding how to manipulate dimensions dynamically.

📝 Sample Output:
// complete code

🔮 Indirect Prompts - Describe What You Want

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

Indirect

"Describe a visual effect where numbers combine to change the size of shapes."

💡 Why this is good: This approach encourages communication of intent rather than just focusing on function names.

īŋŊ What you learn: You gain the ability to express your visual ideas without needing to know every function.

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

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

How mult() is used in actual gallery sketches:

Dynamic Scaling Art

In this sketch, `mult()` is used to dynamically calculate the size of geometric shapes based on mouse position, creating an interactive experience.

// Relevant code showing the function in context
function draw() {
  let size = mult(mouseX, 0.5);
  ellipse(200, 200, size, size);
}...

🔀 Fun Function Combinations

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

mult() + rect()

→ Changing size of rectangles based on mouse position.

Try: "Use `mult()` to set the width and height of a rectangle based on mouse coordinates."

// 5-10 lines showing the combination
function draw() {
  let size = mult(mouseX, 0.5);
  rect(50, 50, size, size);
}

âš ī¸ Common Mistakes & Fixes

❌ Mistake: Incorrectly using `mult()` with non-numeric values.

🤔 Why it's wrong: If you try to multiply strings or non-numeric types, it will lead to errors.

✅ Fix: Always ensure that your inputs are numbers.

đŸ—ēī¸ Learning Path

💡 Project Ideas

  • Create interactive generative art where shapes resize based on mouse position using `mult()`.

🤖 Ask xelsed-alpha6 to Explain

Your prompt:

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

Example prompt:

"Create something using mult()"

🔧 Ask AI to Help Debug

When stuck, try:

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

✨ Creative Combinations with AI

Ask AI to combine functions:

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

📚 Related Functions

Ready to Try mult()?

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

Start Coding with AI →