Here's a complete, interactive Solar System simulation in a single HTML file, using only HTML, CSS, and JavaScript (Canvas API). It features:
- 🌞 The Sun at the center
- 🪐 8 planets orbiting realistically (relative speed & distance)
- 🧭 Mouse controls: drag to pan, scroll to zoom
- ⏯️ Play/Pause toggle
- 🐢/🐇 Speed control (slow down or speed up orbits)
- ✨ Optional orbital trails
- 🌟 A starry background
✅ Just copy-paste this into a .html file and open it in your browser!
<!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;
overflow: hidden;
background-color: black;
font-family: sans-serif;
}
canvas {
display: block;
cursor: grab;
}
.ui {
position: absolute;
top: 15px;
left: 15px;
color: white;
z-index: 100;
background: rgba(0, 0, 0, 0.6);
padding: 10px;
border-radius: 8px;
}
.ui button {
margin: 5px;
padding: 6px 12px;
background: #222;
color: white;
border: 1px solid #444;
cursor: pointer;
border-radius: 4px;
}
.ui button:hover {
background: #333;
}
.info {
position: absolute;
bottom: 10px;
left: 10px;
color: white;
font-size: 12px;
opacity: 0.7;
}
</style>
</head>
<body>
<canvas id="solarSystem"></canvas>
<div class="ui">
<button onclick="togglePause()">Pause / Play</button>
<button onclick="resetView()">Reset View</button>
<button onclick="toggleTrails()">Toggle Trails</button>
<button onclick="changeSpeed(0.5)">Slow Down</button>
<button onclick="changeSpeed(2)">Speed Up</button>
</div>
<div class="info">Drag to Pan • Scroll to Zoom</div>
<script>
const canvas = document.getElementById("solarSystem");
const ctx = canvas.getContext("2d");
let width = window.innerWidth;
let height = window.innerHeight;
canvas.width = width;
canvas.height = height;
window.addEventListener("resize", () => {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
});
// Simulation state
let paused = false;
let showTrails = true;
let simSpeed = 1;
let zoom = 1;
let offsetX = 0;
let offsetY = 0;
let dragging = false;
let lastX = 0;
let lastY = 0;
// Planet data
const planets = [
{
name: "Mercury",
color: "#A9A9A9",
size: 4,
distance: 60,
speed: 0.04,
angle: Math.random() * Math.PI * 2,
trail: []
},
{
name: "Venus",
color: "#FFD700",
size: 6,
distance: 90,
speed: 0.03,
angle: Math.random() * Math.PI * 2,
trail: []
},
{
name: "Earth",
color: "#4682B4",
size: 6,
distance: 120,
speed: 0.025,
angle: Math.random() * Math.PI * 2,
trail: []
},
{
name: "Mars",
color: "#DC143C",
size: 5,
distance: 150,
speed: 0.02,
angle: Math.random() * Math.PI * 2,
trail: []
},
{
name: "Jupiter",
color: "#F4A460",
size: 14,
distance: 220,
speed: 0.01,
angle: Math.random() * Math.PI * 2,
trail: []
},
{
name: "Saturn",
color: "#DAA520",
size: 12,
distance: 300,
speed: 0.008,
angle: Math.random() * Math.PI * 2,
trail: [],
rings: true
},
{
name: "Uranus",
color: "#40E0D0",
size: 9,
distance: 370,
speed: 0.006,
angle: Math.random() * Math.PI * 2,
trail: []
},
{
name: "Neptune",
color: "#0000CD",
size: 9,
distance: 440,
speed: 0.005,
angle: Math.random() * Math.PI * 2,
trail: []
}
];
// Controls
function togglePause() {
paused = !paused;
}
function resetView() {
zoom = 1;
offsetX = 0;
offsetY = 0;
}
function toggleTrails() {
showTrails = !showTrails;
if (!showTrails) planets.forEach(p => p.trail = []);
}
function changeSpeed(factor) {
simSpeed *= factor;
simSpeed = Math.max(0.1, Math.min(5, simSpeed));
}
// Coordinate transformation
function tx(x) { return (x + offsetX) * zoom + width / 2; }
function ty(y) { return (y + offsetY) * zoom + height / 2; }
// Starfield
const stars = [];
for (let i = 0; i < 200; i++) {
stars.push({
x: Math.random() * width * 2 - width,
y: Math.random() * height * 2 - height,
size: Math.random() * 1.5 + 0.5
});
}
function drawStars() {
ctx.fillStyle = "white";
stars.forEach(s => {
ctx.beginPath();
ctx.arc(tx(s.x), ty(s.y), s.size, 0, Math.PI * 2);
ctx.fill();
});
}
// Draw sun
function drawSun() {
const x = tx(0);
const y = ty(0);
const r = 20 * zoom;
// Glow
const gradient = ctx.createRadialGradient(x, y, 0, x, y, r * 2);
gradient.addColorStop(0, "rgba(255,255,0,0.8)");
gradient.addColorStop(0.5, "rgba(255,165,0,0.4)");
gradient.addColorStop(1, "rgba(255,0,0,0.1)");
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(x, y, r * 2, 0, Math.PI * 2);
ctx.fill();
// Core
ctx.fillStyle = "#FFD700";
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fill();
}
// Draw orbit circles
function drawOrbits() {
ctx.strokeStyle = "rgba(255,255,255,0.1)";
ctx.lineWidth = 1;
planets.forEach(p => {
ctx.beginPath();
ctx.arc(tx(0), ty(0), p.distance * zoom, 0, Math.PI * 2);
ctx.stroke();
});
}
// Draw planet
function drawPlanet(p) {
const px = Math.cos(p.angle) * p.distance;
const py = Math.sin(p.angle) * p.distance;
const cx = tx(px);
const cy = ty(py);
const r = p.size * zoom;
// Trail
if (showTrails && p.trail.length > 1) {
ctx.strokeStyle = `${p.color}88`;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(tx(p.trail[0].x), ty(p.trail[0].y));
for (let i = 1; i < p.trail.length; i++) {
ctx.lineTo(tx(p.trail[i].x), ty(p.trail[i].y));
}
ctx.stroke();
}
// Saturn Rings
if (p.rings && r > 3) {
ctx.strokeStyle = "rgba(218,165,32,0.6)";
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.ellipse(cx, cy, r * 2, r * 0.5, 0, 0, Math.PI * 2);
ctx.stroke();
}
// Planet
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, Math.PI * 2);
ctx.fill();
// Highlight
ctx.fillStyle = "rgba(255,255,255,0.3)";
ctx.beginPath();
ctx.arc(cx - r * 0.3, cy - r * 0.3, r * 0.3, 0, Math.PI * 2);
ctx.fill();
}
// Update positions
function updatePlanets() {
if (paused) return;
planets.forEach(p => {
p.angle += p.speed * simSpeed;
const x = Math.cos(p.angle) * p.distance;
const y = Math.sin(p.angle) * p.distance;
p.trail.push({ x, y });
if (p.trail.length > 200) p.trail.shift();
});
}
// Main loop
function animate() {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, width, height);
drawStars();
drawOrbits();
drawSun();
updatePlanets();
planets.forEach(drawPlanet);
requestAnimationFrame(animate);
}
// Mouse events
canvas.addEventListener("mousedown", e => {
dragging = true;
lastX = e.clientX;
lastY = e.clientY;
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("mousemove", e => {
if (dragging) {
const dx = e.clientX - lastX;
const dy = e.clientY - lastY;
offsetX += dx / zoom;
offsetY += dy / zoom;
lastX = e.clientX;
lastY = e.clientY;
}
});
canvas.addEventListener("mouseup", () => {
dragging = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener("wheel", e => {
e.preventDefault();
const factor = e.deltaY < 0 ? 1.1 : 0.9;
zoom *= factor;
zoom = Math.max(0.2, Math.min(4, zoom));
});
animate(); // Start animation
</script>
</body>
</html>
🔭 Features Recap
| Feature | Description |
|---|
| 🌞 Sun | Central glowing object with radial gradient |
| 🪐 Planets | Mercury to Neptune with realistic relative speeds |
| 🧭 Interaction | Drag to move, scroll to zoom |
| ⏱ Speed Control | Slow down or speed up time |
| 🔄 Pause | Stop or resume motion |
| 💫 Orbital Trails | Toggleable visual paths behind each planet |
| 🌌 Background Stars | Randomly generated twinkling stars |
Let me know if you'd like to add:
- Moons (like Earth’s Moon)
- Planet labels
- Elliptical orbits
- Click-to-select planet info
Happy coding! 🌌🚀