Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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