Last active
April 5, 2025 04:00
-
-
Save Priva28/c4becef12fd8dd399cc769f2c7a5c246 to your computer and use it in GitHub Desktop.
SwiftUI CRT effect using metal shaders.
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
import SwiftUI | |
struct ContentView: View { | |
@State private var startDate = Date() | |
var body: some View { | |
VStack { | |
GeometryReader { geo in | |
ZStack { | |
TimelineView(.animation) { _ in | |
Image("someImage") | |
.resizable() | |
.scaledToFill() | |
.blur(radius: 1.2) | |
.layerEffect( | |
ShaderLibrary.crtEffect( | |
.float(-startDate.timeIntervalSinceNow), | |
.float2(geo.size) | |
), | |
maxSampleOffset: .zero | |
) | |
.opacity(0.4) | |
} | |
} | |
.overlay( | |
Text("Some content") | |
.fontWidth(.expanded) | |
.fontWeight(.heavy) | |
.minimumScaleFactor(0.5) | |
.foregroundStyle(.white) | |
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading) | |
.padding(24) | |
.layerEffect( | |
ShaderLibrary.crtEffect( | |
.float(-startDate.timeIntervalSinceNow), | |
.float2(geo.size) | |
), | |
maxSampleOffset: .zero | |
) | |
) | |
} | |
.aspectRatio(1, contentMode: .fit) | |
Spacer() | |
} | |
.background( | |
RadialGradient(stops: [ | |
.init(color: .white.opacity(0.2), location: 0), | |
.init(color: .black, location: 1) | |
], center: .center, startRadius: 10, endRadius: 1000) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please post a screenshot :)