Two Leaflet layers for showing where it is day and where it is night, right now, on any map:
L.dayNightLayer()shades the night side with a smooth ramp through civil, nautical and astronomical twilight, painted per tile.L.nightLightsLayer(url)takes city-lights imagery (NASA Black Marble or similar) and masks it to the night side, so cities stop glowing at noon.
No dependencies beyond Leaflet. One file, about 10 KB.
Leaflet.Terminator draws the day/night boundary as a single polygon with a hard edge. That is the right tool if you want one crisp line. This plugin solves two different problems.
Twilight is a gradient, not a line. Because the shading is computed per tile through Leaflet's own grid rather than as one polygon, it can vary continuously across the screen. Sunset fades through civil, nautical and astronomical twilight into full night, which is what the sky actually does.
City-lights imagery has no idea what time it is. NASA's Black Marble mosaic is a static picture of the whole planet lit up. Drop it on a map and Tokyo blazes at midday.
The usual workaround is mix-blend-mode: screen. Screen against a near-white
basemap washes out to nothing, so over pale tiles the lights only appear where a
day/night overlay has already darkened the ground. That quietly stops working the
moment the basemap is satellite imagery, which is mid-to-dark everywhere and
especially over ocean: screen happily brightens the daylight half too.
Blend modes cannot rescue it from either direction. Put the lights above the
day/night layer and they escape its darkening; put them below and the day side,
where that layer is fully transparent, leaves them untouched. The mask has to be
applied to the lights layer's own alpha channel, which is what
L.nightLightsLayer does.
npm install leaflet-daynightimport 'leaflet';
import 'leaflet-daynight';Or from a CDN, after Leaflet:
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-daynight/src/leaflet-daynight.js"></script>The minimum is one line. The layer creates its own map pane, places itself above
the basemap and below your markers, sets mix-blend-mode: multiply and ignores
pointer events, so nothing else needs configuring:
const map = L.map('map').setView([20, 0], 3);
L.tileLayer('https://{s}.tile.example.com/{z}/{x}/{y}.png').addTo(map);
L.dayNightLayer().addTo(map);With city lights on top:
L.nightLightsLayer(
'https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/VIIRS_CityLights_2012/default/GoogleMapsCompatible_Level8/{z}/{y}/{x}.jpg',
{
attribution: 'City lights: NASA Earth Observatory',
maxNativeZoom: 8, // the NASA imagery is only native to z8
maxZoom: 9, // stop requesting upscaled tiles past here
opacity: 0.9
}
).addTo(map);Both layers track the real clock and redraw once a minute by default. To show a different moment instead, for a time slider or an animation:
const layer = L.dayNightLayer().addTo(map);
layer.setTime(new Date('2026-06-21T03:00:00Z')); // pin to a moment
layer.setTime(null); // back to tracking the clockA pinned layer runs no timer.
Extends L.GridLayer, so every
GridLayer option (opacity, zIndex, noWrap, tileSize, className, and so
on) works too.
| Option | Type | Default | Description |
|---|---|---|---|
color |
[r, g, b] |
[34, 28, 22] |
The night tint. A warm dark tone rather than navy, so multiplying keeps the basemap's own colours instead of flattening land and water to the same shade. |
maxOpacity |
Number |
0.62 |
Darkness at astronomical night. |
blendMode |
String |
'multiply' |
Applied to the layer's pane. Set to null to skip blending. |
sampleStep |
Number |
8 |
Pixels between sun-altitude samples inside a tile. Lower is smoother and slower. |
updateInterval |
Number |
60000 |
Milliseconds between terminator updates. 0 disables the timer. |
time |
Date |
null |
Pin to a moment, or null to track the clock. |
pane |
String |
null |
null lets the layer create its own pane at z-index 250. Name a pane to place it yourself, and the layer will leave the pane's styling alone. |
Extends L.TileLayer, so
subdomains, maxNativeZoom, crossOrigin and the rest behave as usual.
| Option | Type | Default | Description |
|---|---|---|---|
lightsOn |
Number |
-3 |
Sun altitude in degrees where lights begin to appear, just after sunset. |
lightsFull |
Number |
-12 |
Altitude where they reach full strength, at nautical dark. |
blendMode |
String |
'screen' |
Applied to the layer's pane. |
sampleStep |
Number |
8 |
As above. |
updateInterval |
Number |
60000 |
As above. |
time |
Date |
null |
As above. |
pane |
String |
null |
null creates a pane at z-index 260, just above the day/night layer. |
Both layers share these.
| Method | Returns | Description |
|---|---|---|
setTime(date) |
this |
Show an arbitrary moment. Pass null to resume tracking the clock. |
refresh() |
this |
Recompute the terminator and repaint immediately. Useful when a tab regains focus. |
getSubsolarPoint() |
{lat, lng} |
The point currently being drawn as directly under the sun. |
The bare calculation, exposed for anyone who wants the sun's position without a
layer. Returns {lat, lng} in degrees.
Each tile is painted from a coarse grid of sun-altitude samples, one every
sampleStep pixels, written into a small offscreen canvas and then upscaled to
the full tile with bilinear smoothing. Sampling coarsely and letting the browser
interpolate is what turns the twilight bands into a real gradient instead of
visible blocks, and it keeps the per-tile cost to a few hundred trigonometric
calls rather than one per pixel. Sample positions span the tile edges exactly, so
neighbouring tiles line up with no seam.
Working in tile space rather than drawing one world-sized polygon means panning, zooming, antimeridian wrap and the poles all come free from Leaflet, and only the tiles actually on screen are ever computed.
For the city lights, the imagery tile is drawn into a canvas and then cut back
with the same altitude mask using
globalCompositeOperation = 'destination-in',
so the mask lands on the layer's own alpha and the edge falls on exactly the same
terminator as the shading. The tile canvas becomes cross-origin tainted by
drawImage, which is harmless here: tainting only blocks readback, and the mask
is built in a separate canvas from locally created ImageData.
The astronomical core is SunCalc by Vladimir Agafonkin, reduced to just the subsolar-point calculation. It trades precision for size: solar declination runs about 0.12 degrees from a full-precision ephemeris, 0.2 at worst, which is roughly 22 km on the ground. That is well under one screen pixel at any zoom where a terminator is worth looking at. If you need arcsecond accuracy, this is not the library for it.
Any browser with <canvas> and createImageData, which in practice means
everything Leaflet 1.x itself supports. mix-blend-mode is used for the tinting
and degrades to plain alpha compositing where it is missing.
- Astronomical core: SunCalc by Vladimir Agafonkin (MIT).
- City-lights imagery in the demo: NASA Earth Observatory Black Marble, via NASA GIBS.
- Basemaps in the demo: Esri World Imagery and Light Gray Canvas.
Extracted from the live day/night map at liveworldwebcams.com/live-webcam-map/, which is the reference implementation and where these layers run in production over roughly a thousand webcams.
Issues and pull requests are welcome. npm test runs the astronomical sanity
checks, which need no dependencies and no browser.
