From f10ae6c474864a7350a23d0503036e868dc242ff Mon Sep 17 00:00:00 2001 From: simonseyock Date: Thu, 8 Dec 2016 10:11:33 +0100 Subject: [PATCH] Adressed review. Reusing transform Changing either size of the canvas or clearing it adding unit of hitTolerance to jsdoc --- externs/olx.js | 2 +- src/ol/interaction/select.js | 4 ++-- src/ol/interaction/translate.js | 4 ++-- src/ol/render/canvas/replaygroup.js | 20 +++++++++++++++----- src/ol/renderer/layer.js | 2 +- src/ol/renderer/map.js | 4 ++-- src/ol/source/source.js | 2 +- 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index 3ef5688209..2ae1aab718 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -323,7 +323,7 @@ olx.AtPixelOptions.prototype.layerFilter; /** - * Hit-detection tolerance. Pixels inside the radius around the given position + * Hit-detection tolerance in pixels. Pixels inside the radius around the given position * will be checked for features. This only works for the canvas renderer and * not for WebGL. * @type {number|undefined} diff --git a/src/ol/interaction/select.js b/src/ol/interaction/select.js index daf4a0cdd1..c4959c354f 100644 --- a/src/ol/interaction/select.js +++ b/src/ol/interaction/select.js @@ -168,7 +168,7 @@ ol.interaction.Select.prototype.getFeatures = function() { /** * Returns the Hit-detection tolerance. - * @returns {number} Hit tolerance. + * @returns {number} Hit tolerance in pixels. * @api */ ol.interaction.Select.prototype.getHitTolerance = function() { @@ -291,7 +291,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { * Hit-detection tolerance. Pixels inside the radius around the given position * will be checked for features. This only works for the canvas renderer and * not for WebGL. - * @param {number} hitTolerance Hit tolerance. + * @param {number} hitTolerance Hit tolerance in pixels. * @api */ ol.interaction.Select.prototype.setHitTolerance = function(hitTolerance) { diff --git a/src/ol/interaction/translate.js b/src/ol/interaction/translate.js index 2bc23fd46e..9d57e4d37b 100644 --- a/src/ol/interaction/translate.js +++ b/src/ol/interaction/translate.js @@ -212,7 +212,7 @@ ol.interaction.Translate.prototype.featuresAtPixel_ = function(pixel, map) { /** * Returns the Hit-detection tolerance. - * @returns {number} Hit tolerance. + * @returns {number} Hit tolerance in pixels. * @api */ ol.interaction.Translate.prototype.getHitTolerance = function() { @@ -224,7 +224,7 @@ ol.interaction.Translate.prototype.getHitTolerance = function() { * Hit-detection tolerance. Pixels inside the radius around the given position * will be checked for features. This only works for the canvas renderer and * not for WebGL. - * @param {number} hitTolerance Hit tolerance. + * @param {number} hitTolerance Hit tolerance in pixels. * @api */ ol.interaction.Translate.prototype.setHitTolerance = function(hitTolerance) { diff --git a/src/ol/render/canvas/replaygroup.js b/src/ol/render/canvas/replaygroup.js index a6656fb4a4..2a7983b14e 100644 --- a/src/ol/render/canvas/replaygroup.js +++ b/src/ol/render/canvas/replaygroup.js @@ -71,6 +71,12 @@ ol.render.canvas.ReplayGroup = function( * @type {CanvasRenderingContext2D} */ this.hitDetectionContext_ = ol.dom.createCanvasContext2D(1, 1); + + /** + * @private + * @type {ol.Transform} + */ + this.hitDetectionTransform_ = ol.transform.create(); }; ol.inherits(ol.render.canvas.ReplayGroup, ol.render.ReplayGroup); @@ -174,7 +180,7 @@ ol.render.canvas.ReplayGroup.prototype.finish = function() { * @param {ol.Coordinate} coordinate Coordinate. * @param {number} resolution Resolution. * @param {number} rotation Rotation. - * @param {number} hitTolerance Hit tolerance. + * @param {number} hitTolerance Hit tolerance in pixels. * @param {Object.} skippedFeaturesHash Ids of features * to skip. * @param {function((ol.Feature|ol.render.Feature)): T} callback Feature @@ -187,15 +193,19 @@ ol.render.canvas.ReplayGroup.prototype.forEachFeatureAtCoordinate = function( hitTolerance = Math.round(hitTolerance); var contextSize = hitTolerance * 2 + 1; - var transform = ol.transform.compose(ol.transform.create(), + var transform = ol.transform.compose(this.hitDetectionTransform_, hitTolerance + 0.5, hitTolerance + 0.5, 1 / resolution, -1 / resolution, -rotation, -coordinate[0], -coordinate[1]); var context = this.hitDetectionContext_; - context.canvas.width = contextSize; - context.canvas.height = contextSize; - context.clearRect(0, 0, contextSize, contextSize); + + if (context.canvas.width !== contextSize || context.canvas.height !== contextSize) { + context.canvas.width = contextSize; + context.canvas.height = contextSize; + } else { + context.clearRect(0, 0, contextSize, contextSize); + } /** * @type {ol.Extent} diff --git a/src/ol/renderer/layer.js b/src/ol/renderer/layer.js index 67dcca8f78..198d91ca37 100644 --- a/src/ol/renderer/layer.js +++ b/src/ol/renderer/layer.js @@ -35,7 +35,7 @@ ol.inherits(ol.renderer.Layer, ol.Observable); /** * @param {ol.Coordinate} coordinate Coordinate. * @param {olx.FrameState} frameState Frame state. - * @param {number} hitTolerance Hit tolerance. + * @param {number} hitTolerance Hit tolerance in pixels. * @param {function(this: S, (ol.Feature|ol.render.Feature), ol.layer.Layer): T} * callback Feature callback. * @param {S} thisArg Value to use as `this` when executing `callback`. diff --git a/src/ol/renderer/map.js b/src/ol/renderer/map.js index 9edacb4d66..de571dd3ed 100644 --- a/src/ol/renderer/map.js +++ b/src/ol/renderer/map.js @@ -100,7 +100,7 @@ ol.renderer.Map.expireIconCache_ = function(map, frameState) { /** * @param {ol.Coordinate} coordinate Coordinate. * @param {olx.FrameState} frameState FrameState. - * @param {number} hitTolerance Hit tolerance. + * @param {number} hitTolerance Hit tolerance in pixels. * @param {function(this: S, (ol.Feature|ol.render.Feature), * ol.layer.Layer): T} callback Feature callback. * @param {S} thisArg Value to use as `this` when executing `callback`. @@ -189,7 +189,7 @@ ol.renderer.Map.prototype.forEachLayerAtPixel = function(pixel, frameState, call /** * @param {ol.Coordinate} coordinate Coordinate. * @param {olx.FrameState} frameState FrameState. - * @param {number} hitTolerance Hit tolerance. + * @param {number} hitTolerance Hit tolerance in pixels. * @param {function(this: U, ol.layer.Layer): boolean} layerFilter Layer filter * function, only layers which are visible and for which this function * returns `true` will be tested for features. By default, all visible diff --git a/src/ol/source/source.js b/src/ol/source/source.js index 2224022bd8..596794566c 100644 --- a/src/ol/source/source.js +++ b/src/ol/source/source.js @@ -94,7 +94,7 @@ ol.source.Source.toAttributionsArray_ = function(attributionLike) { * @param {ol.Coordinate} coordinate Coordinate. * @param {number} resolution Resolution. * @param {number} rotation Rotation. - * @param {number} hitTolerance Hit tolerance. + * @param {number} hitTolerance Hit tolerance in pixels. * @param {Object.} skippedFeatureUids Skipped feature uids. * @param {function((ol.Feature|ol.render.Feature)): T} callback Feature * callback.