Last active
March 4, 2022 04:58
-
-
Save malwoodsantoro/a99733661dad54b3d2965954dbf89b36 to your computer and use it in GitHub Desktop.
Line segment gradients
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" /> | |
<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> | |
<div id="map"></div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w'; | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/streets-v11', | |
center: [-70.24761, 43.66783], | |
zoom: 16 | |
}); | |
map.on('load', function () { | |
var gradientObject = { 'segment-one': ['#3AB1BA', '#92E9F0'], 'segment-two': ['#BB3131', '#F09E9E'], 'segment-three': ['#C3C332', '#FAFAC8'] } | |
map.addSource('segment-one', { | |
'type': 'geojson', | |
'lineMetrics': true, | |
'data': { | |
"type": "Feature", | |
"properties": { "test": "one" }, | |
"geometry": { | |
"type": "LineString", | |
"coordinates": [ | |
[ | |
-70.24357795715332, | |
43.667421484858984 | |
], | |
[ | |
-70.24492040276527, | |
43.668323672173734 | |
], | |
[ | |
-70.24628162384033, | |
43.667250746808946 | |
] | |
] | |
} | |
} | |
}); | |
map.addSource('segment-two', { | |
'type': 'geojson', | |
'lineMetrics': true, | |
'data': { | |
"type": "Feature", | |
"properties": { "test": "two" }, | |
"geometry": { | |
"type": "LineString", | |
"coordinates": [ | |
[ | |
-70.24628162384033, | |
43.667250746808946 | |
], | |
[ | |
-70.24744302034378, | |
43.668063688402846 | |
], | |
[ | |
-70.24799019098282, | |
43.66761938517289 | |
] | |
] | |
} | |
} | |
}); | |
map.addSource('segment-three', { | |
'type': 'geojson', | |
'lineMetrics': true, | |
'data': { | |
"type": "Feature", | |
"properties": { "test": "three" }, | |
"geometry": { | |
"type": "LineString", | |
"coordinates": [ | |
[ | |
-70.24799019098282, | |
43.66761938517289 | |
], | |
[ | |
-70.2495351433754, | |
43.668694244109616 | |
], | |
[ | |
-70.25055438280106, | |
43.667908473674466 | |
], | |
[ | |
-70.25160312652586, | |
43.66886497805366 | |
] | |
] | |
} | |
} | |
}); | |
for (const property in gradientObject) { | |
map.addLayer({ | |
'id': property, | |
'type': 'line', | |
'source': property, | |
'layout': { | |
'line-join': 'round', | |
'line-cap': 'round' | |
}, | |
'paint': { | |
'line-width': 8, | |
'line-gradient': [ | |
'interpolate', | |
['linear'], | |
['line-progress'], | |
0, | |
gradientObject[property][0], | |
1, | |
gradientObject[property][1] | |
] | |
} | |
}); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment