【问题标题】:oddly shaped SVG drop shadow形状奇特的 SVG 投影
【发布时间】:2012-11-24 04:21:52
【问题描述】:

来源:

<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
 <g>
  <defs>
    <filter id="f1" x="0" y="0" width="200%" height="200%">
      <feOffset result="offOut" in="SourceGraphic" dx="0" dy="0" />
      <feColorMatrix result="matrixOut" in="offOut" type="matrix"
      values="0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0" />
      <feGaussianBlur result="blurOut" in="matrixOut" stdDeviation="10" />
      <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
    </filter>
  </defs>
  <path fill="#E0E0E0" stroke="#FFFFFF" stroke-width="5" d="M 50 50 l 100 0 l -50 86.6 z" filter="url(#f1)"/>
 </g>
</svg>

下面是 Firefox 渲染的样子: Chrome 类似。

为什么投影是不对称的?我希望让它看起来好像照明来自图像的正上方,所以阴影应该是对称的。它最终应该看起来像下面的光栅图像:

(很抱歉内饰的颜色不匹配,但我稍后会修复。)

【问题讨论】:

    标签: svg svg-filters


    【解决方案1】:

    这更像你想要的吗?

    <svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
     <g>
      <defs>
        <filter id="f1" x="-1" y="-1" width="300%" height="300%">
          <feOffset result="offOut" in="SourceGraphic" dx="0" dy="10" />
          <feColorMatrix result="matrixOut" in="offOut" type="matrix"
          values="0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0" />
          <feGaussianBlur result="blurOut" in="matrixOut" stdDeviation="10" />
          <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
        </filter>
      </defs>
      <path fill="#E0E0E0" stroke="#FFFFFF" stroke-width="5" d="M 50 50 l 100 0 l -50 86.6 z" filter="url(#f1)"/>
     </g>
    </svg>
    

    编辑:解释:

    我使滤镜效果区域更大(在&lt;filter&gt; 上使用width/height),使其大于形状的边界框(其宽度的300%)。当然,它必须重新定位(使用x/y),以使形状位于效果区域的中心。我还使用feOffset 上的y 属性将阴影向下移动了一点。

    旁注:正如 Erik Dahlström 在 cmets 中提到的,使用 3 而不是 300% 来表示 widthheight 在这种情况下是等效的,因为 filterUnits 被隐式设置为 objectBoundingBox。对于xy 上的-1-100% 也是如此。我混合了两种符号。

    【讨论】:

    • 你也可以这样写:。这些值与使用过滤器的元素的边界框相关,因为隐式默认值为 filterUnits="objectBoundingBox"。
    • 对。我只是保留了 OP 使用的符号。
    • 是的,看起来好多了。你能解释一下吗?
    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多