Overview
The bounce ball example is a foundational b5 project that demonstrates:- Creating animated graphics with the bounce block
- Using custom functions to calculate boundaries
- Interactive controls with sliders
- Dynamic color mapping based on position
- Real-time parameter adjustments
Example Structure
The bounce ball example consists of two main sections:Playground
The main animation loop that runs at 60 FPS, containing the bouncing ball logic
Factory
Custom definitions for the canvas variable and the boundaries function
Walkthrough
Define the Ball Radius
The example starts with a This slider has:
numberSlider block that controls the radius of the ball:- Default value: 40
- Minimum: 0
- Maximum: 50
- Step: 5
The slider output is used in multiple places: the circle size and the boundary calculations.
Calculate Boundaries
The This function:
boundaries custom function takes the radius and calculates the movement limits:- Takes the canvas size using
canvasSizeblock - Adds radius to 0 for the lower boundary
- Subtracts radius from width/height for upper boundaries
- Ensures the ball stays completely visible within the canvas
View boundaries function implementation
View boundaries function implementation
The boundaries function uses basic math operations:The function outputs three values:
- Lower boundary (0 + radius)
- Upper width boundary (width - radius)
- Upper height boundary (height - radius)
Create Bouncing Animation
Two Each bounce block:
bounce blocks create the x and y animation:- Takes minimum and maximum values from the boundaries function
- Uses a step value of 7 (defined by a number block)
- Automatically increments/decrements to create smooth motion
- Reverses direction when hitting boundaries
Map Position to Color
The ball’s color changes based on its position using This creates a dynamic color effect where:
fillRGBA:- Red value corresponds to horizontal position
- Green value corresponds to vertical position
- Blue and alpha use default values
Canvas Setup
The example includes acnv variable in the Factory that creates a 500x300 canvas:
Variables in the Factory section run once before the Playground starts, similar to the
setup() function in p5.js.Key Concepts
Custom Functions
Custom Functions
The
boundaries function demonstrates how to create reusable logic in b5:- Takes input parameters (radius)
- Performs calculations
- Returns multiple outputs
- Can be used anywhere in the Playground
Effect Blocks
Effect Blocks
The These blocks don’t have wire connections but affect blocks that follow them in the execution order.
strokePicker and fillRGBA blocks are effect blocks that set the drawing context for subsequent shapes:Execution Order
Execution Order
b5 executes blocks from left to right, line by line from top to bottom. The grid coordinates in the JSON reflect this:
- Line “0” contains the radius slider and comments
- Line “1” contains the boundaries function call
- Line “3” contains the bounce blocks
- Line “5” contains the drawing blocks
Experimentation Ideas
Change the Step Size
Modify the number block from 7 to different values to make the ball move faster or slower.
Add More Balls
Duplicate the circle drawing logic with different bounce blocks to create multiple bouncing balls.
Custom Colors
Experiment with the fillRGBA inputs to create different color mapping effects.
Trail Effect
Adjust the background settings to create motion trails as the ball moves.
Next Steps
Drawing Shapes
Learn about different shape blocks and styling options
Interactive Graphics
Explore mouse and keyboard input for interactive projects
Animations
Discover more animation techniques and patterns
Block Reference
Browse the complete block documentation