Below is a simple example using HTML5 Canvas and JavaScript. This code creates a canvas with a drawn rectangle (using the canvas borders) and a ball that bounces around inside it.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bouncing Ball in a Rectangle</title>
<style>
/* Optional: adding a border and background to the canvas */
canvas {
border: 1px solid black;
background-color: #f0f0f0;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="600" height="400"></canvas>
<script>
// Get the canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Define the rectangle area (using the canvas dimensions)
const rect = {
x: 0,
y: 0,
width: canvas.width,
height: canvas.height
};
// Define the ball properties
const ball = {
x: canvas.width / 2, // Start in the middle of the canvas
y: canvas.height / 2,
radius: 10,
vx: 3, // Horizontal speed
vy: 2, // Vertical speed
color: 'red'
};
// Function to draw the rectangle
function drawRectangle() {
ctx.strokeStyle = 'black';
ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
}
// Function to draw the ball
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = ball.color;
ctx.fill();
ctx.closePath();
}
// Update ball position and check for collisions with the rectangle's boundaries
function update() {
// Move the ball
ball.x += ball.vx;
ball.y += ball.vy;
// Check for collision with the left or right boundary
if (ball.x + ball.radius > rect.width || ball.x - ball.radius < rect.x) {
ball.vx *= -1;
}
// Check for collision with the top or bottom boundary
if (ball.y + ball.radius > rect.height || ball.y - ball.radius < rect.y) {
ball.vy *= -1;
}
}
// The main animation loop
function loop() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the rectangle and the ball
drawRectangle();
drawBall();
// Update ball position
update();
// Request the next frame
requestAnimationFrame(loop);
}
// Start the loop
loop();
</script>
</body>
</html>
How It Works
-
Canvas Setup:
A <canvas> element is created with a width of 600 pixels and a height of 400 pixels. The JavaScript code grabs the canvas using its ID and gets its 2D rendering context.
-
Ball and Rectangle:
The rectangle corresponds to the canvas boundaries. The ball is defined with a starting position (center of the canvas), a radius, a velocity (vx and vy), and a color.
-
Drawing and Update Functions:
- The
drawRectangle() function uses strokeRect to outline the area.
- The
drawBall() function uses arc to create the ball's circle and then fills it.
- The
update() function moves the ball and reverses its direction if it reaches any boundary.
-
Animation Loop:
The loop() function clears the canvas, redraws the rectangle and ball, updates the ball’s position, and uses requestAnimationFrame to continue the animation smoothly.
Simply copy this code into an HTML file and open it in a web browser to see the ball bouncing inside the rectangle.