There are some expensive mechanical watches which display the phases of the moon. As well as some smart-watches .... Can't be that difficult to implement then with all these great software tools available for Web programming? Right?! And there is this German children song Guter Mond Du gehst so stille...
Well, the truth is quite different. Starting out with a simplistic heliocentric, circular, model for the moon's and the earth's motions and a ray-tracing scheme to compute the illuminated and visible portion of the moon gives reasonable illuminations. However, for moon rise and set, the simplistic model yields rather inaccurate results.
Of course, we know planetary trajectories are not circles, but ellipses. In addition, these ellipses rotate too: apsidial and nodal precession. Furthermore, the moon's axis experiences axial precession,as well as the earth's axis. More details can be found here. And then there are gravitational influences of the sun and the earth on the moon's motion. In other words: it's not a simple, smooth motion in space... The German song got it all wrong!
The usual sources of wisdom on the WWW were not always comprehensible. A succinct description by Paul Schlyter is a good summary and easy to implement. More details come from books. Celestial Computations: A Gentle Introduction to Computational Astronomy by J.L. Lawrence is quite readable explaining parts of the astronomer's world such as coordinate systems, the notion of time, the computation of planetary motion, etc. It was interesting to learn that geocentric and not heliocentric models are used in which the earth is the origin around which sun, planets, and stars "move". And then there are coordinate systems to choose from: equatorial, ecliptic, horizontal, galactic - take your pick. Time standards à gogo: UTC, LTC, GST, LST.... and where is that first point of Aries serving as the reference for right-angle or longitude on the celestial sphere? Units for angles are switching between degrees - decimal or in degree/minutes/seconds, radians, and hours.... Knowledge of Spherical trigonometry would have helped too.
More precise and a tad more complex computations are given by Jean Meeus Astronomical Algorithms. The initial chapters bring you back to the eighties: computation was expensive and things like numerical precision, interpolation, and iterative computing had to be addressed seriously. Tempi passati. The more serious revelations of that book however were: astronomy seems to be the art of observation and approximations. For the moon's motion, there are up to sixty corrective factors in some formula! Many approximations are based on some low degree polynomial with factors of unknown origins at least for the amateurs. Professionals use astronomical almanacs, but that's beyond our purpose here.
Thus the "simple" project became more complicated than anticipated. Python for development of positional computations and to debug the ray-tracing graphics using matplotlib. Then porting to the WWW using TypeScript with all the quirks of subtle differences in programming languages: how to express scope, how to format comments, and all that crap. And getting a "modern" WWW service to run still requires installation of the necessary environments from wherever they come. And, bear in mind, TypeScript/JavaScript in node.js set-up being an interpretive environment, it's slower than a C implementation by a factor of approx. eight.
In the end, however, here is the moon's illumination for a given date and a set of pre-configured cities.
The computations done here have no pretense to be astronomically correct. In particular, moon rise and set times may not be fully matching values of other WWW sites - presumably due to less accurate approximations of moon position or other factors.
One might be curious how sun, earth, and moon are placed in the solar system. Below a simple illustration. Note that the ellipses are barely eccentric. The lunar ellipse has been scaled up in order to make it visible. In reality, the moon is very, very close to the earth when compared with the distance sun-earth: the distance earth-moon is around 0.25 percent of the distance sun-earth! Also, the diameters of sun, earth, and moon are not up to scale. We show a projection into the ecliptic plane as well as a projection of the vertical positions: the moon's ellipsis is tilted vis-a-vis the ecliptic and thus sometimes above or below the ecliptic plane. And sometimes, in that perspective, the moon is in front of the earth and sometimes hidden behind....