【问题标题】:SVG path with border带边框的 SVG 路径
【发布时间】:2019-07-17 10:19:13
【问题描述】:

如何创建类似于 的填充和轮廓路径

到目前为止,我已经找到了几种方法,但没有一个是特别干净的。

一种方法是使用paint-order,但这不适用于移动设备和 IE。

另一种方式复制路径...但这会产生不必要的数据量。

是否有其他方法可以使用 CSS 为 SVG 路径简单地创建轮廓或边框?

<svg height="50" width="300">
    <path d="M5 20 1215 20" />
</svg>

path {
  fill: red;
  stroke: #646464;
  stroke-width:10px;
  stroke-linejoin: round;
}

这是codepen

【问题讨论】:

  • 我不知道这是否是您的意思 复制路径 但为什么不将线从头到尾跟踪为单个路径然后使用 @987654326 @?
  • 这就是我试图做的,但路径总是被填满......我会创造一个小提琴

标签: css svg


【解决方案1】:

您必须将路径绘制为轮廓:

<svg xmlns="http://www.w3.org/2000/svg" width="220" height="220" viewBox="0 0 220 220">
    <path fill="#ddd" stroke="#3f4141" d="M0 0h220v220H0z"/>
    <path fill="#fff" stroke="#00b400" stroke-width="4" 
     d="M 159.8 30.3
        h -110
        v 120
        h-20
        l 30 40 
          30 -40
        h-20
        v-100
        h90"/>
</svg>

在 Inkscape 中绘制草图,在 SVGOMG 中优化,然后手动调整。

编辑

我有一个使用如下标记的工作解决方案:

  • 将线(任何线)创建为符号
  • 通过将具有不同线宽的两个线条实例层叠在一起来创建人造笔触
  • 添加带有预定义笔划的箭头作为标记
  • 有时在行首显示细线描边...使用另一个使用背景颜色进行遮罩的标记解决此问题。
    • 这种技术只适用于纯色背景。

<svg style="display: inline-block; margin-left: 2em;" width="220" height="220" viewBox="0 0 220 220" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <style>
      .arrow-stroke {
        stroke: #00b400;
        stroke-width: 28;
        /* marker-end etc should be supported but unsure of browser support */
      }
      .arrow-fill {
        stroke: white;
        stroke-width: 20
      }
    </style>
    <marker id="arrow" markerWidth="45" markerHeight="70" refX="5" refY="35" orient="auto" markerUnits="userSpaceOnUse">
      <path fill="#fff" stroke="#00b400" stroke-width="4" d="M 2 25  v-20  l 40,30 -40,30 v-20"/>
    </marker>

    <!-- Used to hide hairline that shows through, fill color must match background color -->
    <marker id="startMask" markerWidth="2" markerHeight="30" refX="1" refY="15" orient="auto" markerUnits="userSpaceOnUse">
      <path fill="#ddd" d="M0 0 v30 h2 v-30 z" />
    </marker>
    
    <symbol id="line">
      <path d="M 159.8 30.3  h -110 v 120"/>
    </symbol>

    <symbol id="line2">
      <path d="M 140 60 l 20 30"/>
    </symbol>
    <symbol id="line3">
      <path d="M 100 80 q 0 40 20 70"/>
    </symbol>
  </defs>
  
  <path id="grey-box" fill="#ddd" stroke="#3f4141" d="M0 0h220v220H0z"/>
  
  <g fill="none">
    <use xlink:href="#line" class="arrow-stroke" />
    <use xlink:href="#line" class="arrow-fill" marker-end="url(#arrow)" marker-start="url(#startMask)" />
  
    <use xlink:href="#line2" class="arrow-stroke" />
    <use xlink:href="#line2" class="arrow-fill" marker-end="url(#arrow)" marker-start="url(#startMask)" />
  
    <use xlink:href="#line3" class="arrow-stroke" />
    <use xlink:href="#line3" class="arrow-fill" marker-end="url(#arrow)" marker-start="url(#startMask)" />
  </g>
</svg>

希望对你有帮助

【讨论】:

  • 为避免路径完全关闭,请不要在末尾添加z。避免填充集fill="transparent".
  • 嗨 Ruskin 感谢您的回复,但这不是我想要的。我想设置路径的样式而不是绘制箭头形状的路径。我现在通过使用路径模式实现了这一点。示例:resources.jointjs.com/tutorial/links-patterns
  • 啊......在这种情况下看看developer.mozilla.org/en/docs/Web/SVG/Element/marker......我会去更新答案
  • 为了避免填充集 fill="none" 而不是 fill="transparent" fill="none" 要快得多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 2019-02-04
  • 2013-04-28
  • 1970-01-01
  • 2019-04-22
相关资源
最近更新 更多