AI Bouncing Ball - Xelsed p5flash-alpha02

60 views ยท Dec 9, 2025

๐ŸŽจ Colorful Bouncing Ball Animation Created with AI assistance using Xelsed p5flash-alpha02 model. Features: - Smooth bouncing ball animation - Dynamic color changes on bounce - Trail effect with transparency - Responsive canvas sizing Generated through automated Playwright testing of P5 AI Studio. Model: Xelsed p5flash-alpha02 (fine-tuned for p5.js)

#ai#p5js#xelsed#generative#animation
๐Ÿ”€ Remix This ๐Ÿ“– View Docs โ›ถ Full Screen โ† Back to Gallery
๐Ÿ“‹ Embed Code

Copy this code to embed this sketch on your website:

<iframe src="https://p5js.ai/embed/9f9b5fd9-e1b1-4da6-93ab-49ec27f6f7ed" width="400" height="400" frameborder="0" allow="autoplay" title="AI Bouncing Ball - Xelsed p5flash-alpha02"></iframe>

Adjust width and height as needed. Preview embed โ†’

โ“ About This Sketch 3 questions

What visual effects does the AI Bouncing Ball sketch create?

The sketch features a colorful bouncing ball that changes colors dynamically upon each bounce, leaving a smooth fading trail that enhances the visual appeal.

Is the AI Bouncing Ball sketch interactive for users?

While the sketch does not have direct user interactions, it responds to the canvas size, adjusting the ball's position and movement within the responsive environment.

What creative coding concepts are showcased in the AI Bouncing Ball sketch?

This sketch demonstrates concepts such as dynamic color manipulation, smooth animation through velocity and position updates, and creating a fading trail effect using transparency.

๐Ÿ’ฌ AI Chat History 4 messages
๐Ÿ‘ค You
Create a colorful bouncing ball animation in p5.js.
The ball should:
- Start in the center of the canvas
- Bounce off all edges
- Change color each time it bounces
- Leave a fading trail behind it
๐Ÿค– AI
This sounds like a fun and classic p5.js animation! I'll create a sketch that features a colorful bouncing ball with a fading trail.

Here's how we'll achieve each requirement:
1. **Start in the center:** We'll initialize the ball's position (`ballX`, `ballY`) to `width / 2` and `height / 2` in the `setup()` function.
2. **Bounce off all edges:** We'll define a velocity (`ballVX`, `ballVY`) for the ball. In `draw()`, we'll check if the ball's edge (`ballX + ballRadius` or `ballX - ballRadius`) goes beyond the canvas boundaries (`width` or `0`). If it does, we'll reverse its corresponding velocity component (`ballVX *= -1` or `ballVY *= -1`).
3. **Change color each time it bounces:** When the ball hits an edge and its velocity is reversed, we'll generate a new random color using `color(random(255), random(255), random(255))` and assign it to `ballColor`.
4. **Leave a fading trail behind it:** Instead of completely clearing the background each frame with `background(220)`, we'll use a semi-transparent background: `background(220, fadeAmount)`. The `fadeAmount` (a low alpha value like 10-20) will cause previous frames to slowly fade out, creating the trail effect.

Let's get this code ready for you!

