Tile opacity patch from tschaub: Fix for #235. Also includes tests to ensure

that code works.


git-svn-id: http://svn.openlayers.org/trunk/openlayers@1433 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2006-09-11 14:30:25 +00:00
parent 720c8507f7
commit 4a65590d3e
4 changed files with 53 additions and 0 deletions

View File

@@ -436,6 +436,18 @@ OpenLayers.Layer.prototype = {
);
},
/**
* Sets the opacity for the entire layer (all images)
* @param {Float} opacity
*/
setOpacity: function(opacity) {
this.opacity = opacity;
for(var i=0; i<this.div.childNodes.length; ++i) {
var element = this.div.childNodes[i];
OpenLayers.Util.setOpacity(element, opacity);
}
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Layer"
};

View File

@@ -102,6 +102,9 @@ OpenLayers.Tile.Image.prototype =
true);
}
this.layer.div.appendChild(this.imgDiv);
if(this.layer.opacity != null) {
OpenLayers.Util.setOpacity(this.imgDiv, this.layer.opacity);
}
},
/** @final @type String */

View File

@@ -125,6 +125,19 @@ OpenLayers.Util.createImage = function(id, px, sz, imgURL, position, border,
return image;
};
/**
* Set the opacity of a DOM Element
* Note that for this function to work in IE, elements must "have layout"
* according to:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/haslayout.asp
*
* @param {DOMElement} element Set the opacity on this DOM element
* @param {Float} opacity Opacity value (0.0 - 1.0)
*/
OpenLayers.Util.setOpacity = function(element, opacity) {
element.style.opacity = opacity;
element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
}
OpenLayers.Util.onImageLoad = function() {
this.style.backgroundColor = null;

View File

@@ -150,6 +150,31 @@
}
function test_08_Layer_WMS_setOpacity (t) {
t.plan( 5 );
var map = new OpenLayers.Map('map');
map.projection = "xx";
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv";
tParams = { layers: 'basic',
format: 'image/png'};
tOptions = { 'opacity': '0.5' };
var tLayer = new OpenLayers.Layer.WMS(name, tUrl, tParams, tOptions);
map.addLayer(tLayer);
map.zoomToMaxExtent();
t.eq(tLayer.opacity, "0.5", "Opacity is set correctly");
t.eq(tLayer.div.firstChild.style.opacity, "0.5", "Opacity on tile is correct");
tLayer.setOpacity("0.6");
t.eq(tLayer.opacity, "0.6", "setOpacity works properly");
t.eq(tLayer.div.firstChild.style.opacity, "0.6", "Opacity on tile is changed correctly");
var pixel = new OpenLayers.Pixel(5,6);
var tile = tLayer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
tile.draw();
t.eq(tile.imgDiv.style.opacity, "0.6", "Tile opacity is set correctly");
}
function test_99_Layer_WMS_destroy (t) {
t.plan( 1 );