Option |
Type |
Default |
Description |
yawn |
Number ( Degrees ) |
60 |
Defines the width of the opening of the arrowhead, given in degrees. The larger the angle, the wider the arrowhead. |
size |
Number | String
( Meters | Percent or Pixels ) |
'15%' |
Determines the size of the arrowhead. Accepts three types of values:
- A number will set the size of the arrowhead to that number of meters
- A string value which is a number with a percent sign ( '15%', '20%', '25%', etc. ) will render arrows whose size is that percentage of the size of the parent polyline. If the polyline has multiple segments, 'size' will take the percent of the average size of the segments.
- A string value which is a number with the suffix 'px' ( '20px', '25px', '30px', etc. ) will render an arrowhead whose size stays at a constant pixel value, regardless of zoom level. Will look strange at low zoom levels or for smaller parent vectors. Ideal for larger parent vectors and at higher zoom levels.
|
frequency |
Number | String
( Number of arrowheads | Meters, Pixels, 'allvertices', 'endonly' ) |
'allvertices' |
How many arrowheads are rendered on a polyline.
- 'allvertices' renders an arrowhead on each vertex.
- 'endonly' renders only one at the end.
- A number value renders that number of arrowheads evenly spaces across the polyline.
- A string value with suffix 'm' (i.e.
'100m' ) will render arrowheads spaced evenly along the polyline with roughly that many meters between each one.
- A string value with suffix 'px' (i.e.
'30px' ) will render arrowheads spaced evenly with roughly that many pixels between each, regardless of zoom level.
|
proportionalToTotal |
Boolean |
false |
Only relevant when size is given as a percent. Useful when frequency is set to 'endonly' . Will render the arrowhead(s) with a size proportional to the entire length of the multi-segmented polyline, rather than proportional to the average length of all the segments. |
offsets |
Object { start?: string; end?: string } |
undefined |
Enables the developer to have the arrowheads start or end at some offset from the start and/or end of the polyline. This option can contain one or both start and end properties. Each must be a string defining the size of the offset in either meters or pixels (i.e. '100m' , '15px' , etc.) |
perArrowheadOptions |
Function (i: number) => ArrowheadOptions |
undefined |
Enables the developer to customize arrowheads on a one-by-one basis. Must be in the form of a function of i , which is the index of the arrowhead as it is rendered in the loop through all arrowheads. Must return an object that is options object, the same type of options object that is the agrument for .arrowheads({ <Options> }) . Cannnot account for frequency or proportionalToTotal from within the perArrowheadOptions callback. See examples for details. |
Per-Arrowhead Options |
L.polyline([coords], { color: 'black', weight: '2' })
.arrowheads({
frequency: '500m',
color: 'darkblue',
perArrowheadOptions: (i) => ({
size: i % 3 === 0 ? '30%' : '15%',
color: i % 2 === 0 ? 'red' : undefined,
fill: (i + 1) % 4 === 0,
yawn: (i + 1) % 4 === 0 ? 35 : undefined,
}),
});
|
L.polyline([coords])
.arrowheads({
size: '20px',
fill: true,
yawn: 30,
frequency: 20,
perArrowheadOptions: (i) => ({
color: `rgba(150, 20, ${0 + 20 * i}, 1)`,
}),
});
|
## Alternatives
After writing this plugin I discovered [Leaflet.PolylineDecorator](https://github.com/bbecquet/Leaflet.PolylineDecorator). This offers some great methods to decorate your lines, potentially with arrowheads.
## Limitations
Arrowheads sometimes look like they're in slightly the wrong orientation in areas of high curvature. This is because of the way leaflet-arrowheads chooses and interpolates the points that it uses to calculate bearings. This may be able to be improved. Feel free to contribute / open a PR.