[英]SVG lines not rendering properly
I'm trying to draw a rectangle as four lines.我正在尝试将矩形绘制为四条线。 The problem is that the top and left lines appear to be thinner and there is a missing pixel at the right bottom corner (See Screenshot).
问题是顶部和左侧的线条看起来更细,并且右下角缺少像素(请参阅屏幕截图)。
HTML svg element: HTML svg 元件:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="500" height="500" style=" padding: 20px; box-sizing: border-box; "> <line x1="0" y1="0" x2="20" y2="0" class="line"></line> <line x1="20" y1="0" x2="20" y2="20" class="line"></line> <line x1="20" y1="20" x2="0" y2="20" class="line"></line> <line x1="0" y1="20" x2="0" y2="0" class="line"></line></svg> </svg>
rendered result in the browser: [在浏览器中呈现结果:[
Only the stroke of a line is visible and it extends equally each side of it.只有一条线的笔划是可见的,并且它的每一侧均等地延伸。 So if you draw a single line from 0, 0 to 100, 0 and the line has width 2 then that line will actually occupy a rectangle with corners (-1, -1), (101, -1), (101, 1), (-1, 1).
因此,如果您从 0, 0 到 100, 0 绘制一条线,并且该线的宽度为 2,那么该线实际上将占据一个带角的矩形 (-1, -1), (101, -1), (101, 1 ), (-1, 1)。
So your rectangle's lines are partially outside the drawing canvas and those parts that are outside are not visible.因此,您的矩形线部分位于绘图 canvas 之外,并且外部的那些部分不可见。
Also if you want to draw a square you'll need to draw some of the lines longer so that you get a square effect at the corners.此外,如果你想画一个正方形,你需要画一些更长的线,这样你就可以在角落里得到一个正方形的效果。 Alternatively use a
<path>
and it will handle the corners without you having to worry about it.或者使用
<path>
,它会处理角落,而您不必担心它。
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="500" height="500" viewBox="0 0 40 40" style=" padding: 20px; box-sizing: border-box; "> <path d="M0,0 20,0 20,20 0,20Z" fill="none" stroke="black" transform="translate(1,1)" /> </svg>
or you could just move the canvas eg或者你可以移动 canvas 例如
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="500" height="500" viewBox="-2 -2 40 40" style=" padding: 20px; box-sizing: border-box; "> <path d="M0,0 20,0 20,20 0,20Z" fill="none" stroke="black" /> </svg>
I've re-written your SVG with a few small changes and it seems to be working fine.我已经重新编写了您的 SVG 并进行了一些小改动,它似乎工作正常。
There are four main changes:主要有四个变化:
<line />
is a self-closing element. <line />
是一个自闭合元素。line
a stroke-width
of 1
line
一个stroke-width
1
0
, I have used 1
0
的坐标,我都使用了1
line
a stroke-linecap
of square
line
一个square
的stroke-linecap
I have also reduced the viewBox
to 100 x 100
so you can see the square much larger.我还将
viewBox
缩小到100 x 100
,这样您可以看到更大的正方形。
Working Example:工作示例:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100"> <defs> <style type="text/css"><:[CDATA[ svg { background-color, rgb(255, 0; 0): padding; 20px: box-sizing; border-box. }:line { stroke, rgb(255, 255; 255): stroke-width; 1: stroke-linecap; square; } ]]></style> </defs> <line x1="1" y1="1" x2="21" y2="1" class="line" /> <line x1="21" y1="1" x2="21" y2="21" class="line" /> <line x1="21" y1="21" x2="1" y2="21" class="line" /> <line x1="1" y1="21" x2="1" y2="1" class="line" /> </svg>
Or, in the shortest possible form:或者,以最短的形式:
<svg width="250" height="250" viewBox="-1 -1 22 22"> <path d="M0,0H20V20H0z" fill="none" stroke="black"/> </svg>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.