turf-bezier
Takes a linestring and outputs a curved version of the line.
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<link rel="stylesheet" href="assets/css/styles.css" type="text/css">
<script src="//cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
<script src="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" type="text/javascript"></script>
<title>Turf and OpenLayers 3 - Bezier</title>
</head>
<body>
<div id="map" class="map"></div>
<script type="text/javascript">
// Declare a formatter to read GeoJSON
var format = new ol.format.GeoJSON();
// Declare a source
var vectorSource = new ol.source.Vector();
// Was unable to get an exact intermediate point (egal distance between both points)
var linestring = turf.lineString([
[2.3469, 48.8589],
[-40.6055, 54.3678],
[-73.9796, 40.6979]
])
var bezier = turf.bezier(linestring);
var feature_linestring = format.readFeatures(linestring, {
featureProjection: 'EPSG:3857'
});
var feature_bezier = format.readFeatures(bezier, {
featureProjection: 'EPSG:3857'
});
// Combine together two Array of ol.Feature, just to play and learn
// var pt2_pt3 = feature_pt2.concat(feature_pt3);
// Add features to the source. Behind, OpenLayers create a FeatureCollection
// vectorSource.addFeatures(pt2_pt3);
vectorSource.addFeatures(feature_bezier);
vectorSource.addFeatures(feature_linestring);
// Declare a vector layer with the already
// created source containing added features
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
// Instanciate a map and add layers
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([-1.563046, 47.214751]),
zoom: 2
})
});
</script>
</body>
</html>