gpt4 book ai didi

css - 在 SVG 中组合 clipPath、pattern 和 linearGradient

转载 作者:行者123 更新时间:2023-11-28 11:23:52 25 4
gpt4 key购买 nike

我正在尝试创建一个背景,该背景由从左到右从绿色到黄色渐变的多个点组成。所以他们的想法是创建一条路径,用渐变填充它,并用一种​​模式裁剪路径:

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>

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

最佳答案

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

但是你可以使用 <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>

关于css - 在 SVG 中组合 clipPath、pattern 和 linearGradient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49427368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com