如何使用Quartz绘制钢笔笔画?
我知道更好的方法是在 OpenGL 中使用纹理。但我仍然想使用 Quartz 在我的应用程序中绘制矢量图形。
我点击了此链接,但它没有按预期工作。 应用纹理来描边 CGContextStrokePath 可能我误解了什么。
你能告诉我解决这个问题的方法吗?
I know a better way is using a texture in OpenGL. But I still want to use Quartz to draw vector graphic in my App.
I followed this link but it did not work as expected. Applying a texture to stroke a CGContextStrokePath
It's possible I misunderstood something.
Can you show me the way to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设“钢笔笔划”是指根据曲线的方向而变粗或变细的笔划,即垂直时粗,水平时细。
作为矢量图形的钢笔笔划可以是闭合形状。沿钢笔笔尖的轴线沿相反方向偏移曲线的两个副本,每个副本是钢笔笔尖宽度的一半。闭合这些曲线,并用钢笔颜色或图案填充它们。四像素宽的水平笔尖将使第一条曲线向右偏移两个像素,第二条曲线向左偏移两个像素。未与轴对齐的笔尖可能需要一点三角学来计算精确的偏移。如果仅使用预设的笔尺寸和角度,则可以使用看起来不错的整数偏移量来伪造偏移量。
当曲线恰好与笔尖的角度相交时,形状可能会低于所需的最小笔画粗细。这可以通过对这个派生形状应用描边来解决,使最终的效果永远不会太薄。这可能会使厚的部分变得更厚,这可以通过改变偏移量来调整。
编辑以回复有关如何使笔划中心比边缘更暗的评论:
我可以想到几种方法来实现笔划形状的中心更暗。可以绘制不同厚度的多个形状,这些形状彼此层叠。最厚的可以填充最浅的颜色,最薄的可以填充最深的颜色。或者可以将原始路径的笔划绘制为图像,进行模糊处理,然后剪辑为钢笔笔划形状。这可以绘制为具有多重混合模式的第二层,使笔划的中心变暗,并在边缘处逐渐变细。或者可以将形状分成小段。沿着路径的每个小段都可以填充从末端的浅色到中间的深色的渐变。
正如 Caleb 在他的回答中提到的那样,钢笔的现实世界功能和性能特征的许多其他方面可以添加到模拟中。包含的方面越多,绘图代码就越复杂。
I'm assuming that "fountain pen stroke" means a stroke that gets thicker or thinner depending upon the direction the curve is headed, i.e. thick when vertical and thin when horizontal.
A fountain pen stroke as vector graphics can be closed shape. Take the curve and offset two copies in opposite directions along the axis of the fountain pen tip, each half the width of the fountain pen tip. Close those curves, and fill them with the fountain pen color or pattern. A horizontal pen tip four pixels wide would offset one curve two pixels to the right and the second curve two pixels to the left. Pen tips not aligned to an axis may require a little trigonometry to calculate exact offsets. If just using preset pen sizes and angles, the offsets could be fudged with integer offsets that just look good.
Where the curve exactly meets the angle of the pen tip, the shape may drop below the minimum desired stroke thickness. This can be address by also applying a stroke to this derived shape, making the resulting effect never too thin. This may make the thick parts thicker, which can be adjusted by changing the offsets.
Edit to respond to comment about how to make the center of the stroke darker than the edges:
I can think of a few ways to accomplish a darker center of the stroke shape. Multiple shapes can be drawn of varying thickness, layered on top of each other. The thickest could be filled with the lightest color, and the thinest the darkest color. Or a stroke of the original path can be drawn to a image, blurred, then clipped to the fountain pen stroke shape. This can be drawn as a second layer with a multiply blending mode, darkening the center of the stroke, and tapering off at the edges. Or the shape can be broken into little segments. Each little segment along the path can be filled with a gradient that goes from light at the ends to dark in the middle.
As Caleb mentions in his answer, there are plenty of additional aspects of a fountain pen's real world functionality and performance characteristics that could be added to a simulation. The more aspects included, the more complex the drawing code.
继伯纳先生的精彩分析之后,我将指出,除了方向之外,手的压力和笔尖结构也会影响线宽。精细的笔尖可以产生在任何方向上几乎相同宽度的线条,但是笔尖上的压力增加会导致笔尖的两半展开,产生更宽的线条并增加墨水流量。使用更灵活的笔尖(如羽毛笔),这种效果显然会比钢笔尖更明显。
您需要决定如何确定沿您的线条的手部压力变化。您可以为每一行手动执行此操作,或者根据方向的变化、距行尾的距离(当书写者完成一行并开始提起笔时压力减小)、书写者的惯用手(左撇子或右撇子)、书写表面等。
Following on Mr. Berna's excellent analysis, I'll point out that beside direction, hand pressure and nib construction also affect the line width. Fine nibs can produce a line that's almost the same width in any direction, but increased pressure on the nib causes the two halves of the nib to spread, producing a wider line and increased ink flow. This effect will obviously be more pronounced with a more flexible nib like a quill pen than with a steel nib.
You'll need to decide how to determine hand pressure changes along your lines. You might do that manually for each line, or come up with some algorithm based on changes in direction, distance from the end of the line (pressure decreases as the writer finishes a line and starts to lift the pen), handedness of the writer (lefty or righty), writing surface, etc.