如何为SVG路径平滑边缘

5

有人能帮帮我吗?我正在尝试找出如何为这个SVG路径获得漂亮、平滑的边缘(目前使用Raphael进行绘制)。这里是问题的CodePen模拟(您可能需要向下滚动以查看窗口中的对角线 - 它与顶部有点偏移)。

http://codepen.io/anon/pen/qbxYOg

<DOCTYPE html>
<body>
<div style="width:3484px; height:2000px;">
    <svg style="width:100%; height:100%;">
            <path fill="#fe0000" stroke="#fe0000" d="M606.182,649.872L739.73,670.2349999999999M743.215,670.366C742.902,669.307,741.7900000000001,668.702,740.73,669.015C739.6700000000001,669.33,739.066,670.442,739.38,671.501C739.694,672.5609999999999,740.806,673.165,741.865,672.851C742.925,672.538,743.53,671.425,743.215,670.366ZM594.333,649L463.09799999999996,628.047M459.614,627.881C459.917,628.943,461.02299999999997,629.559,462.085,629.256C463.14799999999997,628.953,463.763,627.845,463.46,626.784C463.156,625.721,462.04999999999995,625.106,460.988,625.409C459.927,625.713,459.311,626.819,459.614,627.881Z" style="transform: scale(.75); fill: red; stroke: red; stroke-width: 4px"></path>
        </svg>
    </div>
</body>
</html>  

基本上问题是线条看起来很锯齿,没有平滑/抗锯齿,我需要将这个SVG路径从100%缩放到50%的交互式地图中。缩小它会使边缘看起来很锯齿,但我只注意到对于斜线的线条存在此问题... 有人能帮我解决这个问题吗?谢谢!
2个回答

2

该线条正在被平滑处理。如果您使用屏幕放大镜或类似程序,您可以清晰地看到它。

OP 的线条的放大版

它看起来有些粗糙只是因为线条的颜色、与背景的对比度和线条的角度等因素所致。

您不太可能得到比这更好的结果。

为了比较,这里展示了您的示例中启用和关闭抗锯齿效果的区别。

<svg width="700px" height="700px">
  <path fill="#fe0000" stroke="#fe0000" d="M606.182,649.872L739.73,670.2349999999999M743.215,670.366C742.902,669.307,741.7900000000001,668.702,740.73,669.015C739.6700000000001,669.33,739.066,670.442,739.38,671.501C739.694,672.5609999999999,740.806,673.165,741.865,672.851C742.925,672.538,743.53,671.425,743.215,670.366ZM594.333,649L463.09799999999996,628.047M459.614,627.881C459.917,628.943,461.02299999999997,629.559,462.085,629.256C463.14799999999997,628.953,463.763,627.845,463.46,626.784C463.156,625.721,462.04999999999995,625.106,460.988,625.409C459.927,625.713,459.311,626.819,459.614,627.881Z" style="fill: red; stroke: red; stroke-width: 4px" transform="translate(0,-50) scale(.75)"></path>
  <path fill="#fe0000" stroke="#fe0000" shape-rendering="crispEdges" d="M606.182,649.872L739.73,670.2349999999999M743.215,670.366C742.902,669.307,741.7900000000001,668.702,740.73,669.015C739.6700000000001,669.33,739.066,670.442,739.38,671.501C739.694,672.5609999999999,740.806,673.165,741.865,672.851C742.925,672.538,743.53,671.425,743.215,670.366ZM594.333,649L463.09799999999996,628.047M459.614,627.881C459.917,628.943,461.02299999999997,629.559,462.085,629.256C463.14799999999997,628.953,463.763,627.845,463.46,626.784C463.156,625.721,462.04999999999995,625.106,460.988,625.409C459.927,625.713,459.311,626.819,459.614,627.881Z" style="transform: scale(.75); fill: red; stroke: red; stroke-width: 4px"></path>
</svg>


