diff --git a/public/css/style.css b/public/css/style.css index 7b45868..8f794c0 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -91,6 +91,115 @@ html, body { 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 { width: 8px; height: 8px; diff --git a/public/js/map.js b/public/js/map.js index 19e2e8a..3f22ec8 100644 --- a/public/js/map.js +++ b/public/js/map.js @@ -17,7 +17,7 @@ import { LocateControl } from "/js/locate.js"; import { SearchControl } from "/js/search.js"; import { OfflineDataControl } from "/js/offline_data.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 { isLoggedIn } from "/js/auth.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 // down even while the map sits still. const sunLabel = document.querySelector(".topbar__sun"); +const sunChart = document.querySelector(".sun-chart"); const updateSun = () => { const { lng, lat } = map.getCenter(); + if (!sunChart.hidden) sunChart.innerHTML = sunChartHtml(lat, lng); const info = formatSunInfo(lat, lng); sunLabel.hidden = info === null; if (!info) return; + if (info.polar) { + sunLabel.innerHTML = `${SUNRISE_ICON}${info.polar}`; + return; + } const countdownGroup = info.countdown ? `${PENDING_ICON}${info.countdown}` @@ -532,6 +538,19 @@ const updateSun = () => { `${SUNSET_ICON}${info.sunset}` + 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(); map.on("moveend", updateSun); setInterval(updateSun, 60000); diff --git a/public/js/sun.js b/public/js/sun.js index b9b6a8f..5ac4b84 100644 --- a/public/js/sun.js +++ b/public/js/sun.js @@ -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 // 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. export function formatSunInfo(lat, lng, now = new Date()) { 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; let next; @@ -69,3 +73,107 @@ export function formatSunInfo(lat, lng, now = new Date()) { 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) ? `` : ""; + const tick = (date) => + inWindow(date) ? `` : ""; + // 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) + ? `${name} ${time(date)}` + : ""; + + const svg = ` + + + + ${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)} + + + + + + + + + + `; + + const field = (label, value) => `
${label}
${value}
`; + return `${svg} +
+ ${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`)} +
`; +} diff --git a/views/index.erb b/views/index.erb index c09a216..0a19bb5 100644 --- a/views/index.erb +++ b/views/index.erb @@ -3,8 +3,9 @@ MapTool - + +
<% if logged_in %>