Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Stroke opacity work in OpenLayers 3

Tags:

openlayers-3

I cannot get Stroke opacity working in OpenLayers 3 no matter what I try. What I try to achieve is to draw a line to OSM tile map with 0.5 opacity.

Here is sample code:

var lineString = new ol.geom.LineString([
   [4.9020, 52.3667],
   [4.9030, 52.3667],
   [4.9040, 52.3667],
   [4.9050, 52.3667]
]);
lineString.transform('EPSG:4326', 'EPSG:3857');

var lineLayer = new ol.layer.Vector({
    source: new ol.source.Vector({
        features: [new ol.Feature({
            geometry: lineString,
            name: 'Line'
        })]
    }),
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: 'yellow',
            opacity: 0.5,
            width: 15
        })
    })
});    

var view = new ol.View({
    center: ol.proj.transform([4.9020, 52.3667], 'EPSG:4326','EPSG:3857'),
   zoom: 18
});

var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
    lineLayer
  ],
  target: 'map',
  controls: ol.control.defaults({
    attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
      collapsible: false
    })
  }),
  view: view
});

You can see it here: http://jsfiddle.net/abgcvqw3/1/

like image 741
Laowai Avatar asked Nov 29 '22 23:11

Laowai


1 Answers

The opacity is set through the color option, as the fourth element of the color value (the A, for "Alpha" in RGBA).

For example here's how you can have a semi transparent yellow:

color: [255, 255, 0, 0.5]

And here is another notation:

color: 'rgba(255,255,0,0.5)'
like image 128
erilem Avatar answered Dec 23 '22 23:12

erilem