0

我想使用 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>

以下是我所寻求的。

在此处输入图像描述

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

4

1 回答 1

1

您可以使用gelement withisolation: 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>

但在这种情况下,我认为你应该使用maskelement.

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>

于 2017-11-09T14:21:05.030 回答