Branchwell

Glimmer / app · glimmertoday.ai / native / Glimmer / CoinBurst.swift

1import SwiftUI
2
3/// Coins rise from both bottom corners, then settle among brief glints.
4/// It is decoration only and never intercepts touches.
5struct CoinBurst: View {
6 let startedAt: Date
7 let amount: Int
8 static let duration = 2.2
9
10 private static func variation(_ index: Int, _ channel: Int) -> Double {
11 let value = sin(Double(index * 73 + channel * 193) * 12.9898) * 43758.5453
12 return value - floor(value)
13 }
14
15 private struct Flight {
16 let fromLeft: Bool
17 let delay: Double
18 let duration: Double
19 let startX: Double
20 let startY: Double
21 let drift: Double
22 let rise: Double
23 let radius: Double
24 let phase: Double
25 let turn: Double
26 }
27
28 private static let basePairs = 22
29 private static let maxExtraPairs = 8
30 private static let maxDensity = 2.10
31
32 // Set up the particles once so frame updates only advance their motion.
33 private static let flights = (0..<(2 * (Int((Double(basePairs) * maxDensity).rounded()) + maxExtraPairs))).map { index in
34 let fromLeft = index.isMultiple(of: 2)
35 return Flight(
36 fromLeft: fromLeft,
37 delay: variation(index, 1) * 0.34 + (fromLeft ? 0 : 0.055),
38 duration: 1.37 + variation(index, 2) * 0.40,
39 startX: 0.018 + variation(index, 3) * 0.051,
40 startY: 0.95 + variation(index, 6) * 0.085,
41 drift: 0.25 + variation(index, 4) * 0.37,
42 rise: 0.37 + variation(index, 5) * 0.46,
43 radius: 6.5 + pow(variation(index, 8), 1.35) * 6,
44 phase: variation(index, 11) * 2 * .pi,
45 turn: 0.33 + variation(index, 10) * 0.60
46 )
47 }
48
49 private static let outline = CGRect(x: -1, y: -1, width: 2, height: 2)
50 private static let rimPath = Path(ellipseIn: outline)
51 private static let facePath = Path(ellipseIn: outline.insetBy(dx: 0.16, dy: 0.16))
52 private static let ringPath = Path(ellipseIn: outline.insetBy(dx: 0.34, dy: 0.34))
53
54 var body: some View {
55 TimelineView(.animation) { timeline in
56 Canvas { context, size in
57 let elapsed = timeline.date.timeIntervalSince(startedAt)
58 let shortEdge = min(size.width, size.height)
59 let particleScale = min(1.55, max(0.80, sqrt(shortEdge / 398)))
60 let densityScale = min(Self.maxDensity, max(1, sqrt(size.width * size.height / 342_000)))
61 drawCoins(context, size: size, elapsed: elapsed, scale: particleScale, density: densityScale)
62 drawGlints(context, size: size, elapsed: elapsed, scale: particleScale, density: densityScale)
63 }
64 }
65 .ignoresSafeArea()
66 .allowsHitTesting(false)
67 .accessibilityHidden(true)
68 }
69
70 private func drawCoins(_ context: GraphicsContext, size: CGSize, elapsed: Double, scale: Double, density: Double) {
71 let face = Color(red: 0.95, green: 0.76, blue: 0.29)
72 let rim = Color(red: 0.85, green: 0.63, blue: 0.18)
73 let shine = Color(red: 1, green: 0.92, blue: 0.67).opacity(0.72)
74 let addedPairs = min(Self.maxExtraPairs, max(0, amount - 10) / 4)
75 let count = 2 * (Int((Double(Self.basePairs) * density).rounded()) + addedPairs)
76 for flight in Self.flights.prefix(count) {
77 let age = elapsed - flight.delay
78 let progress = age / flight.duration
79 guard progress >= 0, progress < 1 else { continue }
80
81 // Horizontal momentum keeps the coins moving through the top of the arc.
82 let horizontal = flight.startX + flight.drift * age * (1 - 0.10 * age)
83 let x = size.width * (flight.fromLeft ? horizontal : 1 - horizontal)
84 let y = size.height * (flight.startY - flight.rise * 4 * progress * (1 - progress))
85 let radius = flight.radius * scale
86 let rock = sin(flight.phase + flight.turn * 2 * .pi * progress)
87
88 var coin = context
89 coin.opacity = min(1, progress * 12) * min(1, (1 - progress) * 4.5)
90 coin.translateBy(x: x, y: y)
91 coin.rotate(by: .radians(0.24 * rock))
92 coin.scaleBy(x: radius * (0.83 + 0.16 * rock), y: radius)
93 coin.fill(Self.rimPath, with: .color(rim))
94 coin.fill(Self.facePath, with: .color(face))
95 coin.stroke(Self.ringPath, with: .color(shine), lineWidth: 0.10)
96 }
97 }
98
99 private func drawGlints(_ context: GraphicsContext, size: CGSize, elapsed: Double, scale: Double, density: Double) {
100 for index in 0..<Int((14 * density).rounded()) {
101 let age = elapsed - (0.25 + Self.variation(index, 12) * 0.85)
102 let lifetime = 0.35 + Self.variation(index, 13) * 0.10
103 guard age >= 0, age < lifetime else { continue }
104 let shimmer = sin(age / lifetime * .pi)
105 let radius = (4 + Self.variation(index, 16) * 3) * (0.4 + 0.6 * shimmer) * scale
106 var path = Path()
107 for point in 0..<8 {
108 let angle = Double(point) * .pi / 4 - .pi / 2
109 let distance = point.isMultiple(of: 2) ? radius : radius * 0.22
110 let position = CGPoint(x: cos(angle) * distance, y: sin(angle) * distance)
111 if point == 0 { path.move(to: position) } else { path.addLine(to: position) }
112 }
113 path.closeSubpath()
114 var glint = context
115 glint.opacity = shimmer
116 glint.translateBy(x: size.width * (0.08 + Self.variation(index, 14) * 0.84), y: size.height * (0.13 + Self.variation(index, 15) * 0.65))
117 glint.rotate(by: .degrees((age / lifetime - 0.5) * 20))
118 glint.fill(path, with: .color(Color(red: 1, green: 0.93, blue: 0.66)))
119 glint.stroke(path, with: .color(Color(red: 0.91, green: 0.73, blue: 0.29)), lineWidth: 0.8)
120 }
121 }
122}