Created
September 27, 2020 12:29
-
-
Save laevandus/d782ab48185b6d358e00fcc545bf25c1 to your computer and use it in GitHub Desktop.
This file contains 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
struct Wave: Shape { | |
var amplitude: Double | |
var frequency: Double | |
func path(in rect: CGRect) -> Path { | |
let sinCenterY = amplitude | |
let path = CGMutablePath() | |
path.move(to: CGPoint(x: 0, y: sinCenterY)) | |
let width = Double(rect.width) | |
for x in stride(from: 0, through: width, by: 1) { | |
let y = sinCenterY + amplitude * sin(frequency * x) | |
path.addLine(to: CGPoint(x: x, y: y)) | |
} | |
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) | |
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY)) | |
path.addLine(to: CGPoint(x: Double(rect.minX), y: sinCenterY)) | |
return Path(path) | |
} | |
var animatableData: AnimatablePair<Double, Double> { | |
get { | |
return AnimatablePair(amplitude, frequency) | |
} | |
set { | |
amplitude = newValue.first | |
frequency = newValue.second | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment