Draw a stroke bar with a gradient

I am trying to strike a Bezier curve with a linear gradient going from red to green. I am currently doing the following:

NSBezierPath* path = [NSBezierPath bezierPath];
[path setLineWidth: 1];

NSPoint startPoint = {  10, 100 };
NSPoint endPoint   = { 590, 500 };

int r1 = arc4random() % 1000;
int r2 = arc4random() % 1000;
NSPoint cp1 = { 700, -500 + r1 };
NSPoint cp2 = { -500 + r2, 700 };

[path  moveToPoint: startPoint];
[path curveToPoint: endPoint
     controlPoint1: cp1
     controlPoint2: cp2];

if (curves.count == 50) {
    [curves removeObjectAtIndex:0];
}
[curves addObject:path];

int i = 0;
for (NSBezierPath * p in curves) {
    [[redColors objectAtIndex:i++] set];
    [p stroke];
}

And this works fine, but when I convert NSBezierPath pathto CGPathRef myPath = [path quartzPath]and iterate over 'CGPathRef' instead of 'NSBezierPath':

CGPathRef myPath = [path quartzPath];
if (curves.count == size) {
    [paths removeObjectAtIndex:0];
}
[paths addObject:myPath];

CGContextRef c = [[NSGraphicsContext currentContext]graphicsPort];
for (int i = 0; i < paths.count; i++) {
    [self drawPath:c :[paths objectAtIndex:i]:i];
}

My performance drops from about 30 FPS to 5 FPS!

Here is my code for drawPath:

-(void) drawPath:(CGContextRef) c: (CGPathRef) myPath: (int) i {
    CGContextSaveGState(c);
    CGContextAddPath(c, myPath);
    CGContextReplacePathWithStrokedPath(c);
    CGContextClip(c);

    //  Draw a linear gradient from top to bottom
    CGContextDrawLinearGradient(c, cfGradients[i], start, end, 0);

    CGContextRestoreGState(c);
}

redColorsand cfGradients- arrays storing elements with alpha from 0-1 / 0-255, so they do not need to be recreated at each iteration.

This performance is much worse than in Java. Of course, there must be a way to make the strike more effective without all these transitions from NSBezierPathto CGPathRef, etc.

Please, help.

+3
source share

Source: https://habr.com/ru/post/1779396/


All Articles