Last active
January 4, 2016 13:15
-
-
Save wboykinm/43c12f434d6bb47cd913 to your computer and use it in GitHub Desktop.
Bjorn's Mask Plugin for Leaflet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Leaflet Mask</title> | |
<link rel="stylesheet" href="https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.css" /> | |
<style type="text/css"> | |
html, body, #map { | |
width: 100%; | |
height: 100%; | |
margin: 0; | |
background: #fff; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<script src="https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.js"></script> | |
<script type="text/javascript" src="L.Mask.js"></script> | |
<script type="text/javascript"> | |
var map = L.map('map').setView([59.91, 10.65], 10); | |
L.tileLayer('http://{s}.tile.stamen.com/watercolor/{z}/{x}/{y}.png', { | |
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>.' | |
}).addTo(map); | |
L.mask([[59.81, 10.3], [59.99, 11]]).addTo(map); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
L.Mask = L.Rectangle.extend({ | |
options: { | |
stroke: false, | |
color: '#333', | |
fillOpacity: 0.5, | |
clickable: true, | |
outerBounds: new L.LatLngBounds([-90, -360], [90, 360]) | |
}, | |
initialize: function (latLngBounds, options) { | |
L.Polygon.prototype.initialize.call(this, [this._boundsToLatLngs(this.options.outerBounds), this._boundsToLatLngs(latLngBounds)], options); | |
}, | |
getLatLngs: function () { | |
return this._holes[0]; | |
}, | |
setLatLngs: function (latlngs) { | |
this._holes[0] = this._convertLatLngs(latlngs); | |
return this.redraw(); | |
}, | |
setBounds: function (latLngBounds) { | |
this._holes[0] = this._boundsToLatLngs(latLngBounds); | |
return this.redraw(); | |
} | |
}); | |
L.mask = function (latLngBounds, options) { | |
return new L.Mask(latLngBounds, options); | |
}; | |
if (L.Draw && L.Draw.SimpleShape) { | |
L.Draw.Mask = L.Draw.SimpleShape.extend({ | |
statics: { | |
TYPE: 'mask' | |
}, | |
options: { | |
shapeOptions: { | |
stroke: false, | |
color: '#333', | |
fillOpacity: 0.5, | |
clickable: true | |
} | |
}, | |
initialize: function (map, options) { | |
this.type = L.Draw.Mask.TYPE; | |
this._initialLabelText = 'Click and drag to draw mask'; | |
L.Draw.SimpleShape.prototype.initialize.call(this, map, options); | |
}, | |
_drawShape: function (latlng) { | |
if (!this._shape) { | |
this._shape = new L.Mask(new L.LatLngBounds(this._startLatLng, latlng), this.options.shapeOptions); | |
this._map.addLayer(this._shape); | |
} else { | |
this._shape.setBounds(new L.LatLngBounds(this._startLatLng, latlng)); | |
} | |
}, | |
_fireCreatedEvent: function () { | |
var mask = new L.Mask(this._shape.getBounds(), this.options.shapeOptions); | |
L.Draw.SimpleShape.prototype._fireCreatedEvent.call(this, mask); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment