Okay, creating a true 3D planet Earth with realistic lighting, terrain, and interactions in pure HTML, CSS, and JS without external libraries like Three.js is quite complex. What we can achieve is a very convincing illusion of a 3D sphere with a texture map that rotates.
Here's a version that creates a rotating textured sphere, which is the most common way to represent this:
1. HTML (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Planet Earth</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="scene">
<div class="earth-container">
<div class="earth">
<div class="clouds"></div>
</div>
<div class="axis-tilt"></div> <!-- Optional: to visualize tilt -->
</div>
</div>
<div class="controls">
<label for="rotationSpeed">Rotation Speed:</label>
<input type="range" id="rotationSpeed" min="0.1" max="2" step="0.1" value="0.5">
<p>Drag the Earth to rotate it manually.</p>
</div>
<script src="script.js"></script>
</body>
</html>
2. CSS (style.css):
body {
margin: 0;
height: 100vh;
display: flex;
flex-direction: column; /* Stack scene and controls */
align-items: center;
justify-content: center;
background-color: #000010; /* Dark space background */
color: #fff;
font-family: Arial, sans-serif;
overflow: hidden; /* Prevent scrollbars */
}
.scene {
width: 300px; /* Adjust size as needed */
height: 300px;
perspective: 1000px; /* This creates the 3D space */
margin-bottom: 20px;
}
.earth-container {
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d;
/* Initial tilt like Earth's axis */
transform: rotateX(-23.5deg);
}
.earth {
width: 100%;
height: 100%;
position: absolute;
border-radius: 50%;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/The_Earth_seen_from_Apollo_17.jpg/1024px-The_Earth_seen_from_Apollo_17.jpg'); /* Replace with your preferred Earth texture */
/* A common texture: 'https://www.solarsystemscope.com/textures/download/2k_earth_daymap.jpg' */
/* Another good one: 'https://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73909/world.topo.bathy.200412.3x5400x2700.jpg' (very large) */
background-size: cover; /* Or 200% 100% for equirectangular maps to wrap better */
background-position: center;
box-shadow:
inset -20px -15px 40px rgba(0,0,0,0.7), /* Inner shadow for depth */
0 0 30px rgba(0, 150, 255, 0.3); /* Outer atmospheric glow */
transform-style: preserve-3d;
cursor: grab;
}
.clouds {
width: 101%; /* Slightly larger to appear above surface */
height: 101%;
position: absolute;
top: -0.5%;
left: -0.5%;
border-radius: 50%;
background-image: url('https://www.solarsystemscope.com/textures/download/2k_earth_clouds.jpg'); /* Cloud texture */
background-size: cover; /* Or 200% 100% if equirectangular */
background-position: center;
opacity: 0.4; /* Adjust for cloud visibility */
mix-blend-mode: screen; /* Blends well with the earth texture */
transform-style: preserve-3d;
}
/* Optional: visualize the axis tilt */
.axis-tilt {
position: absolute;
width: 5px;
height: 150%; /* Longer than the earth's diameter */
background: rgba(255, 0, 0, 0.5);
top: -25%;
left: calc(50% - 2.5px);
transform-origin: center;
/* This element doesn't rotate with the earth, it shows the container's tilt */
}
.controls {
text-align: center;
padding: 10px;
background-color: rgba(50,50,50,0.5);
border-radius: 8px;
}
.controls label, .controls input {
margin: 5px;
}
3. JavaScript (script.js):
const earth = document.querySelector('.earth');
const clouds = document.querySelector('.clouds');
const earthContainer = document.querySelector('.earth-container');
const rotationSpeedSlider = document.getElementById('rotationSpeed');
let currentYRotation = 0;
let currentXRotation = -23.5; // Corresponds to initial CSS tilt
let autoRotationSpeed = 0.5; // Degrees per frame for Y-axis
let isDragging = false;
let previousMouseX = 0;
let previousMouseY = 0;
function updateRotationSpeed() {
autoRotationSpeed = parseFloat(rotationSpeedSlider.value);
}
rotationSpeedSlider.addEventListener('input', updateRotationSpeed);
updateRotationSpeed(); // Initialize
// --- Mouse Drag Functionality ---
earth.addEventListener('mousedown', (e) => {
isDragging = true;
previousMouseX = e.clientX;
previousMouseY = e.clientY;
earth.style.cursor = 'grabbing';
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const deltaX = e.clientX - previousMouseX;
const deltaY = e.clientY - previousMouseY;
currentYRotation += deltaX * 0.5; // Adjust sensitivity
currentXRotation -= deltaY * 0.5; // Adjust sensitivity
// Clamp X rotation to avoid flipping upside down too easily
currentXRotation = Math.max(-90, Math.min(90, currentXRotation));
// Apply rotation to the container for overall tilt and drag
earthContainer.style.transform = `rotateX(${currentXRotation}deg) rotateY(${currentYRotation}deg)`;
// The earth itself only needs to rotate on its Y axis for texture mapping
// but if we want clouds to move independently during drag, we'd need to adjust its background or a separate rotation
// For simplicity here, we'll just let the container handle drag rotation.
previousMouseX = e.clientX;
previousMouseY = e.clientY;
});
document.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
earth.style.cursor = 'grab';
}
});
document.addEventListener('mouseleave', () => { // In case mouse leaves window while dragging
if (isDragging) {
isDragging = false;
earth.style.cursor = 'grab';
}
});
// --- Animation Loop ---
let earthBgOffsetX = 0;
let cloudsBgOffsetX = 0;
function animate() {
if (!isDragging) {
// Auto-rotate the Earth (texture position)
earthBgOffsetX -= autoRotationSpeed * 0.2; // Adjust multiplier for speed
earth.style.backgroundPositionX = `${earthBgOffsetX}px`;
// Auto-rotate clouds slightly faster (texture position)
cloudsBgOffsetX -= autoRotationSpeed * 0.25; // Slightly faster for parallax
clouds.style.backgroundPositionX = `${cloudsBgOffsetX}px`;
}
// If you prefer rotating the element itself instead of background-position for auto-rotation:
// This would rotate the entire sphere including its lighting.
// Background position keeps lighting static relative to the "sun".
// if (!isDragging) {
// currentYRotation += autoRotationSpeed;
// earthContainer.style.transform = `rotateX(${currentXRotation}deg) rotateY(${currentYRotation}deg)`;
// }
requestAnimationFrame(animate);
}
animate();
Explanation:
-
HTML Structure:
.scene: A container with perspective to create the 3D illusion.
.earth-container: This element will handle the axial tilt and manual drag rotations (rotateX, rotateY).
.earth: The actual sphere. Its background-image is the Earth texture. border-radius: 50% makes it round.
.clouds: A slightly larger sphere for cloud texture, overlaid on the Earth.
.axis-tilt: An optional visual helper to see the tilt.
.controls: For the rotation speed slider.
-
CSS Styling:
perspective: 1000px; on .scene is crucial for 3D transforms to have depth.
transform-style: preserve-3d; on .earth-container, .earth, and .clouds ensures their children are also rendered in 3D space.
border-radius: 50%; creates the circular shape.
- Earth Texture:
background-image on .earth. You'll need a good equirectangular projection map of Earth. I've provided a few example URLs.
background-size: cover; is a good default. For equirectangular maps, background-size: 200% 100%; can sometimes give a better "wrapping" effect if the texture is designed for it, but cover or specific pixel values based on texture aspect ratio often work well.
- Cloud Texture: Similar to Earth, but with
opacity and mix-blend-mode: screen; for a nice overlay effect.
box-shadow: inset ...: This creates a fake lighting effect, making the sphere look more 3D.
transform: rotateX(-23.5deg); on .earth-container gives the initial axial tilt.
-
JavaScript Logic:
- Variables: Store references to elements, current rotation angles, drag state, and mouse positions.
updateRotationSpeed(): Reads value from slider.
- Mouse Dragging:
mousedown: Sets isDragging to true, records initial mouse position.
How to Use:
- Save the HTML as
index.html.
- Save the CSS as
style.css.
- Save the JavaScript as
script.js.
- Make sure all three files are in the same folder.
- Open
index.html in your web browser.
- Important: The Earth and cloud textures are loaded from URLs. If these URLs become invalid, you'll need to find new ones. Search for "Earth texture map equirectangular" and "cloud texture map equirectangular".
This provides a good-looking, interactive 3D Earth illusion!