PageSourceSearch

https://reactflow.com/assets/libs/leaflet/plugins/L.Terminator.js

js reactflow.com collected 2026-09-24 19:01:44 UTC 4,692 bytes, 148 lines download raw bytes

1(function (global, factory) {
2	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('leaflet')) :
3	typeof define === 'function' && define.amd ? define(['leaflet'], factory) :
4	(global.L = global.L || {}, global.L.terminator = factory(global.L));
5}(this, (function (L) { 'use strict';
6
7	L = L && L.hasOwnProperty('default') ? L['default'] : L;
8
9	/* Terminator.js -- Overlay day/night region on a Leaflet map */
10
11	function julian(date) {
12		/* Calculate the present UTC Julian Date. Function is valid after
13		 * the beginning of the UNIX epoch 1970-01-01 and ignores leap
14		 * seconds. */
15		return (date / 86400000) + 2440587.5;
16	}
17
18	function GMST(julianDay) {
19		/* Calculate Greenwich Mean Sidereal Time according to 
20			 http://aa.usno.navy.mil/faq/docs/GAST.php */
21		var d = julianDay - 2451545.0;
22		// Low precision equation is good enough for our purposes.
23		return (18.697374558 + 24.06570982441908 * d) % 24;
24	}
25
26	var Terminator = L.Polygon.extend({
27		options: {
28			color: '#00',
29			opacity: 0.2,
30			fillColor: '#00',
31			fillOpacity: 0.2,
32			resolution: 2
33		},
34
35		initialize: function (options) {
36			this.version = '0.1.0';
37			this._R2D = 180 / Math.PI;
38			this._D2R = Math.PI / 180;
39			L.Util.setOptions(this, options);
40			var latLng = this._compute(this.options.time);
41			this.setLatLngs(latLng);
42		},
43
44		setTime: function (date) {
45			this.options.time = date;
46			var latLng = this._compute(date);
47			this.setLatLngs(latLng);
48		},
49
50		_sunEclipticPosition: function (julianDay) {
51			/* Compute the position of the Sun in ecliptic coordinates at
52				 julianDay.  Following
53				 http://en.wikipedia.org/wiki/Position_of_the_Sun */
54			// Days since start of J2000.0
55			var n = julianDay - 2451545.0;
56			// mean longitude of the Sun
57			var L$$1 = 280.460 + 0.9856474 * n;
58			L$$1 %= 360;
59			// mean anomaly of the Sun
60			var g = 357.528 + 0.9856003 * n;
61			g %= 360;
62			// ecliptic longitude of Sun
63			var lambda = L$$1 + 1.915 * Math.sin(g * this._D2R) +
64				0.02 * Math.sin(2 * g * this._D2R);
65			// distance from Sun in AU
66			var R = 1.00014 - 0.01671 * Math.cos(g * this._D2R) -
67				0.0014 * Math.cos(2 * g * this._D2R);
68			return {lambda: lambda, R: R};
69		},
70
71		_eclipticObliquity: function (julianDay) {
72			// Following the short term expression in
73			// http://en.wikipedia.org/wiki/Axial_tilt#Obliquity_of_the_ecliptic_.28Earth.27s_axial_tilt.29
74			var n = julianDay - 2451545.0;
75			// Julian centuries since J2000.0
76			var T = n / 36525;
77			var epsilon = 23.43929111 -
78				T * (46.836769 / 3600
79					- T * (0.0001831 / 3600
80						+ T * (0.00200340 / 3600
81							- T * (0.576e-6 / 3600
82								- T * 4.34e-8 / 3600))));
83			return epsilon;
84		},
85
86		_sunEquatorialPosition: function (sunEclLng, eclObliq) {
87			/* Compute the Sun's equatorial position from its ecliptic
88			 * position. Inputs are expected in degrees. Outputs are in
89			 * degrees as well. */
90			var alpha = Math.atan(Math.cos(eclObliq * this._D2R)
91				* Math.tan(sunEclLng * this._D2R)) * this._R2D;
92			var delta = Math.asin(Math.sin(eclObliq * this._D2R)
93				* Math.sin(sunEclLng * this._D2R)) * this._R2D;
94
95			var lQuadrant = Math.floor(sunEclLng / 90) * 90;
96			var raQuadrant = Math.floor(alpha / 90) * 90;
97			alpha = alpha + (lQuadrant - raQuadrant);
98
99			return {alpha: alpha, delta: delta};
100		},
101
102		_hourAngle: function (lng, sunPos, gst) {
103			/* Compute the hour angle of the sun for a longitude on
104			 * Earth. Return the hour angle in degrees. */
105			var lst = gst + lng / 15;
106			return lst * 15 - sunPos.alpha;
107		},
108
109		_latitude: function (ha, sunPos) {
110			/* For a given hour angle and sun position, compute the
111			 * latitude of the terminator in degrees. */
112			var lat = Math.atan(-Math.cos(ha * this._D2R) /
113				Math.tan(sunPos.delta * this._D2R)) * this._R2D;
114			return lat;
115		},
116
117		_compute: function (time) {
118			var today = time ? new Date(time) : new Date();
119			var julianDay = julian(today);
120			var gst = GMST(julianDay);
121			var latLng = [];
122
123			var sunEclPos = this._sunEclipticPosition(julianDay);
124			var eclObliq = this._eclipticObliquity(julianDay);
125			var sunEqPos = this._sunEquatorialPosition(sunEclPos.lambda, eclObliq);
126			for (var i = 0; i <= 720 * this.options.resolution; i++) {
127				var lng = -360 + i / this.options.resolution;
128				var ha = this._hourAngle(lng, sunEqPos, gst);
129				latLng[i + 1] = [this._latitude(ha, sunEqPos), lng];
130			}
131			if (sunEqPos.delta < 0) {
132				latLng[0] = [90, -360];
133				latLng[latLng.length] = [90, 360];
134			} else {
135				latLng[0] = [-90, -360];
136				latLng[latLng.length] = [-90, 360];
137			}
138			return latLng;
139		}
140	});
141
142	function terminator(options) {
143		return new Terminator(options);
144	}
145
146	return terminator;
147
148})));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.