【问题标题】:Is it not possible to filter SVG <line> elements?不能过滤 SVG <line> 元素吗?
【发布时间】:2017-01-19 03:32:09
【问题描述】:

我在此页面上的 SVG 中有一些元素:digitalbrent.com/cv。我所说的线条是像素人图像旁边的线条。我想为这些线条添加一种效果,让它们看起来像线条旁边的绿色字母一样有一种发光或雾霾。我认为最好的方法可能是在线条上添加一个微妙的高斯模糊 SVG 过滤器。我尝试使用以下方法申请the filter from this guy's similar question

<line x1="225" y1="20" x2="225" y2="30" stroke-width="2" stroke="#00fc49" filter="url(#glowing);"></line>

<line x1="225" y1="20" x2="225" y2="30" stroke-width="2" stroke="url(#glowing);"></line>

^ 这些方法都不能用于实现过滤器。这是线条和 SVG 元素的完整代码(抱歉缩进。它没有从我的 ide 中正确复制。):

<svg id="line-underlay" width="450" height="300" viewBox="0 0 450 300">
<defs xmlns="http://www.w3.org/2000/svg">
<filter id="glowing" height="100%" filterUnits="userSpaceOnUse">
<feGaussianBlur in="SourceAlpha" stdDeviation="3" result="coloredBlur"/>
<feOffset dx="2" dy="2" result="offsetblur" />
<feMerge>
<feMergeNode in="coloredBlur"/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
</defs>

<line x1="225" y1="20" x2="225" y2="30" stroke-width="2" stroke="#00fc49" filter="url(#glowing);"></line>
<line x1="150" y1="30" x2="300" y2="30" stroke-width="2" stroke="#00fc49"></line>
<line x1="150" y1="30" x2="150" y2="40" stroke-width="2" stroke="#00fc49"></line>
<line x1="300" y1="30" x2="300" y2="40" stroke-width="2" stroke="#00fc49"></line>
<line x1="320" y1="75" x2="350" y2="75" stroke-width="2" stroke="#00fc49"></line>
<line x1="320" y1="75" x2="320" y2="112" stroke-width="2" stroke="#00fc49"></line>
<line x1="284" y1="112" x2="320" y2="112" stroke-width="2" stroke="#00fc49"></line>
<line x1="97" y1="106" x2="160" y2="106" stroke-width="2" stroke="#00fc49"></line>
<line x1="295" y1="136" x2="330" y2="136" stroke-width="2" stroke="#00fc49"></line>
<line x1="300" y1="185" x2="320" y2="185" stroke-width="2" stroke="#00fc49"></line>
<line x1="320" y1="185" x2="320" y2="202" stroke-width="2" stroke="#00fc49"></line>
<line x1="320" y1="202" x2="330" y2="202" stroke-width="2" stroke="#00fc49"></line>
<line x1="65" y1="206" x2="65" y2="220" stroke-width="2" stroke="#00fc49"></line>
<line x1="65" y1="220" x2="140" y2="220" stroke-width="2" stroke="#00fc49"></line>
<line x1="300" y1="242" x2="320" y2="242" stroke-width="2" stroke="#00fc49"></line>
<line x1="320" y1="242" x2="320" y2="260" stroke-width="2" stroke="#00fc49"></line>

</svg>

我搜索了几个不同的来源,包括 this question,这很相似,但在尝试了那里的解决方案后,它似乎仍然不起作用。我还尝试使用 css 将 box-shadow 应用于线条元素,但这也不起作用。我在搜索中完全没有发现任何东西表明您可以对&lt;line&gt; 应用过滤器,现在我想起来了,我从未见过有人在他们的代码中真正做到这一点。我开始怀疑这是否可能?如果是,我做错了什么?这样做的正确方法是什么?

如果您有任何替代方法来实现我正在寻找的模糊效果,请告诉我。我将不胜感激。

【问题讨论】:

    标签: svg svg-filters


    【解决方案1】:

    你有两个问题

    • ; url末尾的filter属性无效
    • 过滤器与形状位于不同的位置,因此您看不到它。我已经调整了过滤器的大小并移动了下面的过滤器

    <svg width="450" height="300" viewBox="200 15 50 20">
    <defs>
    <filter id="glowing" x="210" y="10" height="30" width="25" filterUnits="userSpaceOnUse">
    <feGaussianBlur in="SourceAlpha" stdDeviation="3" result="coloredBlur"/>
    <feOffset dx="2" dy="2" result="offsetblur" />
    <feMerge>
    <feMergeNode in="coloredBlur"/>
    <feMergeNode in="SourceGraphic"/>
    </feMerge>
    </filter>
    </defs>
    
    <line x1="225" y1="20" x2="225" y2="30" stroke-width="2" stroke="#00fc49" filter="url(#glowing)"></line>
    
    
    </svg>

    【讨论】:

    • 感谢罗伯特的回答。我试过你的答案,它确实给了我在你画的线上的高斯模糊效果(一开始我不知道,因为我的背景是黑色的,模糊混合在一起。我不知道如何把它变成绿色,但那是这个问题之外的一些研究的主题)。这种方法有效,我还有很多关于 SVG 的知识,但我想知道我是否理解正确:所以我需要为每一行创建一个新过滤器并将其放置在正确的行的位置?我不能像 CSS 类那样对每一行应用一个过滤器,对吧?
    • 很难使用水平线和垂直线,因为它们没有高度或宽度(除了它们的笔划)。过滤器很容易在具有高度和宽度的事物上重用,因为您可以在那里使用 objectBoundingBox 单元。否则,您需要在同一位置绘制所有线条并将它们转换为您希望它们在父元素上使用变换的位置,以便它们的局部坐标相同并且可以对它们应用相同的过滤器。
    猜你喜欢
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2023-04-04
    相关资源
    最近更新 更多