Packages/MoonriseKit/Sources/SkyRenderer/MoonDisc.swift
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}