import SwiftUI /// Morphing shapes and the cookie/bars/wavy drawing helpers, ported from Shapes.java and Draw.java. enum Shapes { static let n = 120 static let names = ["circle", "cookie4", "cookie6", "cookie7", "cookie9", "cookie12", "clover4", "clover8", "sunny", "burst", "softburst", "star5", "star4", "triangle", "square", "pentagon", "hexagon", "gem", "pill", "flower", "puffy", "bean", "arch"] static let all: [[Float]] = names.map { name in var r = (0.. Double { var x = (d / (2 * .pi)).truncatingRemainder(dividingBy: 1) if x < 0 { x += 1 } return 1 - abs(x * 2 - 1) } static func poly(_ k: Int, _ d: Double, _ round: Double) -> Double { let seg = 2 * .pi / Double(k) let m = ((d.truncatingRemainder(dividingBy: seg)) + seg).truncatingRemainder(dividingBy: seg) return cos(.pi / Double(k)) / cos(m - seg / 2) * (1 - round) + cos(.pi / Double(k)) * round * 1.04 } static func radius(_ name: String, _ d: Double) -> Double { switch name { case "cookie4": return 0.9 + cos(4 * d) * 0.1 case "cookie6": return 0.92 + 0.08 * cos(6 * d) case "cookie7": return 0.925 + 0.075 * cos(7 * d) case "cookie9": return 0.94 + 0.06 * cos(9 * d) case "cookie12": return 0.955 + 0.045 * cos(12 * d) case "clover4": return 0.58 + 0.42 * pow(abs(cos(2 * d)), 0.55) case "clover8": return 0.7 + 0.3 * pow(abs(cos(4 * d)), 0.6) case "sunny": return 0.84 + 0.16 * cos(8 * d) case "burst": return 0.72 + 0.28 * tri(12 * d) case "softburst": return 0.8 + 0.2 * pow(tri(10 * d), 1.6) case "star5": return pow((1 + cos(5 * d)) / 2, 1.8) * 0.5 + 0.5 case "star4": return pow((1 + cos(4 * (.pi / 4 + d))) / 2, 2.2) * 0.48 + 0.52 case "triangle": return poly(3, d - .pi / 2, 0.18) case "square": return poly(4, d + .pi / 4, 0.16) case "pentagon": return poly(5, d - .pi / 2, 0.14) case "hexagon": return poly(6, d, 0.1) case "gem": return poly(4, d, 0.1) case "pill": return 1 / sqrt(pow(sin(d) / 0.6, 2) + pow(cos(d), 2)) case "flower": return 0.55 + 0.45 * abs(cos(3 * d)) case "puffy": return poly(4, d + .pi / 4, 0.3) * (0.965 + 0.035 * cos(16 * d)) case "bean": return (1 + sin(d)) * 0.38 / 2 + 0.62 + 0.05 * cos(2 * d) case "arch": return sin(d) > 0 ? poly(4, d + .pi / 4, 0.25) : 0.92 default: return 1 } } static func smooth(_ a: [Float], _ passes: Int) -> [Float] { var cur = a for _ in 0.. Path { var p = Path() for i in 0...n { let k = i % n let a = 2 * .pi * Double(k) / Double(n) + rot let pt = CGPoint(x: c.x + CGFloat(r[k]) * rad * CGFloat(cos(a)), y: c.y + CGFloat(r[k]) * rad * CGFloat(sin(a))) if i == 0 { p.move(to: pt) } else { p.addLine(to: pt) } } p.closeSubpath() return p } /// Nine-lobed "cookie" outline used for covers (Draw.cookie). static func cookie(center c: CGPoint, radius r: CGFloat, depth: CGFloat, rotation rot: Double) -> Path { var p = Path() for i in 0.. x0 { let a = height * amp let end = x - gap let taper = wavelength * 0.42 var p = Path() p.move(to: CGPoint(x: x0, y: y)) let step = max(1, stroke / 2.5) var cx = x0 while cx < end { let nx = min(cx + step, end) var k = min(1, min((nx - x0) / taper, (end - nx) / taper)) if k < 0 { k = 0 } let s = (3 - k * 2) * k * k let yy = y + s * a * CGFloat(sin(Double((nx - x0) / wavelength * 2) * .pi - Double(phase))) p.addLine(to: CGPoint(x: nx, y: yy)) cx = nx } ctx.stroke(p, with: .color(active), style: style) } if thumb { let w = 1.25 * stroke ctx.fill(Path(roundedRect: CGRect(x: x - w / 2, y: y - thumbHeight / 2, width: w, height: thumbHeight), cornerRadius: w / 2), with: .color(active)) } } /// Bass-reactive morphing shape (Shapes.Animator). final class Animator { var playing = false private var from = 0, to = 6, innerFrom = 3, innerTo = 11 private var t: Float = 1, tInner: Float = 1, morphSpeed: Float = 6 private var rotVel: Float = 0.35, rot: Float = 0, innerRot: Float = 0 private var energy: Float = 0, avg: Float = 0.2, prev: Float = 0, kick: Float = 0, pulse: Float = 0 private var sinceBeat: Float = 10, sinceMorph: Float = 0, clock: Float = 0 private var beatsSinceMorph = 0 private var histV = [Float](repeating: 0, count: 24), histT = [Float](repeating: 0, count: 24) private var histPos = 0 private(set) var cur = [Float](repeating: 1, count: Shapes.n) private(set) var inner = [Float](repeating: 1, count: Shapes.n) private func pick(_ a: Int, _ b: Int) -> Int { while true { let i = Int.random(in: 0..= 0 { energy += (bass - energy) * min(1, 20 * dt) avg += (bass - avg) * min(1, 0.7 * dt) let rise = bass - prev prev = bass clock += dt var floor = bass for i in 0..<24 where clock - histT[i] < 0.2 && histV[i] < floor { floor = histV[i] } histV[histPos] = bass histT[histPos] = clock histPos = (histPos + 1) % 24 let jumpBy = bass - floor if bass > 0.3 && jumpBy > 0.05 && rise > 0.025 && sinceBeat > 0.28 { sinceBeat = 0 kick = min(1, 2.5 * jumpBy + 0.5) beatsSinceMorph += 1 if sinceMorph > 0.8 && (jumpBy > 0.2 || beatsSinceMorph >= 2) { beatsSinceMorph = 0 jump(true) } } } else if sinceBeat > 1.8 { kick = 0.5 sinceBeat = 0 jump(true) } if sinceMorph > 6 { jump(false) } } else { energy += (0 - energy) * min(1, 4 * dt) if sinceMorph > 8 { jump(false) } } kick = max(0, kick - 3.4 * dt) let target: Float = playing ? 0.15 + energy * 0.45 + kick * 0.55 : 0 pulse += (target - pulse) * min(1, 16 * dt) t = min(1, t + morphSpeed * dt) tInner = min(1, tInner + morphSpeed * dt * 0.8) rotVel += ((playing ? 0.4 : 0.12) - rotVel) * min(1, 2.5 * dt) rot += rotVel * dt innerRot -= (0.6 + kick * 3) * dt let e1 = Animator.ease(t), e2 = Animator.ease(tInner) let a = Shapes.all[from], b = Shapes.all[to], c = Shapes.all[innerFrom], d = Shapes.all[innerTo] for i in 0.. Float { let x = f - 1 return 1.7 * x * x + 2.7 * x * x * x + 1 } var settled: Bool { !playing && pulse < 0.01 && t >= 1 && tInner >= 1 && kick <= 0 } func draw(_ ctx: inout GraphicsContext, center c: CGPoint, radius r: CGFloat, fill: Color, innerFill: Color, stroke: UInt32) { let rr = r * CGFloat(0.74 + 0.26 * pulse) let outer = Shapes.path(cur, center: c, radius: rr, rotation: Double(rot)) ctx.fill(outer, with: .color(fill)) ctx.stroke(outer, with: .color(Color(rgb: stroke, alpha: Double(0.35 + 0.65 * kick))), lineWidth: max(2, 0.035 * r)) let ir = rr * CGFloat(0.36 + 0.12 * kick + 0.06 * energy) ctx.fill(Shapes.path(inner, center: c, radius: ir, rotation: Double(innerRot)), with: .color(innerFill)) } } } enum TimeFmt { static func string(_ seconds: Double) -> String { let s = max(0, Int(seconds)) let h = s / 3600, m = (s / 60) % 60, sec = s % 60 return h > 0 ? String(format: "%d:%02d:%02d", h, m, sec) : String(format: "%d:%02d", m, sec) } }