Support hitTolerance in forEachLayerAtPixel

This commit is contained in:
simonseyock
2017-09-23 15:05:24 +02:00
committed by Tim Schaub
parent 3b64641e5a
commit fcc7d87b06
7 changed files with 100 additions and 22 deletions

View File

@@ -612,29 +612,22 @@ PluggableMap.prototype.getFeaturesAtPixel = function(pixel, opt_options) {
* [R, G, B, A] pixel values (0 - 255) and will be `null` for layer types * [R, G, B, A] pixel values (0 - 255) and will be `null` for layer types
* that do not currently support this argument. To stop detection, callback * that do not currently support this argument. To stop detection, callback
* functions can return a truthy value. * functions can return a truthy value.
* @param {S=} opt_this Value to use as `this` when executing `callback`. * @param {module:ol/PluggableMap~AtPixelOptions=} opt_options Configuration options.
* @param {(function(this: U, module:ol/layer/Layer): boolean)=} opt_layerFilter Layer
* filter function. The filter function will receive one argument, the
* {@link module:ol/layer/Layer layer-candidate} and it should return a boolean
* value. Only layers which are visible and for which this function returns
* `true` will be tested for features. By default, all visible layers will
* be tested.
* @param {U=} opt_this2 Value to use as `this` when executing `layerFilter`.
* @return {T|undefined} Callback result, i.e. the return value of last * @return {T|undefined} Callback result, i.e. the return value of last
* callback execution, or the first truthy callback return value. * callback execution, or the first truthy callback return value.
* @template S,T,U * @template S,T
* @api * @api
*/ */
PluggableMap.prototype.forEachLayerAtPixel = function(pixel, callback, opt_this, opt_layerFilter, opt_this2) { PluggableMap.prototype.forEachLayerAtPixel = function(pixel, callback, opt_options) {
if (!this.frameState_) { if (!this.frameState_) {
return; return;
} }
const thisArg = opt_this !== undefined ? opt_this : null; const options = opt_options || {};
const layerFilter = opt_layerFilter !== undefined ? opt_layerFilter : TRUE; const hitTolerance = options.hitTolerance !== undefined ?
const thisArg2 = opt_this2 !== undefined ? opt_this2 : null; opt_options.hitTolerance * this.frameState_.pixelRatio : 0;
const layerFilter = options.layerFilter || TRUE;
return this.renderer_.forEachLayerAtPixel( return this.renderer_.forEachLayerAtPixel(
pixel, this.frameState_, callback, thisArg, pixel, this.frameState_, hitTolerance, callback, null, layerFilter, null);
layerFilter, thisArg2);
}; };

View File

@@ -181,6 +181,7 @@ MapRenderer.prototype.forEachFeatureAtCoordinate = function(coordinate, frameSta
* @abstract * @abstract
* @param {module:ol~Pixel} pixel Pixel. * @param {module:ol~Pixel} pixel Pixel.
* @param {module:ol/PluggableMap~FrameState} frameState FrameState. * @param {module:ol/PluggableMap~FrameState} frameState FrameState.
* @param {number} hitTolerance Hit tolerance in pixels.
* @param {function(this: S, module:ol/layer/Layer, (Uint8ClampedArray|Uint8Array)): T} callback Layer * @param {function(this: S, module:ol/layer/Layer, (Uint8ClampedArray|Uint8Array)): T} callback Layer
* callback. * callback.
* @param {S} thisArg Value to use as `this` when executing `callback`. * @param {S} thisArg Value to use as `this` when executing `callback`.
@@ -192,7 +193,7 @@ MapRenderer.prototype.forEachFeatureAtCoordinate = function(coordinate, frameSta
* @return {T|undefined} Callback result. * @return {T|undefined} Callback result.
* @template S,T,U * @template S,T,U
*/ */
MapRenderer.prototype.forEachLayerAtPixel = function(pixel, frameState, callback, thisArg, MapRenderer.prototype.forEachLayerAtPixel = function(pixel, frameState, hitTolerance, callback, thisArg,
layerFilter, thisArg2) {}; layerFilter, thisArg2) {};

View File

@@ -119,7 +119,7 @@ IntermediateCanvasRenderer.prototype.forEachFeatureAtCoordinate = function(coord
/** /**
* @inheritDoc * @inheritDoc
*/ */
IntermediateCanvasRenderer.prototype.forEachLayerAtCoordinate = function(coordinate, frameState, callback, thisArg) { IntermediateCanvasRenderer.prototype.forEachLayerAtCoordinate = function(coordinate, frameState, hitTolerance, callback, thisArg) {
if (!this.getImage()) { if (!this.getImage()) {
return undefined; return undefined;
} }
@@ -147,4 +147,5 @@ IntermediateCanvasRenderer.prototype.forEachLayerAtCoordinate = function(coordin
} }
} }
}; };
export default IntermediateCanvasRenderer; export default IntermediateCanvasRenderer;

View File

@@ -101,14 +101,15 @@ CanvasLayerRenderer.prototype.dispatchComposeEvent_ = function(type, context, fr
/** /**
* @param {module:ol/coordinate~Coordinate} coordinate Coordinate. * @param {module:ol/coordinate~Coordinate} coordinate Coordinate.
* @param {module:ol/PluggableMap~FrameState} frameState FrameState. * @param {module:ol/PluggableMap~FrameState} frameState FrameState.
* @param {number} hitTolerance Hit tolerance in pixels.
* @param {function(this: S, module:ol/layer/Layer, (Uint8ClampedArray|Uint8Array)): T} callback Layer * @param {function(this: S, module:ol/layer/Layer, (Uint8ClampedArray|Uint8Array)): T} callback Layer
* callback. * callback.
* @param {S} thisArg Value to use as `this` when executing `callback`. * @param {S} thisArg Value to use as `this` when executing `callback`.
* @return {T|undefined} Callback result. * @return {T|undefined} Callback result.
* @template S,T,U * @template S,T,U
*/ */
CanvasLayerRenderer.prototype.forEachLayerAtCoordinate = function(coordinate, frameState, callback, thisArg) { CanvasLayerRenderer.prototype.forEachLayerAtCoordinate = function(coordinate, frameState, hitTolerance, callback, thisArg) {
const hasFeature = this.forEachFeatureAtCoordinate(coordinate, frameState, 0, TRUE, this); const hasFeature = this.forEachFeatureAtCoordinate(coordinate, frameState, hitTolerance, TRUE, this);
if (hasFeature) { if (hasFeature) {
return callback.call(thisArg, this.getLayer(), null); return callback.call(thisArg, this.getLayer(), null);

View File

@@ -182,7 +182,7 @@ CanvasMapRenderer.prototype.renderFrame = function(frameState) {
/** /**
* @inheritDoc * @inheritDoc
*/ */
CanvasMapRenderer.prototype.forEachLayerAtPixel = function(pixel, frameState, callback, thisArg, CanvasMapRenderer.prototype.forEachLayerAtPixel = function(pixel, frameState, hitTolerance, callback, thisArg,
layerFilter, thisArg2) { layerFilter, thisArg2) {
let result; let result;
const viewState = frameState.viewState; const viewState = frameState.viewState;
@@ -201,7 +201,7 @@ CanvasMapRenderer.prototype.forEachLayerAtPixel = function(pixel, frameState, ca
if (visibleAtResolution(layerState, viewResolution) && layerFilter.call(thisArg2, layer)) { if (visibleAtResolution(layerState, viewResolution) && layerFilter.call(thisArg2, layer)) {
const layerRenderer = /** @type {module:ol/renderer/canvas/Layer} */ (this.getLayerRenderer(layer)); const layerRenderer = /** @type {module:ol/renderer/canvas/Layer} */ (this.getLayerRenderer(layer));
result = layerRenderer.forEachLayerAtCoordinate( result = layerRenderer.forEachLayerAtCoordinate(
coordinate, frameState, callback, thisArg); coordinate, frameState, hitTolerance, callback, thisArg);
if (result) { if (result) {
return result; return result;
} }

View File

@@ -557,7 +557,7 @@ WebGLMapRenderer.prototype.hasFeatureAtCoordinate = function(coordinate, frameSt
/** /**
* @inheritDoc * @inheritDoc
*/ */
WebGLMapRenderer.prototype.forEachLayerAtPixel = function(pixel, frameState, callback, thisArg, WebGLMapRenderer.prototype.forEachLayerAtPixel = function(pixel, frameState, hitTolerance, callback, thisArg,
layerFilter, thisArg2) { layerFilter, thisArg2) {
if (this.getGL().isContextLost()) { if (this.getGL().isContextLost()) {
return false; return false;

View File

@@ -163,6 +163,88 @@ describe('ol.renderer.canvas.Map', function() {
}); });
}); });
describe('#forEachLayerAtCoordinate', function() {
let layer, map, target;
beforeEach(function(done) {
target = document.createElement('div');
target.style.width = '100px';
target.style.height = '100px';
document.body.appendChild(target);
map = new Map({
pixelRatio: 1,
target: target,
view: new View({
center: [0, 0],
zoom: 0
})
});
// 1 x 1 pixel black icon
const img = document.createElement('img');
img.onload = function() {
done();
};
img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVR4nGNiAAAABgADNjd8qAAAAABJRU5ErkJggg==';
layer = new VectorLayer({
source: new VectorSource({
features: [
new Feature({
geometry: new Point([0, 0])
})
]
}),
style: new Style({
image: new Icon({
img: img,
imgSize: [1, 1]
})
})
});
});
afterEach(function() {
map.setTarget(null);
document.body.removeChild(target);
});
it('calls callback for clicks inside of the hitTolerance', function() {
map.addLayer(layer);
map.renderSync();
const cb1 = sinon.spy();
const cb2 = sinon.spy();
const pixel = map.getPixelFromCoordinate([0, 0]);
const pixelsInside = [
[pixel[0] + 9, pixel[1]],
[pixel[0] - 9, pixel[1]],
[pixel[0], pixel[1] + 9],
[pixel[0], pixel[1] - 9]
];
const pixelsOutside = [
[pixel[0] + 9, pixel[1] + 9],
[pixel[0] - 9, pixel[1] + 9],
[pixel[0] + 9, pixel[1] - 9],
[pixel[0] - 9, pixel[1] - 9]
];
for (let i = 0; i < 4; i++) {
map.forEachLayerAtPixel(pixelsInside[i], cb1, {hitTolerance: 10});
}
expect(cb1.callCount).to.be(4);
expect(cb1.firstCall.args[0]).to.be(layer);
for (let j = 0; j < 4; j++) {
map.forEachLayerAtPixel(pixelsOutside[j], cb2, {hitTolerance: 10});
}
expect(cb2).not.to.be.called();
});
});
describe('#renderFrame()', function() { describe('#renderFrame()', function() {
let layer, map, renderer; let layer, map, renderer;