Here's a complete solar system simulation in a single HTML file using HTML5 Canvas and JavaScript. This simulation includes the Sun and planets with basic orbital motion:
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>Solar System Simulation</title>
<style>
body {
margin: 0;
background: #000;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
overflow: hidden;
}
canvas {
border: 1px solid #333;
}
</style>
</head>
<body>
<canvas id="solarSystem" width="800" height="800"></canvas>
<script>
const canvas = document.getElementById('solarSystem');
const ctx = canvas.getContext('2d');
// Solar system data (simplified)
const solarSystem = {
sun: {
radius: 30,
color: '#ffeb3b'
},
planets: [
{ name: 'Mercury', radius: 4, distance: 60, speed: 4.1, color: '#8d8d8d', angle: 0 },
{ name: 'Venus', radius: 6, distance: 90, speed: 1.6, color: '#ff9800', angle: 0 },
{ name: 'Earth', radius: 7, distance: 120, speed: 1, color: '#2196f3', angle: 0 },
{ name: 'Mars', radius: 5, distance: 150, speed: 0.8, color: '#f44336', angle: 0 },
{ name: 'Jupiter', radius: 14, distance: 200, speed: 0.4, color: '#ff5722', angle: 0 },
{ name: 'Saturn', radius: 12, distance: 250, speed: 0.3, color: '#ffeb3b', angle: 0 },
{ name: 'Uranus', radius: 10, distance: 300, speed: 0.2, color: '#4dd0e1', angle: 0 },
{ name: 'Neptune', radius: 10, distance: 350, speed: 0.1, color: '#2196f3', angle: 0 }
]
};
// Center of the canvas
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Animation variables
let animationFrameId;
function drawSun() {
ctx.beginPath();
ctx.arc(centerX, centerY, solarSystem.sun.radius, 0, Math.PI * 2);
ctx.fillStyle = solarSystem.sun.color;
ctx.fill();
ctx.closePath();
}
function drawOrbit(distance) {
ctx.beginPath();
ctx.arc(centerX, centerY, distance, 0, Math.PI * 2);
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.stroke();
ctx.closePath();
}
function drawPlanet(planet) {
// Calculate planet position
const x = centerX + planet.distance * Math.cos(planet.angle);
const y = centerY + planet.distance * Math.sin(planet.angle);
// Draw orbit
drawOrbit(planet.distance);
// Draw planet
ctx.beginPath();
ctx.arc(x, y, planet.radius, 0, Math.PI * 2);
ctx.fillStyle = planet.color;
ctx.fill();
ctx.closePath();
// Add planet name
ctx.fillStyle = 'white';
ctx.font = '10px Arial';
ctx.fillText(planet.name, x + planet.radius + 5, y);
}
function updatePlanets() {
solarSystem.planets.forEach(planet => {
// Update angle based on speed (simplified orbital mechanics)
planet.angle += planet.speed * 0.02;
});
}
function animate() {
// Clear canvas
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw sun
drawSun();
// Draw and update planets
solarSystem.planets.forEach(planet => {
drawPlanet(planet);
});
// Update planet positions
updatePlanets();
// Request next frame
animationFrameId = requestAnimationFrame(animate);
}
// Start animation
animate();
// Clean up on page unload
window.addEventListener('unload', () => {
cancelAnimationFrame(animationFrameId);
});
</script>
</body>
</html>
The simulation is self-contained in a single file and will run in any modern web browser without additional dependencies.