Created
June 11, 2024 00:28
-
-
Save dkun7944/122c08e79e2e6ac0e2b5450af57c0efc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MeshGradientTest.swift | |
// | |
// Created by Daniel Kuntz on 6/10/24. | |
// | |
import SwiftUI | |
struct ContentTimerView: View { | |
@State var t: Float = 0.0 | |
@State var timer: Timer? | |
var body: some View { | |
ZStack { | |
AnimatedGradientView(t: t) | |
Text(String(t)) | |
} | |
.onAppear { | |
timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { _ in | |
t += 0.02 | |
} | |
} | |
} | |
} | |
struct ContentTimelineView: View { | |
@State var t: Float = 0.0 | |
@State var timer: Timer? | |
var body: some View { | |
TimelineView(.periodic(from: Date(), by: 0.01)) { ctx in | |
ZStack { | |
AnimatedGradientView(t: Float(ctx.date.timeIntervalSince1970)) | |
Text(String(ctx.date.timeIntervalSince1970)) | |
} | |
} | |
.ignoresSafeArea() | |
} | |
} | |
struct AnimatedGradientView: View { | |
var t: Float | |
var body: some View { | |
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.084, 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() | |
} | |
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 { | |
ContentTimerView() | |
ContentTimelineView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment