拉斐尔用鼠标绘制路径

发布于 2024-10-05 16:23:58 字数 256 浏览 9 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2024-10-12 16:23:59

实际上有一种更好的方法可以做到这一点,即使用path.attr('path')path 是路径部分数组的数组,例如

[
  ['M', 100, 100],
  ['L', 150, 150],
  ['L', 200, 150],
  ['Z']
]

,如果更新它,则不需要每次都从头开始绘制路径。

Raphael.el.addPart = function (point) {
  var pathParts = this.attr('path') || [];
  pathParts.push(point);
  this.attr('path', pathParts);
};

var path = paper.path();
path.addPart(['M', 100, 100]); //moveto 100, 100
path.addPart(['L', 150, 150]); //lineto 150, 150
path.addPart(['L', 200, 150]); //lineto 200, 150
path.addPart(['Z']);           //closepath

There's actually a better way to do this, using path.attr('path'). path is an array of path part arrays, e.g.

[
  ['M', 100, 100],
  ['L', 150, 150],
  ['L', 200, 150],
  ['Z']
]

If you update it then you don't need to draw the path from scratch each time.

Raphael.el.addPart = function (point) {
  var pathParts = this.attr('path') || [];
  pathParts.push(point);
  this.attr('path', pathParts);
};

var path = paper.path();
path.addPart(['M', 100, 100]); //moveto 100, 100
path.addPart(['L', 150, 150]); //lineto 150, 150
path.addPart(['L', 200, 150]); //lineto 200, 150
path.addPart(['Z']);           //closepath
负佳期 2024-10-12 16:23:59

据我所知,你做得对。我要说的唯一一件事是,您可以从一条路径到另一条路径设置动画,而不是替换旧路径,并且您可以强制执行最大帧速率(例如每秒不超过 5 条路径更新,但您需要尝试看看什么有效)为你)。

至于路径的文档,我认为没有什么可说的了。该方法接受 SVG 路径字符串并绘制它。您需要阅读的可能是路径的 SVG 文档。

如何为路径设置动画:

p = canvas.path("M0 0L100 0");
p.animate({path: [["M", 0, 0], ["L", 0, 100]]}, 4000);

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:

p = canvas.path("M0 0L100 0");
p.animate({path: [["M", 0, 0], ["L", 0, 100]]}, 4000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文