理解SVG图像的路径

huangapple go评论53阅读模式
英文:

Understanding the Path of SVG Images

问题

我目前正在尝试理解SVG图像的路径函数。我从维基百科上获取了一个SVG图像(世界地图,但除了一个小部分外,所有内容都已删除)。以下是SVG代码的部分内容:

<path
   d="m 481.4,220 -1,3.2 7.3,-5.2 -2.1,-2.8 V 212 l -1.2,0.5 v 4.5 z"
   id="path6"
   inkscape:connector-curvature="0" />

根据我的理解,SVG路径命令由命令字符和参数组成。在这里,m 表示相对于当前光标位置的移动("move relative to current cursor pos")。初始光标位置是(0,0),所以它移动了0+481.4,0+220,但之后只有坐标没有命令。这些如何解释呢?这些是否隐含了l命令?为了更好地理解,我将讨论移动后的 -1,3.2 这部分。它表示什么意思?

请注意,SVG路径命令的默认行为是"线性"(即l命令),因此在这种情况下,-1,3.2 被解释为相对于当前光标位置的直线段。具体来说:

  • -1 表示在水平方向向左移动1个单位。
  • 3.2 表示在垂直方向向下移动3.2个单位。

这两个值合并起来,描述了从当前光标位置开始的相对路径段。这个路径段将从(481.4, 220)的位置向左移动1个单位,然后向下移动3.2个单位。

SVG路径命令的其他常见命令包括"l"(线性),"c"(贝塞尔曲线),"a"(椭圆弧)等,每个命令都有不同的参数和行为。在您的示例中,使用的是默认的"线性"命令。如果需要其他类型的路径,需要使用相应的命令字符和参数来描述它们。

英文:

I am currently trying to understand the path function of SVG Images. I took a SVG from Wikipedia (map of the world, but everything is removed except for once little thing). This is the SVG Code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
&lt;svg
   xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot;
   xmlns:cc=&quot;http://creativecommons.org/ns#&quot;
   xmlns:rdf=&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;
   xmlns:svg=&quot;http://www.w3.org/2000/svg&quot;
   xmlns=&quot;http://www.w3.org/2000/svg&quot;
   xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;
   xmlns:sodipodi=&quot;http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd&quot;
   xmlns:inkscape=&quot;http://www.inkscape.org/namespaces/inkscape&quot;
   height=&quot;480&quot;
   width=&quot;960&quot;
   version=&quot;1.1&quot;
   id=&quot;svg11&quot;
   sodipodi:docname=&quot;map.svg&quot;
   inkscape:version=&quot;0.92.4 (5da689c313, 2019-01-14)&quot;&gt;
  &lt;metadata
     id=&quot;metadata17&quot;&gt;
    &lt;rdf:RDF&gt;
      &lt;cc:Work
         rdf:about=&quot;&quot;&gt;
        &lt;dc:format&gt;image/svg+xml&lt;/dc:format&gt;
        &lt;dc:type
           rdf:resource=&quot;http://purl.org/dc/dcmitype/StillImage&quot; /&gt;
        &lt;dc:title&gt;&lt;/dc:title&gt;
      &lt;/cc:Work&gt;
    &lt;/rdf:RDF&gt;
  &lt;/metadata&gt;
  &lt;defs
     id=&quot;defs15&quot; /&gt;
  &lt;sodipodi:namedview
     pagecolor=&quot;#ffffff&quot;
     bordercolor=&quot;#666666&quot;
     borderopacity=&quot;1&quot;
     objecttolerance=&quot;10&quot;
     gridtolerance=&quot;10&quot;
     guidetolerance=&quot;10&quot;
     inkscape:pageopacity=&quot;0&quot;
     inkscape:pageshadow=&quot;2&quot;
     inkscape:window-width=&quot;1920&quot;
     inkscape:window-height=&quot;1002&quot;
     id=&quot;namedview13&quot;
     showgrid=&quot;false&quot;
     fit-margin-top=&quot;0&quot;
     fit-margin-left=&quot;0&quot;
     fit-margin-right=&quot;0&quot;
     fit-margin-bottom=&quot;0&quot;
     inkscape:zoom=&quot;256&quot;
     inkscape:cx=&quot;959.32356&quot;
     inkscape:cy=&quot;478.85011&quot;
     inkscape:window-x=&quot;0&quot;
     inkscape:window-y=&quot;27&quot;
     inkscape:window-maximized=&quot;1&quot;
     inkscape:current-layer=&quot;svg11&quot; /&gt;
  &lt;g
     id=&quot;g9&quot;
     style=&quot;stroke:#000000;stroke-width:0.25;stroke-linejoin:bevel&quot;
     transform=&quot;matrix(2.0368307,0,0,2.0374975,-34.589547,-22.659498)&quot;&gt;
    &lt;use
       xlink:href=&quot;#a&quot;
       id=&quot;use2&quot;
       style=&quot;stroke-width:1.5&quot;
       x=&quot;0&quot;
       y=&quot;0&quot;
       width=&quot;100%&quot;
       height=&quot;100%&quot; /&gt;
    &lt;g
       id=&quot;a&quot;
       style=&quot;fill:#ffffff&quot;&gt;

      &lt;path
         d=&quot;m 481.4,220 -1,3.2 7.3,-5.2 -2.1,-2.8 V 212 l -1.2,0.5 v 4.5 z&quot;
         id=&quot;path6&quot;
         inkscape:connector-curvature=&quot;0&quot; /&gt;
    &lt;/g&gt;
  &lt;/g&gt;
&lt;/svg&gt;

The only part I am currently interested in is:

  &lt;path
     d=&quot;m 481.4,220 -1,3.2 7.3,-5.2 -2.1,-2.8 V 212 l -1.2,0.5 v 4.5 z&quot;
     id=&quot;path6&quot;
     inkscape:connector-curvature=&quot;0&quot; /&gt;

From what I understood, you need a command and parameters for the path. So the m would be "move relative to current cursor pos). The cursor is 0, 0, so it moved 0+481.4, 0+220, but after that there are just coordinates without a command. How are these to be interpreteted? Are these implicit l commands? For better unstanding, I am talking e.g. about -1,3.2 after the move. What does these do?

答案1

得分: 1

"Mehdi提供的答案并不完全正确。

如果一个命令重复出现(即与上一个命令相同),则可以省略它。所以,例如

L 1 2 L 3 4 L 5 6

可以缩写为

L 1 2 3 4 5 6

例外情况是对于Mm。在这些情况下,分别替换为Ll

所以在Mehdi的示例中

d=&quot;m 481.4,220 -1,3.2&quot;

实际上等同于

d=&quot;m 481.4,220 l -1,3.2&quot;;

关于这类问题的权威来源是SVG规范的路径部分。"

英文:

The answer by @Mehdi is not quite correct.

If a command is repeated (ie. the same as the previous one) then it can be omitted. So for example

L 1 2 L 3 4 L 5 6

can be abbreviated to

L 1 2 3 4 5 6

The exception is for M and m. In those cases L and l are substituted respectively.

So in @Mehdi's example

d=&quot;m 481.4,220 -1,3.2&quot;

is actually equivalent to

d=&quot;m 481.4,220 l -1,3.2&quot;

The definitive source for answers to questions like these is the Paths section of the SVG specification

huangapple
  • 本文由 发表于 2020年1月3日 17:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定