Swift Package providing a view for capturing a users signature/autograph using drag gestures. It is named autograph to avoid confusion with the overloaded word signature 😀
It accepts a Binding to list of strokes represented as [[CGPoint]]
for storing the strokes.
The points are in normalized form allowing for projection onto arbitrary canvas sizes.
For example if using a SwiftData backed model
@Model
class AutographCard {
var strokes: [[CGPoint]]
}
A View
can be constructed consisting of
import SwiftData
@Query private var autographs: [AutoGraphCard]
var body: some View {
...
if let autograph = autographs.first {
Autograph($autograph.strokes)
}
...
}
You can capture the size of the canvas the autograph is being written on in realtime. Do this by providing a Binding<CGSize>
to the intializers canvasSize
parameter.
The package provides a simple SVG encoder as an extension to [[CGPoint]]
.
This can be used by calling svg(strokeWidth:strokeColor:on:)
providing a CGSize for the aspect ratio.
var body: some View {
...
Autograph($autograph.strokes)
Button("Print") {
print("SVG output is: \n\(autograph.strokes.svg(on: CGSize(width: 2, height: 1)) ?? "nil")")
}
...
}
It has a dependency to my utility package SwiftUIHelpers for tracking the canvas size, as well as the SwiftDocCPlugin for generating documentation.
To display the autograph in a view you can use the provided AutographShape
implementation of Shape, or use the path(in:)
extension directly to create more advanced shapes.
Since the view accepts the type Binding<[[CGPoint]]
any Published
reference of that type can be used by accesing its binding with the $
operator.
The latest documentation is hosted here
The Demo Project included in this package has usage examples based on a SwiftData model scheme, showing how to create and display autographs.