Last active
May 11, 2024 03:03
-
-
Save Koshimizu-Takehito/dff57477bc5c01442cf6a1704a2e2ef2 to your computer and use it in GitHub Desktop.
JuliaSetView
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 id = UUID() | |
var body: some View { | |
NavigationStack { | |
RootView(id: $id) | |
.toolbar { barItem } | |
.tint(.white) | |
} | |
} | |
var barItem: some View { | |
Button("Reset") { | |
id = UUID() | |
} | |
.fontWeight(.semibold) | |
} | |
} | |
typealias Animatable5 = AnimatablePair< | |
Double, AnimatablePair<AnimatablePair<Double, Double>, AnimatablePair<Double, Double>> | |
> | |
struct JuliaSetView: View, Animatable { | |
var scale: Double | |
var constant: CGPoint | |
var location: CGPoint | |
var animatableData: Animatable5 { | |
get { | |
.init(scale, .init(.init(constant.x, constant.y), .init(location.x, location.y))) | |
} | |
set { | |
scale = newValue.first | |
constant.x = newValue.second.first.first | |
constant.y = newValue.second.first.second | |
location.x = newValue.second.second.first | |
location.y = newValue.second.second.second | |
} | |
} | |
var body: some View { | |
Rectangle().colorEffect( | |
ShaderFunction(library: .default, name: "JuliaSet::main")( | |
.boundingRect, | |
.float(scale), | |
.float2(constant.x, constant.y), | |
.float2(location.x, location.y) | |
) | |
) | |
} | |
} | |
private struct RootView: View { | |
@Binding var id: UUID | |
@GestureState private var magnifyBy = 1.0 | |
@State private var constant = CGPoint(x: 0.3575, y: 0.3575) | |
@State private var location = CGPoint.zero | |
@State private var lastLocation = CGPoint.zero | |
@State private var scale: Double = 1 | |
@State private var lastScale: Double = 0.5 | |
var body: some View { | |
ZStack { | |
GeometryReader { geometry in | |
let size = geometry.size | |
JuliaSetView(scale: scale, constant: constant, location: location) | |
.onTapGesture(count: 1) { location in | |
onTap(location: location, in: size) | |
} | |
.gesture(dragGesture(size: size)) | |
.gesture(magnification) | |
} | |
.ignoresSafeArea() | |
VStack { | |
Spacer() | |
Slider(value: $constant.x, in: 0.3504...(constant.y - 0.0001)) | |
Slider(value: $constant.y, in: 0.3506...0.4) | |
} | |
.padding() | |
} | |
.onChange(of: id) { _, _ in | |
reset() | |
} | |
} | |
var magnification: some Gesture { | |
MagnifyGesture() | |
.updating($magnifyBy) { value, gestureState, transaction in | |
gestureState = value.magnification / 2.0 | |
scale = lastScale + value.magnification / 2.0 | |
} | |
.onEnded { value in | |
lastScale = scale | |
} | |
} | |
private func onTap(location: CGPoint, in size: CGSize) { | |
withAnimation { | |
let r = pow(scale, 2) | |
self.location.x += 1/r * (location.x - size.width/2) / size.width | |
self.location.y += 1/r * (location.y - size.height/2) / size.width | |
lastLocation = self.location | |
} | |
} | |
private func dragGesture(size: CGSize) -> some Gesture { | |
DragGesture() | |
.onChanged { action in | |
onDrag(action: action, in: size) | |
} | |
.onEnded { action in | |
onDrag(action: action, in: size) | |
lastLocation = location | |
} | |
} | |
private func onDrag(action: DragGesture.Value, in size: CGSize) { | |
let base = lastLocation | |
let r = pow(scale, 2) | |
self.location.x = base.x + 1/r * (action.translation.width) / size.width | |
self.location.y = base.y + 1/r * (action.translation.height) / size.width | |
} | |
func reset() { | |
let scale = self.scale | |
let duration = scale > 1 ? min(log(scale), 2) : 0.5 | |
withAnimation(.spring(duration: duration)) { | |
self.scale = 1 | |
self.lastScale = 0.5 | |
} completion: { | |
withAnimation(.spring(duration: duration)) { | |
location = .zero | |
lastLocation = .zero | |
} | |
withAnimation(.spring(duration: 1.5)) { | |
constant = CGPoint(x: 0.3575, y: 0.3575) | |
} | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment