mapbox / mapbox/mapbox-gl-js

Line-Gradient inconsistent at different zoom levels

Open
#12,698 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged bug :lady_beetle:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

**mapbox-gl-js version**: v2.14.1

**browser**: Chrome 112, Firefox 112

### Steps to Trigger Behavior

1. I have taken the example for line gradient from the official docs here: https://docs.mapbox.com/mapbox-gl-js/example/line-gradient/
2. I have changed to use a longer / more complex path (a travel across europe). Loading it from my gist here for the example:

```


Create a gradient line using an expression




body {
margin: 0;
padding: 0;
}

#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}

mapboxgl.accessToken = <your-api-key>;

const map = new mapboxgl.Map({
container: 'map',
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/light-v11',
center: [11.3372, 49.46892],
zoom: 12
});

fetch("https://gist.githubusercontent.com/user1408/5335987307ad92cf61722d2de3c6439f/raw/ef68e6a029b91cabffc6d2cfd21c364847852e55/longTravelEu.json")
.then(response => response.json())
.then(json => {
const geojson = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {},
'geometry': {
'coordinates': json.route,
'type': 'LineString'
}
}
]
};

map.on('load', () => {
// 'line-gradient' can only be used with GeoJSON sources
// and the source must have the 'lineMetrics' option set to true
map.addSource('line', {
type: 'geojson',
lineMetrics: true,
data: geojson
});

// the layer must be of type 'line'
map.addLayer({
type: 'line',
source: 'line',
id: 'line',
paint: {
'line-color': 'red',
'line-width': 14,
// 'line-gradient' must be specified using an expression
// with the special 'line-progress' property
'line-gradient': [
'interpolate',
['linear'],
['line-progress'],
0,
'blue',
0.1,
'royalblue',
0.3,
'cyan',
0.5,
'lime',
0.7,
'yellow',
1,
'red'
]
},
layout: {
'line-cap': 'round',
'line-join': 'round'
}
});

});
})
.catch(err => console.error(err));

```

### Link to Demonstration

https://codepen.io/user1408-the-styleful/pen/Jjmpmyy

### Expected Behavior

Line-gradient that
* is consistent at all zoom levels
* is smooth

### Actual Behavior

The gradient changes drastically at different zoom levels:
![image](https://user-images.githubusercontent.com/16258712/236797884-df268458-1021-42df-867c-1e20375f3474.png)

![image](https://user-images.githubusercontent.com/16258712/236797744-f24d11c7-4869-4948-98b1-1e1a2ccfa02c.png)

![image](https://user-images.githubusercontent.com/16258712/236797982-fea426c3-6006-48d8-90df-b476a4d82899.png)

The color on the line changes w/o smooth gradient at certain positions:
![image](https://user-images.githubusercontent.com/16258712/236797388-745f377f-660a-4176-ad23-a161a60dc339.png)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked CodePen reproduction with the official line-gradient example and the longer route, then compare the gradient at different zoom levels. Trace the line-gradient rendering path in mapbox-gl-js and verify the result against the reported expectation: a smooth, consistent gradient at all zoom levels.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
computer-graphics, frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.