📜 ⬆️ ⬇️

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

Suncalc

Over 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 performs the direct calculation of 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 based on formulas from an article about the position of the sun on Astronomy Answers , published under the BSD license, takes ~ 1.5 kb, meets the strict JSLint standards and works not only in the browser, but also on various server platforms such as Node.js.

Library usage example


Get an object with solar phase times for today in London:
var times = SunCalc.getTimes( new Date(), 51.5, -0.1); // (, , )

Show received dawn time:
alert( " : " + times.sunrise.getHours() + ':' + times.sunrise.getMinutes());

Get the position of the sun (azimuth and altitude) during the dawn:
var sunrisePos = SunCalc.getSunPosition(times.sunrise, 51.5, -0.1); // (, , )

Display dawn azimuth in degrees:
alert( " : " + (sunrisePos.azimuth * SunCalc.rad2deg).toFixed(2));

Return times

You can read more about these solar phases in the twilight article on Wikipedia .

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

Possible uses


At first glance, sites for planning travels, for photographers, and the like, come to mind. But you can apply fantasy and make, for example, a design change on the site depending on the real time of the day. Or build light schedules from a certain angle throughout the year on real estate websites. I remember that I was even once written with gratitude from some air conditioner installation company, whose employees are now actively using SunCalc in their work ... And there was also a letter from an Islamist society, which he helped count the right time for prayer. In general, if you think about the mass of applications!

I hope that you will find useful use of this small library. I would welcome comments and feedback in the comments. Thank!

update : added goldenHour, goldenHourEnd times and the ability to add your own 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 the middle - github.com/monoid/starjs

Source: https://habr.com/ru/post/127101/


All Articles