Solar System Simulation in a Single HTML File
Here's a complete solar system simulation in a single HTML file. This simulation uses HTML5 Canvas and JavaScript to create a simple 2D representation of our solar system with planets orbiting the Sun.
<!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>
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
background-color: #000;
color: white;
font-family: Arial, sans-serif;
overflow: hidden;
}
canvas {
border: 1px solid #333;
margin-top: 20px;
}
.controls {
margin: 10px;
display: flex;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
}
button {
padding: 8px 15px;
background-color: #333;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #555;
}
.info {
margin: 10px;
text-align: center;
max-width: 800px;
}
</style>
</head>
<body>
<h1>Solar System Simulation</h1>
<div class="info">
<p>This is a simplified 2D simulation of our solar system. The sizes and distances are not to scale.</p>
</div>
<div class="controls">
<button id="startBtn">Start</button>
<button id="pauseBtn">Pause</button>
<button id="resetBtn">Reset</button>
<button id="speedUpBtn">Speed Up</button>
<button id="slowDownBtn">Slow Down</button>
<span>Speed: <span id="speedDisplay">1x</span></span>
</div>
<canvas id="solarSystem" width="800" height="600"></canvas>
<script>
// Canvas setup
const canvas = document.getElementById('solarSystem');
const ctx = canvas.getContext('2d');
// Simulation parameters
let animationId;
let isRunning = false;
let speed = 1;
let lastTime = 0;
let timeAccumulator = 0;
const timeStep = 1000 / 60; // 60 FPS
// Solar system data (simplified)
const solarSystem = {
sun: {
name: "Sun",
radius: 20,
color: "#FFD700",
x: canvas.width / 2,
y: canvas.height / 2,
mass: 1.989e30
},
planets: [
{
name: "Mercury",
radius: 4,
color: "#A9A9A9",
distance: 50,
angle: 0,
speed: 0.02 * speed,
orbitalSpeed: 0.02,
mass: 3.3011e23
},
{
name: "Venus",
radius: 6,
color: "#E6C229",
distance: 70,
angle: 0,
speed: 0.015 * speed,
orbitalSpeed: 0.015,
mass: 4.8675e24
},
{
name: "Earth",
radius: 6,
color: "#1DA1F2",
distance: 100,
angle: 0,
speed: 0.01 * speed,
orbitalSpeed: 0.01,
mass: 5.972e24,
moon: {
radius: 2,
color: "#FFFFFF",
distance: 15,
angle: 0,
speed: 0.05 * speed,
orbitalSpeed: 0.05
}
},
{
name: "Mars",
radius: 5,
color: "#C1440E",
distance: 130,
angle: 0,
speed: 0.008 * speed,
orbitalSpeed: 0.008,
mass: 6.417e23
},
{
name: "Jupiter",
radius: 12,
color: "#C88B38",
distance: 180,
angle: 0,
speed: 0.004 * speed,
orbitalSpeed: 0.004,
mass: 1.898e27
},
{
name: "Saturn",
radius: 10,
color: "#D8BFD8",
distance: 230,
angle: 0,
speed: 0.003 * speed,
orbitalSpeed: 0.003,
mass: 5.683e26,
ring: {
innerRadius: 12,
outerRadius: 20,
color: "rgba(200, 200, 200, 0.5)"
}
},
{
name: "Uranus",
radius: 8,
color: "#AFEEEE",
distance: 280,
angle: 0,
speed: 0.002 * speed,
orbitalSpeed: 0.002,
mass: 8.681e25
},
{
name: "Neptune",
radius: 8,
color: "#4682B4",
distance: 320,
angle: 0,
speed: 0.001 * speed,
orbitalSpeed: 0.001,
mass: 1.024e26
}
]
};
// Draw the solar system
function drawSolarSystem() {
// Clear canvas
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw stars in the background
drawStars();
// Draw the Sun
ctx.beginPath();
ctx.arc(solarSystem.sun.x, solarSystem.sun.y, solarSystem.sun.radius, 0, Math.PI * 2);
ctx.fillStyle = solarSystem.sun.color;
ctx.fill();
// Draw planets and their orbits
solarSystem.planets.forEach(planet => {
// Calculate planet position
const planetX = solarSystem.sun.x + planet.distance * Math.cos(planet.angle);
const planetY = solarSystem.sun.y + planet.distance * Math.sin(planet.angle);
// Draw orbit path
ctx.beginPath();
ctx.arc(solarSystem.sun.x, solarSystem.sun.y, planet.distance, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(255, 255, 255, 0.1)";
ctx.stroke();
// Draw planet
ctx.beginPath();
ctx.arc(planetX, planetY, planet.radius, 0, Math.PI * 2);
ctx.fillStyle = planet.color;
ctx.fill();
// Draw planet name
ctx.fillStyle = "white";
ctx.font = "10px Arial";
ctx.fillText(planet.name, planetX - 15, planetY - planet.radius - 5);
// Draw Saturn's rings if it's Saturn
if (planet.name === "Saturn" && planet.ring) {
drawSaturnRings(planetX, planetY, planet.ring);
}
// Draw Earth's moon if it's Earth
if (planet.name === "Earth" && planet.moon) {
const moonX = planetX + planet.moon.distance * Math.cos(planet.moon.angle);
const moonY = planetY + planet.moon.distance * Math.sin(planet.moon.angle);
ctx.beginPath();
ctx.arc(moonX, moonY, planet.moon.radius, 0, Math.PI * 2);
ctx.fillStyle = planet.moon.color;
ctx.fill();
}
});
}
// Draw Saturn's rings
function drawSaturnRings(planetX, planetY, ring) {
ctx.beginPath();
ctx.ellipse(
planetX, planetY,
ring.outerRadius, ring.outerRadius * 0.3,
0, 0, Math.PI * 2
);
ctx.strokeStyle = ring.color;
ctx.lineWidth = ring.outerRadius - ring.innerRadius;
ctx.stroke();
}
// Draw random stars in the background
function drawStars() {
// Only draw stars once
if (canvas.starsDrawn) return;
for (let i = 0; i < 200; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const radius = Math.random() * 1.5;
const opacity = Math.random() * 0.8 + 0.2;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = `rgba(255, 255, 255, ${opacity})`;
ctx.fill();
}
canvas.starsDrawn = true;
}
// Update planet positions
function updatePlanets(deltaTime) {
const timeFactor = deltaTime / 16; // Normalize for 60 FPS
solarSystem.planets.forEach(planet => {
// Update planet angle
planet.angle += planet.orbitalSpeed * speed * timeFactor;
// Update moon angle if it's Earth
if (planet.name === "Earth" && planet.moon) {
planet.moon.angle += planet.moon.orbitalSpeed * speed * timeFactor;
}
});
}
// Animation loop
function animate(currentTime) {
if (!isRunning) return;
// Calculate delta time
const deltaTime = currentTime - lastTime;
lastTime = currentTime;
// Accumulate time for fixed time step
timeAccumulator += deltaTime;
// Update at fixed time steps
while (timeAccumulator >= timeStep) {
updatePlanets(timeStep);
timeAccumulator -= timeStep;
}
// Draw the solar system
drawSolarSystem();
// Request next frame
animationId = requestAnimationFrame(animate);
}
// Start the simulation
function startSimulation() {
if (isRunning) return;
isRunning = true;
lastTime = performance.now();
animationId = requestAnimationFrame(animate);
}
// Pause the simulation
function pauseSimulation() {
isRunning = false;
if (animationId) {
cancelAnimationFrame(animationId);
}
}
// Reset the simulation
function resetSimulation() {
pauseSimulation();
// Reset planet angles
solarSystem.planets.forEach(planet => {
planet.angle = 0;
if (planet.moon) {
planet.moon.angle = 0;
}
});
// Redraw the solar system
drawSolarSystem();
}
// Change simulation speed
function changeSpeed(factor) {
speed *= factor;
document.getElementById('speedDisplay').textContent = speed.toFixed(1) + "x";
// Update planet speeds
solarSystem.planets.forEach(planet => {
planet.speed = planet.orbitalSpeed * speed;
if (planet.moon) {
planet.moon.speed = planet.moon.orbitalSpeed * speed;
}
});
}
// Event listeners
document.getElementById('startBtn').addEventListener('click', startSimulation);
document.getElementById('pauseBtn').addEventListener('click', pauseSimulation);
document.getElementById('resetBtn').addEventListener('click', resetSimulation);
document.getElementById('speedUpBtn').addEventListener('click', () => changeSpeed(2));
document.getElementById('slowDownBtn').addEventListener('click', () => changeSpeed(0.5));
// Initialize
drawSolarSystem();
</script>
</body>
</html>
Features of this Solar System Simulation:
-
Interactive Controls:
- Start/Pause/Reset buttons
- Speed controls (speed up/slow down)
- Current speed display
-
Solar System Elements:
- The Sun at the center
- All 8 planets with their relative sizes and colors
- Earth's Moon
- Saturn's rings
- Starry background
-
Simulation Details:
- Planets orbit the Sun at different speeds
- Orbits are shown as faint circles
- Planet names are displayed
- The simulation uses a fixed time step for consistent movement
-
Technical Implementation:
- Pure HTML5 Canvas and JavaScript
- No external dependencies
- Responsive design
- Animation loop with delta time for smooth movement
You can save this code as a single HTML file and open it in any modern web browser to see the solar system simulation in action.