如何在不混合重叠但与背景混合的情况下绘制一系列具有不同不透明度的线条?
How to draw a sequence of lines with different opacities without blending overlaps but blending with the background?
我正在尝试为绘图应用程序添加基本的压力敏感度。
当我尝试绘制笔划时,我 运行 遇到了一个问题,其中不透明度取决于压力 - 线段的重叠点混合了两条线,这会产生较暗的点:
这是我的代码:
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, path.thickness);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
for (Segment *segment in segments) {
CGContextMoveToPoint(context, segment.start.x, segment.start.y);
CGContextAddLineToPoint(context, segment.end.x, segment.end.y);
CGContextSetAlpha(context, segment.alpha);
CGContextStrokePath(context);
}
我想避开线帽相交处的圆圈,但仍然能够使背景透明。
我已经尝试过上下文混合模式,但没有什么是完全令人满意的 - kCGBlendModeDestinationAtop
给了我平滑的线条,但在线条的最末端有一个伪影,我失去了与背景的融合。
条带不是问题。
万一有人遇到同样的问题,我的解决方法是将线段绘制到 CGLayer(Core Graphics 层 - 不是 Core Animation 层也不是 Core Graphics Transparency 层)。
我将 CGLayer 上下文混合模式设置为 DestinationAtop,然后在我的真实 CGContext 中使用正常混合渲染 CGLayer。
工作得很好。
我正在尝试为绘图应用程序添加基本的压力敏感度。 当我尝试绘制笔划时,我 运行 遇到了一个问题,其中不透明度取决于压力 - 线段的重叠点混合了两条线,这会产生较暗的点:
这是我的代码:
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, path.thickness);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
for (Segment *segment in segments) {
CGContextMoveToPoint(context, segment.start.x, segment.start.y);
CGContextAddLineToPoint(context, segment.end.x, segment.end.y);
CGContextSetAlpha(context, segment.alpha);
CGContextStrokePath(context);
}
我想避开线帽相交处的圆圈,但仍然能够使背景透明。
我已经尝试过上下文混合模式,但没有什么是完全令人满意的 - kCGBlendModeDestinationAtop
给了我平滑的线条,但在线条的最末端有一个伪影,我失去了与背景的融合。
条带不是问题。
万一有人遇到同样的问题,我的解决方法是将线段绘制到 CGLayer(Core Graphics 层 - 不是 Core Animation 层也不是 Core Graphics Transparency 层)。 我将 CGLayer 上下文混合模式设置为 DestinationAtop,然后在我的真实 CGContext 中使用正常混合渲染 CGLayer。 工作得很好。