setup() function in p5.js, definitions in the Factory run once before the Playground starts executing.
Factory Tabs
The Factory is organized into three tabs for different types of custom blocks:- Variable
- Function
- Object
Define reusable values that are set once and used throughout your program.Properties:
- Runs once at initialization
- No input nodes
- Static output value
- Changes require restarting the sketch
Each tab can contain multiple custom blocks of that type. Click the tab name to switch between them.
Creating Custom Blocks
Name Your Block
Click the rename icon (pencil) in the section controls to give your block a meaningful name.
Block Preview
Each custom block in the Factory displays a live preview showing:- The block’s name
- Its visual appearance
- Input and output nodes
Section Controls
Each Factory section has controls for managing the custom block:Rename
Click the pencil icon to edit the block name:- Click to enter edit mode
- Type the new name
- Click away to finish
- Invalid names are rejected
Delete
Click the trash icon to remove the custom block:- Deletes the block definition
- Removes the block from the Factory
- Updates all instances in the Playground
Resize
Drag the bottom edge of any section to resize it:- Larger sections give more workspace
- Useful for complex functions
- Height is saved with the project
Using Custom Blocks
Once created, custom blocks can be used in the Playground:- Via Search
- Via Drag
Double-click an empty block room and search for your custom block name. Custom blocks appear in search results alongside predefined blocks.
How Variables Work
- Execute once at program start
- Have no input nodes
- Provide a constant output
- Changing them restarts the entire sketch
Variables are perfect for constants like colors, sizes, or initial positions that won’t change during execution.
How Functions Work
- Only execute when called from Playground
- Can accept multiple parameters
- Can return values through output nodes
- Can contain complex logic
Updates and Re-initialization
When you modify the Factory:Variable Changes
- Effect: Restarts the entire sketch
- Reason: Variables initialize before Playground execution
- Use case: Adjusting initial parameters
Function Changes
- Effect: Updates function definition immediately
- Reason: Functions execute on-demand
- Use case: Tweaking logic while running
Factory Panel Management
The Factory panel can be:Resized
Drag the vertical separator to adjust the Factory width relative to the Playground.Folded
Click the fold icon to collapse the Factory:- Gives maximum space to Playground
- Factory remains accessible but hidden
- Previous width is restored when unfolded
Section Organization
Each tab can contain multiple sections:- Sections are listed vertically
- Each has its own resizable code canvas
- All sections share the same tab space
- Scroll to access sections below the fold
Best Practices
Descriptive Names
Use clear, descriptive names for custom blocks so they’re easy to find in search.
Single Responsibility
Keep each custom block focused on one task for better reusability.
Test in Playground
Test custom blocks in the Playground to verify they work as expected.
Document with Comments
Use comment blocks to explain complex logic in your custom blocks.
Next Steps
Playground
Use your custom blocks in the main canvas
Block Search
Learn how to add custom blocks quickly