Build a bouncing ball animation
In this tutorial, you’ll create a complete interactive project: a colorful bouncing ball that responds to your inputs. You’ll learn how to use the Factory to create custom functions, work with effect blocks, and build dynamic animations.
Set up the canvas
First, create a custom variable to set up your canvas size.You can also use a numberSlider for the height to make it adjustable:This creates a slider with current value 300, min 0, max 600, and step 100.
- Click the variable tab in the Factory section
- Click the + button to add a new variable
- Name it
cnv - In the variable’s canvas, add a createCanvas block
- Add two number blocks for width and height:
- Width:
500 - Height:
300
- Width:
- Connect them to the createCanvas inputs
Variables in the Factory run once before the Playground starts, just like the
setup function in p5.js. Any change to variables will reinitiate your sketch.Add a comment and set the ball size
Navigate to the Playground canvas and add context to your code.
- Add a comment block in row 0, column 0
- Type:
🏀Welcome to b5! This is Bounce Ball example. Feel free to play around! - Add a numberSlider block next to it (row 0, column 1):
- Current value:
40 - Min:
0 - Max:
50 - Step:
5
- Current value:
- Add another comment block:
⬅️Radius of the circle
Create a boundaries function
Create a custom function to calculate movement boundaries based on the ball’s radius.Add comment blocks to explain each calculation:
- Click the function tab in the Factory
- Click + to add a new function
- Name it
boundaries - Add a num input block (this will be the radius parameter)
- Add a canvasSize block to get width and height
- Create the boundary calculations:
- Add an add block
- Connect:
0(number) +radius(num input) - This outputs to the first return value
- Add a subtract block
- Connect:
width(canvasSize output 0) -radius(num input) - This outputs to the second return value
- Add a subtract block
- Connect:
height(canvasSize output 1) -radius(num input) - This outputs to the third return value
Functions can have multiple outputs. The boundaries function returns three values: the lower boundary, upper width boundary, and upper height boundary.
➡️Take the radius of the circle into the function⬅️The lower boundaries of the width and height⬆️The upper boundary of the width⬆️The upper boundary of the height
Use the boundaries function
Back in the Playground, use your custom function.
- In row 1, column 2, add your boundaries function block
- Connect the radius slider (row 0, column 1 output) to the boundaries input
- Add a number block with value
7for the bounce step - Add comment blocks:
➡️You can see the definition of the function under the function tab⬅️Each step of bouncing
Add bouncing motion
Create the bouncing behavior for both x and y coordinates.
- Add a comment in row 3, column 0:
➡️Move the value between (radius) and (width - radius) - Add a bounce block in row 3, column 1 with inputs:
- Input 0: Lower boundary (from boundaries output 0)
- Input 1: Upper width boundary (from boundaries output 1)
- Input 2: Step size (the number 7)
- Add another comment:
➡️Move the value between (radius) and (height - radius) - Add another bounce block in row 3, column 3 with inputs:
- Input 0: Lower boundary (from boundaries output 0)
- Input 1: Upper height boundary (from boundaries output 2)
- Input 2: Step size (the number 7)
Draw the ball with dynamic color
Create a colorful ball that changes based on its position.
- In row 5, column 0, add a strokePicker block
- Set the color to
#4a90e2ff(blue stroke) - Add a comment:
➡️Map the x position to red value, and y position to green value - Add a fillRGBA block in row 5, column 2 with inputs:
- R (red): Connect the x-position bounce output
- G (green): Connect the y-position bounce output
- B (blue): Leave empty (defaults to 0)
- A (alpha): Leave empty (defaults to 255)
- Add a circle block in row 5, column 3 with inputs:
- x: Connect the x-position bounce output
- y: Connect the y-position bounce output
- diameter: Connect the radius slider output
The fillRGBA block is an effect block. It affects the circle because it’s positioned to the left in the same row, creating a fill color that changes as the ball moves.
Add final comments
Add one more comment for clarity:
- In row 6, column 0, add:
⬆️Use the color picker to quickly set the stroke color
Experiment and save
Now that your project is complete, try these experiments:
- Adjust the radius slider and watch the ball size change
- Change the bounce step from 7 to other values for faster/slower motion
- Try different stroke colors with the strokePicker
- Modify the canvas height slider (if you added one in the variable)
- Press
⌘/ctrl + Sto download the.b5.jsonfile - Your project is saved with all blocks, wires, and configurations
Understanding the data flow
Let’s trace how data flows through your bouncing ball program:- Canvas setup (Factory variable
cnv): Creates a 500×300 canvas (runs once) - Radius definition (row 0): Slider sets the ball radius (40)
- Boundary calculation (row 1): Custom function calculates movement limits based on radius
- Position animation (row 3): Two bounce blocks animate x and y coordinates within boundaries
- Visual rendering (row 5): Stroke color + dynamic fill color + circle drawing
Key concepts you learned
Factory variables
Create reusable setup code that runs once, like canvas creation
Custom functions
Build your own blocks with inputs and multiple outputs for complex logic
Effect blocks
Use position-based blocks like fillRGBA and strokePicker to affect subsequent blocks
Animation
Leverage the 60 FPS Playground loop to create smooth, interactive animations
Next steps
Now that you’ve built a complete project, you can:- Load examples: Click the Files icon (top-left) to load example projects with detailed comments
- Experiment with libraries: Try blocks from different categories (math, color, shape, etc.)
- Share your work: Export your project and share the JSON file with others
- Build more complex projects: Combine multiple functions, variables, and effect blocks
Remember: b5 executes blocks from left to right, line by line from top to bottom. This sequential execution makes it easy to predict how your program will run.
Full project structure
Your complete bouncing ball project includes: Factory:- 1 variable (
cnv) with canvas setup - 1 function (
boundaries) with boundary calculations
- Row 0: Welcome comment, radius slider, radius comment
- Row 1: Function comment, boundaries function call, step number, step comment
- Row 3: X-bounce comment, x-bounce block, y-bounce comment, y-bounce block
- Row 5: Stroke picker, color comment, fillRGBA block, circle block
- Row 6: Stroke picker comment