Created
March 15, 2021 08:55
-
-
Save chriseidhof/9dd4ebd90df0984042e36243cbd50e44 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
/// Takes an absolute path and fits it inside the proposed rectangle. | |
struct FittingShape: Shape { | |
var absolutePath: Path | |
func path(in rect: CGRect) -> Path { | |
let p = absolutePath | |
let boundingRect = p.boundingRect | |
let scale = min(rect.width/boundingRect.width, rect.height/boundingRect.height) | |
let scaled = p.applying(.init(scaleX: scale, y: scale)) | |
let scaledBoundingRect = scaled.boundingRect | |
let offsetX = scaledBoundingRect.midX - rect.midX | |
let offsetY = scaledBoundingRect.midY - rect.midY | |
return scaled.offsetBy(dx: -offsetX, dy: -offsetY) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment