拉斐尔用鼠标绘制路径
我正在使用 raphael javascript 库,我想用鼠标画一条直线。我想让用户单击某处,放置路径的一个点,然后让线条跟随鼠标,直到再次单击,此时线条将永久放置在画布上。
现在,唯一的方法似乎是在单击时创建一条路径,在移动鼠标时不断删除并重新绘制它,然后在再次单击时再次创建它,始终跟踪绘制模式。虽然这有效,但有点复杂和混乱(特别是构建“Mx yLx y”字符串来定义新路径),我想知道是否有更好的方法来做到这一点。路径上的拉斐尔文档还有一些不足之处。
谢谢!
I'm using the raphael javascript library, and I'd like to draw a straight line using the mouse. I'd like to let the user click somewhere, place a single point of the path, and then have the line follow the mouse until they click again, at which point the line is placed permanently on the canvas.
Right now the only way to do that seems to be to create a path when they click, constantly remove and redraw it when they move the mouse, and then create it once more when they click again, keeping track of the drawing mode all throughoutj. While this works, it's a bit convoluted and messy (especially building up 'Mx yLx y' strings to define the new path), and I was wondering if there's a better way to do this. The raphael documentation on path leaves a little to be desired.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上有一种更好的方法可以做到这一点,即使用
path.attr('path')
。path
是路径部分数组的数组,例如,如果更新它,则不需要每次都从头开始绘制路径。
There's actually a better way to do this, using
path.attr('path')
.path
is an array of path part arrays, e.g.If you update it then you don't need to draw the path from scratch each time.
据我所知,你做得对。我要说的唯一一件事是,您可以从一条路径到另一条路径设置动画,而不是替换旧路径,并且您可以强制执行最大帧速率(例如每秒不超过 5 条路径更新,但您需要尝试看看什么有效)为你)。
至于路径的文档,我认为没有什么可说的了。该方法接受 SVG 路径字符串并绘制它。您需要阅读的可能是路径的 SVG 文档。
如何为路径设置动画:
From what I can tell you're doing it right. The only thing I will ad is that you could animate from one path to another instead of replacing the old one and you could enforce a maximum frame rate (say no more than 5 path updates per second, but you need to try and see what works for you).
As for the documentation for path I don't think there is anything more that can be said. The method accepts a SVG path string and draws it. What you need to read may be the SVG documentation for path.
How to animate a path: