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

时间:2013-04-09 13:26:45

标签: javascript html5 google-chrome firefox svg

为了创建SVG,我在SVG中绘制了几行。 问题是它在Chrome和Firefox中看起来有所不同。

Chrome:未绘制最后一行 Firefox:Firts line not drawn

BTW:互联网浏览器版本看起来很模糊,但这不是主要问题。

那么谁现在呢?

我做错了什么。

为您提供一些背景信息:我通常是从JavaScript动态绘制此网格。我是否必须编写丑陋的黑客来处理这些不同的SVG浏览器呈现行为? (我不想使用任何库,只需简单的JavScript)

在此处查看此codepen.io: 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>

1 个答案:

答案 0 :(得分:1)

您的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"/>

Theres a fuller explanation here