1
谢谢您的回复。经过昨天长时间的工作,我发现这里真正需要的是更平滑或抗锯齿的“外观”。我基本上能够在 Raphael JS 中模拟这个效果(它是渲染画布上的线条的工具),通过添加与线条相同颜色的微光来生成更多 SVG 元素,这些元素在完全相同的坐标处是更粗的线条,但透明度较低,从而为我提供了所需的抗锯齿效果(或者使线条看起来更加平滑)。
以下是由 Raphael 生成的代码,用于创建深红色实线,然后是多个 SVG 元素,具有相同颜色的更粗的描边,但透明度较低,以产生轻微的抗锯齿效果:
<path fill="none" stroke="#fe0000" d="M606.182,649.872C606.182,649.872,739.73,670.2349999999999,739.73,670.2349999999999M743.215,670.366C742.902,669.307,741.7900000000001,668.702,740.73,669.015C739.6700000000001,669.33,739.066,670.442,739.38,671.501C739.694,672.5609999999999,740.806,673.165,741.865,672.851C742.925,672.538,743.53,671.425,743.215,670.366C743.215,670.366,743.215,670.366,743.215,670.366M594.333,649C594.333,649,463.09799999999996,628.047,463.09799999999996,628.047M459.614,627.881C459.917,628.943,461.02299999999997,629.559,462.085,629.256C463.14799999999997,628.953,463.763,627.845,463.46,626.784C463.156,625.721,462.04999999999995,625.106,460.988,625.409C459.927,625.713,459.311,626.819,459.614,627.881C459.614,627.881,459.614,627.881,459.614,627.881" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" opacity="0.3" transform="matrix(1,0,0,1,0,0)" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); stroke-linejoin: round; stroke-linecap: round; opacity: 0.3;"></path>
<path fill="none" stroke="#fe0000" d="M606.182,649.872C606.182,649.872,739.73,670.2349999999999,739.73,670.2349999999999M743.215,670.366C742.902,669.307,741.7900000000001,668.702,740.73,669.015C739.6700000000001,669.33,739.066,670.442,739.38,671.501C739.694,672.5609999999999,740.806,673.165,741.865,672.851C742.925,672.538,743.53,671.425,743.215,670.366C743.215,670.366,743.215,670.366,743.215,670.366M594.333,649C594.333,649,463.09799999999996,628.047,463.09799999999996,628.047M459.614,627.881C459.917,628.943,461.02299999999997,629.559,462.085,629.256C463.14799999999997,628.953,463.763,627.845,463.46,626.784C463.156,625.721,462.04999999999995,625.106,460.988,625.409C459.927,625.713,459.311,626.819,459.614,627.881C459.614,627.881,459.614,627.881,459.614,627.881" stroke-linejoin="round" stroke-linecap="round" stroke-width="4" opacity="0.3" transform="matrix(1,0,0,1,0,0)" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); stroke-linejoin: round; stroke-linecap: round; opacity: 0.3;"></path>
<path fill="none" stroke="#fe0000" d="M606.182,649.872C606.182,649.872,739.73,670.2349999999999,739.73,670.2349999999999M743.215,670.366C742.902,669.307,741.7900000000001,668.702,740.73,669.015C739.6700000000001,669.33,739.066,670.442,739.38,671.501C739.694,672.5609999999999,740.806,673.165,741.865,672.851C742.925,672.538,743.53,671.425,743.215,670.366C743.215,670.366,743.215,670.366,743.215,670.366M594.333,649C594.333,649,463.09799999999996,628.047,463.09799999999996,628.047M459.614,627.881C459.917,628.943,461.02299999999997,629.559,462.085,629.256C463.14799999999997,628.953,463.763,627.845,463.46,626.784C463.156,625.721,462.04999999999995,625.106,460.988,625.409C459.927,625.713,459.311,626.819,459.614,627.881C459.614,627.881,459.614,627.881,459.614,627.881" stroke-linejoin="round" stroke-linecap="round" stroke-width="6" opacity="0.3" transform="matrix(1,0,0,1,0,0)" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); stroke-linejoin: round; stroke-linecap: round; opacity: 0.3;"></path>
<path fill="#fe0000" stroke="#fe0000" d="M606.182,649.872L739.73,670.2349999999999M743.215,670.366C742.902,669.307,741.7900000000001,668.702,740.73,669.015C739.6700000000001,669.33,739.066,670.442,739.38,671.501C739.694,672.5609999999999,740.806,673.165,741.865,672.851C742.925,672.538,743.53,671.425,743.215,670.366ZM594.333,649L463.09799999999996,628.047M459.614,627.881C459.917,628.943,461.02299999999997,629.559,462.085,629.256C463.14799999999997,628.953,463.763,627.845,463.46,626.784C463.156,625.721,462.04999999999995,625.106,460.988,625.409C459.927,625.713,459.311,626.819,459.614,627.881Z" fill-opacity="1" stroke-width="3" opacity="1" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); fill-opacity: 1; opacity: 1;"></path>

请注意每个笔画宽度和不透明度设置以及所有坐标都相同。 希望这次讨论能对未来的某个人有所帮助!谢谢!

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