【问题标题】:Adding feDropShadow to a vertical line in SVG makes it disappear将 feDropShadow 添加到 SVG 中的垂直线使其消失
【发布时间】:2018-05-25 07:24:49
【问题描述】:

我有以下 SVG 文档:

  <svg preserveAspectRatio="xMinYMin meet" viewBox="0 0 21 484" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <filter id="dropShadow">
      <feDropShadow dx="4" dy="0" stdDeviation="4"></feDropShadow>
    </filter>
  </defs>
  <g id="Artboard" stroke-width="5" stroke="#FF0000" fill="#000000" stroke-linecap="round">
    <path style="filter: url(#dropShadow)" d="M7.5,8.5 L7.5,471.5" id="path-1"></path>
  </g>
</svg>

在 Firefox 中,当我打开 SVG 文档时,它只会显示一条非常细(不是 5 宽)的垂直线。在 Chrome 中,它不显示任何内容(在 codepen 中也不显示,这里:https://codepen.io/jwir3/pen/BJBqEK)。

我不太确定我在这里做错了什么,但它与过滤器有关,因为如果我从path 定义中删除filter: url(#dropShadow),则该行会按预期显示。

【问题讨论】:

标签: svg svg-filters dropshadow


【解决方案1】:

如果您的形状没有高度或宽度,则不能使用objectBoundingBox units。

当适用元素的几何形状没有宽度或没有高度时,不应使用关键字 objectBoundingBox,例如水平或垂直线的情况,即使该线在查看时由于具有非零值而具有实际粗细笔划宽度,因为边界框计算忽略笔划宽度。当适用元素的几何图形没有宽度或高度并且指定了 objectBoundingBox 时,将忽略给定的效果(例如,渐变或过滤器)。

filterUnits 的默认值是 objectBoundingBox 单位,因此您需要将其更改为 userSpaceOnUse 即

<svg preserveAspectRatio="xMinYMin meet" viewBox="0 0 21 484" xmlns="http://www.w3.org/2000/svg">
  <title>Line Drop Shadow</title>
  <description>A red line with 5px width thickness and round caps, having a drop-shadow. This highlights the regression documented in PURP-1017.</description>
  <defs>
    <filter id="dropShadow" filterUnits="userSpaceOnUse">
      <feDropShadow dx="4" dy="0" stdDeviation="4"></feDropShadow>
    </filter>
  </defs>
  <g id="Artboard" stroke-width="5" stroke="#FF0000" fill="#000000" stroke-linecap="round">
    <path style="filter: url(#dropShadow)" d="M7.5,8.5 L7.5,471.5" id="path-1"></path>
  </g>
</svg>

【讨论】:

    【解决方案2】:

    在处理过滤器时,不同的浏览器处理不同的stroke。
    Chrome将笔画视为一个像素为零的值,因此不包含在过滤区域中。
    所以,为了让不同浏览器的结果看起来一样,最好把path换成stroke-width ="5",一个没有stroke的宽5px的矩形(stroke="none")

    此外,过滤区域的默认值是:x =" - 10% "" y = "- 10%" `` width = "120%" `` height = "120%"- 大的模糊尺寸通常会被截断。

    默认情况下,filterUnits = "objectBoundingBox",因此这些值以百分比指定。

    为了更容易计算filter region 操作的大小,请以像素为单位指定filterUnits = "userSpaceOnUse" and then you can specify all dimensions for thefilter region` 的值。

    <svg preserveAspectRatio="xMinYMin meet" width="100%" height="100%" viewBox="0 0 21 484" xmlns="http://www.w3.org/2000/svg" >
      <defs>
        <filter id="dropShadow" filterUnits = "userSpaceOnUse" x="4" y="0"  width="12" height="472">
          <feDropShadow dx="6" dy="4" stdDeviation="3"></feDropShadow>
        </filter>
      </defs>
      <g id="Artboard"  fill="#FF0000" filter="url(#dropShadow)" >
       <!-- <path style="filter: url(#dropShadow)" d="M7.5,8.5 L7.5,471.5" id="path-1" stroke-width="5" ></path>--> 
        <rect x="5" y="5" width="5" stroke="none" height="463"  /> 
      </g>
    </svg>

    【讨论】:

      【解决方案3】:

      在大多数情况下,切换到userSpaceOnUse 是正确的答案,但有以下限制:

      1. 滤镜效果区域将应用​​ canvas 的 -10% 到 120%,而不是 元素 的边界框(使用更多内存和处理时间)
      2. 对于大型动态 SVG(例如由 d3 创建的),可能很难计算所需的过滤器 x/y/width/height 以确保过滤器适用于所有元素。

      另一种(不太优雅的)解决方案是将过滤器应用于&lt;g&gt; 并使用其中的隐藏节点为组提供正确的宽度或高度:

        <svg xmlns="http://www.w3.org/2000/svg">
        <defs>
          <filter id="dropShadow" width="20">
            <feDropShadow dx="4" dy="0" stdDeviation="4"></feDropShadow>
          </filter>
        </defs>
        <g id="Artboard" style="filter: url(#dropShadow)">
          <circle r="5" cx="0" cy="0" visibility="hidden"></circle>
          <path d="M10,10 L10,100" stroke-width="5" stroke="#FF0000" fill="#000000" stroke-linecap="round"></path>
        </g>
      </svg>

      【讨论】:

        猜你喜欢
        • 2020-05-16
        • 2021-12-28
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        • 2021-11-16
        • 2019-05-05
        • 1970-01-01
        • 2021-03-09
        相关资源
        最近更新 更多