Last active
March 4, 2022 04:50
-
-
Save malwoodsantoro/ad8574d8fc678bf1347b7c4f6025589c to your computer and use it in GitHub Desktop.
Use draw plugin and Turf.js to add white mask around drawn polygon
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>Show drawn polygon area</title> | |
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> | |
<script src="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js"></script> | |
<link href="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css" rel="stylesheet" /> | |
<style> | |
body { | |
margin: 0; | |
padding: 0; | |
} | |
#map { | |
position: absolute; | |
top: 0; | |
bottom: 0; | |
width: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<style> | |
.calculation-box { | |
height: 75px; | |
width: 150px; | |
position: absolute; | |
bottom: 40px; | |
left: 10px; | |
background-color: rgba(255, 255, 255, 0.9); | |
padding: 15px; | |
text-align: center; | |
} | |
p { | |
font-family: 'Open Sans'; | |
margin: 0; | |
font-size: 13px; | |
} | |
</style> | |
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script> | |
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.0/mapbox-gl-draw.js"></script> | |
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.0/mapbox-gl-draw.css" | |
type="text/css" /> | |
<div id="map"></div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w'; | |
var map = new mapboxgl.Map({ | |
container: 'map', // container id | |
style: 'mapbox://styles/mapbox/satellite-v9', //hosted style id | |
center: [-91.874, 42.76], // starting position | |
zoom: 12 // starting zoom | |
}); | |
var draw = new MapboxDraw({ | |
displayControlsDefault: false, | |
controls: { | |
polygon: true, | |
trash: true | |
} | |
}); | |
map.addControl(draw); | |
map.on('draw.create', updateArea); | |
map.on('draw.delete', removeMask); | |
//map.on('draw.update', updateArea); | |
function updateArea(e) { | |
var data = draw.getAll(); | |
if (data.features.length > 0) { | |
var masked = turf.mask(data); | |
map.addSource('mask-source', { | |
'type': 'geojson', | |
'data': masked | |
} | |
); | |
map.addLayer({ | |
'id': 'mask', | |
'type': 'fill', | |
'source': 'mask-source', | |
'layout': {}, | |
'paint': { | |
'fill-color': '#fff' | |
} | |
}); | |
} | |
} | |
function removeMask(e) { | |
if (map.getLayer('mask')) map.removeLayer('mask'); | |
if (map.getSource('mask-source')) map.removeSource('mask-source'); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment