Skip to content

Commit

Permalink
[ios] Restore line smoothing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanregisser committed Jun 4, 2018
1 parent e2f784b commit d774c8a
Showing 1 changed file with 60 additions and 21 deletions.
81 changes: 60 additions & 21 deletions ios/RNSketchCanvas/RNSketchCanvas/RNSketchData.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,32 @@ - (instancetype)initWithId:(int) pathId strokeColor:(UIColor*) strokeColor strok
}

- (CGRect)addPoint:(CGPoint) point {
NSValue *lastPointValue = _points.lastObject;
[_points addObject: [NSValue valueWithCGPoint: point]];

CGRect updateRect = CGRectMake(point.x, point.y, 0, 0);
CGRect updateRect;

if (lastPointValue) {
CGPoint lastPoint = lastPointValue.CGPointValue;
updateRect = CGRectUnion(updateRect, CGRectMake(lastPoint.x, lastPoint.y, 0, 0));
NSUInteger pointsCount = _points.count;
if (pointsCount >= 3) {
CGPoint a = _points[pointsCount - 3].CGPointValue;
CGPoint b = _points[pointsCount - 2].CGPointValue;
CGPoint c = point;
CGPoint prevMid = midPoint(a, b);
CGPoint currentMid = midPoint(b, c);

updateRect = CGRectMake(prevMid.x, prevMid.y, 0, 0);
updateRect = CGRectUnion(updateRect, CGRectMake(b.x, b.y, 0, 0));
updateRect = CGRectUnion(updateRect, CGRectMake(currentMid.x, currentMid.y, 0, 0));
} else if (pointsCount >= 2) {
CGPoint a = _points[pointsCount - 2].CGPointValue;
CGPoint b = point;
CGPoint mid = midPoint(a, b);

updateRect = CGRectMake(a.x, a.y, 0, 0);
updateRect = CGRectUnion(updateRect, CGRectMake(mid.x, mid.y, 0, 0));
} else {
updateRect = CGRectMake(point.x, point.y, 0, 0);
}

updateRect = CGRectInset(updateRect, -_strokeWidth * 2, -_strokeWidth * 2);

return updateRect;
Expand All @@ -63,25 +80,21 @@ - (void)drawLastPointInContext:(CGContextRef)context {
return;
};

CGPoint fromPoint = _points[pointsCount - (pointsCount > 1 ? 2 : 1)].CGPointValue;
CGPoint toPoint = _points[pointsCount - 1].CGPointValue;

[self drawSegmentInContext:context from:fromPoint to:toPoint];
[self drawInContext:context pointIndex:pointsCount - 1];
}
- (void)drawInContext:(CGContextRef)context {
NSValue *prevPointValue = _points.count == 1 ? _points[0] : nil;
for (NSValue *pointValue in _points) {
if (!prevPointValue) {
prevPointValue = pointValue;
continue;
}

[self drawSegmentInContext:context from:prevPointValue.CGPointValue to:pointValue.CGPointValue];
prevPointValue = pointValue;
NSUInteger pointsCount = _points.count;
for (NSUInteger i = 0; i < pointsCount; i++) {
[self drawInContext:context pointIndex:i];
}
}

- (void)drawSegmentInContext:(CGContextRef)context from:(CGPoint)fromPoint to:(CGPoint)toPoint {
- (void)drawInContext:(CGContextRef)context pointIndex:(NSUInteger)pointIndex {
NSUInteger pointsCount = _points.count;
if (pointIndex >= pointsCount) {
return;
};

BOOL isErase = [Utility isSameColor:_strokeColor color:[UIColor clearColor]];

CGContextSetStrokeColorWithColor(context, _strokeColor.CGColor);
Expand All @@ -90,8 +103,34 @@ - (void)drawSegmentInContext:(CGContextRef)context from:(CGPoint)fromPoint to:(C
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetBlendMode(context, isErase ? kCGBlendModeClear : kCGBlendModeNormal);
CGContextBeginPath(context);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);

if (pointsCount >= 3 && pointIndex >= 2) {
CGPoint a = _points[pointIndex - 2].CGPointValue;
CGPoint b = _points[pointIndex - 1].CGPointValue;
CGPoint c = _points[pointIndex].CGPointValue;
CGPoint prevMid = midPoint(a, b);
CGPoint currentMid = midPoint(b, c);

// Draw a curve
CGContextMoveToPoint(context, prevMid.x, prevMid.y);
CGContextAddQuadCurveToPoint(context, b.x, b.y, currentMid.x, currentMid.y);
} else if (pointsCount >= 2 && pointIndex >= 1) {
CGPoint a = _points[pointIndex - 1].CGPointValue;
CGPoint b = _points[pointIndex].CGPointValue;
CGPoint mid = midPoint(a, b);

// Draw a line to the middle of points a and b
// This is so the next draw which uses a curve looks correct and continues from there
CGContextMoveToPoint(context, a.x, a.y);
CGContextAddLineToPoint(context, mid.x, mid.y);
} else if (pointsCount >= 1) {
CGPoint a = _points[pointIndex].CGPointValue;

// Draw a single point
CGContextMoveToPoint(context, a.x, a.y);
CGContextAddLineToPoint(context, a.x, a.y);
}

CGContextStrokePath(context);
}

Expand Down

0 comments on commit d774c8a

Please sign in to comment.