• Stars
    star
    1,104
  • Rank 42,038 (Top 0.9 %)
  • Language
    Swift
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŽƒ Create hand-drawn, sketchy, comic shape in Swift

โค๏ธ Support my apps โค๏ธ

โค๏ธโค๏ธ๐Ÿ˜‡๐Ÿ˜๐Ÿค˜โค๏ธโค๏ธ

Description

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.

Here's how to draw a green rectangle

RoughView()
    .fill(.yellow)
    .fillStyle(.hachure)
    .hachureAngle(-41)
    .hachureGap(-1)
    .fillWeight(-1)
    .stroke(.systemTeal)
    .strokeWidth(2)
    .curveTightness(0)
    .curveStepCount(9)
    .dashOffset(-1)
    .dashGap(-1)
    .zigzagOffset(-9)

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 configurable

  • maxRandomnessOffset
  • toughness
  • bowing
  • fill
  • stroke
  • strokeWidth
  • curveTightness
  • curveStepCount
  • fillStyle
  • fillWeight
  • hachureAngle
  • hachureGap
  • dashOffset
  • dashGap
  • zigzagOffset

Shapes

RoughSwift supports all primitive shapes, including SVG path

  • line
  • rectangle
  • ellipse
  • circle
  • linearPath
  • arc
  • curve
  • polygon
  • path

Fill style

Most of the time, we use fill for solid fill color inside shape, stroke for shape border, and fillStyle for sketchy fill style.

Available fill styles

  • crossHatch
  • dashed
  • dots
  • hachure
  • solid
  • starBurst
  • zigzag
  • zigzagLine

Here's how to draw circles in different fill styles. The default fill style is hachure

struct StylesView: View {
    var body: some View {
        LazyVGrid(columns: [.init(), .init(), .init()], spacing: 12) {
            RoughView()
                .fill(.red)
                .fillStyle(.crossHatch)
                .circle()
                .frame(width: 100, height: 100)

            RoughView()
                .fill(.green)
                .fillStyle(.dashed)
                .circle()
                .frame(width: 100, height: 100)

            RoughView()
                .fill(.purple)
                .fillStyle(.dots)
                .circle()
                .frame(width: 100, height: 100)

            RoughView()
                .fill(.cyan)
                .fillStyle(.hachure)
                .circle()
                .frame(width: 100, height: 100)

            RoughView()
                .fill(.orange)
                .fillStyle(.solid)
                .circle()
                .frame(width: 100, height: 100)

            RoughView()
                .fill(.gray)
                .fillStyle(.starBurst)
                .circle()
                .frame(width: 100, height: 100)

            RoughView()
                .fill(.yellow)
                .fillStyle(.zigzag)
                .circle()
                .frame(width: 100, height: 100)

            RoughView()
                .fill(.systemTeal)
                .fillStyle(.zigzagLine)
                .circle()
                .frame(width: 100, height: 100)
        }
    }
}

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.

struct SVGView: View {
    var apple: String {
        "M85 32C115 68 239 170 281 192 311 126 274 43 244 0c97 58 146 167 121 254 28 28 40 89 29 108 -25-45-67-39-93-24C176 409 24 296 0 233c68 56 170 65 226 27C165 217 56 89 36 54c42 38 116 96 161 122C159 137 108 72 85 32z"
    }

    var body: some View {
        VStack {
            RoughView()
                .stroke(.systemTeal)
                .fill(.red)
                .draw(Path(d: apple))
                .frame(width: 300, height: 300)
        }
    }
}

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

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

.package(url: "https://github.com/onmyway133/RoughSwift"),

Author

Khoa Pham, [email protected]

Credit

  • rough for the generator that powers RoughSwift. All the hard work is done via rough in JavascriptCore.
  • SVGPath for constructing UIBezierPath from SVG path

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.

More Repositories

1

awesome-ios-animation

โ˜” A collection of iOS animation repos
5,305
star
2

awesome-ios-architecture

๐Ÿฏ Better ways to structure iOS apps
5,056
star
3

PushNotifications

๐Ÿ‰ A macOS, Linux, Windows app to test push notifications on iOS and Android
JavaScript
2,230
star
4

DeepDiff

๐Ÿฆ€Amazingly incredible extraordinary lightning fast diffing in Swift
Swift
2,049
star
5

IconGenerator

๐Ÿฑ A macOS app to generate app icons
JavaScript
1,403
star
6

FinderGo

๐Ÿข Open terminal quickly from Finder
Swift
1,084
star
7

Snowflake

โ„๏ธ SVG in Swift
Swift
956
star
8

GifCapture

๐Ÿ‡ Gif capture app for macOS
Swift
916
star
9

