使用NSBezierPath绘制变化的线宽? [英] Drawing varying line widths with NSBezierPath?

查看:101
本文介绍了使用NSBezierPath绘制变化的线宽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以用数位板绘制的矢量对象,并将遵循来自笔的压力信息(通过适当地增加/减少线宽).现在,我知道如何从NSEvent中获取压力信息,但是,当然,NSBezierPath不支持变化的线宽.

I would like to create a vector object that can be drawn with a pen tablet and will honor the pressure information from the pen (by increasing/decreasing line width appropriately). Now I know how to get the pressure info out of the NSEvent, but of course NSBezierPath doesn't support varying line widths.

因此,我试图通过计算曲线两端的垂直线并将它们与同一条曲线连接起来,以生成一条Bezier路径作为我的线条的轮廓.它几乎可以在我的小测试项目中针对单个曲线段工作:

So I'm trying to generate a Bezier path that is the outline of my line, by calculating the perpendicular lines at the ends of the curve and connecting those with the very same curve. It nearly works in my little test project for a single curve segment:

https://github.com/uliwitness/WideningBezierPathTest

但这是一个大问题:线在中间变细到几乎只有2个像素(应该只从32变到8).有谁知道我将如何调整控制点,以使弯曲部分中的线的两个边缘之间的距离大致相同?

but this has one big issue: The line thins to barely 2 pixels in the middle (it should only go from 32 to 8). Does anyone know how I'd adjust the control points so the two edges of the line in the curved part are at roughly the same distance ?

有人有想法吗?有什么建议吗?有用的文章?

Anyone have ideas? Suggestions? Useful articles?

推荐答案

感谢其他人的建议,我设法将一些可行的方法拼凑而成.我已将更改提交到存储库,但是如果您想查看旧代码,可以回溯一些修订.这是我现在要做的:

Thanks to suggestions from others I've managed to cobble something together that works. I've committed the changes to the repository, but you can go back a few revisions if you want to see the older code. Here's what I do now:

  1. 我使用bezierPathByFlatteningPath展平了路径.这给了我直线段.
  2. 然后,我在每个线段的起点和终点计算垂直线的起点和终点(这些线的长度与此时的线长一样).
  3. 我创建了一个包含平行四边形的贝塞尔曲线路径,该平行四边形由两条垂直线以及连接它们的线组成.这样可以使每个线段的线宽平滑过渡.
  4. 一旦有了这些线段,就可以画出路径的末端垂直线,再加上每个线段的边(但不能画出线段之间的垂直线)到新的贝塞尔曲线路径中,然后我就可以填充它来绘制笔划了所需的线宽变化.

如果要使用它进行打印或在HiDPI下使用,则可能不得不考虑贝塞尔曲线路径的平坦度",但是对于1倍屏幕显示来说,它看起来还不错.

If you want to use this for printing or under HiDPI, you might have to muck with the "flatness" of the bezier path, but for 1x screen display it looks fine.

这篇关于使用NSBezierPath绘制变化的线宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