Ticket #235 (closed feature: fixed)

Opened 7 years ago

Last modified 7 years ago

patch that allows layer opacity to be set

Reported by: tschaub Owned by:
Priority: minor Milestone: 2.1 Release
Component: general Version:
Keywords: Cc:
State:

Description

The following patch allows you to create a partially transparent layer. You do this by setting an opacity option when constructing the layer. For example:

            var shade = new OpenLayers.Layer.WMS("Shaded Relief",
                "http://ims.cr.usgs.gov:80/servlet19/com.esri.wms.Esrimap/USGS_EDC_Elev_NED_3", 
                {layers: "HR-NED.IMAGE", reaspect: "false", transparent: 'true'},
                {isBaseLayer: false, opacity: 0.30});

Note that if you want the layer to act as an overlay, you have to add the transparent: 'true' parameter to your WMS layer - even though the server has nothing to do with the layer transparency in this case. The key part here is the opacity: 0.30 option.

An example of this partially transparent shaded relief in action can be seen here:  http://dev.geocartic.com/openlayers/examples/layer-opacity.html

The simple patch to follow creates a partially opaque (or transparent) layer based on the opacity option.

Also, if you want to change the opacity of a layer after it has been added to the map, you could add the following method to the Layer class in Layer.js:

    /**
     * Sets the opacity for the entire layer (all images)
     * @param {Float} opacity
     */
    setOpacity: function(opacity) {
        for(var i=0; i<this.div.childNodes.length; ++i) {
            var element = this.div.childNodes[i];
            OpenLayers.Util.setOpacity(element, opacity);
        }
    },

Attachments

layer_opacity.patch Download (1.2 KB) - added by tschaub 7 years ago.
patch that allows layer opacity to be set

Change History

Changed 7 years ago by tschaub

patch that allows layer opacity to be set

Changed 7 years ago by tschaub

Guess the layer should keep track of it's opacity after it has been set:

    /**
     * Sets the opacity for the entire layer (all images)
     * @param {Float} opacity
     */
    setOpacity: function(opacity) {
        for(var i=0; i<this.div.childNodes.length; ++i) {
            var element = this.div.childNodes[i];
            OpenLayers.Util.setOpacity(element, opacity);
        }
        this.opacity = opacity;
    },

My Layer.js is too far out of synch to make nice patches for this.

Changed 7 years ago by crschmidt

  • status changed from new to closed
  • resolution set to fixed

Fixed.

Note: See TracTickets for help on using tickets.