Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hit detection support to WebGL vector layer #15120

Merged
merged 8 commits into from Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/webgl-vector-layer.html
Expand Up @@ -8,3 +8,4 @@
experimental: true
---
<div id="map" class="map"></div>
<div id="info">&nbsp;</div>
45 changes: 45 additions & 0 deletions examples/webgl-vector-layer.js
Expand Up @@ -42,3 +42,48 @@ const map = new Map({
zoom: 1,
}),
});

const featureOverlay = new WebGLLayer({
source: new VectorSource(),
map: map,
style: {
'stroke-color': 'rgba(255, 255, 255, 0.7)',
'stroke-width': 2,
},
});

let highlight;
const displayFeatureInfo = function (pixel) {
const feature = map.forEachFeatureAtPixel(pixel, function (feature) {
return feature;
});

const info = document.getElementById('info');
if (feature) {
info.innerHTML = feature.get('ECO_NAME') || '&nbsp;';
} else {
info.innerHTML = '&nbsp;';
}

if (feature !== highlight) {
if (highlight) {
featureOverlay.getSource().removeFeature(highlight);
}
if (feature) {
featureOverlay.getSource().addFeature(feature);
}
highlight = feature;
}
};

map.on('pointermove', function (evt) {
if (evt.dragging) {
return;
}
const pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(pixel);
});

map.on('click', function (evt) {
displayFeatureInfo(evt.pixel);
});
1 change: 0 additions & 1 deletion site/src/doc/errors/index.md
Expand Up @@ -225,7 +225,6 @@ Layer opacity must be a number.
### 66

`forEachFeatureAtCoordinate` cannot be used on a WebGL layer if the hit detection logic has not been enabled.
This is done by providing adequate shaders using the `hitVertexShader` and `hitFragmentShader` properties of `WebGLPointsLayerRenderer`.

### 67

Expand Down