```js
// sketch.js
let ballX, ballY; // Ball's position
let ballVX, ballVY; // Ball's velocity
let ballRadius = 30; // Radius of the ball (diameter will be ballRadius * 2)
let ballColor; // Current color of the ball
let fadeAmount = 15; // Alpha value for the fading background (0-255)

function setup() {
createCanvas(windowWidth, windowHeight);

// Initialize ball in the center
ballX = width / 2;
ballY = height / 2;

// Initialize ball velocity with random values
// Ensure velocity is not too small or zero to make it move
ballVX = random(3, 8); // Random speed between 3 and 8
if (random() < 0.5) ballVX *= -1; // Randomly choose initial horizontal direction

ballVY = random(3, 8); // Random speed between 3 and 8
if (random() < 0.5) ballVY *= -1; // Randomly choose initial vertical direction

// Initialize ball color with a random color
ballColor = color(random(255), random(255), random(255));

// Set the color mode to RGB (default, but good to be explicit)
colorMode(RGB);
}

function draw() {
// Draw a semi-transparent background to create the fading trail
background(220, fadeAmount);
// Reference: https://p5js.org/reference/#/p5/background

// Update ball position based on velocity
ballX += ballVX;
ballY += ballVY;
// Reference: https://p5js.org/reference/#/p5/+=

// Check for horizontal bounces
// If the ball hits the right edge OR the left edge
if (ballX + ballRadius >= width || ballX - ballRadius <= 0) {
ballVX *= -1; // Reverse horizontal velocity

// Change ball color randomly on bounce
ballColor = color(random(255), random(255), random(255));
// Reference: https://p5js.org/reference/#/p5/color
// Reference: https://p5js.org/reference/#/p5/random
}

// Check for vertical bounces
// If the ball hits the bottom edge OR the top edge
if (ballY + ballRadius >= height || ballY - ballRadius <= 0) {
ballVY *= -1; // Reverse vertical velocity

// Change ball color randomly on bounce
ballColor = color(random(255), random(255), random(255));
}

// Draw the ball
fill(ballColor); // Set fill color
// Reference: https://p5js.org/reference/#/p5/fill
noStroke(); // Remove outline
// Reference: https://p5js.org/reference/#/p5/noStroke
circle(ballX, ballY, ballRadius * 2); // Draw a circle (x, y, diameter)
// Reference: https://p5js.org/reference/#/p5/circle
}

function windowResized() {
resizeCanvas(windowWidth, windowHeight);
// Reference: https://p5js.org/reference/#/p5/windowResized
// Adjust ball position if canvas resizes
ballX = constrain(ballX, ballRadius, width - ballRadius);
ballY = constrain(ballY, ballRadius, height - ballRadius);
// Reference: https://p5js.org/reference/#/p5/constrain
}
```

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bouncing Ball Animation</title>
<link rel="stylesheet" href="style.css">
<!-- Load the p5.js library from CDN -->
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.3/lib/p5.min.js"></script>
</head>
<body>
<!-- Load your sketch.js file -->
<script src="sketch.js"></script>
</body>
</html>
```

```css
body {
margin: 0;
padding: 0;
overflow: hidden; /* Prevent scrollbars if canvas is slightly larger than window */
}

canvas {
display: block; /* Remove extra space below the canvas */
}
```
๐Ÿ‘ค You
Please improve the previous bouncing ball animation:
- Make the colors more vibrant (use HSB color mode)
- Add a more visible trail effect using transparency
- Make the ball size vary slightly as it moves
- Add a subtle shadow under the ball
Iteration 2 improvements requested.
๐Ÿค– AI
Okay, let's enhance your bouncing ball animation with more vibrant colors, a more visible trail, a subtly varying ball size, and a soft shadow!

Here's a breakdown of the changes and why they're implemented:

1. **Vibrant Colors with HSB:**
* We'll switch the color mode to HSB (Hue, Saturation, Brightness). HSB makes it easier to create vibrant colors by keeping Saturation and Brightness high (e.g., 80-100) and only varying the Hue.
* `colorMode(HSB, 360, 100, 100, 100)`: Sets the range for Hue to 0-360 (degrees), and Saturation, Brightness, and Alpha to 0-100.
* `ballColor = color(random(360), 80, 90);`: When initializing and on bounce, we'll pick a random Hue, but keep Saturation at 80% and Brightness at 90% for a vivid look.

2. **More Visible Trail:**
* The `fadeAmount` for the background was 15 (out of 255). For a more prominent trail, we need the background to fade slower, meaning a lower alpha value.
* I'll change `fadeAmount` to `8` (out of 100, as we are in HSB mode where alpha is 0-100).
* `background(0, 0, 100, fadeAmount);`: In HSB, `0, 0, 100` represents pure white.

3. **Varying Ball Size:**
* We'll introduce a `currentBallSize` variable.
* In `draw()`, we'll use a `sin()` function based on `frameCount` to create a smooth, oscillating effect for the ball's size.
* `currentBallSize = ballRadius + sin(frameCount * 0.1) * 5;`: This will make the ball's radius oscillate between `ballRadius - 5` and `ballRadius + 5` pixels. The `0.1` controls the speed of the oscillation.

4. **Subtle Shadow:**
* A shadow can be drawn as a slightly larger, darker, and semi-transparent ellipse *before* the main ball.
* `fill(0, 0, 0, 30);`: A dark gray/black color with low transparency (30% alpha).
* `circle(ballX + 10, ballY + 10, currentBallSize * 2.2);`: The shadow is offset by `10` pixels down and right from the ball's center and is `20%` larger in diameter (`2.2` instead of `2`).

Here are the updated files:

```js
// sketch.js - Iteration 2 Improvements
let ballX, ballY; // Ball's position
let ballVX, ballVY; // Ball's velocity
let ballRadius = 30; // Base radius of the ball
let currentBallSize; // Current radius, varies slightly
let ballColor; // Current color of the ball

