Skip to content

Instantly share code, notes, and snippets.

@antranapp
Created June 13, 2024 05:00
Show Gist options
  • Save antranapp/745ab7f04d3a0ec2f059c1c08d31aa9e to your computer and use it in GitHub Desktop.
Save antranapp/745ab7f04d3a0ec2f059c1c08d31aa9e to your computer and use it in GitHub Desktop.
Animated Mesh Gradient
import SwiftUI
struct ContentView: View {
@State var t: Float = 0.0
@State var timer: Timer?
var body: some View {
ZStack {
MeshGradient(
width: 3,
height: 3,
points: [
[0.0, 0.0],
[0.5, 0.0],
[1.0, 0.0],
[sinInRange(-0.8...(-0.2), 0.439, 0.342, t), sinInRange(0.3...0.7, 3.42, 0.984, t)],
[sinInRange(0.1...0.8, 0.239, 0.084, t), sinInRange(0.2...0.8, 5.21, 0.242, t)],
[sinInRange(1.0...1.5, 0.939, 0.684, t), sinInRange(0.4...0.8, 0.25, 0.642, t)],
[sinInRange(-0.8...0.0, 1.439, 0.442, t), sinInRange(1.4...1.9, 3.42, 0.984, t)],
[sinInRange(0.3...0.6, 0.339, 0.784, t), sinInRange(1.0...1.2, 1.22, 0.772, t)],
[sinInRange(1.0...1.5, 0.939, 0.056, t), sinInRange(1.3...1.7, 0.47, 0.342, t)]
],
colors: [
.black, .black, .black,
.orange, .red, .orange,
.indigo, .black, .green
],
background: .black)
.ignoresSafeArea()
}
.onAppear {
timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { _ in
t += 0.02
})
}
}
func sinInRange(_ range: ClosedRange<Float>, _ offset: Float, _ timeScale: Float, _ t: Float) -> Float {
let amplitude = (range.upperBound - range.lowerBound) / 2
let midPoint = (range.upperBound + range.lowerBound) / 2
return midPoint + amplitude * sin(timeScale * t + offset)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment