Below is a simple example of a solar system simulation using HTML, CSS, and JavaScript. This simulation will include the Sun and the eight planets (Mercury to Neptune) in our solar system. Note that this is a highly simplified model and does not account for the actual orbital mechanics or relative sizes and distances of the celestial bodies.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Solar System Simulation</title>
<style>
/* Add some basic styling to our simulation */
body {
background-color: #000;
overflow: hidden;
}
#solar-system {
position: relative;
width: 600px;
height: 600px;
border: 1px solid white;
margin: 20px auto;
}
.planet {
position: absolute;
border-radius: 50%;
background-color: #fff;
}
#sun {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #ff9900;
}
</style>
</head>
<body>
<div id="solar-system">
<div id="sun"></div>
<!-- Planets will be added dynamically -->
</div>
<script>
// Define the planets and their properties
const planets = [
{ name: 'Mercury', radius: 5, orbit: 50, speed: 0.05, color: '#ccc' },
{ name: 'Venus', radius: 10, orbit: 80, speed: 0.03, color: '#fff' },
{ name: 'Earth', radius: 12, orbit: 110, speed: 0.02, color: '#0ff' },
{ name: 'Mars', radius: 8, orbit: 140, speed: 0.01, color: '#f00' },
{ name: 'Jupiter', radius: 20, orbit: 200, speed: 0.005, color: '#ff0' },
{ name: 'Saturn', radius: 18, orbit: 250, speed: 0.003, color: '#0f0' },
{ name: 'Uranus', radius: 15, orbit: 300, speed: 0.002, color: '#00f' },
{ name: 'Neptune', radius: 12, orbit: 350, speed: 0.001, color: '#f0f' },
];
// Function to create a planet element
function createPlanet(planet, index) {
const planetElement = document.createElement('div');
planetElement.classList.add('planet');
planetElement.style.width = `${planet.radius}px`;
planetElement.style.height = `${planet.radius}px`;
planetElement.style.background = planet.color;
planetElement.style.top = `50%`;
planetElement.style.left = `50%`;
planetElement.style.transform = `translate(-50%, -50%) translate(${planet.orbit}px, 0) rotate(0deg)`;
planetElement.style.transformOrigin = 'center';
planetElement.style.transition = 'transform 0s linear';
document.getElementById('solar-system').appendChild(planetElement);
// Animate the planet
let angle = 0;
function animate() {
angle += planet.speed;
planetElement.style.transform = `translate(-50%, -50%) translate(${planet.orbit}px, 0) rotate(${angle}deg)`;
requestAnimationFrame(animate);
}
animate();
}
// Create and animate each planet
planets.forEach(createPlanet);
</script>
</body>
</html>
This code creates a basic simulation where each planet orbits around the Sun. The size, orbit distance, speed, and color of each planet are defined in the planets array. The createPlanet function dynamically creates a div element for each planet and animates its movement around the Sun using the requestAnimationFrame function.
Please note, this is a simplified model and doesn't reflect the actual complexities of planetary orbits, such as elliptical paths and varying speeds. For a more accurate simulation, consider using a library or framework that supports complex astronomical calculations.