javascript - SVG渐变色

标签 javascript html css svg canvas

您好,我在这里使用 SVG 我尝试像这样将渐变添加到 SVG enter image description here

白色和灰色渐变,但我无法获得所需的输出。谁能指出我正确的方向。

<svg viewBox="0 0 400 400">
				<defs>  
	  <linearGradient id="GradientRepeat" x1="0" y1="1" x2="0" y2="0" spreadMethod="repeat">
      <stop offset="0.05" stop-color="#fff" stop-opacity="0"/>
      <stop offset="1" stop-color="#777" stop-opacity="1"/>
    </linearGradient>
	  </defs>
    <circle class="sub-menu-circle" cx="0" cy="200" r="160" fill="url(#GradientRepeat)" />
 </svg>

<svg viewBox="0 0 700 700" class="bubble-svg">
	<defs>

		<linearGradient id="GradientRepeat" x1="0" y1="1" x2="0" y2="0" spreadMethod="repeat" gradientTransform="rotate(170)">
			<stop offset="0%" stop-color="#fff" stop-opacity="0"/>
			<stop offset="10%" stop-color="#bdbdbd" stop-opacity="0.5"/>
			<stop offset="20%" stop-color="#fff" stop-opacity="0"/>
			<stop offset="30%" stop-color="#bdbdbd" stop-opacity="0.5"/>
			<stop offset="40%" stop-color="#fff" stop-opacity="0"/>
			<stop offset="50%" stop-color="#bdbdbd" stop-opacity="1"/>

			<stop offset="60%" stop-color="#fff" stop-opacity="0"/>
			<stop offset="70%" stop-color="#bdbdbd" stop-opacity="0.5"/>
			<stop offset="80%" stop-color="#bdbdbd" stop-opacity="0"/>
			<stop offset="100%" stop-color="#fbfbfb" stop-opacity="0.5"/>

		</linearGradient>
	</defs>
	<circle class="sub-menu-circle" cx="0" cy="200" r="160" fill="url(#GradientRepeat)" />
</svg>

最佳答案

微调灰色和白色阴影的渲染取决于您使用的视频卡、浏览器和操作系统。

因此,我发送了几个选项。您可以使用您喜欢的任何选项或通过更改颜色为自己稍微调整。

<svg viewBox="0 0 700 700" class="bubble-svg">
	<defs>

		<linearGradient id="GradientRepeat" x1="0" y1="0" x2="0" y2="1" spreadMethod="repeat" gradientTransform="rotate(170)">
		
			<stop offset="0%" stop-color="#B4B4B5" stop-opacity="1"/>
			<stop offset="17%" stop-color="#fff" stop-opacity="0.8"/>
			
			<stop offset="30%" stop-color="#B4B4B5" stop-opacity="0.6"/>
			<stop offset="49%" stop-color="#fff" stop-opacity="0.8"/>
			
			<stop offset="61%" stop-color="#B4B4B5" stop-opacity="0.6"/>
			<stop offset="80%" stop-color="#fff" stop-opacity="0.8"/>
	  	<stop offset="95%" stop-color="#B4B4B5" stop-opacity="1"/>
	
		</linearGradient>
	</defs>
	<circle class="sub-menu-circle" cx="0" cy="200" r="160" fill="url(#GradientRepeat)" />
</svg>

2#变体

<svg viewBox="0 0 700 700" class="bubble-svg">
	<defs>

		<linearGradient id="GradientRepeat" x1="0" y1="0" x2="0" y2="1" spreadMethod="repeat" gradientTransform="rotate(170)">
		
			<stop offset="0%" stop-color="#B4B4B5" stop-opacity="1"/>
			<stop offset="17%" stop-color="#F6F6F6" stop-opacity="0.8"/>
			
			<stop offset="30%" stop-color="#B4B4B5" stop-opacity="0.6"/>
			<stop offset="49%" stop-color="#F6F6F6" stop-opacity="0.8"/>
			
			<stop offset="61%" stop-color="#B4B4B5" stop-opacity="0.6"/>
			<stop offset="80%" stop-color="#F6F6F6" stop-opacity="0.8"/>
				
			<stop offset="95%" stop-color="#B4B4B5" stop-opacity="1"/>
		</linearGradient>
	</defs>
	<circle class="sub-menu-circle" cx="0" cy="200" r="160" fill="url(#GradientRepeat)" />
</svg>  

3#变体

<svg viewBox="0 0 700 700" class="bubble-svg">
	<defs>

		<linearGradient id="GradientRepeat" x1="0" y1="0" x2="0" y2="1" spreadMethod="repeat" gradientTransform="rotate(170)">
		
			<stop offset="0%" stop-color="#ABABAC" stop-opacity="1"/>
			<stop offset="17%" stop-color="#fff" stop-opacity="0.8"/>
			
			<stop offset="30%" stop-color="#ABABAC" stop-opacity="0.6"/>
			<stop offset="49%" stop-color="#fff" stop-opacity="0.8"/>
			
			<stop offset="61%" stop-color="#ABABAC" stop-opacity="0.6"/>
			<stop offset="80%" stop-color="#fff" stop-opacity="0.8"/>
			
			
			<stop offset="95%" stop-color="#ABABAC" stop-opacity="1"/>

		</linearGradient>
	</defs>
	<circle class="sub-menu-circle" cx="0" cy="200" r="160" fill="url(#GradientRepeat)" />
</svg>  

为了更精确地调整您的品味,最好使用 stop-colorstop-opacity 选择值。

更新

要使方法 spreadMethod ="repeat" 在您的示例中开始工作,您需要将梯度覆盖范围减少三倍。为此,设置 x1 ="0"y1 ="0"x2 ="0"y2 ="0.33"
结果,调整一个波的色调,我们得到它的完全相同的副本,与大量 stop-offset

的选项相比,这简化了过程

<svg viewBox="0 0 700 700" class="bubble-svg">
	<defs>

		<linearGradient id="GradientRepeat" x1="0" y1="0" x2="0" y2="0.33" 
		   spreadMethod="repeat"  gradientTransform="rotate(170)">
		
			<stop offset="10%" stop-color="#ABABAC" stop-opacity="1"/>
			<stop offset="50%" stop-color="#ffffff" stop-opacity="0.8"/>
			<stop offset="85%" stop-color="#ABABAC" stop-opacity="1"/>
		</linearGradient>
	</defs>
	<circle class="sub-menu-circle" cx="0" cy="200" r="160" fill="url(#GradientRepeat)" />
</svg>  

关于javascript - SVG渐变色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59676000/

相关文章:

javascript - 悬停更改图像时的垂直导航

javascript - 单击时选择 AutoPostBack

javascript - 第二次和第三次点击时的事件

php - 在 html 中定位表单

jquery - 如何在 slider 更改时将类添加到 Bootstrap 缩略图

javascript - 哪个版本的 ECMA 规范引入了提升?

html - 如何进行媒体查询,一种包括 iPad pro,另一种不包括桌面?

html - Bootstrap 下拉菜单未弹出

html - 如何处理流体 div 的高度?

html - 如何删除表单元素之间的空白