【问题标题】:svg noise not shown in firefoxsvg 噪声未在 Firefox 中显示
【发布时间】:2019-03-03 06:29:05
【问题描述】:

为我的项目制作了一个多云的 svg 背景。 它在 MS Edge、Google Chrome 中完美运行,甚至在 XFCE 中作为桌面背景也能正常运行。 但它在 Firefox 中不起作用。 Win下都不行,Linux下也不行。 也许需要一些拐杖?

<svg version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%">      
<defs>
 <radialGradient id="Background"  cx="50%" cy="50%" r="120%">
     <stop offset="0%" stop-opacity="1" stop-color="#369" />
     <stop offset="170%" stop-opacity="1" stop-color="#000"/>
   </radialGradient>  
   <filter id="noise" x="0" y="0" width="100%" height="100%">   
     <feImage xlink:href="#bkgrad" result="image" x="0" y="0"/>    
     <feTurbulence  baseFrequency="0.001" seed="999" type="fractalNoise" numOctaves="8" result="fnoise"/>        
     <feColorMatrix   in="fnoise" result="snoise"
                 type="saturate"
                 values="0" />    
     <feBlend in="SourceGraphics" in2="snoise" mode="multiply"></feBlend>
     <feBlend in="snoise" in2="image" mode="multiply"></feBlend>
    </filter> 
    <polyline id="Hexagon" stroke="#000" stroke-opacity="0.15" fill="none" stroke-width = "1.2px"
       points="0,0 8,0 12,6.9 8,13.8 0,13.8 8,13.8 12,6.9 20,6.9 24,13.8 20,6.9 24,0 32,0 "/>
    <pattern id="Hex_Pattern" patternUnits="userSpaceOnUse" patternTransform="translate(0,0)"
      width="24" height="13.8" >
      <use xlink:href="#Hexagon"/>
    </pattern> 
</defs>
<rect x="0" y="0" width="100%" height="100%" fill="url(#Background)"  id="bkgrad" ></rect>
<rect x="0" y="0" width="100%" height="100%" style="filter:url('#noise')" fill="url(#Background)"/>
<rect x="0" y="0" width="100%" height="100%" fill="url(#Hex_Pattern)" id="hexes"/>
</svg>

【问题讨论】:

  • Firefox 不支持 图像不是完整的 SVG 或 png 文件。

标签: firefox svg svg-filters


【解决方案1】:

&lt;feBlend&gt; 过滤器的属性值为in="SourceGraphic",而不是in="SourceGraphics"。由于该错误,Firefox 没有渲染整个过滤器,而其他浏览器和渲染器使用后备并使用最后一个过滤器结果作为第一个源,有效地将 feColorMatrix 的结果与自身相乘。 (此行为是 CSS filter spec 中的新行为,未在 SVG 1.1 中定义。)

由于过滤器那部分的输出无论如何都不会使用,所以将其删除。

此外,正如 Robert Longson 所指出的,feImage 不支持对 Firefox 中的内部片段的引用。但你真的不需要它。引用的图像与过滤器的源图形相同,因此您可以简单地删除该图元并将输入重新路由到其他图元:

<svg version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%">      
<defs>
 <radialGradient id="Background"  cx="50%" cy="50%" r="120%">
     <stop offset="0%" stop-opacity="1" stop-color="#369" />
     <stop offset="170%" stop-opacity="1" stop-color="#000"/>
   </radialGradient>  
   <filter id="noise" x="0" y="0" width="100%" height="100%">   
     <feTurbulence  baseFrequency="0.001" seed="999" type="fractalNoise" numOctaves="8" result="fnoise"/>        
     <feColorMatrix   in="fnoise" result="snoise"
                 type="saturate"
                 values="0" />    
     <feBlend in="snoise" in2="SourceGraphic" mode="multiply"></feBlend>
    </filter> 
    <polyline id="Hexagon" stroke="#000" stroke-opacity="0.15" fill="none" stroke-width = "1.2px"
       points="0,0 8,0 12,6.9 8,13.8 0,13.8 8,13.8 12,6.9 20,6.9 24,13.8 20,6.9 24,0 32,0 "/>
    <pattern id="Hex_Pattern" patternUnits="userSpaceOnUse" patternTransform="translate(0,0)"
      width="24" height="13.8" >
      <use xlink:href="#Hexagon"/>
    </pattern> 
</defs>
<rect x="0" y="0" width="100%" height="100%" style="filter:url(#noise)" fill="url(#Background)"/>
<rect x="0" y="0" width="100%" height="100%" fill="url(#Hex_Pattern)" id="hexes"/>
</svg>

【讨论】:

  • 我认为 Firefox nightlies 现在实现了 CSS 过滤器规范后备。
猜你喜欢
  • 1970-01-01
  • 2015-02-03
  • 2021-12-24
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 2014-03-06
  • 2017-07-12
相关资源
最近更新 更多