当我指定1px时,为什么我的SVG线条模糊或高度为2px?

1.2*_*tts 11 svg

我正在创建一个SVG线,它在我的网页中显得模糊.更清楚的是,它看起来比1px的笔划宽度大.为什么会发生这种情况,有没有办法在SVG中修复它?

这是代码.当我自己运行此代码时,它并不模糊.当它在我的网页中时,该线看起来大约是2px而不是1.

#HorizontalLine1178  {
	stroke:rgb(154,154,154);
	stroke-width:1;
}
Run Code Online (Sandbox Code Playgroud)
<svg style="width:100%;">
    <line id="HorizontalLine1178" y2="97" y1="97" x2="100%" x1="62" >
</svg>
Run Code Online (Sandbox Code Playgroud)

myf*_*myf 20

因为当它的Y坐标位于整个像素上时,1px笔划就在它周围,从而"抗锯齿"(参考Paul LeBeau的优秀插图).在这种情况下使用半像素坐标,或应用shape-rendering="crispEdges"它将为您进行像素舍入,但即使在圆形对象上也会产生锐边:

<svg style="width:100%; background-color: white" stroke="black" fill="white" stroke-width="1">
	<line y2="10.0" y1="10.0" x2="90%" x1="10">
		<title>.0</title>
	</line>
	<line y2="15.5" y1="15.5" x2="90%" x1="10">
		<title>.5</title>
	</line>
	<line y2="20.0" y1="20.0" x2="90%" x1="10" shape-rendering="crispEdges">
		<title>.0 + crispEdges</title>
	</line>

	<circle cy="50" cx="20" r="10">
		<title>.0</title>
	</circle>
	<circle cy="49.5" cx="44.5" r="10">
		<title>.5</title>
	</circle>
	<circle cy="50" cx="70" r="10" shape-rendering="crispEdges">
		<title>.0 + crispEdges</title>
	</circle>

	<rect x="90" y="40" width="20" height="20">
		<title>.0</title>
	</rect>
	<rect x="120" y="40" width="20" height="20" shape-rendering="crispEdges">
		<title>.0 + crispEdges</title>
	</rect>
	<rect x="149.5" y="39.5" width="20" height="20">
		<title>.5</title>
	</rect>

	<rect x="190" y="40" width="20" height="20" stroke="none" fill="black">
		<title>.0 + fill, no stroke</title>
	</rect>
	<rect x="219.5" y="39.5" width="20" height="20" stroke="none" fill="black">
		<title>.5 + fill, no stroke</title>
	</rect>
</svg>
Run Code Online (Sandbox Code Playgroud)

  • 如果你像我一样喜欢这个答案 - 但你是在对自己说,_"嘿,这肯定在我的SVG中有很多额外的角色,而我正在努力保持小事!"_ - 然后尝试放置这个打开`<svg>`标签之后,记得在`</ svg>`之前关闭它:`<g shape-rendering ="crispEdges">`.适用于每一个对象,真正改善了我的工作. (2认同)