如何在JavaScript中绘制具有指定厚度的曲线外部?

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

How do you plot the outside of a curved line with a given thickness in JavaScript?

问题

我正在尝试在另一条给定厚度的线内创建一条线。例如,

<svg viewBox="0 0 100 100">
    <path 
        stroke-width="4" stroke="black"
        d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z" fill="none" />
</svg>

这是一个线宽为4像素的心形。我该如何计算内部具有相同线宽的新线的点?

英文:

I'm trying to create a line with a given thickness inside another line of a given thickness. For example,

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

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

&lt;svg viewBox=&quot;0 0 100 100&quot;&gt;

    &lt;path 
      stroke-width=&quot;4&quot; stroke=&quot;black&quot;
      d=&quot;M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z&quot; fill=&quot;none&quot; /&gt;
  &lt;/svg&gt;

<!-- end snippet -->

Is the shape of a heart with a line thickness of 4px
How do I calculate the points of a new line inside this heart line with the same thickness?

答案1

得分: 1

在另一个路径上偏移计算是矢量图形编辑器(如Illustrator或Inkscape)的领域。

如果您只想要渲染出看起来像是偏移路径的东西,可以使用一些掩蔽技巧来帮助:

英文:

Computing a path at an offset from another is the domain of vector grafic editors like Illustrator or Inkscape.

If all you want to do is rendering something that looks like a path at an offset, a bit of masking trickery will help:

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

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

&lt;svg viewBox=&quot;0 0 100 100&quot;&gt;
  &lt;defs&gt;
    &lt;path id=&quot;heart&quot; stroke-linecap=&quot;square&quot;
      d=&quot;M 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 A 20,20 0,0,1 50,30&quot; /&gt;
    &lt;mask id=&quot;mask&quot;&gt;
      &lt;use href=&quot;#heart&quot; stroke=&quot;black&quot; stroke-width=&quot;4&quot; fill=&quot;white&quot; /&gt;
    &lt;/mask&gt;
  &lt;/defs&gt;
  &lt;use href=&quot;#heart&quot; stroke=&quot;grey&quot; stroke-width=&quot;4&quot; fill=&quot;none&quot; /&gt;
  &lt;use href=&quot;#heart&quot; mask=&quot;url(#mask)&quot; stroke=&quot;blue&quot; stroke-width=&quot;12&quot; fill=&quot;none&quot; /&gt;
&lt;/svg&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定