import SwiftUI /// The sky behind everything (P9): the web's atmosphere, drawn as it is in /// internal/look/static/app.css. The sky's gradient (`body`), two beams of /// warm light that sway and breathe (`.light`, `.light::after`), and four /// banks of coloured fog on their own slow paths whose colours drift /// (`.fog`, `.fog i`), on the same keyframes, periods and delays. /// /// - Drawn by a `Canvas` under a `TimelineView` at 30 fps, paused while the /// scene isn't active (a hidden window, a backgrounded app). /// - Reduce Motion holds it at the stylesheet's reduced-motion pose: no /// animation, the beam at `skewX(-14deg)`. Snapshots draw that pose too /// (`.skyFrozen()`). public struct Sky: View { @Environment(\.colorScheme) private var scheme @Environment(\.accessibilityReduceMotion) private var reduceMotion @Environment(\.skyFrozen) private var frozen @Environment(\.scenePhase) private var phase public init() {} public var body: some View { let palette = Palette(scheme) Group { if reduceMotion || frozen { Canvas { context, size in SkyPainter(palette: palette, time: nil).paint(&context, size) } } else { TimelineView(.animation(minimumInterval: 1.0 / 30, paused: phase != .active)) { timeline in Canvas { context, size in SkyPainter(palette: palette, time: timeline.date.timeIntervalSinceReferenceDate).paint(&context, size) } } } } .ignoresSafeArea() .accessibilityHidden(true) .allowsHitTesting(false) } } public extension EnvironmentValues { /// Draws the sky still, at its reduced-motion pose (snapshots, and any /// screen that must not move). @Entry var skyFrozen: Bool = false } public extension View { func skyFrozen(_ frozen: Bool = true) -> some View { environment(\.skyFrozen, frozen) } } /// One frame of the sky. `time` nil is the reduced-motion pose. struct SkyPainter { let palette: Palette let time: TimeInterval? func paint(_ context: inout GraphicsContext, _ size: CGSize) { let rect = CGRect(origin: .zero, size: size) let p = palette // html { background: var(--sky) } under body's gradient, 165°: // --sky-1 0%, --sky-2 at --sky-stop, --sky-3 100%. context.fill(Path(rect), with: .color(p[.sky])) let (start, end) = Self.cssLine(degrees: 165, in: rect) context.fill(Path(rect), with: .linearGradient( Gradient(stops: [.init(color: p[.sky1], location: 0), .init(color: p[.sky2], location: p.skyStop), .init(color: p[.sky3], location: 1)]), startPoint: start, endPoint: end)) // .light sits before .fog in the page: the fog is drawn over it. drawLight(&context, size) drawFog(&context, size) } // MARK: The light /// `.light`: top -30%, left 18%, 46% × 120% of the page, a gradient from /// --beam-1 through --beam-2 (45%) to --beam-3 (78%), blurred 40px, /// swaying from its top centre (`sway` 9s) and breathing (`breathe` /// 6s). Its `::after` (left 95%, 45% × 85% of the beam, --shine-1 into /// --shine-2 at 80%) sways on its own (13s, -4s, alternate-reverse) /// inside it, under the same blur and breath. func drawLight(_ context: inout GraphicsContext, _ size: CGSize) { let p = palette let beam = CGRect(x: size.width * 0.18, y: -size.height * 0.30, width: size.width * 0.46, height: size.height * 1.2) let shine = CGRect(x: beam.minX + beam.width * 0.95, y: beam.minY, width: beam.width * 0.45, height: beam.height * 0.85) let parent: CGAffineTransform, child: CGAffineTransform, opacity: Double if let time { let sway = Self.easeInOut(Self.progress(time, period: 9, delay: 0, reverse: false)) parent = Self.skewTranslate(beam, skew: Self.mix(-24, -4, sway), translate: Self.mix(-0.18, 0.22, sway)) let swayAfter = Self.easeInOut(Self.progress(time, period: 13, delay: -4, reverse: true)) child = Self.skewTranslate(shine, skew: Self.mix(-24, -4, swayAfter), translate: Self.mix(-0.18, 0.22, swayAfter)) opacity = Self.mix(0.45, 1, Self.easeInOut(Self.progress(time, period: 6, delay: 0, reverse: false))) } else { // @media (prefers-reduced-motion): .light { transform: skewX(-14deg) } parent = Self.skewTranslate(beam, skew: -14, translate: 0) child = .identity opacity = 1 } context.drawLayer { layer in layer.addFilter(.blur(radius: 40)) layer.opacity = opacity var beamLayer = layer beamLayer.concatenate(parent) beamLayer.fill(Path(beam), with: .linearGradient( Gradient(stops: [.init(color: p[.beam1], location: 0), .init(color: p[.beam2], location: 0.45), .init(color: p[.beam3], location: 0.78)]), startPoint: CGPoint(x: beam.midX, y: beam.minY), endPoint: CGPoint(x: beam.midX, y: beam.maxY))) var shineLayer = layer shineLayer.concatenate(child.concatenating(parent)) shineLayer.fill(Path(shine), with: .linearGradient( Gradient(stops: [.init(color: p[.shine1], location: 0), .init(color: p[.shine2], location: 0.8)]), startPoint: CGPoint(x: shine.midX, y: shine.minY), endPoint: CGPoint(x: shine.midX, y: shine.maxY))) } } // MARK: The fog struct Bank { let token: Palette.Token let width: Double, height: Double /// left (or, with `fromRight`, right) and bottom, in % of the fog's box. let x: Double, bottom: Double let fromRight: Bool let path: [(tx: Double, ty: Double, scale: Double)] let period: Double, delay: Double let reverse: Bool } static let pathA: [(tx: Double, ty: Double, scale: Double)] = [(-0.18, 0.06, 0.9), (0.14, -0.28, 1.25), (0.36, 0.04, 1)] static let pathB: [(tx: Double, ty: Double, scale: Double)] = [(0.16, -0.04, 1.1), (-0.20, 0.22, 0.85), (-0.38, -0.14, 1.2)] static let pathC: [(tx: Double, ty: Double, scale: Double)] = [(0.10, 0.10, 0.8), (-0.32, -0.40, 1.3), (0.28, -0.18, 1)] /// `.fog i:nth-child(1…4)`. static let banks: [Bank] = [ Bank(token: .lagoon, width: 0.62, height: 0.34, x: -0.04, bottom: 0.10, fromRight: false, path: pathA, period: 12, delay: 0, reverse: false), Bank(token: .orchid, width: 0.58, height: 0.36, x: -0.06, bottom: 0.20, fromRight: true, path: pathB, period: 15, delay: -5, reverse: false), Bank(token: .rose, width: 0.36, height: 0.22, x: 0.40, bottom: 0.02, fromRight: false, path: pathC, period: 10, delay: -3, reverse: false), Bank(token: .fog4, width: 0.70, height: 0.18, x: 0.10, bottom: 0.30, fromRight: false, path: pathB, period: 17, delay: -9, reverse: true), ] /// `.fog`: inset -20% of the page, its colours turning (`tint`, 18s, /// hue-rotate -20° to 28°); each bank an ellipse blurred 56px. func drawFog(_ context: inout GraphicsContext, _ size: CGSize) { let box = CGRect(x: -size.width * 0.2, y: -size.height * 0.2, width: size.width * 1.4, height: size.height * 1.4) let opacities = palette.fogOpacity context.drawLayer { fog in if let time { let tint = Self.easeInOut(Self.progress(time, period: 18, delay: 0, reverse: false)) fog.addFilter(.hueRotation(.degrees(Self.mix(-20, 28, tint)))) } for (index, bank) in Self.banks.enumerated() { let w = box.width * bank.width, h = box.height * bank.height let x = bank.fromRight ? box.maxX - box.width * bank.x - w : box.minX + box.width * bank.x let y = box.maxY - box.height * bank.bottom - h var frame = CGRect(x: x, y: y, width: w, height: h) if let time { let t = Self.progress(time, period: bank.period, delay: bank.delay, reverse: bank.reverse) let pose = Self.keyframe(bank.path, t) // translate(tx%, ty%) of its own box, then scale about its centre. let centre = CGPoint(x: frame.midX + w * pose.tx, y: frame.midY + h * pose.ty) frame = CGRect(x: centre.x - w * pose.scale / 2, y: centre.y - h * pose.scale / 2, width: w * pose.scale, height: h * pose.scale) } fog.drawLayer { bankLayer in bankLayer.addFilter(.blur(radius: 56)) bankLayer.opacity = opacities[index] bankLayer.fill(Path(ellipseIn: frame), with: .color(palette[bank.token])) } } } } // MARK: CSS animation, as a browser runs it /// Where an `ease-in-out infinite` animation is within its keyframes at /// `time`: 0…1, running `alternate` (or `alternate-reverse`), `delay` /// seconds in (negative: started that long before). static func progress(_ time: TimeInterval, period: Double, delay: Double, reverse: Bool) -> Double { let elapsed = time - delay let cycles = elapsed / period let iteration = floor(cycles) var p = cycles - iteration let backwards = (Int(iteration) % 2 != 0) != reverse if backwards { p = 1 - p } return p } /// A three-keyframe path (0%, 50%, 100%) at progress `t`, each half /// eased (`ease-in-out`). static func keyframe(_ path: [(tx: Double, ty: Double, scale: Double)], _ t: Double) -> (tx: Double, ty: Double, scale: Double) { let (a, b, local) = t < 0.5 ? (path[0], path[1], t / 0.5) : (path[1], path[2], (t - 0.5) / 0.5) let e = easeInOut(local) return (mix(a.tx, b.tx, e), mix(a.ty, b.ty, e), mix(a.scale, b.scale, e)) } /// Between two keyframes' values (`from`, `to`) at eased progress t. static func mix(_ a: Double, _ b: Double, _ t: Double) -> Double { a + (b - a) * t } /// `skewX(deg) translateX(t × width)` about the box's top centre /// (`transform-origin: 50% 0`). static func skewTranslate(_ box: CGRect, skew degrees: Double, translate: Double) -> CGAffineTransform { let origin = CGPoint(x: box.midX, y: box.minY) let skew = CGAffineTransform(a: 1, b: 0, c: CGFloat(tan(degrees * .pi / 180)), d: 1, tx: 0, ty: 0) return CGAffineTransform(translationX: -origin.x + box.width * translate, y: -origin.y) .concatenating(skew) .concatenating(CGAffineTransform(translationX: origin.x, y: origin.y)) } /// `cubic-bezier(.42, 0, .58, 1)` at x. static func easeInOut(_ x: Double) -> Double { cubicBezier(x, 0.42, 0, 0.58, 1) } static func cubicBezier(_ x: Double, _ x1: Double, _ y1: Double, _ x2: Double, _ y2: Double) -> Double { if x <= 0 { return 0 } if x >= 1 { return 1 } func sample(_ t: Double, _ a1: Double, _ a2: Double) -> Double { let u = 1 - t return 3 * u * u * t * a1 + 3 * u * t * t * a2 + t * t * t } var lo = 0.0, hi = 1.0, t = x for _ in 0..<30 { let v = sample(t, x1, x2) if abs(v - x) < 1e-6 { break } if v < x { lo = t } else { hi = t } t = (lo + hi) / 2 } return sample(t, y1, y2) } /// A CSS linear-gradient's line for `degrees` (0° up, clockwise) over /// `rect`: through its centre, as long as the box's projection on it. static func cssLine(degrees: Double, in rect: CGRect) -> (CGPoint, CGPoint) { let a = degrees * .pi / 180 let dx = sin(a), dy = -cos(a) let half = (abs(rect.width * dx) + abs(rect.height * dy)) / 2 let c = CGPoint(x: rect.midX, y: rect.midY) // Screen y runs down, as CSS's does: (sin, -cos) is already the line. return (CGPoint(x: c.x - dx * half, y: c.y - dy * half), CGPoint(x: c.x + dx * half, y: c.y + dy * half)) } }