A 3D planet Earth! That's a great challenge. Here's a basic implementation using HTML, CSS, and JavaScript. We'll use CSS3 for 3D transformations, HTML5 Canvas for texture mapping, and JavaScript for animations.
Note: This example won't be a photorealistic Earth, but rather a stylized representation. For a more realistic approach, you'd need to use advanced techniques like WebGL, Three.js, or even shaders.
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="styles.css">
</head>
<body>
<div class="earth-container">
<canvas id="earth-canvas" width="512" height="512"></canvas>
<div class="earth-3d"></div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
body {
background-color: #212121;
perspective: 1000px; /* Enable 3D perspective */
}
.earth-container {
position: relative;
width: 512px;
height: 512px;
margin: 50px auto;
transform-style: preserve-3d; /* Preserve 3D transformations */
}
#earth-canvas {
display: none; /* Hide the canvas, we'll use it as a texture source */
}
.earth-3d {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
background-size: cover;
transform: rotateX(20deg) rotateY(20deg); /* Initial rotation */
transition: transform 0.5s ease-in-out;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.earth-3d:hover {
transform: rotateX(20deg) rotateY(380deg); /* Rotate on hover */
}
JavaScript (script.js)
// Get the canvas and context
const canvas = document.getElementById('earth-canvas');
const ctx = canvas.getContext('2d');
// Set canvas dimensions
canvas.width = 512;
canvas.height = 512;
// Function to draw the Earth texture
function drawEarthTexture() {
// Create a radial gradient for the Earth
const gradient = ctx.createRadialGradient(256, 256, 100, 256, 256, 250);
gradient.addColorStop(0, '#87CEEB'); // Light blue (clouds)
gradient.addColorStop(0.4, '#4682B4'); // Blue (oceans)
gradient.addColorStop(0.7, '#228B22'); // Green (continents)
gradient.addColorStop(1, '#786C3B'); // Brown (mountains)
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Add some simple continent shapes
ctx.fillStyle = '#228B22';
ctx.beginPath();
ctx.arc(180, 250, 50, 0, Math.PI * 2); // Africa
ctx.fill();
ctx.beginPath();
ctx.arc(300, 200, 70, 0, Math.PI * 2); // Asia
ctx.fill();
ctx.beginPath();
ctx.arc(120, 180, 40, 0, Math.PI * 2); // Europe
ctx.fill();
ctx.beginPath();
ctx.arc(250, 350, 80, 0, Math.PI * 2); // South America
ctx.fill();
// Add some cloud patterns (simple circles)
ctx.fillStyle = '#FFFFFF';
ctx.globalAlpha = 0.2;
for (let i = 0; i < 50; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const radius = Math.random() * 20 + 10;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fill();
}
ctx.globalAlpha = 1;
}
// Draw the Earth texture
drawEarthTexture();
// Get the 3D Earth element
const earth3d = document.querySelector('.earth-3d');
// Use the canvas as a texture for the 3D Earth
earth3d.style.backgroundImage = `url(${canvas.toDataURL()})`;
// Animate the Earth rotation
function animateEarth() {
const currentRotation = earth3d.style.transform.match(/rotateY\((\d+)deg\)/);
let rotation = currentRotation ? parseInt(currentRotation[1]) : 20;
rotation += 0.5; // Speed of rotation
earth3d.style.transform = `rotateX(20deg) rotateY(${rotation}deg)`;
requestAnimationFrame(animateEarth);
}
animateEarth();
Here's what's happening:
- HTML: We create a container
.earth-container with a <canvas> element (hidden) and a <div> for the 3D Earth.
- CSS: We style the container, enable 3D perspective, and define the
.earth-3d element with a spherical shape using border-radius. We also add a transition for smooth rotation.
- JavaScript:
- We draw a simple Earth texture on the
<canvas> using gradients and basic shapes for continents and clouds.
- We use the canvas as a texture for the
.earth-3d element by setting its backgroundImage to the canvas data URL.
- We animate the Earth's rotation using
requestAnimationFrame, updating the transform property of .earth-3d.
Open index.html in a browser to see the 3D rotating Earth!
Keep in mind that this is a simplified example. For a more realistic Earth, consider:
- Using a high-resolution Earth texture map (e.g., from NASA's Blue Marble collection)
- Implementing WebGL or Three.js for hardware-accelerated 3D rendering
- Adding atmosphere, lighting, and shading effects
- Incorporating accurate continent shapes and details
Have fun experimenting and improving this basic 3D Earth example!