awesome-machine-learning

๐ŸŽฐ A curated list of machine learning resources, preferably CoreML
794
star
10

Swiftlane

๐Ÿš€ Build utilities in pure Swift
Swift
692
star
11

blog

๐Ÿ What you don't know is what you haven't learned
666
star
12

awesome-swiftui

๐ŸŒฎ Awesome resources, articles, libraries about SwiftUI
642
star
13

EasyConfetti

๐ŸŽŠ Fancy confetti effects in Swift
Swift
561
star
14

XcodeWay

โ›ต An Xcode Source Editor Extension that helps navigating to many places easier
Swift
550
star
15

Smile

๐Ÿ˜„ Emoji in Swift
Swift
512
star
16

EasyAnchor

โš“๏ธ Declarative, extensible, powerful Auto Layout
Swift
455
star
17

EasyStash

๐Ÿ—ณEasy data persistence in Swift
Swift
377
star
18

Scale

๐ŸŽ Unit converter in Swift
Swift
323
star
19

EasyDropdown

๐Ÿ’ง Fantastic dropdown in Swift
Swift
309
star
20

XcodeColorSense2

๐Ÿ‰ An Xcode source editor extension that shows hex color info
Swift
285
star
21

Arcane

๐Ÿ”ฑ CommonCrypto in Swift, and more
Swift
283
star
22

awesome-android-animation

๐Ÿ’ฆ A collection of Android animation repos
Java
253
star
23

EasyTheme

๐Ÿ‘•๐Ÿ‘š Theme management in Swift
Swift
241
star
24

awesome-voip

๐Ÿค™Learning VoIP, RTP, pjsip and SIP
237
star
25

SwiftHash

๐Ÿ• MD5 in pure Swift
Swift
215
star
26

EasyClosure

๐Ÿฉ Unified communication patterns with easy closure in Swift
Swift
201
star
27

Github.swift

:octocat: Unofficial GitHub API client in Swift
Swift
186
star
28

emoji

โค๏ธ List of emojis
Swift
163
star
29

SwiftSnippets

๐Ÿฎ A collection of Swift snippets to be used in Xcode
Shell
162
star
30

Wave

๐ŸŒŠ Declarative chainable animations in Swift
Swift
127
star
31

awesome-online-earning

๐Ÿ’ฐWays to earn extra
124
star
32

Spek

๐ŸŽ Function builder BDD testing framework in Swift
Swift
122
star
33

FontAwesomeSwiftUI

Easy to use FontAwesome 5 in SwiftUI
Swift
97
star
34

Recipes

๐ŸฃMaking Recipes iOS app
Swift
91
star
35

MathSolver

โŒจ๏ธCamera calculator with Vision
Swift
87
star
36

XcodeColorSense

๐ŸŽˆ An Xcode plugin that makes working with color easier
Swift
77
star
37

Micro

๐ŸŽFast diffing and type safe SwiftUI style data source for UICollectionView
Swift
76
star
38

Omnia

๐Ÿ—ฟ Everything your projects need to know
Swift
62
star
39

notes

๐Ÿ“” Issues and solutions I found during development, mostly iOS
62
star
40

Sketch-Action

๐Ÿ”Ž A Sketch plugin to simulate Spotlight
Objective-C
59
star
41

MainThreadGuard

๐Ÿ’‚โ€โ™‚๏ธ Tracking UIKit access on main thread
Swift
58
star
42

EasyFake

๐Ÿคนโ€โ™€๏ธ Fake data generation in Swift
JavaScript
57
star
43

Reindeers

โ„๏ธ XML and HTML parser in Swift
Swift
55
star
44

awesome-git-commands

๐Ÿด Indispensable git commands for everyday use
55
star
45

RxLifeCycle

๐Ÿน Easy life cycle observation
Swift
53
star
46

Upstream

โ›ฒ๏ธ Adapted Data Source in Swift and a little bit more
Swift
53
star
47

BigBigNumbers

๐Ÿ”ขSay the number out loud
Swift
49
star
48

GifMagic

๐Ÿ’ˆ Gif maker and extractor in Swift
Swift
48
star
49

awesome-android-architecture

๐Ÿฐ Better ways to structure Android apps
45
star
50

AppStoreConnect

๐ŸŽ Interact with AppStoreConnect
Swift
45
star
51

TestTarget

๐Ÿณ Notes on configuring test targets
Swift
44
star
52

PhotoFeed

๐Ÿ›ต Instagram in Swift 4
Swift
43
star
53

awesome-conference-videos

๐ŸŽธ Conference videos. Visit http://learntalks.com/
40
star
54

CollectionUpdateExample

