如demo所示的svg实现的环形进度条,实际上未能完全实现环形渐变,本质还是水平渐变,环形度数超过250度,就会体现出水平渐变。svg能否实现环形渐变?就像css的conic-gradient一样。因为conic-gradient会产生锯齿,所以才使用svg来实现,渐变效果不太理想。
关键代码
<div style="width:150px;height:150px">
<svg width="100%" height="100%" class="progress" viewBox="0 0 100 100">
<defs>
<linearGradient id="grad1">
<stop offset="0%" stop-color="#29D65A"></stop>
<stop offset="100%" stop-color="#b3eac3"></stop>
</linearGradient>
</defs>
<circle class="c1" cx="50%" cy="50%" r="45%" stroke="#eee" stroke-width="5" fill="none" stroke-linecap="round" stroke-dasharray="283"></circle>
<path d="M 50 50 m 45 0 a 45 45 0 1 1 -0.08879722072777696 -2.825573378819097" fill="none" stroke="url(#grad1)" stroke-width="5" stroke-linecap="round"></path>
</svg>
</div>
不能,SVG 只支持线性渐变和径向渐变。不过你可以使用
clipPath
和foreignObject
配合 CSS 实现环形渐变:My Struggle to Use and Animate a Conic Gradient in SVG