Back to Home

SunCalc - JavaScript library for calculating the position of the sun and solar phases

javascript · library · open source · sun · astronomy · suncalc

SunCalc - JavaScript library for calculating the position of the sun and solar phases

    Suncalc

    More than a year ago, I presented to your attention my SunCalc project , which allows you to conveniently study the trajectories of the sun and the phases of sunlight (sunset, dawn, various types of twilight, etc.) during a given day and at a given location.

    Now I decided to release the code that directly calculates these values ​​as a separate open source library and publish it along with examples and documentation on GitHub: github.com/mourner/suncalc The

    library is written based on formulas from an article on the position of the sun on the Astronomy Answers website , published under the BSD license, takes ~ 1.5 kb, meets strict JSLint standardsand works not only in the browser, but on different server platforms, such as Node.js .

    Library Usage Example


    Get an object with times of solar phases for today in London:
    var times = SunCalc.getTimes(new Date(), 51.5, -0.1); // (дата, широта, долгота)

    Show dawn time received:
    alert("Время рассвета: " + times.sunrise.getHours() + ':' + times.sunrise.getMinutes());

    Get the position of the sun (azimuth and altitude) during dawn:
    var sunrisePos = SunCalc.getSunPosition(times.sunrise, 51.5, -0.1); // (время, широта, долгота)

    Display azimuth of dawn in degrees:
    alert("Азимут рассвета: " + (sunrisePos.azimuth * SunCalc.rad2deg).toFixed(2));

    Return times

    • sunrise : dawn (the upper edge of the sun appears on the horizon)
    • sunriseEnd : end of dawn (the lower edge of the sun touches the horizon)
    • goldenHourEnd : the end of the golden hour (soft light, the most suitable time for photography)
    • solarNoon : sunny noon (the sun is at its highest point)
    • goldenHour : the beginning of the "golden hour"
    • sunsetStart : the beginning of sunset (the lower edge of the sun touches the horizon)
    • sunset : sunset (the sun sets completely below the horizon, evening civil twilight begins - the time when the sun is already below the horizon, but the street is still light enough to do without artificial lighting)
    • dusk : start of evening nautical twilight (time when it’s already dark enough, but you can still navigate the horizon in the sea
    • nauticalDusk : beginning of the evening astronomical twilight (visually dark, but not enough for astronomical observations)
    • night : start of night (dark enough for most astronomical observations)
    • nightEnd : end of night (and the beginning of morning astronomical twilight)
    • nauticalDawn : early morning nautical twilight
    • dawn : morning dawn (the beginning of morning civil twilight)
    More information about these solar phases can be found in the article on twilight on Wikipedia .

    You can also add phases manually, for example:
    SunCalc.addTime(20, "runAwayFromBeach", "goToBeach");
    This code will add to the results of the SunCalc.getTimesrunAwayFromBeach function (the moment when the sun rises above 20 °) and goToBeach (when the sun sets below 20 °).

    Possible applications


    At first glance, sites come to mind for travel planning, for photographers and the like. But you can apply imagination and make, for example, a design change on the site depending on the real time of the day. Or build light graphs from a certain angle throughout the year on real estate sites. I remember they even wrote to me once with gratitude from some air conditioning installation company, whose employees are now actively using SunCalc in their work ... And there was also a letter from one Islamist society, which he helped to calculate the right time for prayer. In general, if you think about it, there are tons of applications!

    I hope that you will find a useful application for this small library. I will be glad to comments and feedback in the comments. Thanks!

    update: added times goldenHour, goldenHourEnd and the ability to add your times (corresponding to a certain height of the sun) manually.

    update 2 : a good alternative for more serious tasks (calculations for the moon, planets, etc.) from middle - github.com/monoid/starjs

    Read Next