用画布绘制流畅的线条
到目前为止,平滑线上的所有线程都不是正确的。
如何使用javascript HTML5 canvas通过N个点绘制平滑曲线?
两者都会导致锯齿状线条。我所说的平滑是指使用 x,y 点作为控制点来使线条平滑。该线不需要穿过这些点。它只需要在给定 n 个点的情况下画一条平滑的线。
基本上我正在记录每个线段,然后当用户向上移动鼠标时,它会平滑该线。
我已经尝试使用 bezierCurveTo 自己的方法,但这只能平滑所有其他点,然后连接点仍然很粗糙。互联网似乎认为我正在寻找的东西称为 B 样条曲线。我尝试将线性代数矩阵应用于该问题,但失败了,哈哈。
这是我能得到的最佳曲线(图片)。红色线是“平滑”线,您可以看到它平滑了所有其他点,但不连续。 中的代码
这是使用如何使用 javascript HTML5 canvas 通过 N 个点绘制平滑曲线?
我的代码做了同样的事情
http://www.square-bracket.com/images/smoothlines.png
感谢你的帮助!
So far none of the threads here on smooth lines are correct.
how to draw smooth curve through N points using javascript HTML5 canvas?
Smooth user drawn lines in canvas
Both result in jagged lines. By smooth I mean using the x,y points as control points to make the line smooth. The line does NOT need to go through the points. It simply has to draw a smooth line given n points.
Basically I'm recording each line segment, then when the user mouses up, it wil smooth the line.
I've tried my own method using bezierCurveTo but that only smooths every other point, and then the connecting points are harsh still. The internet seems to think what I'm looking for is called B-Spline curves. I tried applying a linear algebra matrix to the problem, but I failed at that lol.
Here is the best curve I can get, (image). The line in red is the "smoothed" line, as you can see it smooths every other point, but not continuous. This is using the code from
how to draw smooth curve through N points using javascript HTML5 canvas?
My code does the same thing
http://www.square-bracket.com/images/smoothlines.png
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在线下面的点中保持相同的切线。检查http://jsfiddle.net/FHKuf/4/。
编辑:
抱歉,今天才注意到您的评论。刚好在测试相关的东西并记住了你的问题。碰巧我过去确实写过一些代码来插入一些行。它被称为Catmull-Rom(只是我在谷歌上搜索的一个参考) )它经过中间控制点。我确实将代码更改为我的测试,并认为您可能会对它有所用处。请参阅http://jsfiddle.net/FHKuf/6/。
You need to keep the same tangent in the points bellowing in the line. Check http://jsfiddle.net/FHKuf/4/.
Edit:
Sorry, just noticed your comment today. Just happened to be testing something related and remembered your question. It happens that in the past I did wrote some code to interpolate some lines. It is called Catmull-Rom(just a ref. that I googleed) it passes by the middle control points. I did changed the code to my test and thought that you may have some use to it. See at http://jsfiddle.net/FHKuf/6/.
我正在探索所有技术,但没有找到任何在画布上平滑自由绘图的适当解决方案。然后我简单地使用了具有不同逻辑的quadraticCurveTo,而不使用原始的鼠标点。
我首先计算控制点(中点)并用控制点替换旧的鼠标移动点。我这样做了两次,最后将quadraticCurveTo应用于最终的数组,我得到了超级平滑的绘图。
太棒了。我没有使用这个沉重的 paper.js 和其他平滑库来做到这一点。
这是我的代码:
I was exploring all techniques but did not get any proper solution for smooth free drawing on canvas. Then I simply used quadraticCurveTo with different logic without using original mouse points.
I first calculated control point(mid-point) and replace old mouse move point with the control point. I did this 2 times and finally applied quadraticCurveTo to the final array and I got super smooth drawing.
It was amazing. I did it without using this heavy paper.js and other smoothing libraries.
Here is my code:
@Manoj Verma 使用线点作为控制点是个好主意!我对你的代码做了一些改进。线条仍然很流畅,没有不必要的操作。我还修复了绘图错误直到最后一点
@Manoj Verma nice idea to use line points as control points! I made some improvements to your code. Still nice smooth line without unnecessary operations. Also i fixed bug with drawing till last point