Overview
Variables in b5 work like the setup function in p5.js - they run once before your Playground canvas starts executing. Custom variables let you define reusable values that appear as blocks throughout your project.Any change made to a variable will re-initiate the running sketch, as variables are executed during the setup phase.
Creating a Variable
Open the Factory
Navigate to the Factory section in the b5 editor. You’ll see three tabs: variable, function, and object.
Add a New Variable
Click the new button at the bottom of the variable content area to create a new variable section.
Name Your Variable
Click the rename icon (pencil) in the section controls to give your variable a meaningful name.
Variable Block Characteristics
- No Inputs: Rendered variable blocks don’t take any inputs
- Static Outputs: They provide consistent output values
- Single Output Node: Variables have one output node that can connect to multiple blocks
- Run Once: Execute only during initialization, before the Playground loop starts
Example: Canvas Variable
Here’s a real example from the b5 source code that creates a canvas with specific dimensions:cnv variable block that:
- Sets the canvas width to 500 pixels
- Uses a slider (0-600 range) for adjustable height
- Calls
createCanvaswith these dimensions - Outputs the canvas object for use in Playground
Using Variables in Playground
Once created, your custom variable blocks can be used in two ways:Method 1: Block Search
Double-click any empty block room in the Playground and search for your variable name.Method 2: Drag and Drop
Drag the preview block directly from the Factory into your Playground canvas.The preview block appears on the right side of each variable section in the Factory, showing exactly how your variable will look when used.
Variable Canvas Properties
| Property | Value | Description |
|---|---|---|
| Max Lines | 3 | Maximum number of lines in the variable canvas |
| Max Blocks per Line | 3 | Maximum blocks that can fit in one line |
| Execution | Once | Runs only during initialization |
| Resizable | Yes | Drag the bottom bar to adjust section height |
Managing Variables
Renaming
- Click the pencil icon in the section controls
- Edit the name directly in the preview block
- Click outside or press Enter to save
Deleting
Click the trash icon in the section controls to remove the variable and all its instances from your project.Best Practices
- Use descriptive names: Choose names that clearly indicate what the variable stores (e.g.,
circleRadius,bgColor) - Keep it simple: Variables are best for single values or simple operations
- Group related setup: Put related initialization logic in the same variable (like canvas setup)
- Test before using: Verify your variable works correctly in the Factory before using it extensively in Playground
Troubleshooting
Variable not appearing in block search?- Ensure the variable name is valid (no special characters)
- Check that it doesn’t conflict with built-in block names
- This is expected behavior - variables run during setup, so changes require reinitialization
- Check the wire connections in your variable canvas
- Ensure the final block in your variable has an output node connected
Next Steps
Custom Functions
Learn how to create reusable functions with inputs and outputs
Save & Load Projects
Understand the .b5.json file format and how to save your work