Skip to content

Add sun position, sunrise/sunset, and day/night shading to tide charts - #11

Merged
vinc3m1 merged 1 commit into
mainfrom
claude/tide-graphs-sunrise-sunset-kz241g
Jun 27, 2026
Merged

vinc3m1 merged 1 commit into
mainfrom
claude/tide-graphs-sunrise-sunset-kz241g

Conversation

@vinc3m1

@vinc3m1 vinc3m1 commented Jun 27, 2026

Copy link
Copy Markdown
Owner

Summary

Adds on-device solar calculations to display sunrise/sunset times and a day/night gradient shading on tide charts. The sun module uses standard NOAA/SunCalc astronomy (mean anomaly → ecliptic longitude → declination → hour angle) to compute sun altitude from coordinates and date, accurate to ~1–3 minutes for display purposes. No network requests or DOM dependencies.

Key changes

  • New src/engine/sun.ts: Pure, DOM-free solar calculations

    • sunAltitude(date, lat, lon) — sun altitude in radians above the horizon at an instant
    • sunTimes(noon, lat, lon) — sunrise/sunset for the local day, with polar day/night detection
    • sunBandStops(startMs, endMs, lat, lon) — gradient stops (0..1 offsets, 0..1 night intensity) for SVG <linearGradient>, collapsing flat runs to minimize stops
    • Handles edge cases: polar day (midnight sun), polar night, and "white nights" (civil twilight all day) without special-casing
  • TideChart.svelte enhancements:

    • Accepts sunLat / sunLon props (null hides shading)
    • Renders a <linearGradient id="nightBand"> with computed stops, applied to a background <rect>
    • Marks exact sunrise/sunset instants with dashed hairlines and small circles, each with an accessible <title>
    • Night tint opacity scales with twilight intensity (0 at sunrise level, 1 at civil twilight threshold)
  • Home.svelte integration:

    • Computes sunTimes for the displayed day at the selected location
    • Passes coordinates to TideChart
    • Displays sunrise/sunset times in text below the extremes table (with sun/moon emoji, responsive to polar day/night)
    • Uses formatTime to show times in the location's timezone
  • Styling (app.css):

    • New --night color variable (#050a16, deep indigo) for the shading band
  • E2E tests (tests/e2e/sun.spec.ts):

    • Verifies sunrise/sunset text display
    • Confirms night band gradient and sun mark hairlines render
    • Guards against path-count regression (shading is not a path)
  • Unit tests (src/engine/sun.test.ts):

    • Validates sunrise/sunset against public almanacs (London summer solstice, NYC equinox)
    • Tests polar day/night detection (Tromsø)
    • Confirms altitude calculations and gradient stop ordering
    • Verifies flat-run collapsing and edge cases (zero-width window, all-daylight)

Implementation notes

  • All calculations work in absolute UTC Date instants, matching the predictor's contract; timezone conversion happens only in the view layer (formatTime)
  • The algorithm is display-grade (~1–3 min accuracy), sufficient for visual shading and readable times; refraction and elevation assumptions move the exact minute by 1–2 min
  • Gradient stops are computed on-device (microseconds) rather than fetched, keeping the app offline-capable
  • Polar day/night is detected by checking whether the sun crosses the horizon; if not, alwaysUp or alwaysDown is set and times are null
  • The night intensity function maps altitude from sunrise level (−0.833°) to civil twilight (−6°) onto 0..1, allowing smooth twilight gradients

https://claude.ai/code/session_01UZFFjCf4Q2ZfogfCrdgeyp

Shade the tide chart with a day → twilight → night gradient and mark the
exact sunrise/sunset instants with faint hairlines, plus the times in text
under the day's extremes for mobile (no hover there).

Sun position is pure astronomy from the selected point's lat/lon, so it's
computed on-device (new src/engine/sun.ts) rather than fetched — no network,
works offline and in the prerendered pages, and fits the DOM-free engine.

- engine/sun.ts: sunAltitude(), sunTimes() (with polar day/night flags), and
  sunBandStops() which samples altitude and collapses flat runs into a small
  set of SVG gradient stops. Handles polar day/night and white nights with no
  special-casing.
- TideChart: night-band rect behind the curve + sunrise/sunset hairlines with
  accessible <title> tooltips. Curve is still exactly two <path>s.
- Home: sunrise/sunset text line in the day card; passes the chosen point's
  coords (not the snapped gauge) to the chart.
- app.css: --night theme colour for both themes.
- Tests: engine unit tests (almanac cross-checks + polar/edge cases) and an
  e2e spec for the text, band, and hairlines.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UZFFjCf4Q2ZfogfCrdgeyp
@vinc3m1
vinc3m1 enabled auto-merge (squash) June 27, 2026 07:50
@vinc3m1
vinc3m1 merged commit e4e6ae0 into main Jun 27, 2026
2 checks passed
@vinc3m1
vinc3m1 deleted the claude/tide-graphs-sunrise-sunset-kz241g branch June 27, 2026 07:51

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0f25ac27a3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/routes/Home.svelte
Comment on lines +302 to +305
{#if sun.sunrise && sun.sunset}
<span title="Sunrise">☀&#xFE0E; {formatTime(sun.sunrise, tz, $settings.timeFormat)}</span>
<span class="sep">·</span>
<span title="Sunset">☾&#xFE0E; {formatTime(sun.sunset, tz, $settings.timeFormat)}</span>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clamp sun times to the displayed day

For high-latitude days near the midnight-sun boundary, only one horizon crossing may fall inside the local 24h chart window; sunTimes(...) can still return the paired event just after dayEnd (for example Tromsø on May 17 returns a sunset around 00:05 on May 18). The chart already suppresses out-of-window marks, but this branch prints both non-null times, so the side card can list a sunset that is not on the displayed day and does not appear in the chart. Please check each returned time against [dayStart, dayEnd] before using the two-time display.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants