leaflet-arrowheads
Leaflet-Arrowheads is a small plugin for leaflet to quickly draw arrowheads on polylines for vector visualization.
👀 DEMO 👀
Installation
Leaflet-Arrowheads compatible with leaflet 1.7.1+. It has 2 dependencies: Leaflet itself, and Leaflet GeometryUtil.
You can use npm to install leaflet-arrowheads:
npm install leaflet-arrowheads --save
Then you can simply import its content into your project:
import 'leaflet-arrowheads';
Without ES6 Imports
Grab the source file and include it in your project. You can include the source file in your header, but it must come after a link to Leaflet GeometryUtil, which must come after a link to the leaflet source. Your main project javascript will come after this, like so:
<head>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src=".../scripts/leaflet.geometryutil.js"></script>
<script src=".../scripts/leaflet-arrowheads.js"></script>
<script src=".../yourProjectScript.js" defer></script>
</head>
Usage
Arrowheads can be applied to any polyline, whether unisegmental, multisegmental, continuous, or discontinuous:
var myVector = L.polyline([coords]).arrowheads();
Arrowheads will be added to your polyline and will automatically be added to and removed from the map when you call add and remove methods on your polyline:
myVector.addTo(map) or myVector.remove()
If you need to access the arrowheads directly, you can call the .getArrowheads()
method on your polyline.
myVector.getArrowheads(); // returns the arrowheads polyline object
myVector.getArrowheads().remove(); // removes arrowheads from map
Arrowheads can also be deleted from their parent polyline entirely:
myVector.deleteArrowheads();
Arrowheads can take a configuration object as its argument:
var myVector = L.polyline([ coords ]).arrowheads({ <Options> });
You can also use arrowheads on a GeoJSON that contains LineString
or MultiLineString
features by adding it as an option:
var myGeoJson = L.geoJSON(geoJsonData, { arrowheads: { <Options> } });
Options
Arrowheads offers a variety of options for rendering and styling arrowheads. See the options table below.
Arrowheads inherit all options from L.Path. Arrowheads also inherit all options from their parent polylines, except fill
, fillOpacity
, and smoothFactor
. These can be changed manually when defining the arrowheads' options, but changing smoothFactor will result in improperly rendered arrows.
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 | String ( Meters or Percent or Pixels ) |
'15%' | Determines the size of the arrowhead. Accepts three types of values:
|
frequency | Number | String ( Number of arrowheads | Meters, Pixels, 'allvertices', 'endonly' ) |
'allvertices' | How many arrowheads are rendered on a polyline.
|
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 { |
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. |
There are many different ways to combine these various options. See examles below. Many combinations are untested, so if you encounter a problem, open and issue or a PR.
Examples
A demo project is available for viewing at https://codesandbox.io/s/leaflet-arrowheads-example-zfxxc. The web page alone without the code: https://zfxxc.csb.app/
There is also a small typescript example sandbox here.
Polylines in this demo have popups which each contain the code for that polyline. Click around, and feel free to look through the codesandbox for more detail.
Yawn Options | |||
L.polyline([]).arrowheads()(Standard option gives 60 degree yawn) |
L.polyline([]).arrowheads({ yawn: 90 }) |
||
L.polyline([]).arrowheads({ yawn: 40 }) |
.arrowheads({ yawn: 40, fill: true }) |
Alternatives
After writing this plugin I discovered 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.