如何在SVG中使用描边作为裁剪路径?

5

我有一个如下的路径:

<path class="path" d="M0,550L0,366.6666666666667C0,366.6666666666667,95.43389463154384,198.61111111111114,143.31860620206734,183.33333333333337C191.20331777259085,168.0555555555556..."></path>

这在我的页面中显示如下:

Path

我希望它成为一组矩形显示的裁剪路径。目前,它看起来像这样:

Path as clipPath

然而,我仍然希望它是一个2px的线条,在进入新的<rect>领域时更改颜色。我目前正在思考通过将路径的描边变为<clipPath>来解决这个问题,但我也可以接受其他方法使其工作。
1个回答

10

<clipPath> 不适用于这种情况,正确的解决方案是使用 <mask>

<svg width="500" height="240">

  <defs>
    <mask id="graph">
      <path d="M 0,150 L 100,20 L 200,210 L 300,100 L 400,130 L 500,50"
            fill="none" stroke="white" stroke-width="4"/>
    </mask>
  </defs>

  <g mask="url(#graph)">
    <rect y="0" width="500" height="60" fill="red"/>
    <rect y="60" width="500" height="60" fill="blue"/>
    <rect y="120" width="500" height="60" fill="green"/>
    <rect y="180" width="500" height="60" fill="yellow"/>
  </g>
</svg>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接