krz/moonrise

Night-sky companion for iPhone, Apple Watch and Vision Pro: what's up tonight, the Moon, planets, launches and missions, and a Library of the solar system. SwiftUI app with a Cloudflare Workers backend. astronomy cloudflare-workers ios night-sky space swiftui visionos watchos

Packages/MoonriseKit/Sources/SkyRenderer/MoonDisc.swift

04b2b1d914788057bcd20945b876847386eb8f65
moonrise/Packages/MoonriseKit/Sources/SkyRenderer/MoonDisc.swift history · blame · raw

27 lines · 1061 bytes

1 symbol in this file
 1import SwiftUI
 2
 3/// The moon at a given phase, filling its frame.
 4public struct MoonDisc: View {
 5    public var fraction: Double
 6    public var waxing: Bool
 7    public var light: Color
 8    public var dark: Color
 9
10    public init(fraction: Double, waxing: Bool, light: Color = SkyStyle().moonLight, dark: Color = SkyStyle().moonDark) {
11        self.fraction = fraction
12        self.waxing = waxing
13        self.light = light
14        self.dark = dark
15    }
16
17    public var body: some View {
18        Canvas { context, size in
19            let r = min(size.width, size.height) / 2
20            let c = CGPoint(x: size.width / 2, y: size.height / 2)
21            context.fill(Path(ellipseIn: CGRect(x: c.x - r, y: c.y - r, width: 2 * r, height: 2 * r)), with: .color(dark))
22            context.fill(Path.moonLit(center: c, radius: r, fraction: fraction, waxing: waxing), with: .color(light))
23        }
24        .aspectRatio(1, contentMode: .fit)
25        .accessibilityLabel("Moon \(Int((fraction * 100).rounded())) percent lit, \(waxing ? "waxing" : "waning")")
26    }
27}