Last active
March 4, 2022 04:24
-
-
Save malwoodsantoro/091d3400ee8c9f09b1a0750974762bdc to your computer and use it in GitHub Desktop.
Display a bézier curve using Turf.js
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>Display a map on a webpage</title> | |
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"> | |
<link href="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css" rel="stylesheet"> | |
<script src="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js"></script> | |
<script src='https://unpkg.com/@turf/[email protected]/turf.min.js'></script> | |
<style> | |
body { margin: 0; padding: 0; } | |
#map { position: absolute; top: 0; bottom: 0; width: 100%; } | |
</style> | |
</head> | |
<body> | |
<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/streets-v11', // style URL | |
center: [-0.73821, 17.35132], // starting position [lng, lat] | |
zoom: 0 // starting zoom | |
}); | |
var line = turf.lineString([ | |
[-89.296875, 38.54816], | |
[3.1640625, | |
60.930432202923335], | |
[95.2734375, 35.17380] | |
]); | |
var curved = turf.bezierSpline(line, {sharpness: 1}); | |
console.log(curved) | |
map.on('load', function () { | |
map.addSource('route', { | |
'type': 'geojson', | |
'data': curved | |
}); | |
map.addLayer({ | |
'id': 'route', | |
'type': 'line', | |
'source': 'route', | |
'layout': { | |
'line-join': 'round', | |
'line-cap': 'round' | |
}, | |
'paint': { | |
'line-color': '#888', | |
'line-width': 4 | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment