Sun & moon chart: click the topbar's sun readout

A horizon-style chart drawn from the vendored suncalc, so fully offline:
real sun and moon altitude over the 24 h around solar noon, sunrise/
sunset lines, dawn/noon/dusk on the time axis, the moon in its current
phase, plus moonrise/set, phase and current sun/moon position below.
Polar day/night now shows as a label instead of hiding the readout.
This commit is contained in:
randy committed 2026-11-12 11:54:49 +01:00
1 parent cf483d183c
commit ca2e67599b
4 files changed
+240 -3

No files matched your search

+109
View File
@@ -91,6 +91,115 @@ html, body {
display: none; display: none;
} }
.topbar__sun {
cursor: pointer;
}
.topbar__sun:hover {
color: #fff;
}
/* Horizon-style sun & moon chart, dropped down from the topbar's sun
readout (sun.js sunChartHtml). */
.sun-chart {
position: fixed;
top: 52px;
left: 50%;
transform: translateX(-50%);
z-index: 30;
width: 340px;
max-width: calc(100vw - 32px);
box-sizing: border-box;
padding: 10px;
background: #1a1a1a;
color: #ddd;
border-radius: 6px;
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
font-size: 12px;
}
.sun-chart[hidden] {
display: none;
}
.sun-chart__graph {
display: block;
width: 100%;
height: auto;
}
.sun-chart__night {
fill: #262a33;
}
.sun-chart__horizon {
stroke: #888;
stroke-width: 1;
}
.sun-chart__mark {
stroke: #555;
stroke-dasharray: 3 3;
}
.sun-chart__tick {
stroke: #888;
}
.sun-chart__label {
fill: #bbb;
font-size: 9px;
text-anchor: middle;
font-variant-numeric: tabular-nums;
}
.sun-chart__sun-path {
fill: none;
stroke: #666;
stroke-width: 2;
}
.sun-chart__sun-path--day {
stroke: #f2b632;
}
.sun-chart__moon-path {
fill: none;
stroke: #8fa3c7;
stroke-width: 1;
stroke-dasharray: 2 3;
}
.sun-chart__sun {
fill: #f2b632;
}
.sun-chart__moon-shadow {
fill: #3a3f4b;
stroke: #8fa3c7;
stroke-width: 0.5;
}
.sun-chart__moon {
fill: #e8ecf4;
}
.sun-chart__fields {
display: grid;
grid-template-columns: auto 1fr;
gap: 2px 12px;
margin: 8px 0 0;
font-variant-numeric: tabular-nums;
}
.sun-chart__fields dt {
color: #999;
}
.sun-chart__fields dd {
margin: 0;
}
.topbar__offline-status { .topbar__offline-status {
width: 8px; width: 8px;
height: 8px; height: 8px;
+20 -1
View File
@@ -17,7 +17,7 @@ import { LocateControl } from "/js/locate.js";
import { SearchControl } from "/js/search.js"; import { SearchControl } from "/js/search.js";
import { OfflineDataControl } from "/js/offline_data.js"; import { OfflineDataControl } from "/js/offline_data.js";
import { LAYERS_ICON, WAYPOINT_ICON, SUNRISE_ICON, SUNSET_ICON, PENDING_ICON } from "/js/icons.js"; import { LAYERS_ICON, WAYPOINT_ICON, SUNRISE_ICON, SUNSET_ICON, PENDING_ICON } from "/js/icons.js";
import { formatCoords, formatSunInfo } from "/js/sun.js"; import { formatCoords, formatSunInfo, sunChartHtml } from "/js/sun.js";
import { notifySidePanelOpen, onlyOneSidePanelOpen } from "/js/panels.js"; import { notifySidePanelOpen, onlyOneSidePanelOpen } from "/js/panels.js";
import { isLoggedIn } from "/js/auth.js"; import { isLoggedIn } from "/js/auth.js";
import { DebugControl } from "/js/debug.js"; import { DebugControl } from "/js/debug.js";
@@ -518,11 +518,17 @@ coordsLabel.addEventListener("click", async () => {
// plus a periodic tick so the "Xh Ym to sunset" countdown keeps counting // plus a periodic tick so the "Xh Ym to sunset" countdown keeps counting
// down even while the map sits still. // down even while the map sits still.
const sunLabel = document.querySelector(".topbar__sun"); const sunLabel = document.querySelector(".topbar__sun");
const sunChart = document.querySelector(".sun-chart");
const updateSun = () => { const updateSun = () => {
const { lng, lat } = map.getCenter(); const { lng, lat } = map.getCenter();
if (!sunChart.hidden) sunChart.innerHTML = sunChartHtml(lat, lng);
const info = formatSunInfo(lat, lng); const info = formatSunInfo(lat, lng);
sunLabel.hidden = info === null; sunLabel.hidden = info === null;
if (!info) return; if (!info) return;
if (info.polar) {
sunLabel.innerHTML = `<span class="topbar__group"><span class="topbar__icon">${SUNRISE_ICON}</span><span>${info.polar}</span></span>`;
return;
}
const countdownGroup = info.countdown const countdownGroup = info.countdown
? `<span class="topbar__group"><span class="topbar__icon">${PENDING_ICON}</span><span>${info.countdown}</span></span>` ? `<span class="topbar__group"><span class="topbar__icon">${PENDING_ICON}</span><span>${info.countdown}</span></span>`
@@ -532,6 +538,19 @@ const updateSun = () => {
`<span class="topbar__group"><span class="topbar__icon">${SUNSET_ICON}</span><span>${info.sunset}</span></span>` + `<span class="topbar__group"><span class="topbar__icon">${SUNSET_ICON}</span><span>${info.sunset}</span></span>` +
countdownGroup; countdownGroup;
}; };
// Click opens the horizon-style sun & moon chart (sun.js), kept in sync by
// the same moveend/minute tick as the label itself.
sunLabel.addEventListener("click", (event) => {
event.stopPropagation();
sunChart.hidden = !sunChart.hidden;
updateSun();
});
document.addEventListener("click", (event) => {
if (!sunChart.contains(event.target)) sunChart.hidden = true;
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape") sunChart.hidden = true;
});
updateSun(); updateSun();
map.on("moveend", updateSun); map.on("moveend", updateSun);
setInterval(updateSun, 60000); setInterval(updateSun, 60000);
+109 -1
View File
@@ -1,4 +1,4 @@
import { getTimes } from "/vendor/suncalc/suncalc.js"; import { getMoonIllumination, getMoonPosition, getMoonTimes, getPosition, getTimes } from "/vendor/suncalc/suncalc.js";
// A Locus Pro-style toolbar readout: DMS coordinates (plain decimal degrees // A Locus Pro-style toolbar readout: DMS coordinates (plain decimal degrees
// read less like a "GPS unit" and are harder to scan at a glance) plus // read less like a "GPS unit" and are harder to scan at a glance) plus
@@ -50,6 +50,10 @@ const DAY_MS = 24 * 60 * 60 * 1000;
// front of each rather than this module owning any markup. // front of each rather than this module owning any markup.
export function formatSunInfo(lat, lng, now = new Date()) { export function formatSunInfo(lat, lng, now = new Date()) {
const today = getTimes(now, lat, lng); const today = getTimes(now, lat, lng);
// Polar day/night: no rise/set to count down to, but still worth a label
// — it's also what opens the sun/moon chart below.
if (today.alwaysUp) return { polar: "Polar day" };
if (today.alwaysDown) return { polar: "Polar night" };
if (today.sunrise === null || today.sunset === null) return null; if (today.sunrise === null || today.sunset === null) return null;
let next; let next;
@@ -69,3 +73,107 @@ export function formatSunInfo(lat, lng, now = new Date()) {
countdown: next ? `${formatCountdown(next.at - now)} to ${next.label}` : null, countdown: next ? `${formatCountdown(next.at - now)} to ${next.label}` : null,
}; };
} }
// --- Sun & moon chart (click on the topbar's sun readout) ----------------
// Horizon-card style: 24 h centered on the solar noon nearest to now, real
// sun/moon altitudes from suncalc (vendored, so fully offline), horizon at
// 0°, sunrise/sunset lines, the moon drawn in its current phase. Times are
// in the browser's own time zone, same as the topbar readout.
const STEP_MS = 10 * 60 * 1000;
const W = 320;
const H = 130;
const LABEL_BAND = 14; // above the graph: sunrise/sunset; below it: dawn, solar noon, dusk
const PHASE_NAMES = [
"New moon", "Waxing crescent", "First quarter", "Waxing gibbous",
"Full moon", "Waning gibbous", "Last quarter", "Waning crescent",
];
function phaseName(phase) {
return PHASE_NAMES[Math.round(phase * 8) % 8];
}
// Lit part of a moon disk of radius r centered on 0,0: the bright half
// (right side; the caller mirrors it while waning) closed by the
// terminator, an elliptical arc whose width follows the phase.
// ponytail: northern-hemisphere orientation, no tilt from
// getMoonIllumination().angle - parallacticAngle — add if this travels south.
export function moonPhasePath(phase, r) {
const rx = (r * Math.abs(Math.cos(2 * Math.PI * phase))).toFixed(2);
const gibbous = phase > 0.25 && phase < 0.75 ? 1 : 0;
return `M0,${-r} A${r},${r} 0 0 1 0,${r} A${rx},${r} 0 0 ${gibbous} 0,${-r}Z`;
}
function nearestSolarNoon(now, lat, lng) {
const noon = getTimes(now, lat, lng).solarNoon;
if (now - noon > DAY_MS / 2) return getTimes(new Date(now.getTime() + DAY_MS), lat, lng).solarNoon;
if (noon - now > DAY_MS / 2) return getTimes(new Date(now.getTime() - DAY_MS), lat, lng).solarNoon;
return noon;
}
const time = (date) => (date ? formatTime(date) : "—");
const degrees = (value) => `${Math.round(value)}°`;
export function sunChartHtml(lat, lng, now = new Date()) {
const noon = nearestSolarNoon(now, lat, lng);
const start = noon.getTime() - DAY_MS / 2;
const samples = [];
for (let t = start; t <= start + DAY_MS; t += STEP_MS) {
const date = new Date(t);
samples.push({ t, sun: getPosition(date, lat, lng).altitude, moon: getMoonPosition(date, lat, lng).altitude });
}
const alts = samples.flatMap((sample) => [sample.sun, sample.moon]);
const top = Math.max(...alts, 10) + 8;
const bottom = Math.min(...alts, -10) - 8;
const x = (t) => (((t - start) / DAY_MS) * W).toFixed(1);
const y = (alt) => (((top - alt) / (top - bottom)) * H).toFixed(1);
const path = (key) => samples.map((sample, i) => `${i ? "L" : "M"}${x(sample.t)},${y(sample[key])}`).join("");
const sunTimes = getTimes(noon, lat, lng);
const moonTimes = getMoonTimes(noon, lat, lng);
const sunNow = getPosition(now, lat, lng);
const moonNow = getMoonPosition(now, lat, lng);
const illumination = getMoonIllumination(now);
const horizon = y(0);
const inWindow = (date) => date && date.getTime() >= start && date.getTime() <= start + DAY_MS;
const marker = (date) =>
inWindow(date) ? `<line class="sun-chart__mark" x1="${x(date.getTime())}" x2="${x(date.getTime())}" y1="0" y2="${H}"/>` : "";
const tick = (date) =>
inWindow(date) ? `<line class="sun-chart__tick" x1="${x(date.getTime())}" x2="${x(date.getTime())}" y1="${H - 5}" y2="${H}"/>` : "";
// Kept clear of the edges so a label for an event near the window's
// ends doesn't get cut off.
const label = (date, name, y) =>
inWindow(date)
? `<text class="sun-chart__label" x="${Math.min(Math.max(x(date.getTime()), 34), W - 34)}" y="${y}">${name} ${time(date)}</text>`
: "";
const svg = `
<svg class="sun-chart__graph" viewBox="0 ${-LABEL_BAND} ${W} ${H + 2 * LABEL_BAND}" role="img" aria-label="Sun and moon altitude over the day">
<defs><clipPath id="sun-chart-day"><rect width="${W}" height="${horizon}"/></clipPath></defs>
<rect class="sun-chart__night" y="${horizon}" width="${W}" height="${H - horizon}"/>
${marker(sunTimes.sunrise)}${marker(sunTimes.sunset)}
${tick(sunTimes.dawn)}${tick(sunTimes.solarNoon)}${tick(sunTimes.dusk)}
${label(sunTimes.sunrise, "Sunrise", -4)}${label(sunTimes.sunset, "Sunset", -4)}
${label(sunTimes.dawn, "Dawn", H + 10)}${label(sunTimes.solarNoon, "Noon", H + 10)}${label(sunTimes.dusk, "Dusk", H + 10)}
<path class="sun-chart__moon-path" d="${path("moon")}"/>
<path class="sun-chart__sun-path" d="${path("sun")}"/>
<path class="sun-chart__sun-path sun-chart__sun-path--day" d="${path("sun")}" clip-path="url(#sun-chart-day)"/>
<line class="sun-chart__horizon" x1="0" x2="${W}" y1="${horizon}" y2="${horizon}"/>
<circle class="sun-chart__sun" cx="${x(now.getTime())}" cy="${y(sunNow.altitude)}" r="7"/>
<g transform="translate(${x(now.getTime())},${y(moonNow.altitude)})">
<circle class="sun-chart__moon-shadow" r="7"/>
<path class="sun-chart__moon" d="${moonPhasePath(illumination.phase, 7)}"${illumination.waxing ? "" : ` transform="scale(-1,1)"`}/>
</g>
</svg>`;
const field = (label, value) => `<dt>${label}</dt><dd>${value}</dd>`;
return `${svg}
<dl class="sun-chart__fields">
${field("Sun now", `${degrees(sunNow.altitude)} alt · ${degrees(sunNow.azimuth)} az`)}
${field("Moonrise", time(moonTimes.rise))}
${field("Moonset", time(moonTimes.set))}
${field("Moon", `${phaseName(illumination.phase)} · ${Math.round(illumination.fraction * 100)}%`)}
${field("Moon now", `${degrees(moonNow.altitude)} alt · ${degrees(moonNow.azimuth)} az`)}
</dl>`;
}
+2 -1
View File
@@ -3,8 +3,9 @@
<span class="topbar__brand" title="rev <%= revision %>">MapTool</span> <span class="topbar__brand" title="rev <%= revision %>">MapTool</span>
<span class="topbar__center"> <span class="topbar__center">
<span class="topbar__coords"></span> <span class="topbar__coords"></span>
<span class="topbar__sun" hidden></span> <span class="topbar__sun" title="Sun &amp; moon" hidden></span>
</span> </span>
<div class="sun-chart" hidden></div>
<span class="topbar__offline-status" hidden></span> <span class="topbar__offline-status" hidden></span>
<div class="topbar__auth"> <div class="topbar__auth">
<% if logged_in %> <% if logged_in %>