Here's a complete HTML + JavaScript file that creates a rotating hexagon with balls bouncing inside. The balls are affected by gravity, collide with the walls of the hexagon, and also bounce off each other using simple physics.
You can copy this code into a .html file and open it in your browser to see it in action.
✅ Features:
- Rotating hexagon (centered on screen)
- Multiple balls bouncing inside
- Gravity applied
- Wall collisions with reflection
- Ball-to-ball elastic collision
- Fully contained in one HTML file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Rotating Hexagon with Bouncing Balls</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #111;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let width, height;
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
}
window.addEventListener("resize", resize);
resize();
// Vector class
class Vec {
constructor(x, y) {
this.x = x || 0;
this.y = y || 0;
}
add(v) {
return new Vec(this.x + v.x, this.y + v.y);
}
sub(v) {
return new Vec(this.x - v.x, this.y - v.y);
}
scale(s) {
return new Vec(this.x * s, this.y * s);
}
mag() {
return Math.hypot(this.x, this.y);
}
norm() {
const m = this.mag();
return m === 0 ? new Vec(0, 0) : this.scale(1 / m);
}
dot(v) {
return this.x * v.x + this.y * v.y;
}
rotate(theta) {
const cos = Math.cos(theta), sin = Math.sin(theta);
return new Vec(this.x * cos - this.y * sin, this.x * sin + this.y * cos);
}
reflect(normal) {
normal = normal.norm();
const dot = this.dot(normal);
return this.sub(normal.scale(2 * dot));
}
}
// Ball object
class Ball {
constructor(pos, vel, radius, color) {
this.pos = pos;
this.vel = vel;
this.radius = radius;
this.color = color;
}
update(dt) {
const gravity = new Vec(0, 900); // gravity strength
this.vel = this.vel.add(gravity.scale(dt));
this.pos = this.pos.add(this.vel.scale(dt));
}
draw(ctx) {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.pos.x, this.pos.y, this.radius, 0, 2 * Math.PI);
ctx.fill();
}
}
// Create random balls
const balls = [];
const numBalls = 15;
for (let i = 0; i < numBalls; i++) {
const r = 15 + Math.random() * 10;
const x = width / 2 + (Math.random() - 0.5) * 300;
const y = height / 2 + (Math.random() - 0.5) * 300;
const vx = (Math.random() - 0.5) * 400;
const vy = (Math.random() - 0.5) * 400;
const color = `hsl(${Math.random() * 360}, 70%, 60%)`;
balls.push(new Ball(new Vec(x, y), new Vec(vx, vy), r, color));
}
// Generate hexagon points
function generateHexagon(center, size, angle) {
const points = [];
for (let i = 0; i < 6; i++) {
const theta = angle + (i * Math.PI) / 3;
const x = center.x + size * Math.cos(theta);
const y = center.y + size * Math.sin(theta);
points.push(new Vec(x, y));
}
return points;
}
let rotationAngle = 0;
function checkBallCollision(b1, b2) {
const delta = b2.pos.sub(b1.pos);
const dist = delta.mag();
if (dist < b1.radius + b2.radius) {
const normal = delta.norm();
const v1n = b1.vel.dot(normal);
const v2n = b2.vel.dot(normal);
if (v1n <= v2n) return;
const dv = v1n - v2n;
const j = (2 * dv) / (b1.mass + b2.mass);
b1.vel = b1.vel.sub(normal.scale(j * b2.mass));
b2.vel = b2.vel.add(normal.scale(j * b1.mass));
// Move them apart slightly to avoid sticking
const overlap = 0.5 * (dist - b1.radius - b2.radius + 0.1);
b1.pos = b1.pos.sub(normal.scale(overlap));
b2.pos = b2.pos.add(normal.scale(overlap));
}
}
function checkWallCollision(ball, hexPoints) {
for (let i = 0; i < hexPoints.length; i++) {
const p1 = hexPoints[i];
const p2 = hexPoints[(i + 1) % hexPoints.length];
const edge = p2.sub(p1);
const normal = new Vec(-edge.y, edge.x).norm(); // outward normal
const toPoint = ball.pos.sub(p1);
const dist = toPoint.dot(normal);
if (dist < ball.radius) {
// Reflect velocity
ball.vel = ball.vel.reflect(normal);
// Push out of wall
ball.pos = ball.pos.sub(normal.scale(dist - ball.radius));
}
}
}
function animate(timeStamp) {
const dt = 0.016; // ~60fps
ctx.clearRect(0, 0, width, height);
const center = new Vec(width / 2, height / 2);
const hexSize = Math.min(width, height) / 3;
rotationAngle += 0.002;
const hexPoints = generateHexagon(center, hexSize, rotationAngle);
// Draw hexagon
ctx.beginPath();
ctx.strokeStyle = "#fff";
ctx.lineWidth = 2;
ctx.moveTo(hexPoints[0].x, hexPoints[0].y);
for (let i = 1; i < 6; i++) {
ctx.lineTo(hexPoints[i].x, hexPoints[i].y);
}
ctx.closePath();
ctx.stroke();
// Update and draw balls
for (let ball of balls) {
ball.update(dt);
checkWallCollision(ball, hexPoints);
}
// Check ball-to-ball collisions
for (let i = 0; i < balls.length; i++) {
for (let j = i + 1; j < balls.length; j++) {
checkBallCollision(balls[i], balls[j]);
}
}
for (let ball of balls) {
ball.draw(ctx);
}
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
🧪 Tips to Try:
- Change
numBalls to increase or decrease the number of bouncing balls.
- Adjust gravity strength or rotation speed for different effects.
- You can change colors, sizes, or even make the hexagon follow the mouse.
Let me know if you want enhancements like sound effects, trails, or interactivity!