Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stroking paths using a pattern color to simulate brush texture

I am making a finger painting app and I am having a hard time giving my brush a nice brush-like feel and texture.

I have this code:

    UIColor * brushTexture = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Brush_Black.png"]]; 
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), brushTexture.CGColor); 

but my output is like this:
Image of Mouse

The texture of my brush is tiled. How can I make it fit to the stroke Size and stop the tiling of the image?

like image 529
SeongHo Avatar asked Nov 23 '11 04:11

SeongHo


1 Answers

Here i found a solution!

    UIImage *texture = [UIImage imageNamed:"brush.png"]    
    CGPoint vector = CGPointMake(currentPoint.x - endPoint.x, currentTPoint.y - endPoint.y);
    CGFloat distance = hypotf(vector.x, vector.y);
    vector.x /= distance;
    vector.y /= distance;
    for (CGFloat i = 0; i < distance; i += 1.0f) {
        CGPoint p = CGPointMake(endPoint.x + i * vector.x, endPoint.y + i * vector.y);
        [texture drawAtPoint:p blendMode:blendMode alpha:0.5f];
    }
like image 157
SeongHo Avatar answered Nov 10 '22 14:11

SeongHo