App/Sources/Confetti.swift

main
fold/App/Sources/Confetti.swift history · blame · raw

60 lines · 2942 bytes

4 symbols in this file
 1import Renderer
 2import SwiftUI
 3
 4/// A burst of paper confetti thrown up from the middle of the view, tumbling as it falls out of
 5/// the bottom over `duration` seconds from when the view appears. Pieces in the paper's colours
 6/// show its front on one side and its back on the other.
 7struct Confetti: View {
 8    nonisolated static let duration = 2.5
 9    @State private var pieces = (0..<90).map { _ in Piece() }
10    @State private var start = Date.now
11
12    var body: some View {
13        TimelineView(.animation) { timeline in
14            Canvas { context, size in
15                let elapsed = timeline.date.timeIntervalSince(start)
16                for piece in pieces { piece.draw(in: context, size: size, at: elapsed) }
17            }
18        }
19    }
20}
21
22private let front = colour(PaperRenderer.frontColour)
23private let back = colour(PaperRenderer.backColour)
24private let accents = [Color(red: 0.96, green: 0.74, blue: 0.20), Color(red: 0.16, green: 0.58, blue: 0.56),
25                       Color(red: 0.26, green: 0.44, blue: 0.86)]
26
27private func colour(_ c: SIMD4<Float>) -> Color {
28    Color(red: Double(c.x), green: Double(c.y), blue: Double(c.z))
29}
30
31private struct Piece {
32    /// Where it is thrown from and its initial velocity, as fractions of the view's size.
33    let origin = SIMD2(Double.random(in: 0.35...0.65), Double.random(in: 0.3...0.4))
34    let velocity = SIMD2(Double.random(in: -0.7...0.7), Double.random(in: -1.6...(-0.8)))
35    let size = CGSize(width: Double.random(in: 6...11), height: Double.random(in: 4...9))
36    /// Radians per second in the plane of the screen and about the piece's own long axis.
37    let spin = Double.random(in: -6...6), tumble = Double.random(in: 4...10)
38    let phase = Double.random(in: 0...(2 * .pi))
39    /// One colour on each side.
40    let sides: (Color, Color) = Bool.random() ? (front, back) : { let c = accents.randomElement()!; return (c, c) }()
41
42    /// Air drag and the speed it falls at once drag has taken out the throw, per second and in
43    /// fractions of the view's height per second.
44    private static let drag = 2.5, fallSpeed = 0.6
45
46    func draw(in context: GraphicsContext, size view: CGSize, at t: Double) {
47        guard t >= 0, t < Confetti.duration else { return }
48        let slowed = (1 - exp(-Self.drag * t)) / Self.drag
49        let x = origin.x + velocity.x * slowed + 0.02 * sin(3 * t + phase)
50        let y = origin.y + (velocity.y - Self.fallSpeed) * slowed + Self.fallSpeed * t
51        let turn = cos(tumble * t + phase)
52        var piece = context
53        piece.opacity = min(1, (Confetti.duration - t) / 0.4)
54        piece.translateBy(x: x * view.width, y: y * view.height)
55        piece.rotate(by: .radians(spin * t + phase))
56        piece.scaleBy(x: 1, y: turn)
57        let rect = CGRect(x: -size.width / 2, y: -size.height / 2, width: size.width, height: size.height)
58        piece.fill(Path(rect), with: .color(turn >= 0 ? sides.0 : sides.1))
59    }
60}