Below is one complete HTML file that uses a canvas with JavaScript to simulate a red ball bouncing inside a spinning hexagon. You can save this code as (for example) “index.html” and open it in a browser.
How It Works:
• A canvas is centered in the browser window.
• A regular hexagon is defined (in a “flat‐top” orientation) in simulation coordinates (with its center at (0,0)).
• A ball with a fixed radius is given an initial velocity and position.
• In each animation frame the ball’s new position is computed. Then for each of the six edges the code computes the distance from the ball’s center to the line. If the ball has “penetrated” an edge (taking its radius into account) and is moving toward that edge, its velocity is reflected about the edge’s inward normal and the ball is pushed out so it stays within the hexagon.
• The hexagon is drawn after rotating the canvas (by an ever‐increasing angle) so that the hexagon appears to spin, while the ball “bounces” inside the moving walls.
Here is the full code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ball Bouncing in a Spinning Hexagon</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #111;
}
canvas {
display: block;
margin: auto;
background: #222;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 600;
canvas.height = 600;
// Center of the canvas
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Regular hexagon parameters (flat-top)
const R = 150; // horizontal extent parameter
const h = R * Math.sqrt(3) / 2; // half the vertical height
// Hexagon vertices defined relative to (0,0)
const hexagonVertices = [
{ x: -R/2, y: -h }, // Top-left
{ x: R/2, y: -h }, // Top-right
{ x: R, y: 0 }, // Right
{ x: R/2, y: h }, // Bottom-right
{ x: -R/2, y: h }, // Bottom-left
{ x: -R, y: 0 } // Left
];
// Ball properties
const ball = {
x: 0, // starting at center of hexagon simulation coordinates
y: 0,
vx: 3, // horizontal velocity (try adjusting these values)
vy: 2.5, // vertical velocity
radius: 10
};
// Rotation angle for the hexagon (in radians)
let hexRotation = 0;
// Helper: Dot product of two vectors
function dot(v1, v2) {
return v1.x * v2.x + v1.y * v2.y;
}
// The update and draw loop
function update() {
// Update ball position
ball.x += ball.vx;
ball.y += ball.vy;
// For every edge of the hexagon, check for collision.
// The polygon is defined in counter-clockwise order.
for (let i = 0; i < hexagonVertices.length; i++) {
const A = hexagonVertices[i];
const B = hexagonVertices[(i + 1) % hexagonVertices.length];
// Compute edge vector from A to B
const edge = { x: B.x - A.x, y: B.y - A.y };
const edgeLength = Math.hypot(edge.x, edge.y);
// For a counter-clockwise (CCW) polygon, the inward normal is obtained by
// rotating the edge vector 90° counter-clockwise:
const n = { x: -edge.y / edgeLength, y: edge.x / edgeLength };
// Compute distance from ball center to the line defined by A and normal n.
const relPos = { x: ball.x - A.x, y: ball.y - A.y };
const distance = dot(relPos, n);
// If the ball is closer than its radius to the edge and is moving toward it...
if (distance < ball.radius && dot({ x: ball.vx, y: ball.vy }, n) < 0) {
// Calculate penetration depth and push the ball out along n.
const penetration = ball.radius - distance;
ball.x += n.x * penetration;
ball.y += n.y * penetration;
// Reflect the ball's velocity about the edge's normal.
const vDotN = dot({ x: ball.vx, y: ball.vy }, n);
ball.vx -= 2 * vDotN * n.x;
ball.vy -= 2 * vDotN * n.y;
}
}
// Increase rotation of the hexagon for a spinning effect.
hexRotation += 0.01;
// Draw the scene
draw();
// Request the next frame.
requestAnimationFrame(update);
}
function draw() {
// Clear the canvas.
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Save context state
ctx.save();
// Move origin to the center of the canvas.
ctx.translate(centerX, centerY);
// Rotate for the spinning hexagon.
ctx.rotate(hexRotation);
// Draw hexagon
ctx.beginPath();
ctx.moveTo(hexagonVertices[0].x, hexagonVertices[0].y);
for (let i = 1; i < hexagonVertices.length; i++) {
ctx.lineTo(hexagonVertices[i].x, hexagonVertices[i].y);
}
ctx.closePath();
ctx.lineWidth = 3;
ctx.strokeStyle = "#fff";
ctx.stroke();
// Draw the ball (its simulation coordinates remain fixed relative to the hexagon)
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, 2 * Math.PI);
ctx.fillStyle = "red";
ctx.fill();
// Restore context state.
ctx.restore();
}
// Start the animation loop.
update();
</script>
</body>
</html>
Usage Notes:
- You can adjust the ball’s initial velocity (
vx and vy), its radius, or the hexagon size (R) as you like.
- The simulation logic computes collisions for each hexagon edge and reflects the velocity accordingly.
- The entire scene (both the ball and the hexagon) is drawn relative to the canvas center, and the hexagon is rotated for a spinning appearance.
Open the file in your browser to see a red ball bouncing inside a spinning hexagon!