teastman / Leaflet.pattern

Plugin for leaflet that allows for use of fill patterns in Paths.
BSD 2-Clause "Simplified" License
124 stars 59 forks source link

Leaflet.pattern

Provides the ability to use SVG patterns as backgrounds for Leaflet Paths.

Requires Leaflet 0.7.0 or newer.

Usage Examples

You can define a pattern in 2 ways.

  1. Using a pre-defined provided pattern.
  2. Creating a custom pattern.

In either case the Pattern object needs to be initialized. This can be done as follows.

var pattern = new L.Pattern({options});

Options All custom and pre-defined patterns can make use of the following options.

Pre-Built Patterns

Pre-Built patterns are just an easier way to use some common patterns. Pre-Built patterns typically have their own special options, but all can use the base options mentioned above.

Stripes

var stripes = new L.StripePattern({options}); stripes.addTo(map);

Options

Usage

Once the pre-built patterns are defined you can use them by adding them as the fill pattern property of any Path in leaflet.

var circle = new L.Circle({LatLng}, {radius}, { fillPattern: stripes, fillOpacity: 1.0}); circle.addTo(_map);

Custom Patterns

To create custom patterns you must first create some shapes to define what the pattern looks like.

Shapes

Options

All shapes have the following options.

Path

var shape = new L.PatternPath({ d: 'M10 0 L7 20 L25 20 Z', fill: true });

Options

Circle

var shape = new L.PatternCircle({ x: 12, y: 12, radius: 10, fill: true });

Options

Rectangle

var shape = new L.PatternRect({ x: 5, y: 5, width: 40, height: 40, rx: 10, ry: 10, fill: true });

Options

Usage

Once the paths are defined you can use them by adding them to a Pattern.

var pattern = new L.Pattern({options}); pattern.addShape(shape); pattern.addTo(map);

Finally you can now use the pattern in the fill pattern property of any Path in leaflet.

var circle = new L.Circle({LatLng}, {radius}, { fillPattern: pattern, fillOpacity: 1.0}); circle.addTo(_map);