|
| 1 | +// |
| 2 | +// BlobShape.swift |
| 3 | +// SwiftGlass |
| 4 | +// |
| 5 | +// Created by Ming on 21/4/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +@available(iOS 15.0, macOS 14.0, watchOS 10.0, tvOS 15.0, visionOS 1.0, *) |
| 11 | +public struct BlobGlassModifier: ViewModifier { |
| 12 | + // Parameters |
| 13 | + let color: Color |
| 14 | + let blobIntensity: CGFloat |
| 15 | + let animationSpeed: Double |
| 16 | + let complexity: Int |
| 17 | + let material: Material |
| 18 | + let gradientOpacity: Double |
| 19 | + let shadowOpacity: Double |
| 20 | + |
| 21 | + // Animation state |
| 22 | + @State private var animationProgress: CGFloat = 0.0 |
| 23 | + |
| 24 | + // Initializer |
| 25 | + public init( |
| 26 | + color: Color, |
| 27 | + blobIntensity: CGFloat, |
| 28 | + animationSpeed: Double, |
| 29 | + complexity: Int, |
| 30 | + material: Material, |
| 31 | + gradientOpacity: Double, |
| 32 | + shadowOpacity: Double |
| 33 | + ) { |
| 34 | + self.color = color |
| 35 | + self.blobIntensity = min(max(blobIntensity, 0.1), 1.0) // Constrain between 0.1 and 1.0 |
| 36 | + self.animationSpeed = animationSpeed |
| 37 | + self.complexity = min(max(complexity, 4), 12) // Constrain between 4 and 12 |
| 38 | + self.material = material |
| 39 | + self.gradientOpacity = gradientOpacity |
| 40 | + self.shadowOpacity = shadowOpacity |
| 41 | + } |
| 42 | + |
| 43 | + // ViewModifier Body |
| 44 | + public func body(content: Content) -> some View { |
| 45 | + content |
| 46 | + // Clip content to animated blob shape |
| 47 | + .clipShape( |
| 48 | + BlobShape( |
| 49 | + complexity: complexity, |
| 50 | + animationProgress: animationProgress, |
| 51 | + intensity: blobIntensity |
| 52 | + ) |
| 53 | + ) |
| 54 | + // Blob glass background and effects |
| 55 | + .background( |
| 56 | + BlobShape( |
| 57 | + complexity: complexity, |
| 58 | + animationProgress: animationProgress, |
| 59 | + intensity: blobIntensity |
| 60 | + ) |
| 61 | + .fill(material) |
| 62 | + .overlay( |
| 63 | + BlobShape( |
| 64 | + complexity: complexity, |
| 65 | + animationProgress: animationProgress, |
| 66 | + intensity: blobIntensity |
| 67 | + ) |
| 68 | + .stroke( |
| 69 | + LinearGradient( |
| 70 | + colors: [ |
| 71 | + color.opacity(gradientOpacity), |
| 72 | + color.opacity(0), |
| 73 | + color.opacity(gradientOpacity), |
| 74 | + color.opacity(0) |
| 75 | + ], |
| 76 | + startPoint: .topLeading, |
| 77 | + endPoint: .bottomTrailing |
| 78 | + ), |
| 79 | + lineWidth: 1.5 |
| 80 | + ) |
| 81 | + ) |
| 82 | + .shadow( |
| 83 | + color: color.opacity(shadowOpacity), |
| 84 | + radius: 10, |
| 85 | + x: 0, |
| 86 | + y: 5 |
| 87 | + ) |
| 88 | + ) |
| 89 | + .onAppear { |
| 90 | + // Start the continuous animation |
| 91 | + withAnimation( |
| 92 | + Animation |
| 93 | + .linear(duration: 10 / animationSpeed) |
| 94 | + .repeatForever(autoreverses: true) |
| 95 | + ) { |
| 96 | + animationProgress = 1.0 |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// BlobShape |
| 103 | +@available(iOS 15.0, macOS 14.0, watchOS 10.0, tvOS 15.0, visionOS 1.0, *) |
| 104 | +public struct BlobShape: Shape { |
| 105 | + // Control points for the blob's perimeter |
| 106 | + var controlPoints: [UnitPoint] |
| 107 | + var animationProgress: CGFloat |
| 108 | + var intensity: CGFloat |
| 109 | + |
| 110 | + // Initializer |
| 111 | + public init(complexity: Int = 8, animationProgress: CGFloat = 0.0, intensity: CGFloat = 0.5) { |
| 112 | + self.controlPoints = BlobShape.generateControlPoints(count: complexity) |
| 113 | + self.animationProgress = animationProgress |
| 114 | + self.intensity = intensity |
| 115 | + } |
| 116 | + |
| 117 | + // Animatable |
| 118 | + public var animatableData: CGFloat { |
| 119 | + get { animationProgress } |
| 120 | + set { animationProgress = newValue } |
| 121 | + } |
| 122 | + |
| 123 | + // Path Generation |
| 124 | + public func path(in rect: CGRect) -> Path { |
| 125 | + let center = CGPoint(x: rect.midX, y: rect.midY) |
| 126 | + let minDimension = min(rect.width, rect.height) |
| 127 | + let radius = minDimension / 2 |
| 128 | + |
| 129 | + // Calculate points on the blob's perimeter |
| 130 | + let points = controlPoints.map { unitPoint -> CGPoint in |
| 131 | + let angle = 2 * .pi * unitPoint.x + (animationProgress * 2 * .pi) |
| 132 | + let distortionAmount = sin(angle * 3 + animationProgress * 4) * intensity * 0.2 |
| 133 | + let blobRadius = radius * (1 + distortionAmount) |
| 134 | + let pointX = center.x + cos(angle) * blobRadius |
| 135 | + let pointY = center.y + sin(angle) * blobRadius |
| 136 | + return CGPoint(x: pointX, y: pointY) |
| 137 | + } |
| 138 | + |
| 139 | + // Use cubic Bézier curves for smoothness |
| 140 | + var path = Path() |
| 141 | + guard points.count > 2 else { return path } |
| 142 | + |
| 143 | + path.move(to: points[0]) |
| 144 | + |
| 145 | + // Calculate control points for smooth cubic Bézier curves |
| 146 | + let count = points.count |
| 147 | + for i in 0..<count { |
| 148 | + let prev = points[(i - 1 + count) % count] |
| 149 | + let curr = points[i] |
| 150 | + let next = points[(i + 1) % count] |
| 151 | + let next2 = points[(i + 2) % count] |
| 152 | + |
| 153 | + // Calculate control points for smoothness |
| 154 | + let control1 = CGPoint( |
| 155 | + x: curr.x + (next.x - prev.x) / 6, |
| 156 | + y: curr.y + (next.y - prev.y) / 6 |
| 157 | + ) |
| 158 | + let control2 = CGPoint( |
| 159 | + x: next.x - (next2.x - curr.x) / 6, |
| 160 | + y: next.y - (next2.y - curr.y) / 6 |
| 161 | + ) |
| 162 | + |
| 163 | + path.addCurve(to: next, control1: control1, control2: control2) |
| 164 | + } |
| 165 | + |
| 166 | + path.closeSubpath() |
| 167 | + return path |
| 168 | + } |
| 169 | + |
| 170 | + // Helper: Generate evenly distributed control points around a circle |
| 171 | + private static func generateControlPoints(count: Int) -> [UnitPoint] { |
| 172 | + var points = [UnitPoint]() |
| 173 | + let angleStep = 1.0 / CGFloat(count) |
| 174 | + |
| 175 | + for i in 0..<count { |
| 176 | + let angle = CGFloat(i) * angleStep |
| 177 | + points.append(UnitPoint(x: angle, y: angle)) |
| 178 | + } |
| 179 | + |
| 180 | + return points |
| 181 | + } |
| 182 | +} |
0 commit comments