有没有办法使用 Javascript 合并两个路径元素(svg)?
我使用 SVG 绘制了两条路径线,并将这些元素保存到我的 javascript 代码中的两个变量中:“Line1”和“Line2”,并且我需要将这两条线合并为一个路径元素。有办法这样做吗?
I have drawn two path lines using SVG and I've saved these elements into two variables in my javascript code: 'Line1', and 'Line2', and I need to merge the two lines into one single path element. Is there a way to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的路径是相对定义的(小写字母)还是绝对定义的(大写字母)?如果是绝对路径,则连接两个路径很简单,只需附加 d 属性的值即可。如果你有两个这样的路径:
那么这个 JavaScript 代码:
将导致你有一个像这样的路径:
Here's a jsFiddle< /a>,它适用于 Firefox 4(需要 HTML5 解析器,以便您可以拥有内联 SVG)。
如果您的路径是相对的,那么您将必须在附加路径之间添加一些内容,以便第二个路径从正确的位置开始。
Are your paths defined relatively (small letters) or absolutely (capitals)? If absolute, joining two paths is trivial, just append the values of the
d
attribute. If you have two paths like this:Then this JavaScript code:
Will lead to you having a single path like this:
Here's a jsFiddle, it works in Firefox 4 (needs an HTML5 parser so you can have inline SVG).
If your paths are relative then you're going to have to add something between the appended paths so that the second one starts in the correct place.
连接
d
属性通常,您可以简单地连接多个
元素的 pathdatad
属性来获得复合路径 - 无论是否这些命令使用相对、绝对、速记(S
、T
、V
、H
)或高度精简的命令。不幸的是,您可能会遇到一些使用
M
或m
作为可互换的启动命令的»不好的做法<。事实上,每个第一个
m
或M
命令(在路径数据d
属性中)描述绝对< /strong> 坐标 – 因为没有前面的点。因此,大多数情况下,您可以将前m
命令更改为大写并连接。例外:小写的
m
命令引入了一行隐式相对的l
lineto命令。 (例如多边形/折线)。摘要:
d
属性以大写M
命令开头 - 您可以安全地连接它们。除非路径有不同的转换。在这种情况下,您可能需要事先展平转换。m
,但只有两个值(x 和 y) - 将第一个m
命令更改为大写M< /code> 和连接
m
但有两个以上的后续值(x 和 y) - 它隐式地引入了相对 linetos:更改第一个M
命令转为大写M
,在第三个值之前添加相对 linto 命令l
并连接示例 1:以(不必要的)相对
m
命令开头的路径修复:只需将每个起始
m
替换为绝对M
示例 2:相邻 linetos 的
m
命令例外情况是
m
命令后跟坐标 - 用作后续l
(相对行号)的简写。 (另请参阅w3c 规范。)修复:插入
l
命令等于
或
示例3:通过
getPathData()
修复伪相对m
命令目前仍然是草案,主要浏览器本身不支持。
不过,您可以使用Jarek Foksa 的polyfill。。
getPathData()
将返回命令对象数组并标准化重复这样的命令:
由于隐式 linetos 被转换为显式
l
命令,我们可以将第一个 moveto 更改为M
建议:仅使用相对的
m
命令(如果它们实际上是)相对:l
命令的简写(例如m 20 0 10 0 0 10 -10 0z
)实际合并形状:删除重叠的形状
如果您需要合并形状 - paper.js 有一些强大的路径操作,如联合、减去等
此处解释:“合并两个基于贝塞尔曲线的形状合二为一以创建新的轮廓”
Concatenate
d
attributesUsually, you can simply concatenate the pathdata
d
attribute of several<path>
elements to get a compound path – no matter if these are using relative, absolute, shorthand (S
,T
,V
,H
) or highly minified commands.Unfortunately, you may encounter some »bad practices« using
M
orm
as interchangeable starting commands.In fact, every first
m
orM
command (in a pathdatad
attribute) describes absolute coordinates – since there are no preceding points. So most often you can change the firstm
commands to uppercase and concatenate.Exception: the lowercase
m
command introduces a row of implicit relativel
lineto commands. (For instance for polygons/polylines).Summary:
d
attributes start with a uppercaseM
command – you can safely concatenate them. Unless paths have different transformations. In this case you may need to flatten transformation beforehand.m
but has only two values (x and y) – change the firstm
command to uppercaseM
and concatenatem
but has more than two subsequent values (x and y) – it's implicitely introducing relative linetos: change the firstM
command to uppercaseM
, prepend a relative linto commandl
before the third value and concatenateExample 1: paths starting with (unnecessary) relative
m
commandFix: just replace each starting
m
with an absoluteM
Example 2:
m
command for adjacent linetosThe exception are
m
commands followed by coordinates – used as a shorthand for succeedingl
(relative linetos). (See also w3c specs.)Fix: insert
l
commandsequals
or
Example 3: fix pseudo relative
m
commands viagetPathData()
Currently still a draft and not natively supported by major browser.
However you can use Jarek Foksa's polyfill..
getPathData()
will return an array of command objects and normalizerepeated commands like this:
Since the implicit linetos are converted to explicit
l
commands we can change the first moveto toM
Recommendation: only use relative
m
commands if they are actually relative:l
commands (likem 20 0 10 0 0 10 -10 0z
)Actually merging shapes: removing overlapping shapes
If you need to merge shapes - paper.js has some powerful path operations like unite, subtract etc.
Explained here: "Merging two bezier-based shapes into one to create a new outline"