Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unclosed SVG path appears to be closed

I am writing some stuff with d3 and I came across a a bizarre problem. Paths that are closed end with a 'Z' character, but every path I make closes ( and fills ) regardless of whether I include a Z or not. Even in isolation with examples copied from the spec this happens. For example:

<svg>     <path d="M 40 60 L 10 60 L 40 42.68 M 60 60 L 90 60 L 60 42.68"/> </svg> 

I am baffled as to what the problem could be. I would appreciate any insight.

like image 897
ballaw Avatar asked Apr 18 '12 15:04

ballaw


People also ask

How do I close an SVG path?

The "closepath" (Z or z) ends the current subpath by connecting it back to its initial point. An automatic straight line is drawn from the current point to the initial point of the current subpath. This path segment may be of zero length.

How does SVG path work?

It is often placed at the end of a path node, although not always. There is no difference between the uppercase and lowercase command. The path will move to point ( 10 , 10 ) and then move horizontally 80 points to the right, then 80 points down, then 80 points to the left, and then back to the start.

What is fill rule in SVG?

The fill-rule attribute is a presentation attribute defining the algorithm to use to determine the inside part of a shape. Note: As a presentation attribute, fill-rule can be used as a CSS property. You can use this attribute with the following SVG elements: <altGlyph> <path>


2 Answers

The relevant line from the SVG specification, regarding filling paths:

The fill operation fills open subpaths by performing the fill operation as if an additional "closepath" command were added to the path to connect the last point of the subpath with the first point of the subpath.

So, as far as the fill is concerned, there's an implicit "Z" at the end. However, for the stroke, there's no implied closure, so it won't draw a stroke connecting the last point to the first point unless you explicitly add a "Z".

If you only want to apply a stroke, use CSS to disable the fill:

path {   fill: none;   stroke: #000;   stroke-width: 1.5px; } 

(I see you answered your own question, but I thought others might still find an explanation useful.)

like image 120
mbostock Avatar answered Oct 14 '22 04:10

mbostock


I up-voted the answer, but sometimes css is not an option when you try to convert the svg to canvas and would like to prevent the svg path from filling or closing. Equivalent to the css solution but with no css...

<svg fill="white" fill-opacity="0" stroke="#000" stroke-width="1.5">     <path d="M 40 60 L 10 60 L 40 42.68 M 60 60 L 90 60 L 60 42.68"/> </svg> 
like image 45
SYK Avatar answered Oct 14 '22 02:10

SYK