[英]SVG stroke issue
我正在用SVG创建一个图表。 我用三个圆形元素创建它,并添加一个路径(三角形)来制作一个空白区域。 但我不知道如何在底部隐藏一个几乎可见的薄边框。 也许我做错了什么?
`https://codepen.io/Groude/pen/VgmVvG`
<svg viewBox="0 0 32 32">
<defs></defs>
<circle
r="16"
cx="16"
cy="16"
class="circle--progress"
stroke-dasharray="100 100"
></circle>
<circle
r="16"
cx="16"
cy="16"
fill="none"
stroke="#3FC364"
stroke-dasharray="100 100"
></circle>
<circle
r="16"
cx="16"
cy="16"
fill="none"
stroke="#EDBB40"
stroke-dasharray="66 100"
></circle>
<circle
r="16"
cx="16"
cy="16"
fill="none"
stroke="#FF8832"
stroke-dasharray="33 100"
></circle>
<path d="M16 16 L100 50 L200 -50 Z" fill="white"></path>
</svg>
<div class="text">
<div class="text__percentage">100%</div>
<div class="text__description">Sentiment<br />score</div>
</div>
</div>
.wrapper {
position: relative;
width: 400px;
height: 400px;
padding: 20px 0;
margin: 0 auto;
}
svg {
width: 100%;
height: 100%;
border-radius: 50%;
transform: rotate(90deg);
}
circle {
fill: transparent;
stroke-width: 3;
}
.circle--progress {
fill: transparent;
stroke: #C4C4C4;
stroke-width: 32;
stroke-opacity: .25;
}
.text {
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-family: sans-serif;
text-align: center;
}
.text__percentage {
font-size: 60px;
font-weight: bold;
}
.text__description {
font-size: 18px;
font-weight: 500;
line-height: 16px;
}
我假设您使用谷歌浏览器或类似的基于Blink或Webkit的浏览器来测试此SVG图像。 在Mozilla Firefox或Microsoft Edge中打开页面不会显示您指出的非常薄的边框,因此这是您的浏览器而不是代码的问题。 我建议向Google发送有关此问题的错误报告。
在此期间,要解决包括Chrome在内的所有浏览器的此问题,请考虑使用SVG <clipPath>
元素并将其应用于除白色三角形之外的所有形状。 然后,在CSS中删除svg
选择器中的border-radius
属性。
<svg viewBox="0 0 32 32">
<defs>
<clipPath id="circle-viewport">
<circle r="16" cx="16" cy="16" />
</clipPath>
</defs>
<circle
r="16"
cx="16"
cy="16"
class="circle--progress"
stroke-dasharray="100 100"
clip-path="url(#circle-viewport)"
></circle>
<circle
r="16"
cx="16"
cy="16"
fill="none"
stroke="#3FC364"
stroke-dasharray="100 100"
clip-path="url(#circle-viewport)"
></circle>
<circle
r="16"
cx="16"
cy="16"
fill="none"
stroke="#EDBB40"
stroke-dasharray="66 100"
clip-path="url(#circle-viewport)"
></circle>
<circle
r="16"
cx="16"
cy="16"
fill="none"
stroke="#FF8832"
stroke-dasharray="33 100"
clip-path="url(#circle-viewport)"
></circle>
<path d="M16 16 L100 50 L200 -50 Z" fill="white"></path>
</svg>
问题似乎在浏览器(Chrome,Safary或同一引擎上的其他人)如何使用border-radius:50%
呈现svg。 用圆形面具剪裁它没有帮助。
一种解决方案是使图形看起来相同而不用border-radius
切割(遮蔽)它看起来是圆形的。 这将需要欺骗彩色线段的圆半径和笔触宽度:
<circle
r="15" <--
cx="16"
cy="16"
fill="none"
stroke="#3FC364"
stroke-dasharray="100 100"
></circle>
circle {
fill: transparent;
stroke-width: 2; <--
}
并进行循环以适应图表的所需圆圈:
<circle
r="8" <--
cx="16"
cy="16"
class="circle--progress"
stroke-dasharray="100 100"
></circle>
.circle--progress {
fill: transparent;
stroke: #C4C4C4;
stroke-width: 16; <-- 8 radius + 8 half stroke width= 16 visible radius
stroke-opacity: .25;
}
请参阅完整示例: https : //codepen.io/anon/pen/zeogLV
如果需要border-radius
来使图形与背景更好地融合 - 将其包装在div中并将border-radius
应用于div。
请参阅示例https://codepen.io/anon/pen/exgOvm?editors=1100
我改变了身体背景,因此剪裁会更清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.