RoughSwift allows us to easily make shapes in hand drawn, sketchy, comic style in SwiftUI.
Support iOS, tvOS
Support all shapes: line, rectangle, circle, ellipse, linear path, arc, curve, polygon, svg path
Generate UIBezierPath for CAShapeLayer
Easy cusomizations with Options
Easy composable APIs
Convenient draw functions
Platform independant APIs which can easily support new platforms
Test coverage
Immutable and type safe data structure
SVG elliptical arc
There are Example project where you can explore further.
Basic
Use generator in draw function to specify which shape to render. The returned CALayer contains the rendered result in correct size and is updated everytime generator is instructed.
SVG shape can be bigger or smaller than the specifed layer size, so RoughSwift scales them to your requested size. This way we can compose and transform the SVG shape.
With all the primitive shapes, we can create more beautiful things. The only limit is your imagination.
Here’s how to create chart
struct Chartview: View {
var heights: [CGFloat] {
Array(0 ..< 10).map { _ in CGFloat.random(in: 0 ..< 150) }
}
var body: some View {
HStack {
ForEach(0 ..< 10) { index in
VStack {
Spacer()
RoughView()
.fill(.yellow)
.rectangle()
.frame(height: heights[index])
}
}
}
.padding(.horizontal)
.padding(.bottom, 100)
}
}
Advance with Drawable, Generator and Renderer
Behind the screen, we composes Generator and Renderer.
We can instantiate Engine or use a shared Engine for memory efficiency, to make Generator. Every time we instruct Generator to draw a shape, the engine works hard to figure out information about the sketchy shape in Drawable.
The name of these concepts follow rough.js for better code reasoning.
For iOS, there is a Renderer that can handle Drawable and transform it into UIBezierPath and CALayer. There will be more Renderer that can render into graphics context, image and for other platforms like macOS and watchOS.
let layer = CALayer()
let size = CGSize(width: 200, heigh: 200)
let renderer = Renderer(layer: layer)
let generator = Engine.shared.generator(size: bounds.size)
let drawable: Drawable = Rectangle(x: 10, y: 10, width: 100, height: 50)
let drawing = generate.generate(drawable: drawable)
renderer.render(drawing: drawing)
Installation
Add the following line to the dependencies in your Package.swift file
❤️ Support my apps ❤️
❤️❤️😇😍🤘❤️❤️
Description
RoughSwift allows us to easily make shapes in hand drawn, sketchy, comic style in SwiftUI.
UIBezierPath
forCAShapeLayer
There are Example project where you can explore further.
Basic
Use
generator
indraw
function to specify which shape to render. The returnedCALayer
contains the rendered result in correctsize
and is updated everytimegenerator
is instructed.Here’s how to draw a green rectangle
The beauty of
CALayer
is that we can further animate, transform (translate, scale, rotate) and compose them into more powerful shapes.Options
Options
is used to custimize shape. It is immutable struct and apply to one shape at a time. The following properties are configurableShapes
RoughSwift supports all primitive shapes, including SVG path
Fill style
Most of the time, we use
fill
for solid fill color inside shape,stroke
for shape border, andfillStyle
for sketchy fill style.Available fill styles
Here’s how to draw circles in different fill styles. The default fill style is hachure
SVG
SVG shape can be bigger or smaller than the specifed layer size, so RoughSwift scales them to your requested
size
. This way we can compose and transform the SVG shape.Creative shapes
With all the primitive shapes, we can create more beautiful things. The only limit is your imagination.
Here’s how to create chart
Advance with Drawable, Generator and Renderer
Behind the screen, we composes
Generator
andRenderer
.We can instantiate
Engine
or use a sharedEngine
for memory efficiency, to makeGenerator
. Every time we instructGenerator
to draw a shape, the engine works hard to figure out information about the sketchy shape inDrawable
.The name of these concepts follow
rough.js
for better code reasoning.For iOS, there is a
Renderer
that can handleDrawable
and transform it intoUIBezierPath
andCALayer
. There will be moreRenderer
that can render into graphics context, image and for other platforms like macOS and watchOS.Installation
Add the following line to the dependencies in your
Package.swift
fileAuthor
Khoa Pham, onmyway133@gmail.com
Credit
Contributing
We would love you to contribute to RoughSwift, check the CONTRIBUTING file for more info.
License
RoughSwift is available under the MIT license. See the LICENSE file for more info.