将SVG中的绝对命令’C’转换为相对命令’c’时出现意外结果

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

Unexpected result converting absolute SVG 'C' command to relative 'c'

问题

我尝试将绝对C命令转换为相对c坐标,用于SVG路径d属性字符串中的三次贝塞尔曲线,以获得相同的结果。

在这个测试中,两个结果应该看起来相同。但它们并不相同。
一定有些我不明白的地方。

有人能解释我漏掉了什么吗?

我了解理论和Cc之间的区别。

在我看来,数字是正确的。结果却不是。

英文:

I tried to get the same result converting absolute C commands to relative c coordinates for cubic Bézier curves in a SVG-path d attribute string.

In this test, both results should look the same. They are not.
There must be something I don't understand.

Somebody can explain what I am missing?

I know the theory and the difference between both C and c.

In my eyes the numbers are okay. The result isn't.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;svg width=&quot;200&quot; height=&quot;200&quot; viewBox=&quot;50 100 200 200&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;

&lt;g id=&quot;boom&quot; stroke=&quot;green&quot; stroke-width=&quot;0.75&quot; fill=&quot;lightblue&quot;&gt; 
   &lt;path d=&quot;M 100,200 C 105,190 95,170 100,160 &quot;/&gt;
   &lt;path d=&quot;M 150,200 c 5,-10 -10,-20 5,-10 &quot;/&gt;
&lt;/g&gt;
&lt;g id=&quot;circles&quot; stroke=&quot;none&quot; fill=&quot;red&quot;&gt; 
   &lt;circle cx=&quot;105&quot; cy=&quot;190&quot; r=&quot;1&quot;/&gt;
   &lt;circle cx= &quot;95&quot; cy=&quot;170&quot; r=&quot;1&quot;/&gt;
   &lt;circle cx=&quot;100&quot; cy=&quot;160&quot; r=&quot;1&quot;/&gt;
   &lt;circle cx=&quot;155&quot; cy=&quot;190&quot; r=&quot;1&quot;/&gt;
   &lt;circle cx=&quot;145&quot; cy=&quot;170&quot; r=&quot;1&quot;/&gt;
   &lt;circle cx=&quot;150&quot; cy=&quot;160&quot; r=&quot;1&quot;/&gt;
&lt;/g&gt;
&lt;/svg&gt;

<!-- end snippet -->

答案1

得分: 2

你的 x/y 计算有错误:

要从绝对值中减去的偏移量是相对于最后一个在路径上的位置/点的,所以控制点和最终点都相对于同一个已知路径点:

M 100 200 
C 105 190  95 170  100 160  // (M: x:-100, y:-200) 
C 105 140  95 130  100 110  // (preceding C: x:-100, y:-160) 

变成了:

M 100 200 
c 5 -10  -5 -30  0 -40 
c 5 -20  -5 -30  0 -50

你已经减去了C命令的两个控制点的 x/y 值 - 这样是行不通的。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->

svg{
height:20em;
overflow:visible;
}

<!-- 语言: lang-html -->

<svg viewBox="98.56 110 2.89 90">
<path id="pathPrev" 
d="M 100 200 
C 105 190 95 170 100 160 
C 105 140 95 130 100 110" 
fill="none" stroke="#000000" stroke-width="1" ></path>
</svg>

<svg viewBox="98.56 110 2.89 90">
<path id="pathPrev" 
d="M 100 200 
c 5 -10  -5 -30  0 -40 
c 5 -20  -5 -30  0 -50" 
fill="none" stroke="#000000" stroke-width="1" ></path>
</svg>

<!-- 结束代码片段 -->

你可以使用在线工具来轻松验证你的计算,比如

SVG路径编辑器
SVG路径命令工具(也可用作库)

英文:

Your x/y calculation has an error:

The offsets to subtract from absolute values are relative to the last on-path position/point, so both control points and the final point are all relative to the same last known path point:

M 100 200 
C 105 190  95 170  100 160  // (M: x:-100, y:-200) 
C 105 140  95 130  100 110  // (preceding C: x:-100, y:-160) 

becomes:

M 100 200 
c 5 -10  -5 -30  0 -40 
c 5 -20  -5 -30  0 -50

You have subtracted the two control point x/y values of the C command – won't work.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

svg{
height:20em;
overflow:visible;
}

<!-- language: lang-html -->

&lt;svg viewBox=&quot;98.56 110 2.89 90&quot;&gt;
&lt;path id=&quot;pathPrev&quot; 
d=&quot;M 100 200 
C 105 190 95 170 100 160 
C 105 140 95 130 100 110&quot; 
fill=&quot;none&quot; stroke=&quot;#000000&quot; stroke-width=&quot;1&quot; &gt;&lt;/path&gt;
&lt;/svg&gt;

&lt;svg viewBox=&quot;98.56 110 2.89 90&quot;&gt;
&lt;path id=&quot;pathPrev&quot; 
d=&quot;M 100 200 
c 5 -10  -5 -30  0 -40 
c 5 -20  -5 -30  0 -50&quot; 
fill=&quot;none&quot; stroke=&quot;#000000&quot; stroke-width=&quot;1&quot; &gt;&lt;/path&gt;
&lt;/svg&gt;

<!-- end snippet -->

You can easily cross-check your calculations with online tools like

svg path editor
svg path commander (also usable as a library)

huangapple
  • 本文由 发表于 2023年2月27日 00:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573435.html
匿名

发表评论

匿名网友

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

确定