Drawing grid with jQuery SVG produces 2px lines instead of 1px

后端 未结 3 804
礼貌的吻别
礼貌的吻别 2020-12-28 18:57

I know it\'s not the jQuery SVG library causing this issue, but when rendering horizontal or vertical SVG lines on integral x,y coordinates, lines are 2px wide instead of 1p

相关标签:
3条回答
  • 2020-12-28 19:12

    Just use a 1px wide rectangle with a fill (only for horizontal/vertical lines of course).

    e.g. for a full width horizontal line:

    <rect x="0" y="0" width="100%" height="1" fill="#c2c2c2"></rect>
    

    Perfect 1px wide lines every time.

    0 讨论(0)
  • 2020-12-28 19:28

    So you want one pixel wide horizontal and vertical lines to be displayed with sharp rather than fuzzy edges.

    If all the co-ordinates in your SVG are integers, maybe you can offset the whole image by 0.5, by using a transform element:

    // first group has no transform 
    // lines appear blurred, because they are centered on boundary of two pixels
    <g id = "blurred">  
        <line x1="10" y1="10" x2="110" y2="10" stroke="black" stroke-width="1"/>
        <line x1="10" y1="10" x2="10" y2="100" stroke="black" stroke-width="1"/>
    </g>
    
        // second group has half pixel transform - 
        // lines will appear sharp because they are translated to a pixel centre
        <g id="sharp" transform="translate(0.5,0.5)">
        <line x1="120" y1="10" x2="220" y2="10" stroke="black" stroke-width="1"/>
        <line x1="120" y1="10" x2="120" y2="100" stroke="black" stroke-width="1"/>
    </g>
    

    svg renders like this

    But this will not work for horizontal/vertical lines which are not defined with integer coordinates, or where another transformation moves them off the integer coordinates.

    So if this idea cannot be used to solve your problem, then you probably need a 'pixel-snapping' technique. This would analyse a line before drawing it. If the line is vertical or horizontal (after transformation) then you need to adjust the coordinates so that they will end up in a pixel center after transformation.

    You may be interested in looking at the WPF Pixel Snapping explanation for background information regarding this common problem.

    0 讨论(0)
  • 2020-12-28 19:29

    A way to solve the problem is to apply some CSS to the lines:

    .thelines{
        shape-rendering:crispEdges
    }
    

    The spec chapter on shape-rendering property.

    Basically it turns off antialiasing for the lines, and the lines that are NOT straight horizontal or vertical may NOT look very pretty.

    But probably you'd better stick to adding .5 to the lines' coordinates, because browsers do what they are told to: the line is on exact coordinates and the stroke is painted on both sides of the line, half pixel here and half pixel there.

    0 讨论(0)
提交回复
热议问题