// Alpha value for the fading background (0-100 for HSB color mode)
// Lower value = slower fade = more visible trail
let fadeAmount = 8;

function setup() {
createCanvas(windowWidth, windowHeight);

// Initialize ball in the center
ballX = width / 2;
ballY = height / 2;

// Initialize ball velocity with random values
// Ensure velocity is not too small or zero to make it move
ballVX = random(3, 8); // Random speed between 3 and 8
if (random() < 0.5) ballVX *= -1; // Randomly choose initial horizontal direction

ballVY = random(3, 8); // Random speed between 3 and 8
if (random() < 0.5) ballVY *= -1; // Randomly choose initial vertical direction

// Set color mode to HSB: Hue (0-360), Saturation (0-100), Brightness (0-100), Alpha (0-100)
colorMode(HSB, 360, 100, 100, 100);
// Reference: https://p5js.org/reference/#/p5/colorMode

// Initialize ball color with a random, vibrant HSB color
// High saturation (80) and brightness (90) for vibrancy
ballColor = color(random(360), 80, 90);
}

function draw() {
// Draw a semi-transparent white background to create the fading trail
// 0,0,100 is pure white in HSB
background(0, 0, 100, fadeAmount);
// Reference: https://p5js.org/reference/#/p5/background

// Update ball position based on velocity
ballX += ballVX;
ballY += ballVY;
// Reference: https://p5js.org/reference/#/p5/+=

// Vary the ball size slightly using a sine wave based on frameCount
// Oscillation amplitude of 5 pixels around ballRadius
currentBallSize = ballRadius + sin(frameCount * 0.1) * 5;
// Reference: https://p5js.org/reference/#/p5/sin
// Reference: https://p5js.org/reference/#/p5/frameCount

// --- Draw the subtle shadow first ---
fill(0, 0, 0, 30); // Dark gray/black with 30% transparency in HSB
noStroke(); // No outline for the shadow
// Shadow is offset down and right, and slightly larger than the ball
circle(ballX + 10, ballY + 10, currentBallSize * 2.2);

// --- Draw the main ball ---
fill(ballColor); // Set fill color to the ball's current vibrant color
// Reference: https://p5js.org/reference/#/p5/fill
noStroke(); // Remove outline for the ball
// Reference: https://p5js.org/reference/#/p5/noStroke
circle(ballX, ballY, currentBallSize * 2); // Draw a circle (x, y, diameter) with varying size
// Reference: https://p5js.org/reference/#/p5/circle

// Check for horizontal bounces
// If the ball hits the right edge OR the left edge
if (ballX + currentBallSize >= width || ballX - currentBallSize <= 0) {
ballVX *= -1; // Reverse horizontal velocity

// Change ball color randomly on bounce (new vibrant HSB color)
ballColor = color(random(360), 80, 90);
// Reference: https://p5js.org/reference/#/p5/color
// Reference: https://p5js.org/reference/#/p5/random
}

// Check for vertical bounces
// If the ball hits the bottom edge OR the top edge
if (ballY + currentBallSize >= height || ballY - currentBallSize <= 0) {
ballVY *= -1; // Reverse vertical velocity

// Change ball color randomly on bounce (new vibrant HSB color)
ballColor = color(random(360), 80, 90);
}
}

function windowResized() {
resizeCanvas(windowWidth, windowHeight);
// Reference: https://p5js.org/reference/#/p5/windowResized
// Adjust ball position if canvas resizes to keep it within bounds
ballX = constrain(ballX, currentBallSize, width - currentBallSize);
ballY = constrain(ballY, currentBallSize, height - currentBallSize);
// Reference: https://p5js.org/reference/#/p5/constrain
}
```

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bouncing Ball Animation - Iteration 2</title>
<link rel="stylesheet" href="style.css">
<!-- Load the p5.js library from CDN -->
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.3/lib/p5.min.js"></script>
</head>
<body>
<!-- Load your sketch.js file -->
<script src="sketch.js"></script>
</body>
</html>
```

```css
body {
margin: 0;
padding: 0;
overflow: hidden; /* Prevent scrollbars if canvas is slightly larger than window */
}

canvas {
display: block; /* Remove extra space below the canvas */
}
```
๐ŸŽฌ Video Preview