【问题标题】:CSS: apply mix-blend-mode property to specific elements onlyCSS:仅将 mix-blend-mode 属性应用于特定元素
【发布时间】:2017-11-09 13:28:31
【问题描述】:

我想使用 mix-blend-mode 属性使某些特定的 SVG 路径在笔划上采用背景图像,使路径看起来好像是一条擦除路径。以下代码是我所达到的。但是,如您所见,mix-blend 属性会影响其他路径,这些路径需要在笔划上没有任何背景图像时出现。因此,我要求一种方法将 mix-blend-mode 属性仅应用于单独的元素组,并使其他元素不受影响。

g.erasing image{
mix-blend-mode:  lighten;
}
<html>

<body>

  <svg height="400" width="450">
    <!-- this is the background image -->
    <image id="background" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" width="400" height="450"></image>

      <g class="drawing">
         <!-- these are the drawing paths -->
         <path  d="M 100 350 l 150 -300" stroke="red" stroke-width="8" fill="none" />
         <path  d="M 250 50 l 150 300" stroke="red" stroke-width="8" fill="none" />
         <path  d="M 175 200 l 150 0" stroke="green" stroke-width="8" fill="none" />
         <path  d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="8" fill="none" />
      </g>


      <g class="erasing">  
          <!-- these are the erasing paths -->
         <path d="M 0 0 L 400 450" stroke="black" stroke-width="20"  />
         <path d="M 0 0 L 200 300" stroke="black" stroke-width="20"  />
         <image xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" width="400" height="450"></image>
      </g>

  </svg>

</body>
</html>

以下是我所寻求的。

注意:我可以使用屏蔽来做到这一点,但在某些浏览器中速度很慢。

【问题讨论】:

    标签: html css svg image-masking mix-blend-mode


    【解决方案1】:

    您可以使用g元素和isolation: isolate;来指定mix-blend-mode作用下的元素。

    circle{
      mix-blend-mode: lighten;
    }
    g{
      isolation: isolate;
    }
    <svg width="200px" height="200px">
      <rect width="100%" height="100%" fill="pink"/>
      <circle cx="100" cy="80" r="60" fill="red"/>
      <circle cx="70" cy="130" r="60" fill="green"/>
      <circle cx="130" cy="130" r="60" fill="blue"/>
    </svg>
    <svg width="200px" height="200px">
      <rect width="100%" height="100%" fill="pink"/>
      <g>
        <circle cx="100" cy="80" r="60" fill="red"/>
        <circle cx="70" cy="130" r="60" fill="#0f0"/>
        <circle cx="130" cy="130" r="60" fill="blue"/>
      </g>
    </svg>

    但在这种情况下,我认为你应该使用mask 元素。

    g.drawing{
      mask: url(#erasing);
    }
    <svg height="400" width="450">
      <!-- this is the background image -->
      <image id="background" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" width="400" height="450"></image>
      <g class="drawing">
         <!-- these are the drawing paths -->
         <path  d="M 100 350 l 150 -300" stroke="red" stroke-width="8" fill="none" />
         <path  d="M 250 50 l 150 300" stroke="red" stroke-width="8" fill="none" />
         <path  d="M 175 200 l 150 0" stroke="green" stroke-width="8" fill="none" />
         <path  d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="8" fill="none" />
      </g>
      <defs>
        <mask id="erasing">
          <rect width="100%" height="100%" fill="white"/>
          <!-- these are the erasing paths -->
          <path d="M 0 0 L 400 450" stroke="black" stroke-width="20"  />
          <path d="M 0 0 L 200 300" stroke="black" stroke-width="20"  />
        </mask>
      </defs>
    </svg>

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      相关资源
      最近更新 更多