SVG网格渲染Chrome,Firefox,IE -错误的线对齐-模糊的线

SVG Grid Rendering Chrome,Firefox,IE - Wrong Line Alignment - Blurred Lines

本文关键字:错误 模糊 对齐 Firefox 网格 Chrome SVG IE      更新时间:2023-09-26

为了创建SVG,我在SVG中绘制了几条线。问题是它在Chrome和Firefox中看起来不一样。

Chrome:最后一条线未绘制Firefox:第一行未绘制

顺便说一句:ie版本看起来模糊,但这不是主要问题。

那么现在是谁呢?

我做错了什么。

给你一些背景信息:我通常动态地从JavaScript绘制这个网格。我是否必须编写丑陋的hack来处理这些不同的SVG浏览器呈现行为?(我不想使用任何库,但普通的JavScript)

请参阅此codependency。输入输出:http://codepen.io/mjost/full/kuaFH

下面是代码:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" baseProfile="full" id="phoenix10_5" viewBox="0 0 960 768"> 
<defs>
<style type="text/css">
<![CDATA[
svg
{
    shape-rendering: crispEdges;
    stroke-linecap: butt;
}
text
{
    alignment-baseline: auto;
}
]]>
</style>
</defs>
<rect x="0" y="0" width="960" height="768" rx="0" ry="0" fill="rgb(255, 255, 255)"/>
<g>
<rect x="69" y="44" width="746" height="187" rx="0" ry="0" fill="rgb(255, 255, 255)"/>
<g>
<svg x="69" y="44" width="746" height="187" viewBox="0 0 746 187">
    <rect x="0" y="0" width="746" height="187" style="fill: #FFFFFF"/>
    <g>
        <line stroke="#000000" stroke-width="1" x1="0" y1="0" x2="746" y2="0"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="18" x2="746" y2="18"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="37" x2="746" y2="37"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="56" x2="746" y2="56"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="74" x2="746" y2="74"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="93" x2="746" y2="93"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="112" x2="746" y2="112"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="130" x2="746" y2="130"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="149" x2="746" y2="149"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="168" x2="746" y2="168"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="187" x2="746" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="0" y1="0" x2="0" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="74" y1="0" x2="74" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="149" y1="0" x2="149" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="223" y1="0" x2="223" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="298" y1="0" x2="298" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="373" y1="0" x2="373" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="447" y1="0" x2="447" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="522" y1="0" x2="522" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="596" y1="0" x2="596" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="671" y1="0" x2="671" y2="187"/>
        <line stroke="#000000" stroke-width="1" x1="746" y1="0" x2="746" y2="187"/>
    </g>
    </svg>
</g>
</g>
</svg>

你的viewBox是"0 0 960 768"所以这就是你能看到的。当你画

<line stroke="#000000" stroke-width="1" x1="0" y1="0" x2="746" y2="0"/>

你说你想要一个从y=-0.5到y=0.5的1像素宽的描边,因为每边有1/2的描边。然而,由于viewBox,从-0.5到0的所有笔画都被剪切了,所以你要求看到一个清晰的边缘笔画1/2像素宽,这对于UA来说是不可能的。有时UA会在viewBox内显示它,你会看到它,有时它会在viewBox外绘制它,然后你就看不到了。

如果你想使用crispEdges笔画,最好以0.5个单位绘制,例如

<line stroke="#000000" stroke-width="1" x1="0.5" y1="0.5" x2="746" y2="0.5"/>

这里有更详细的解释