Of course. While I cannot execute the JavaScript from the <script> tag you provided due to security restrictions, I can access its content and explain what it does.
The script you've linked is a Leaflet.js plugin for controlling a NASA Web WorldWind instance.
Here's a breakdown of its functionality:
Purpose
The primary goal of this script is to create a set of on-screen UI controls (like zoom, pan, tilt, and rotate buttons) that look and feel like standard Leaflet controls but are used to navigate a 3D Web WorldWind globe.
How it Works
- Creates a Leaflet Control: It defines a new control class,
L.Control.WorldWind, which extends Leaflet's base L.Control.
- Adds UI Elements: When this control is added to a Leaflet map, its
onAdd function runs. This function creates a <div> element and populates it with several buttons:
- Zoom: Zoom In (+) and Zoom Out (-)
- Pan: Pan Up, Down, Left, and Right (using arrow icons)
- Tilt: Tilt Up and Down
- Rotate: Rotate Left and Right
- Links to Web WorldWind: The script assumes that your Leaflet
map object has a property named worldwind (map.worldwind), which holds the instance of your Web WorldWind globe.
- Event Handling: When a user clicks any of the buttons, the corresponding event handler is triggered. For example:
- Clicking "Zoom In" calls the
_zoomIn function, which decreases the range of the WorldWind navigator, effectively zooming in.
- Clicking "Pan Up" calls the
_panUp function, which changes the latitude of the WorldWind navigator's lookAtLocation.
- Similar logic applies to tilting (changing the
tilt angle) and rotating (changing the heading).
Dependencies
- Leaflet.js (
L): The mapping library framework.
- jQuery (
$): Used for some DOM manipulation and event handling within the control.
- NASA Web WorldWind: The 3D virtual globe that this plugin is designed to control.
In Summary
This script is a "bridge" that allows a user to navigate a 3D Web WorldWind globe using a familiar 2D map-style control panel provided by the Leaflet library. It's a useful utility for applications that integrate both Leaflet and Web WorldWind.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Virtual Earth with Day/Night Cycle</title>
<!-- This script tag loads the entire NASA Web WorldWind library from their official CDN. -->
<script src="https://files.worldwind.arc.nasa.gov/artifactory/web/0.9.0/worldwind.min.js" type="text/javascript"></script>
<style>
/* Basic styling to make the globe fill the entire page */
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000;
}
/* The canvas is where the 3D globe will be drawn */
#globeCanvas {
width: 100%;
height: 100%;
}
/* A simple panel for instructions */
#infoPanel {
position: absolute;
top: 10px;
left: 10px;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 10px 15px;
border-radius: 5px;
font-family: sans-serif;
font-size: 14px;
max-width: 300px;
}
</style>
</head>
<body>
<!-- This is the canvas element where the 3D globe will be rendered. -->
<canvas id="globeCanvas">
Your browser does not support HTML5 Canvas.
</canvas>
<!-- This div will hold some helpful on-screen instructions. -->
<div id="infoPanel">
<b>Interactive 3D Earth</b>
<p>
- Drag with the mouse to rotate.<br>
- Use the scroll wheel to zoom.<br>
- The day/night shadow and star positions are updated in real-time.
</p>
</div>
<script>
// Wait for the window to load before running the globe setup.
window.addEventListener("load", function () {
// 1. Create a new WorldWindow ('wwd') and link it to the canvas.
var wwd = new WorldWind.WorldWindow("globeCanvas");
// 2. Define the layers for our globe.
// We will stack them from bottom to top.
var layers = [
// Layer 1: A high-resolution satellite imagery layer from NASA's Blue Marble collection.
{layer: new WorldWind.BMNGLandsatLayer(), enabled: true},
// Layer 2: A layer for the stars, which will be positioned correctly for the current date.
{layer: new WorldWind.StarFieldLayer(), enabled: true},
// Layer 3: The atmosphere layer. This creates the blue glow around the globe
// and is essential for rendering the day/night terminator line.
{layer: new WorldWind.AtmosphereLayer(), enabled: true},
// Layer 4 (Optional but helpful): On-screen compass, zoom, and pan controls.
{layer: new WorldWind.ViewControlsLayer(wwd), enabled: true},
// Layer 5 (Optional but helpful): Displays latitude/longitude at the mouse cursor.
{layer: new WorldWind.CoordinatesDisplayLayer(wwd), enabled: true}
];
// 3. Add all the defined layers to the WorldWindow.
for (var i = 0; i < layers.length; i++) {
if (layers[i].enabled) {
wwd.addLayer(layers[i].layer);
}
}
// --- Day/Night Cycle Animation ---
// Find the specific layers we need to manipulate for the animation.
var starFieldLayer = layers[1].layer;
var atmosphereLayer = layers[2].layer;
// This function will be called on every animation frame.
function runCycle() {
// Get the current date and time.
var currentTime = new Date();
// Update the StarField layer with the current time. This correctly
// positions the sun and stars for astronomical calculations.
starFieldLayer.time = currentTime;
// The AtmosphereLayer needs to know where the sun is to draw the day/night
// shadow correctly. We get the sun's position from the StarField layer.
atmosphereLayer.lightLocation = starFieldLayer.sunGeographicLocation;
// Redraw the scene with the updated information.
wwd.redraw();
// Request the next animation frame to create a smooth, continuous loop.
window.requestAnimationFrame(runCycle);
}
// Start the animation loop!
runCycle();
});
</script>
</body>
</html>