Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions debug/query-raster.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html>
<head>
<title>Mapbox GL JS debug page</title>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no"
/>
<link rel="stylesheet" href="../dist/mapbox-gl.css" />
<style>
body {
margin: 0;
padding: 0;
}
html,
body,
#map {
height: 100%;
}
#swatch {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
}
</style>
</head>

<body>
<div id="map"></div>
<div id="swatch"></div>

<script src="../dist/mapbox-gl-dev.js"></script>
<script src="../debug/access_token_generated.js"></script>
<script>
var map = (window.map = new mapboxgl.Map({
container: "map",
zoom: 12.5,
center: [-77.01866, 38.888],
style: "mapbox://styles/mapbox/satellite-v9",
hash: true,
}));

map.on("load", function () {

map.on("mousemove", (event) => {
const pixel = map.colorForLayerPoint(
"satellite",
event.point
);

document.getElementById(
"swatch"
).style.backgroundColor = `rgba(${pixel[0]},${pixel[1]},${pixel[2]},${pixel[3]})`;
});
});
</script>
</body>
</html>
55 changes: 55 additions & 0 deletions src/render/painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,61 @@ class Painter {
}
}

colorForLayerPoint(layerId: string, point: PointLike) {

const layer = this.style._layers[layerId];
if (layer.type !== 'raster') { return }

const sourceCache = this.style._getLayerSourceCache(layer);

sourceCache.prepare(this.context);

const coordsAscending: Array<OverscaledTileID> = sourceCache.getVisibleCoordinates();
const coordsDescending: Array<OverscaledTileID> = coordsAscending.slice().reverse();

// Rebind the main framebuffer now that all offscreen layers have been rendered:
// this.context.bindFramebuffer.set(null);
this.context.viewport.set([0, 0, this.width, this.height]);

this.context.clear({
color: 'rgba(0,0,0,0)', //options.showOverdrawInspector ? Color.black : clearColor,
depth: 1,
});
this.clearStencil();

this._showOverdrawInspector = false; //options.showOverdrawInspector;

this.renderPass = "translucent";

// For symbol layers in the translucent pass, we add extra tiles to the renderable set
// for cross-tile symbol fading. Symbol layers don't use tile clipping, so no need to render
// separate clipping masks
const coords = sourceCache
? coordsDescending
: undefined;

this._renderTileClippingMasks(
layer,
sourceCache,
sourceCache ? coordsAscending : undefined
);
this.renderLayer(this, sourceCache, layer, coords);

const gl = this.context.gl;
var pixel = new Uint8Array(4);
gl.readPixels(
point.x * window.devicePixelRatio,
this.height - point.y * window.devicePixelRatio,
1,
1,
gl.RGBA,
gl.UNSIGNED_BYTE,
pixel
);

return pixel;
}

renderLayer(painter: Painter, sourceCache?: SourceCache, layer: StyleLayer, coords?: Array<OverscaledTileID>) {
if (layer.isHidden(this.transform.zoom)) return;
if (layer.type !== 'background' && layer.type !== 'sky' && layer.type !== 'custom' && !(coords && coords.length)) return;
Expand Down
4 changes: 4 additions & 0 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,10 @@ class Map extends Camera {

/** @section {Querying features} */

colorForLayerPoint(layerId: string, point: PointLike) {
return this.painter.colorForLayerPoint(layerId, point)
}

/**
* Returns an array of [GeoJSON](http://geojson.org/)
* [Feature objects](https://tools.ietf.org/html/rfc7946#section-3.2)
Expand Down