要创建一个单个 HTML 文件,其中包含两个小球在六边形内的运动动画,并且考虑到重力、摩擦力和弹力的作用,可以使用 HTML、CSS 和 JavaScript。以下是一个示例代码,展示了如何实现这个动画。
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Six-Sided Animation</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
.container {
display: flex;
gap: 50px;
}
.hexagon {
position: relative;
width: 200px;
height: 200px;
border: 2px solid black;
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
}
.ball {
position: absolute;
width: 20px;
height: 20px;
background-color: red;
border-radius: 50%;
}
</style>
</head>
<body>
<div class="container">
<div class="hexagon" id="earthHexagon">
<div class="ball" id="earthBall"></div>
</div>
<div class="hexagon" id="moonHexagon">
<div class="ball" id="moonBall"></div>
</div>
</div>
<script>
const earthHexagon = document.getElementById('earthHexagon');
const earthBall = document.getElementById('earthBall');
const moonHexagon = document.getElementById('moonHexagon');
const moonBall = document.getElementById('moonBall');
const hexagonWidth = 200;
const hexagonHeight = 200;
const ballRadius = 10;
function getHexagonVertices() {
const vertices = [];
const angle = Math.PI / 3;
for (let i = 0; i < 6; i++) {
const x = hexagonWidth / 2 + (hexagonWidth / 2) * Math.cos(angle * i);
const y = hexagonHeight / 2 + (hexagonHeight / 2) * Math.sin(angle * i);
vertices.push({ x, y });
}
return vertices;
}
const vertices = getHexagonVertices();
function getDistanceToEdge(x, y) {
let minDistance = Infinity;
for (let i = 0; i < 6; i++) {
const next = (i + 1) % 6;
const dx = vertices[next].x - vertices[i].x;
const dy = vertices[next].y - vertices[i].y;
const length = Math.sqrt(dx * dx + dy * dy);
const t = ((x - vertices[i].x) * dx + (y - vertices[i].y) * dy) / (length * length);
if (t < 0) {
const distance = Math.sqrt((x - vertices[i].x) ** 2 + (y - vertices[i].y) ** 2);
if (distance < minDistance) minDistance = distance;
} else if (t > 1) {
const distance = Math.sqrt((x - vertices[next].x) ** 2 + (y - vertices[next].y) ** 2);
if (distance < minDistance) minDistance = distance;
} else {
const distance = Math.abs((y - vertices[i].y) * dx - (x - vertices[i].x) * dy) / length;
if (distance < minDistance) minDistance = distance;
}
}
return minDistance;
}
function updateBallPosition(ball, gravity, friction, springConstant) {
let x = parseFloat(ball.style.left) || hexagonWidth / 2;
let y = parseFloat(ball.style.top) || hexagonHeight / 2;
let vx = 0;
let vy = 0;
function animate() {
const distanceToEdge = getDistanceToEdge(x, y);
const force = springConstant * (ballRadius - distanceToEdge);
const frictionForce = friction * Math.sqrt(vx * vx + vy * vy);
vx += force * Math.cos(Math.atan2(y - hexagonHeight / 2, x - hexagonWidth / 2)) / 100 - frictionForce * vx / 100;
vy += force * Math.sin(Math.atan2(y - hexagonHeight / 2, x - hexagonWidth / 2)) / 100 - frictionForce * vy / 100 - gravity / 100;
x += vx;
y += vy;
if (x < 0) x = 0;
if (x > hexagonWidth) x = hexagonWidth;
if (y < 0) y = 0;
if (y > hexagonHeight) y = hexagonHeight;
ball.style.left = `${x}px`;
ball.style.top = `${y}px`;
requestAnimationFrame(animate);
}
animate();
}
updateBallPosition(earthBall, 9.8, 0.1, 100);
updateBallPosition(moonBall, 1.6, 0.05, 50);
</script>
</body>
</html>