Here's a simple HTML5 simulation of Earth's formation using JavaScript and the <canvas> element. This will visually represent key stages of Earth's formation (accretion, molten state, cooling crust, atmosphere development) in a stylized way:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Earth Formation Simulation</title>
<style>
body {
margin: 0;
background: #000;
overflow: hidden;
color: white;
font-family: Arial, sans-serif;
}
canvas {
display: block;
}
#controls {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
background: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 8px;
}
</style>
</head>
<body>
<div id="controls">
<button onclick="resetSimulation()">Restart</button>
<p id="stageIndicator">Stage: Accretion</p>
</div>
<canvas id="earthCanvas"></canvas>
<script>
const canvas = document.getElementById('earthCanvas');
const ctx = canvas.getContext('2d');
let width = canvas.width = window.innerWidth;
let height = canvas.height = window.innerHeight;
// Responsive canvas
window.addEventListener('resize', () => {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
});
// Simulation variables
let stage = 'accretion';
let progress = 0;
let particles = [];
let rotation = 0;
// Earth properties
const earth = {
x: width/2,
y: height/2,
radius: 0,
coreRadius: 0,
moltenGlow: 0,
crustSegments: [],
atmosphereAlpha: 0,
cloudOffset: 0
};
// Create particle field for accretion
function createParticles() {
particles = [];
for(let i = 0; i < 300; i++) {
particles.push({
angle: Math.random() * Math.PI * 2,
distance: 200 + Math.random() * 400,
size: Math.random() * 2 + 1,
captured: false
});
}
}
function resetSimulation() {
progress = 0;
stage = 'accretion';
earth.radius = 0;
createParticles();
document.getElementById('stageIndicator').textContent = 'Stage: Accretion';
}
// Animation loop
function animate() {
ctx.clearRect(0, 0, width, height);
// Rotation for dynamic effect
rotation += 0.005;
// Handle each stage
switch(stage) {
case 'accretion':
updateAccretion();
drawAccretion();
break;
case 'molten':
updateMolten();
drawMolten();
break;
case 'crust':
updateCrust();
drawCrust();
break;
case 'atmosphere':
updateAtmosphere();
drawAtmosphere();
break;
}
requestAnimationFrame(animate);
}
// Stage 1: Accretion
function updateAccretion() {
progress += 0.002;
if(progress >= 1) {
progress = 0;
stage = 'molten';
document.getElementById('stageIndicator').textContent = 'Stage: Molten Earth';
}
// Move particles toward center
particles.forEach(p => {
if(!p.captured) {
p.distance -= 0.5;
if(p.distance <= earth.radius + 5) {
p.captured = true;
earth.radius += p.size * 0.1;
}
}
});
}
function drawAccretion() {
// Draw space dust
ctx.fillStyle = '#888';
particles.forEach(p => {
const x = earth.x + Math.cos(p.angle) * p.distance;
const y = earth.y + Math.sin(p.angle) * p.distance;
ctx.beginPath();
ctx.arc(x, y, p.size, 0, Math.PI*2);
ctx.fill();
});
// Growing core
ctx.beginPath();
ctx.arc(earth.x, earth.y, earth.radius, 0, Math.PI*2);
ctx.fillStyle = '#ffcc00';
ctx.fill();
}
// Stage 2: Molten Earth
function updateMolten() {
progress += 0.001;
if(progress >= 1) {
progress = 0;
stage = 'crust';
document.getElementById('stageIndicator').textContent = 'Stage: Crust Formation';
generateCrustSegments();
}
earth.coreRadius = earth.radius * (0.5 + progress * 0.3);
earth.moltenGlow = Math.sin(progress * Math.PI);
}
function drawMolten() {
// Core glow
const gradient = ctx.createRadialGradient(
earth.x, earth.y, earth.coreRadius * 0.5,
earth.x, earth.y, earth.coreRadius
);
gradient.addColorStop(0, '#ff9900');
gradient.addColorStop(1, '#ff0000');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(earth.x, earth.y, earth.coreRadius, 0, Math.PI*2);
ctx.fill();
// Outer molten layer
ctx.beginPath();
ctx.arc(earth.x, earth.y, earth.radius, 0, Math.PI*2);
ctx.fillStyle = `rgba(255, 69, 0, ${0.3 + earth.moltenGlow * 0.5})`;
ctx.fill();
}
// Generate crust segments
function generateCrustSegments() {
earth.crustSegments = [];
for(let i = 0; i < 12; i++) {
earth.crustSegments.push({
angle: Math.random() * Math.PI * 2,
size: Math.random() * 0.3 + 0.1,
offset: Math.random() * Math.PI * 2
});
}
}
// Stage 3: Crust Formation
function updateCrust() {
progress += 0.001;
if(progress >= 1) {
progress = 0;
stage = 'atmosphere';
document.getElementById('stageIndicator').textContent = 'Stage: Atmosphere Development';
}
// Animate crust plates
earth.crustSegments.forEach(seg => {
seg.angle += 0.0005 * progress;
});
}
function drawCrust() {
// Draw molten base
ctx.beginPath();
ctx.arc(earth.x, earth.y, earth.radius, 0, Math.PI*2);
ctx.fillStyle = '#1a1a1a';
ctx.fill();
// Draw crust segments
earth.crustSegments.forEach(seg => {
const segmentAngle = seg.angle + seg.offset * Math.sin(progress * Math.PI);
ctx.save();
ctx.translate(earth.x, earth.y);
ctx.rotate(segmentAngle);
ctx.beginPath();
ctx.moveTo(0, -earth.radius * 0.95);
ctx.arc(0, -earth.radius * 0.95, earth.radius * seg.size, 0, Math.PI);
ctx.fillStyle = '#4a4a4a';
ctx.fill();
ctx.restore();
});
}
// Stage 4: Atmosphere
function updateAtmosphere() {
progress += 0.001;
earth.atmosphereAlpha = Math.min(0.4, progress * 0.5);
earth.cloudOffset += 0.001;
}
function drawAtmosphere() {
// Draw planet surface
drawCrust();
// Atmosphere glow
const gradient = ctx.createRadialGradient(
earth.x, earth.y, earth.radius,
earth.x, earth.y, earth.radius + 30
);
gradient.addColorStop(0, 'rgba(100, 149, 237, 0)');
gradient.addColorStop(1, 'rgba(100, 149, 237, 0.4)');
ctx.beginPath();
ctx.arc(earth.x, earth.y, earth.radius + 15, 0, Math.PI*2);
ctx.fillStyle = gradient;
ctx.fill();
// Clouds
ctx.save();
ctx.translate(earth.x, earth.y);
ctx.rotate(earth.cloudOffset);
ctx.globalAlpha = 0.3;
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.arc(-30, -15, 15, 0, Math.PI*2);
ctx.arc(20, -20, 12, 0, Math.PI*2);
ctx.arc(0, 10, 18, 0, Math.PI*2);
ctx.fill();
ctx.globalAlpha = 1;
ctx.restore();
}
// Initialize and start
resetSimulation();
animate();
</script>
</body>
</html>
Key Features:
-
Four Stages of Earth's Formation:
- Accretion (particles forming a protoplanet)
- Molten Earth (glowing core with liquid magma)
- Crust Formation (tectonic-like segments)
- Atmosphere Development (blue atmosphere with clouds)
-
Interactive Controls:
- Restart button to reset the simulation
- Stage indicator showing current formation phase
-
Visual Elements:
- Dynamic particle system for accretion
- Radial gradients for molten glow
- Animated tectonic segments
- Semi-transparent atmosphere with moving clouds
How to Use:
- Save this code as
earth-simulation.html
- Open in any modern browser
- Click "Restart" to reset the simulation
Educational Value:
This project demonstrates:
- HTML5 canvas animation
- JavaScript simulation techniques
- Basic astronomy concepts
- Staged process visualization
Possible Enhancements:
- Add sound effects for different stages
- Include educational tooltips with scientific facts
- Add a timeline scrubber
- Implement more accurate planetary physics
- Add mobile touch controls
Would you like me to explain any specific part in more detail or help you expand on this simulation?