webdial

Incomplete JS port of moondial.
git clone https://code.literati.org/webdial.git
Log | Files | Refs

webdial.js (2661B)


      1 function WebDial(canvas) {
      2     var continents;
      3     var sun = new Sun();
      4 
      5     function drawPoly(ctx, lon2x, lat2y, points) {
      6         ctx.beginPath();
      7         ctx.moveTo(lon2x(points[0][0]), lat2y(points[0][1]));
      8         var i;
      9         for (i = 1; i < points.length; i++ ) {
     10             ctx.lineTo(lon2x(points[i][0]), lat2y(points[i][1]));
     11         }
     12         ctx.closePath();
     13         ctx.fill();
     14     }
     15 
     16     function drawMap(ctx, lon2x, lat2y) {
     17         ctx.fillStyle = "rgb(0, 192, 0)";
     18         $.each(continents, function(continent, polygons) {
     19             console.debug("Drawing", continent);
     20             $.each(polygons, function(i, points) {
     21                 drawPoly(ctx, lon2x, lat2y, points);
     22             });
     23         });
     24     }
     25 
     26     function drawSun(ctx, x, y) {
     27         ctx.strokeStyle = "rgb(192, 192, 0)";
     28         ctx.beginPath();
     29         ctx.arc(x, y, 5, 0, Math.PI*2, false);
     30         ctx.stroke();
     31     }
     32 
     33     function drawNight(ctx, lon2x, lat2y) {
     34         var jd = sun.cal_to_jd(new Date());
     35         var epsilon = sun.obliquity(jd);
     36         var geometric_lon = sun.longitude_radius_low(jd).longitude;
     37         var lon = sun.apparent_longitude_low(jd, geometric_lon);
     38         var equ = sun.ecl_to_equ(lon, 0.0, epsilon);
     39         var st = sun.sidereal_time_greenwich(jd);
     40         var geo = sun.equ_to_geo(equ.ra, equ.dec, st);
     41         console.debug("jd:", jd, "epsilon:", epsilon, "geometric_lon:",
     42                       geometric_lon, "equ:", equ, "geo:", geo, "st:", st);
     43         drawSun(ctx, lon2x(geo.longitude), lat2y(geo.latitude));
     44         var points = sun.terminator(geo.latitude, geo.longitude,
     45                                     sun.sun_rst_altitude);
     46         console.debug(points);
     47         ctx.fillStyle = "rgba(0, 0, 0, 0.25)";
     48         drawPoly(ctx, lon2x, lat2y, points);
     49     }
     50 
     51     function draw() {
     52         var w = canvas.width();
     53         var h = canvas.height();
     54         function lon2x(lon) {
     55             return (lon + 180.0) * w / 360.0;
     56         }
     57 
     58         function lat2y(lat) {
     59             return (90.0 - lat) * h / 180.0;
     60         }
     61 
     62         var ctx = canvas[0].getContext("2d");
     63         canvas[0].width = w;
     64         canvas[0].height = h;
     65         drawMap(ctx, lon2x, lat2y);
     66         drawNight(ctx, lon2x, lat2y);
     67     }
     68 
     69     function mapCallback(data) {
     70         continents = data;
     71         draw();
     72         $(window).resize(draw);
     73     }
     74 
     75     function errCallback(xhr, status, error) {
     76         console.debug("error: ", xhr, status, error);
     77     }
     78 
     79     this.go = function() {
     80         $.ajax("continent.json",
     81                {dataType: "json",
     82                 success: mapCallback,
     83                 error: errCallback
     84                });
     85     }
     86 }