Permalink
Browse files

Uses bezier curves to improve stroke

  • Loading branch information...
DarthMike committed Aug 9, 2015
1 parent 35628bd commit 02042cd85d3d721b85a2134823ada5589e08dd38
@@ -21,6 +21,7 @@
47D7808F1B1F1D1400076BD6 /* CircleDrawCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47D7808E1B1F1D1400076BD6 /* CircleDrawCommand.swift */; };
47D780931B1F238300076BD6 /* FreehandDrawController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47D780921B1F238300076BD6 /* FreehandDrawController.swift */; };
47E090BD1B2F007D002549B2 /* ComposedCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47E090BC1B2F007D002549B2 /* ComposedCommand.swift */; };
E79E86ED1B66EDB500E319D4 /* Segment.swift in Sources */ = {isa = PBXBuildFile; fileRef = E79E86EC1B66EDB500E319D4 /* Segment.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -52,6 +53,7 @@
47D7808E1B1F1D1400076BD6 /* CircleDrawCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CircleDrawCommand.swift; sourceTree = "<group>"; };
47D780921B1F238300076BD6 /* FreehandDrawController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FreehandDrawController.swift; sourceTree = "<group>"; };
47E090BC1B2F007D002549B2 /* ComposedCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComposedCommand.swift; sourceTree = "<group>"; };
E79E86EC1B66EDB500E319D4 /* Segment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Segment.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -82,6 +84,7 @@
47D780921B1F238300076BD6 /* FreehandDrawController.swift */,
47702F9E1B0E448800417ACF /* Toolbar.swift */,
47702FA01B0E452100417ACF /* Toolbar.xib */,
E79E86EB1B66EDA300E319D4 /* Utilities */,
);
name = Code;
sourceTree = "<group>";
@@ -152,6 +155,14 @@
name = DrawCommands;
sourceTree = "<group>";
};
E79E86EB1B66EDA300E319D4 /* Utilities */ = {
isa = PBXGroup;
children = (
E79E86EC1B66EDB500E319D4 /* Segment.swift */,
);
name = Utilities;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@@ -254,6 +265,7 @@
buildActionMask = 2147483647;
files = (
47919A461B0CE08600D7BFE9 /* DrawViewController.swift in Sources */,
E79E86ED1B66EDB500E319D4 /* Segment.swift in Sources */,
47D7808F1B1F1D1400076BD6 /* CircleDrawCommand.swift in Sources */,
47919A441B0CE08600D7BFE9 /* AppDelegate.swift in Sources */,
47D780891B1F1A9900076BD6 /* DrawCommands.swift in Sources */,
@@ -72,12 +72,15 @@ class FreehandDrawController : NSObject {
}
private func continueAtPoint(point: CGPoint) {
let lineCommand = LineDrawCommand(a: self.lastPoint, b: point, width: self.width, color: self.color)
let segment = Segment(a: self.lastPoint, b: point)
let lineCommand = LineDrawCommand(current: segment, previous: lastSegment, width: self.width, color: self.color)
self.canvas.executeCommands([lineCommand])
self.lineStrokeCommand?.addCommand(lineCommand)
self.lastPoint = point
self.lastSegment = segment
}
private func endAtPoint(point: CGPoint) {
@@ -86,6 +89,7 @@ class FreehandDrawController : NSObject {
}
self.lastPoint = CGPointZero
self.lastSegment = nil
self.lineStrokeCommand = nil
}
@@ -99,4 +103,5 @@ class FreehandDrawController : NSObject {
private var lineStrokeCommand: ComposedCommand?
private var commandQueue: Array<DrawCommand> = []
private var lastPoint: CGPoint = CGPointZero
private var lastSegment: Segment?
}
@@ -25,21 +25,43 @@ THE SOFTWARE.
import UIKit
struct LineDrawCommand : DrawCommand {
let a: CGPoint
let b: CGPoint
let current: Segment
let previous: Segment?
let width: CGFloat
let color: UIColor
// MARK: DrawCommand
func execute(canvas: Canvas) {
self.configure(canvas)
if let previous = self.previous {
self.drawQuadraticCurve(canvas)
} else {
self.drawLine(canvas)
}
}
private func configure(canvas: Canvas) {
CGContextSetStrokeColorWithColor(canvas.context, self.color.CGColor)
CGContextSetLineWidth(canvas.context, self.width)
CGContextSetLineCap(canvas.context, kCGLineCapRound)
CGContextMoveToPoint(canvas.context, a.x, a.y)
CGContextAddLineToPoint(canvas.context, b.x, b.y)
}
private func drawLine(canvas: Canvas) {
CGContextMoveToPoint(canvas.context, self.current.a.x, self.current.a.y)
CGContextAddLineToPoint(canvas.context, self.current.b.x, self.current.b.y)
CGContextStrokePath(canvas.context)
}
private func drawQuadraticCurve(canvas: Canvas) {
if let previousMid = self.previous?.midPoint {
let currentMid = self.current.midPoint
CGContextMoveToPoint(canvas.context, previousMid.x, previousMid.y)
CGContextAddQuadCurveToPoint(canvas.context, current.a.x, current.a.y, currentMid.x, currentMid.y)
CGContextStrokePath(canvas.context)
}
}
}
@@ -0,0 +1,34 @@
/*
The MIT License (MIT)
Copyright (c) 2015-present Badoo Trading Limited.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import UIKit
struct Segment {
let a: CGPoint
let b: CGPoint
var midPoint: CGPoint {
return CGPoint(x: (a.x + b.x) / 2, y: (a.y + b.y) / 2)
}
}

0 comments on commit 02042cd

Please sign in to comment.