SVG中相邻的矩形元素之间会留下一个微小的间隙。

5

我正在使用SVG创建调色板。为此,我需要创建许多rect元素,并仅更改每个元素的样式。

问题是,有时候在元素之间会出现微小的白色缝隙,就像这张图片中所示:

svg palette

这个快照是在OSX上的Chrome中拍摄的。Safari看起来也类似。

在我的代码中,viewBox不是固定的,可以根据矩形的数量而变化。此外,宽度和高度也可以根据所选分辨率而变化。换句话说,我不能通过这些进行调整以使其正确显示。

我尝试将每个矩形创建得比必要的宽一点,但效果是相同的。 在上面的图片中,顶行的宽度为100.1,而底部为100。 将宽度设为101可以解决问题,但我无法使用这个方法,因为在某些情况下,一个rect的期望宽度可能只有一位数,因此额外的1会导致重叠问题。

我试图添加描边,但这并不起作用,因为使其正常工作所需的宽度取决于viewBoxrect的大小。

您见过这个问题吗?

这是我的代码:

<html>
<body>
    <svg width="1000" height="1000" viewBox="0 0 5000 5000">
        <g>
            <rect x="0" y="0" width="100.1" height="100" style="fill:rgb(0,0,255)"></rect>
            <rect x="100" y="0" width="100.1" height="100" style="fill:rgb(0,0,250)"></rect>
            <rect x="200" y="0" width="100.1" height="100" style="fill:rgb(0,0,245)"></rect>
            <rect x="300" y="0" width="100.1" height="100" style="fill:rgb(0,0,240)"></rect>
            <rect x="400" y="0" width="100.1" height="100" style="fill:rgb(0,0,235)"></rect>
            <rect x="500" y="0" width="100.1" height="100" style="fill:rgb(0,0,230)"></rect>
            <rect x="600" y="0" width="100.1" height="100" style="fill:rgb(0,0,225)"></rect>
            <rect x="700" y="0" width="100.1" height="100" style="fill:rgb(0,0,220)"></rect>
            <rect x="800" y="0" width="100.1" height="100" style="fill:rgb(0,0,215)"></rect>
            <rect x="900" y="0" width="100.1" height="100" style="fill:rgb(0,0,210)"></rect>
            <rect x="1000" y="0" width="100.1", height="100" style="fill:rgb(0,0,205)"></rect>
            <rect x="1100" y="0" width="100.1" height="100" style="fill:rgb(0,0,200)"></rect>
            <rect x="1200" y="0" width="100.1" height="100" style="fill:rgb(0,0,195)"></rect>
            <rect x="1300" y="0" width="100.1" height="100" style="fill:rgb(0,0,190)"></rect>
            <rect x="1400" y="0" width="100.1" height="100" style="fill:rgb(0,0,185)"></rect>
            <rect x="0" y="100" width="100" height="100" style="fill:rgb(0,0,255)"></rect>
            <rect x="100" y="100" width="100" height="100" style="fill:rgb(0,10,250)"></rect>
            <rect x="200" y="100" width="100" height="100" style="fill:rgb(0,20,245)"></rect>
            <rect x="300" y="100" width="100" height="100" style="fill:rgb(0,30,240)"></rect>
            <rect x="400" y="100" width="100" height="100" style="fill:rgb(0,40,235)"></rect>
            <rect x="500" y="100" width="100" height="100" style="fill:rgb(0,50,230)"></rect>
            <rect x="600" y="100" width="100" height="100" style="fill:rgb(0,60,225)"></rect>
            <rect x="700" y="100" width="100" height="100" style="fill:rgb(0,70,220)"></rect>
            <rect x="800" y="100" width="100" height="100" style="fill:rgb(0,80,215)"></rect>
            <rect x="900" y="100" width="100" height="100" style="fill:rgb(0,90,210)"></rect>
            <rect x="1000" y="100" width="100" height="100" style="fill:rgb(0,100,205)"></rect>
            <rect x="1100" y="100" width="100" height="100" style="fill:rgb(0,110,200)"></rect>

            <rect x="1200" y="100" width="100" height="100" style="fill:rgb(0,120,195)"></rect>
            <rect x="1300" y="100" width="100" height="100" style="fill:rgb(0,130,190)"></rect>
            <rect x="1400" y="100" width="100" height="100" style="fill:rgb(0,140,185)"></rect>
        </g>
    </svg>
</body>
</html>
1个回答

4
那是反锯齿,尝试在

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接