A marker is a reusable graphic typically used in charts, graphs, or arrows to indicate the beginning, middle, and end of a path, line, polygon, or polyline. In the image below, the square start point, the smaller circular polymarkers, and the arrow head are all markers.
Markers are re-usable elements, so they are usually defined in the defs section.
<defs>
<marker>
// marker code goes here
</marker>
</defs>
The marker itself can be anything - a simple shape element or a complex group. The shape that we use must be defined as a child of the marker element. In this example we'll just use a circle as the marker
<marker>
<circle cx="5" cy="5" r="2.5" fill="black" />
</marker>
The marker id is used to identify the marker to be used on the marked object. Here we'll just create an id of "ball"
<marker id="ball">
<circle cx="5" cy="5" r="2.5" fill="black" />
</marker>
The refX and refY attributes define the reference point of the marker. It's the point at which the line intersects with the marker. Since we want it to be attached to the center of the ball, we'll make refX = cx and refY = cy.
<marker id="ball"
refX="5" refY="5">
<circle cx="5" cy="5" r="2.5" fill="black" />
</marker>
The markerWidth and markerHeight are the width and height of the marker. Usually the refX and refY points are at the center of the marker width and height values.
<marker id="ball"
refX="5" refY="5"
markerWidth="10" markerHeight="10">
<circle cx="5" cy="5" r="2.5" fill="black" />
</marker>
Markers are attached using marker-start (the beginning of the polyline/polygon/etc), marker-mid (the middle points, if any), and marker-end (the end of the line) properties
Attaches the marker with the designated id (url(#box) in this case) to the first point in a polyline.
1
2
3
4
<polyline
marker-start="url(#box)"
points="50,150 150,150 250,150 350,150 450,150"
stroke="black" stroke-width="2" fill="none" />
Attaches a marker to all of the points between the start and end points of a polyline.
1
2
3
4
<polyline
marker-mid="url(#ball)"
points="50,150 150,150 250,150 350,150 450,150"
stroke="black" stroke-width="2" fill="none" />
Attaches the marker to the last point of the polyline.
1
2
3
4
<polyline
marker-end="url(#arrowhead)"
points="50,150 150,150 250,150 350,150 450,150"
stroke="black" stroke-width="2" fill="none" />
1
2
3
4
5
6
<polyline
marker-start="url(#box)"
marker-end="url(#arrowhead)"
marker-mid="url(#ball)"
points="50,150 150,150 250,150 350,150 450,150"
stroke="black" stroke-width="2" fill="none" />
Marker orientation is best explained with an example. Let's say we want to create an arrow that points to the upper right hand corner of the svg
The arrowhead is clearly pointing in the wrong direction. To get it pointed so that it is aligned with the arrow body, we have to use the orient attribute for the marker element.
auto - marker is aligned with the line
auto-start-reverse - for marker-start, the marker is aligned backwards.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<defs>
<marker id="arrowhead"
orient="auto-start-reverse"
refX="5" refY="5"
markerWidth="10" markerHeight="10">
<polygon
points="0,0 10,5 0,10" fill="black" stroke="black" />
</marker>
</defs>
<polyline
marker-start="url(#arrowhead)"
marker-end="url(#arrowhead-3)"
stroke="black" strokeWidth="2"
fill="none"
points="50, 250 450,50" />
degrees - the marker set at an angle of the specified number of degrees
1
2
3
4
5
<marker
id="arrowhead"
orient="25deg"
refX="5" refY="5"
markerWidth="10" markerHeight="10">