Skip to content

Instantly share code, notes, and snippets.

@SatoTakeshiX
Created January 31, 2021 06:34
Show Gist options
  • Save SatoTakeshiX/c7ac3d6c82258aae693a997b86036b69 to your computer and use it in GitHub Desktop.
Save SatoTakeshiX/c7ac3d6c82258aae693a997b86036b69 to your computer and use it in GitHub Desktop.
SuperEllipse
// Created by satoutakeshi on 2021/01/30 in MIT License
//
import SwiftUI
struct SuperEllipse: View {
var body: some View {
Image(systemName: "moon")
.resizable()
.frame(width: 100, height: 100)
.background(Color.yellow)
.clipShape(SuperEllipseShape(rate: 0.75))
}
}
struct SuperEllipseShape: Shape {
let rate: CGFloat
func path(in rect: CGRect) -> Path {
let handleX: CGFloat = rect.size.width * rate / 2
let handleY: CGFloat = rect.size.height * rate / 2
let left = CGPoint(x: rect.minX, y: rect.midY)
let top = CGPoint(x: rect.midX, y: rect.minY)
let right = CGPoint(x: rect.maxX, y: rect.midY)
let bottom = CGPoint(x: rect.midX, y: rect.maxY)
var path = Path()
path.move(to: left)
path.addCurve(to: top,
control1: CGPoint(x: left.x, y: left.y - handleY),
control2: CGPoint(x: top.x - handleX, y: top.y))
path.addCurve(
to: right,
control1: CGPoint(x: top.x + handleX, y: top.y),
control2: CGPoint(x: right.x, y: right.y - handleY)
)
path.addCurve(
to: bottom,
control1: CGPoint(x: right.x, y: right.y + handleY),
control2: CGPoint(x: bottom.x + handleX, y: bottom.y)
)
path.addCurve(
to: left,
control1: CGPoint(x: bottom.x - handleX, y: bottom.y),
control2: CGPoint(x: left.x, y: left.y + handleY)
)
return path
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
SuperEllipse()
}
}
@SatoTakeshiX
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment