Actually, there is nothing too hard, especially if you have a good astronomical algorithm library. Nowadays we could do this using only plain JavaScript and a HTML5 canvas.

The algorithm is the following:
- Find the current equatorial coordinates of the Sun, the hour angle and declination (thus we automatically get the latitude and longitude of the subsolar point).
- Map each pixel of the Earth map to the corresponding geographic coordinates using map projection transformations (actually it's convenient to have angle values in equatorial coordinate form for the following calculations).
- For each pixel of the map check whether it distant from the subsolar point farther than 90 degrees (because the shadow edge is a great circle).
- If so, convert RGB values of the pixel to HSV color representation and decrease the Value channel by two (or get the same pixel from a night version of the map), then put processed pixel data back using reverse HSV to RGB conversion.
- That's all.

- To find estimate subsolar point coordinates it's only necessary to correct the current UTC time with the equation of time (to get the hour angle) and find the position of the Sun on the ecliptic using the amount of time since the vernal eqinox (to find the declination). It's also possible to use this approximate algorithm. But there are more precise algorithms which have deal with Earth axial precessions and nutations, Earth elliptical motion and other periodic theories, which are described in the book
*Astronomical Algorithms by Jean Meeus*. Nevertheless, it's very hard to implement and debug them properly, so we will use existing JavaScript implementation by Henning Umland. - Because we use equirectangular map projection, there is no need for pixel to spherical coordinate conversion - the longitude and latitude are mapped directly to the cartesian coordinates. We only need to adjust the origin of the map.
- To find the angular distance between the subsolar point and the pixel being processed we need to use the spherical law of cosines in application to the angular coordinates of the corresponding points.
- Because HTML5 canvas has ability to draw semitransparent rectangles we do not need to mess with HSV color representation, and it's also possible to apply some optimizations to the algorithm. As we know that a projection of the sunlight shadow on the Earth map is a sine-like figure (unambiguous mapping), we only need to find the first top or bottom pixel (depends on the current season) of the shadow for a given pixel column and draw one pixel wide semitransparent rectangle to the opposite map edge.
- The Sunlight World Map HML5 Demo and a Windows Desktop Gadget are available at GitHub.

¹ Make sure that

*daylight saving time*settings are configured properly on your system

## No comments:

## Post a Comment