在 SVG 中组合 clipPath、pattern 和 linearGradient

sdv*_*ksv 5 css svg

我正在尝试创建一个由从左到右从绿色到黄色渐变的多个点组成的背景。所以他们的想法是创建一个路径,用渐变填充它,然后用一个图案剪辑路径:

https://codepen.io/Deka87/pen/pLPqJE?editors=1000

<svg width='100' height='100' viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <linearGradient id="img-dotted-gradient">
      <stop offset="0%" stop-color="green"></stop>
      <stop offset="100%" stop-color="yellow"></stop>
    </linearGradient>
    <pattern id="img-dotted-dots" x="0" y="0" width=".1" height=".1">
      <circle cx="2" cy="2" r="2" fill="green"></circle>
    </pattern>
  </defs>
  <path d="M0 0 H 100 V 100 H 0 Z" fill="url(#img-dotted-gradient)" clip-path="url(#img-dotted-dots)"></path>
</svg>
Run Code Online (Sandbox Code Playgroud)

渐变工作正常,剪辑路径工作正常(独立)。然而他们并没有走到一起。任何帮助,将不胜感激!

Pau*_*eau 5

只有在一个<clipPath>事物的元素的形状。颜色和填充被忽略,所以你不能那样做。

但是您可以使用 a<mask>代替。

<svg width='100' height='100' viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <linearGradient id="img-dotted-gradient">
      <stop offset="0%" stop-color="green"></stop>
      <stop offset="100%" stop-color="yellow"></stop>
    </linearGradient>
    <pattern id="img-dotted-dots" x="0" y="0" width=".1" height=".1">
      <circle cx="2" cy="2" r="2" fill="white"></circle>
    </pattern>
    <mask id="img-dotted-mask">
      <rect width="100" height="100" fill="url(#img-dotted-dots)"/>
    </mask>
  </defs>
  <path d="M0 0 H 100 V 100 H 0 Z" fill="url(#img-dotted-gradient)" mask="url(#img-dotted-mask)"></path>
</svg>
Run Code Online (Sandbox Code Playgroud)

  • 正是我所需要的!谢谢你! (2认同)