【问题标题】:CSS — How to blur a SVG mask?CSS — 如何模糊 SVG 蒙版?
【发布时间】:2019-05-12 06:05:00
【问题描述】:

我尝试模糊 SVG 剪辑路径但没有成功。我尝试了不同的解决方案,但都没有奏效。我不确定除了filter之外是否还有其他解决方案。

伪代码

  • SVG 剪辑路径应显示下面的文本
  • 应模糊 SVG 时的边缘

提前谢谢你。

HTML

    .wrapper {
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
    }
    
    .h1, blur {
      width: 100vw;
      height: 100vh;
    }
    
    .h1 {
      position: absolute;
      top: 0;
      left: 0;
      margin: 0;
      padding: 0;
      font-size: 4em;
      clip-path: url(#svgPath);
      background-color: blue;
    }
    
    .blur {
      position: absolute;
      top: 0;
      left: 0;
      margin: 0;
      padding: 0;
      font-size: 4em;
      color: blue;
      background-color: white;
      filter: blur(8px)
    }
<div class="wrapper">
      <h1 class="blur">
        Our principles inform everything we do. Whether you're preparing content, designing an interface or developing an entire service, start by reading these.
      </h1>
      <h1 class="h1">
        Our principles inform everything we do. Whether you're preparing content, designing an interface or developing an entire service, start by reading these.
      </h1>
    </div>
    
    <svg id="googlesMain" height="0" width="0">
      <defs>
        <filter id="f1" x="0" y="0">
          <feGaussianBlur in="SourceGraphic" stdDeviation="15" />
        </filter>
      </defs>
        <clipPath id="svgPath">
           <circle id="clipPath" cx="250" cy="250" r="250"/>
        </clipPath>
    </svg>

【问题讨论】:

    标签: html css svg clip-path


    【解决方案1】:

    使用&lt;mask&gt; 而不是&lt;clipPath&gt;。剪辑路径不能模糊,但&lt;mask&gt; 中的元素可以。

    以下内容可以在 Firefox 中使用,但在其他浏览器中会出现问题:

    .wrapper {
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
    }
        
    .h1, .blur {
      width: 100vw;
      height: 100vh;
    }
        
    .h1 {
      position: absolute;
      top: 0;
      left: 0;
      margin: 0;
      padding: 0;
      font-size: 4em;
      -webkit-mask: url(#svgPath);
      mask: url(#svgPath);
      background-color: blue;
    }
        
    .blur {
      position: absolute;
      top: 0;
      left: 0;
      margin: 0;
      padding: 0;
      font-size: 4em;
      color: blue;
      background-color: white;
      filter: blur(8px)
    }
    <div class="wrapper">
      <h1 class="blur">
        Our principles inform everything we do. Whether you're preparing content, designing an interface or developing an entire service, start by reading these.
      </h1>
      <h1 class="h1">
        Our principles inform everything we do. Whether you're preparing content, designing an interface or developing an entire service, start by reading these.
      </h1>
    </div>
        
    <svg id="googlesMain" height="0" width="0">
      <defs>
        <filter id="f1" x="0" y="0">
          <feGaussianBlur in="SourceGraphic" stdDeviation="15" />
        </filter>
        <mask id="svgPath">
          <circle id="clipPath" cx="250" cy="250" r="250" fill="white" filter="url(#f1)"/>
        </mask>
      </defs>
    </svg>
        

    要使其在其他浏览器中工作,请尝试将其转换为独立的 SVG。然后改用mask-image CSS 规则。

    <svg xmlns="http://www.w3.org/2000/svg">
      <defs>
        <filter id="f1" x="0" y="0">
          <feGaussianBlur in="SourceGraphic" stdDeviation="15" />
        </filter>
      </defs>
      <circle cx="250" cy="250" r="250" fill="white" filter="url(#f1)" />
    </svg>
    

    使用 CSS:

    .h1 {
      ...
      -webkit-mask-image: url(mask.svg);
      mask-image: url(mask.svg);
      ...
    }
    

    【讨论】:

    • 我要试试。在没有与其他浏览器兼容的实际 SVG 独立设备的情况下,还有其他方法吗?
    • 是的。您可以将文本移动到 SVG 中。这样您就可以避免在 HTML 内容上使用 SVG 掩码时(非 Firefox)浏览器支持不佳的问题。
    • 谢谢。不幸的是,SVG 路径的边缘仍然很锋利。我试图模糊剪辑路径的边缘但没有成功。
    • 可能是因为我使用的是clipPath。我需要这个图层来用光标移动蒙版——codepen.io/diegooriani/pen/VqYOmg
    • 您不能使用clipPath。剪切路径总是尖锐的。无法模糊剪切路径。
    【解决方案2】:

    好的,这是一种将radial-gradient() 用作mask-image 的方法。

    var h1 = document.getElementById('masked');
    
    document.addEventListener('mousemove', mouseListen, false);
    
    function mouseListen(e){ 
      setMaskPos(e.clientX,e.clientY);
    }
    
    function setMaskPos(x,y) {
      h1.setAttribute("style", "-webkit-mask-image: radial-gradient(circle at " + x + "px " + y + "px, black 0px, black 200px, transparent 250px)");
    }
    
    // Initialise the mask off screen
    setMaskPos(-999,0);
    .wrapper {
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
    }
    
    .h1, blur {
      width: 100vw;
      height: 100vh;
    }
    
    .h1 {
      position: absolute;
      top: 0;
      left: 0;
      margin: 0;
      padding: 0;
      font-size: 4em;
      background-color: white;
    }
    
    .blur {
      position: absolute;
      top: 0;
      left: 0;
      margin: 0;
      padding: 0;
      font-size: 4em;
      background-color: white;
      filter: blur(8px)
    }
    <div class="wrapper">
      <h1 class="blur">
        Our principles inform everything we do. Whether you're preparing content, designing an interface or developing an entire service, start by reading these.
      </h1>
      <h1 id="masked" class="h1">
        Our principles inform everything we do. Whether you're preparing content, designing an interface or developing an entire service, start by reading these.
      </h1>
    </div>

    【讨论】:

    • 保罗,谢谢。这正是我想要达到的目标。很高兴知道我不能模糊剪辑路径。
    猜你喜欢
    • 1970-01-01
    • 2016-04-11
    • 2020-05-05
    • 1970-01-01
    • 2019-08-06
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多