Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
  <head>
    <title>Bouncing Ball Animation</title>
    <script src="bouncing-ball.js"></script>
  </head>
  <body>
    <canvas id="canvas" width="300" height="300"></canvas>
  </body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                // Set up canvas and context
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

// Set up initial position and velocity for ball
let x = 50;
let y = 50;
let vx = 5;
let vy = 2;

// Set up constants for ball size and bounce
const ballRadius = 10;
const bounceFactor = 0.8;

// Set up drawing functions for ball and person
const drawBall = () => {
  ctx.beginPath();
  ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
  ctx.fillStyle = 'red';
  ctx.fill();
  ctx.closePath();
}

const drawPerson = () => {
  // Draw the body of the person
  ctx.beginPath();
  ctx.moveTo(75, 250);
  ctx.lineTo(75, 75);
  ctx.lineTo(225, 75);
  ctx.lineTo(225, 250);
  ctx.stroke();

  // Draw the head of the person
  ctx.beginPath();
  ctx.arc(150, 50, 25, 0, Math.PI * 2);
  ctx.stroke();

  // Draw the arms of the person
  ctx.beginPath();
  ctx.moveTo(75, 100);
  ctx.lineTo(50, 150);
  ctx.moveTo(225, 100);
  ctx.lineTo(250, 150);
  ctx.stroke();
}

// Set up animation function
const animate = () => {
  // Clear the canvas
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  // Draw the person and ball
  drawPerson();
  drawBall();

  // Update the position of the ball
  x += vx;
  y += vy;

  // Check if ball has hit the left or right wall
  if (x + vx > canvas.width - ballRadius || x + vx < ballRadius) {
    vx = -vx * bounceFactor;
  }

  // Check if ball has hit the top or bottom wall
  if (y + vy > canvas.height - ballRadius || y + vy < ballRadius) {
    vy = -vy * bounceFactor;
  }

  // Continue the animation
  requestAnimationFrame(animate);
}

// Start the animation
animate();

              
            
!
999px

Console