【问题标题】:How can I create a "glow" around a rectangle with svg?如何使用 svg 在矩形周围创建“发光”?
【发布时间】:2012-03-26 15:57:29
【问题描述】:

我有类似以下的内容:

<svg id="svgLogo1" style="left:0; top:0; position:absolute"
        width="980" height="80" viewBox="0 0 980 80" 
        xmlns="http://www.w3.org/2000/svg">
    <rect x="0" y="5" width="980" height="54" rx="6" ry="6" 
            style="stroke-width:2; xstroke:#FFF; fill:#555"/>
</svg>

我想围绕它创建一个白光。

有什么方法可以让我在 svg 中做到这一点。我环顾四周,我只能找到“阴影”,这并不是我真正想要的,因为我想要在矩形的所有四个边周围都有一个阴影(发光)。

【问题讨论】:

  • 您可能会发现 this 很有帮助
  • 您是否可以选择添加 2 个阴影?一个左右上下,一个上下左右?
  • 感谢您的建议。问题是我看不到如何创建白色阴影。我在创造黑色而不是白色方面取得了很多成功。
  • 一个没有 x、y 偏移的阴影看起来像一个发光...

标签: svg svg-filters


【解决方案1】:

以下是一些提供不同类型效果的过滤器:

  • 投影(透明黑色阴影,轻微偏移)
  • 黑光(固定颜色)
  • 对象颜色发光(采用应用对象的颜色)

一个例子:

有一个demo here

代码:

<!-- a transparent grey drop-shadow that blends with the background colour -->
<filter id="shadow" width="1.5" height="1.5" x="-.25" y="-.25">
    <feGaussianBlur in="SourceAlpha" stdDeviation="2.5" result="blur"/>
    <feColorMatrix result="bluralpha" type="matrix" values=
            "1 0 0 0   0
             0 1 0 0   0
             0 0 1 0   0
             0 0 0 0.4 0 "/>
    <feOffset in="bluralpha" dx="3" dy="3" result="offsetBlur"/>
    <feMerge>
        <feMergeNode in="offsetBlur"/>
        <feMergeNode in="SourceGraphic"/>
    </feMerge>
</filter>

<!-- a transparent grey glow with no offset -->
<filter id="black-glow">
    <feColorMatrix type="matrix" values=
                "0 0 0 0   0
                 0 0 0 0   0
                 0 0 0 0   0
                 0 0 0 0.7 0"/>
    <feGaussianBlur stdDeviation="2.5" result="coloredBlur"/>
    <feMerge>
        <feMergeNode in="coloredBlur"/>
        <feMergeNode in="SourceGraphic"/>
    </feMerge>
</filter>

<!-- a transparent glow that takes on the colour of the object it's applied to -->
<filter id="glow">
    <feGaussianBlur stdDeviation="2.5" result="coloredBlur"/>
    <feMerge>
        <feMergeNode in="coloredBlur"/>
        <feMergeNode in="SourceGraphic"/>
    </feMerge>
</filter>

【讨论】:

    【解决方案2】:

    颜色矩阵不能真正用于使事物发出不同的颜色,只能以某种方式转换现有颜色。

    但我们可以这样做...

    <filter id="white-glow">
        <feFlood result="flood" flood-color="#ffffff" flood-opacity="1"></feFlood>
        <feComposite in="flood" result="mask" in2="SourceGraphic" operator="in"></feComposite>
        <feMorphology in="mask" result="dilated" operator="dilate" radius="2"></feMorphology>
        <feGaussianBlur in="dilated" result="blurred" stdDeviation="5"></feGaussianBlur>
        <feMerge>
            <feMergeNode in="blurred"></feMergeNode>
            <feMergeNode in="SourceGraphic"></feMergeNode>
        </feMerge>
    </filter>
    

    参见我根据Drew's answer制作的this fiddle

    以下是过滤器功能的细分:

    • 将洪水填充与源相结合以对其进行着色(feFloodfeComposite)。
    • 稍微扩展这个彩色对象(feMorphologyoperator="dilate"
    • 应用我们良好的旧模糊效果使其发光! (feGaussianBlur)
    • 把这个彩色的、膨胀的、发光的物体贴在源下面 (feMerge)

    【讨论】:

    • 如果矩形的填充使用 alpha 通道,如“rgba(3,3,3,0.9)”,阴影会修改矩形的颜色。有没有办法避免这种情况?
    • 您可以添加另一个“feComposite”步骤来消除原始对象下方的任何发光像素。我给你做了一个小提琴:) jsfiddle.net/4nz8o1p8
    • 将鼠标悬停在该小提琴中的对象上以应用发光 - 你会看到它们的颜色没有改变:)
    • 附加的 'feComposite' 似乎在 firefox 和 safari 中不起作用。
    • 啊,真烦人!恐怕不知道如何解决这个问题:/
    【解决方案3】:

    试试这个:

    <svg id="svgLogo1" style="left: 0px; top: 0px;
      position: absolute;" width="980" height="80" viewBox="0 0 980 80"
      xmlns="http://www.w3.org/2000/svg" version="1.1" >
        <defs>
            <filter id="dropGlow" width="1.5" height="1.5" x="-.25" y="-.25">
                <feGaussianBlur id="feGaussianBlur5384" in="SourceAlpha" stdDeviation="15.000000" result="blur"/>
                <feColorMatrix id="feColorMatrix5386" result="bluralpha" type="matrix" values="-1 0 0 0 1 0 -1 0 0 1 0 0 -1 0 1 0 0 0 0.800000 0 "/>
                <feOffset id="feOffset5388" in="bluralpha" dx="0.000000" dy="0.000000" result="offsetBlur"/>
                <feMerge id="feMerge5390">
                    <feMergeNode id="feMergeNode5392" in="offsetBlur"/>
                    <feMergeNode id="feMergeNode5394" in="SourceGraphic"/>
                </feMerge>
            </filter>
        </defs>
        <rect x="0" y="5" width="980" height="54" rx="6" ry="6"
            style="stroke-width: 2; xstroke: #FFFFFF; fill: #555555; filter:url(#dropGlow)"/>
    </svg>
    

    我在 Inkscape 中创建了原始过滤器,但它适用于任何应用都一样好。

    【讨论】:

    • +1 用于提及 Inkscape。我尝试了提供的 svg,但它不起作用
    【解决方案4】:

    如果您使用模糊滤镜,请谨慎行事。特别是模糊在 CPU 资源方面的成本可能很高。因此,它也可能更快地消耗电池。使用工具(例如 OS X 活动监视器)观察过滤器的效果,尤其是在涉及动画或视频的情况下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      相关资源
      最近更新 更多