diff --git a/lib/OpenLayers/Renderer.js b/lib/OpenLayers/Renderer.js index 651d53f48f..685fce3ac8 100644 --- a/lib/OpenLayers/Renderer.js +++ b/lib/OpenLayers/Renderer.js @@ -76,12 +76,8 @@ OpenLayers.Renderer = OpenLayers.Class({ /** * Property: featureDx * {Number} Feature offset in x direction. Will be calculated for and - * applied to the current feature while rendering. Looking at the center of - * the feature bounds and the renderer extent, we calculate how many world - * widths the two are away from each other. This value is used to shift the - * feature as close as possible to the center of the current renderer - * extent. This ensures that the feature is visible in the current - * viewport. + * applied to the current feature while rendering (see + * ). */ /** @@ -206,15 +202,7 @@ OpenLayers.Renderer = OpenLayers.Class({ if (!bounds.intersectsBounds(this.extent, {worldBounds: worldBounds})) { style = {display: "none"}; } else { - this.featureDx = 0; - if (worldBounds) { - bounds = feature.geometry.getBounds(); - var worldWidth = worldBounds.getWidth(), - rendererCenterX = (this.extent.left + this.extent.right) / 2, - featureCenterX = (bounds.left + bounds.right) / 2, - worldsAway = Math.round((featureCenterX - rendererCenterX) / worldWidth); - this.featureDx = worldsAway * worldWidth; - } + this.calculateFeatureDx(bounds, worldBounds); } var rendered = this.drawGeometry(feature.geometry, style, feature.id); if(style.display != "none" && style.label && rendered !== false) { @@ -235,6 +223,29 @@ OpenLayers.Renderer = OpenLayers.Class({ } }, + /** + * Method: calculateFeatureDx + * {Number} Calculates the feature offset in x direction. Looking at the + * center of the feature bounds and the renderer extent, we calculate how + * many world widths the two are away from each other. This distance is + * used to shift the feature as close as possible to the center of the + * current enderer extent, which ensures that the feature is visible in the + * current viewport. + * + * Parameters: + * bounds - {} Bounds of the feature + * worldBounds - {} Bounds of the world + */ + calculateFeatureDx: function(bounds, worldBounds) { + this.featureDx = 0; + if (worldBounds) { + var worldWidth = worldBounds.getWidth(), + rendererCenterX = (this.extent.left + this.extent.right) / 2, + featureCenterX = (bounds.left + bounds.right) / 2, + worldsAway = Math.round((featureCenterX - rendererCenterX) / worldWidth); + this.featureDx = worldsAway * worldWidth; + } + }, /** * Method: drawGeometry diff --git a/lib/OpenLayers/Renderer/Canvas.js b/lib/OpenLayers/Renderer/Canvas.js index 707a4ab81c..12d6083c44 100644 --- a/lib/OpenLayers/Renderer/Canvas.js +++ b/lib/OpenLayers/Renderer/Canvas.js @@ -685,24 +685,15 @@ OpenLayers.Renderer.Canvas = OpenLayers.Class(OpenLayers.Renderer, { this.hitContext.clearRect(0, 0, width, height); } var labelMap = []; - var feature, style, bounds; + var feature, geometry, style; var worldBounds = (this.map.baseLayer && this.map.baseLayer.wrapDateLine) && this.map.getMaxExtent(); for (var id in this.features) { if (!this.features.hasOwnProperty(id)) { continue; } feature = this.features[id][0]; - - this.featureDx = 0; - if (worldBounds) { - bounds = feature.geometry.getBounds(); - var worldWidth = worldBounds.getWidth(); - var rendererCenterX = (this.extent.left + this.extent.right) / 2; - var featureCenterX = (bounds.left + bounds.right) / 2; - var worldsAway = Math.round((featureCenterX - rendererCenterX) / worldWidth); - this.featureDx = worldsAway * worldWidth; - } - + geometry = feature.geometry; + this.calculateFeatureDx(geometry.getBounds(), worldBounds); style = this.features[id][1]; - this.drawGeometry(feature.geometry, style, feature.id); + this.drawGeometry(geometry, style, feature.id); if(style.label) { labelMap.push([feature, style]); }