๐Ÿ›ถ Demo batch update in UITableView and UICollectionView
Swift
38
star
55

Construction

๐Ÿ‘ท The many ways to construct and configure your entity
Swift
34
star
56

PastePal

PastePal is the best universal clipboard manager for Mac, iPhone and iPad.
31
star
57

github-extended

:octocat: A Chrome extension to discover more repositories
JavaScript
30
star
58

EasyNetworking

๐Ÿ›ฐEasy networking with async/await
Swift
28
star
59

Runtime-Headers

๐Ÿ’ฟ iOS, macOS runtime headers
Objective-C
28
star
60

UsingPlayground

๐Ÿ‘ถ Demo how to use Playground
Swift
26
star
61

EasySwiftUI

๐Ÿฐ Extra sugar for SwiftUI
Swift
25
star
62

Farge

๐ŸŽˆTell the name of hex color
Swift
24
star
63

Avengers

๐Ÿ‘พDemo how to use CoreML with IBM Watson, Azure Custom Vision, Turi Create
Swift
21
star
64

ISO8601

๐ŸŽ— Super lightweight ISO8601 Date Formatter in Swift
Swift
21
star
65

SlidingMenu

๐ŸŽฟ Demo how to create sliding menu on Android
Java
20
star
66

Paramount

๐Ÿ—ผ Like Flipboard FLEX, but allows custom action
Swift
20
star
67

Codelime

Codelime is a powerful code snippet manager with many dev tools for iOS development.
19
star
68

DynamicTableViewCellHeight

๐ŸŸ Demo how to use Autolayout to make varying row height
Objective-C
18
star
69

github-chat

๐Ÿ’ฌ A Chrome extension to enable chat within GitHub
JavaScript
18
star
70

Resolver

๐ŸŽŠ A simple resolver in Swift
Swift
15
star
71

archives

๐Ÿšฃ Only deliberate practice will make you better
Objective-C
14
star
72

ParticlePlayground

๐ŸŽŠ A Swift playground to play with CAEmitterLayer
Swift
14
star
73

json_resolve

๐ŸฐEasy resolving deep json using keypath in Dart
Dart
12
star
74

Then

๐Ÿƒ Promise A+ in Swift
Swift
11
star
75

LighterAppDelegate

๐ŸŽฃ Lighter AppDelegate by dispatching events
Swift
10
star
76

Xkcd

๐Ÿฆ„ Simple Xkcd iOS app
Swift
8
star
77

Border

๐Ÿ“ Make border for all views
Swift
8
star
78

Xmas

๐ŸŽ… ๐ŸŽ„ โ›„ Xmas plugin for Xcode
Objective-C
7
star
79

github-changelogs-maker

๐Ÿป Generate changelogs between 2 latest releases
JavaScript
7
star
80

linphone-iphone

Forked from linphone.org
Objective-C
7
star
81

EasyTracker

๐Ÿ•ต๏ธ Trackers in Swift
Swift
6
star
82

FantasticDisplay

๐ŸŽข Fantastic HUD and toast in Swift
Swift
6
star
83

ComputerScienceSwift

๐Ÿ‘จโ€๐Ÿ’ป Practice computer science in Swift
Swift
6
star
84

PushHero

Push Hero is a native macOS application written in pure Swift that allows us to easily test push notifications
6
star
85

onmyway133

What you don't know is what you haven't learned
5
star
86

Minion

๐Ÿ Working with Xcode plugins
Swift
5
star
87

SketchHeaders

๐Ÿ’ฟ Headers from Sketch
Objective-C
4
star
88

homebrew-formulae

๐Ÿš— Homebrew formulae
Ruby
3
star
89

chat_webrtc

โŒจ๏ธ Simple chat application using Node.js and WebRTC Data Channel
JavaScript
3
star
90

DarkSide

๐ŸŒŒ My dark theme based on Atom One Dark
Shell
3
star
91

android-architecture-components-kotlin

๐Ÿฅ‹ Samples for Android Architecture Components in Kotlin
Kotlin
2
star
92

slackbot-yoda

๐Ÿ’ Slackbot Yoda
JavaScript
2
star
93

onmyway133.github.io

Showcase
HTML
2
star
94

traffic_simulator

๐Ÿš— Traffic simulator using enchant.js
JavaScript
2
star
95

rtpproxy

๐Ÿฅ‚ Forked from http://www.rtpproxy.org/ with IP handover support
C
2
star
96

Shipmunk

Search, bookmark and track keyword rating for your favorite apps
2
star
97

Dust

๐Ÿš€ Unofficial, simple push notification
Swift
1
star
98

MarqueeTextBlock

๐ŸŒด How to make marquee TextBlock on Windows Phone
C#
1
star