Support fillOpacity and strokeOpacity on shape symbolizers
This commit is contained in:
@@ -400,8 +400,6 @@ ol.renderer.canvas.VectorRenderer.renderCircle_ = function(circle) {
|
||||
canvas.height = size;
|
||||
canvas.width = size;
|
||||
|
||||
context.globalAlpha = circle.opacity;
|
||||
|
||||
if (fillColor) {
|
||||
context.fillStyle = fillColor;
|
||||
}
|
||||
@@ -416,9 +414,13 @@ ol.renderer.canvas.VectorRenderer.renderCircle_ = function(circle) {
|
||||
context.arc(mid, mid, circle.size / 2, 0, twoPi, true);
|
||||
|
||||
if (fillColor) {
|
||||
goog.asserts.assertNumber(circle.fillOpacity);
|
||||
context.globalAlpha = circle.fillOpacity;
|
||||
context.fill();
|
||||
}
|
||||
if (strokeColor) {
|
||||
goog.asserts.assertNumber(circle.strokeOpacity);
|
||||
context.globalAlpha = circle.strokeOpacity;
|
||||
context.stroke();
|
||||
}
|
||||
return canvas;
|
||||
|
||||
@@ -22,9 +22,10 @@ ol.style.ShapeType = {
|
||||
* @typedef {{type: (ol.style.ShapeType),
|
||||
* size: (number),
|
||||
* fillColor: (string|undefined),
|
||||
* fillOpacity: (number|undefined),
|
||||
* strokeColor: (string|undefined),
|
||||
* strokeWidth: (number|undefined),
|
||||
* opacity: (number)}}
|
||||
* strokeOpacity: (number|undefined),
|
||||
* strokeWidth: (number|undefined)}}
|
||||
*/
|
||||
ol.style.ShapeLiteralOptions;
|
||||
|
||||
@@ -51,6 +52,13 @@ ol.style.ShapeLiteral = function(options) {
|
||||
goog.asserts.assertString(options.fillColor, 'fillColor must be a string');
|
||||
}
|
||||
|
||||
/** @type {number|undefined} */
|
||||
this.fillOpacity = options.fillOpacity;
|
||||
if (goog.isDef(options.fillOpacity)) {
|
||||
goog.asserts.assertNumber(
|
||||
options.fillOpacity, 'fillOpacity must be a number');
|
||||
}
|
||||
|
||||
/** @type {string|undefined} */
|
||||
this.strokeColor = options.strokeColor;
|
||||
if (goog.isDef(this.strokeColor)) {
|
||||
@@ -58,6 +66,13 @@ ol.style.ShapeLiteral = function(options) {
|
||||
this.strokeColor, 'strokeColor must be a string');
|
||||
}
|
||||
|
||||
/** @type {number|undefined} */
|
||||
this.strokeOpacity = options.strokeOpacity;
|
||||
if (goog.isDef(this.strokeOpacity)) {
|
||||
goog.asserts.assertNumber(
|
||||
this.strokeOpacity, 'strokeOpacity must be a number');
|
||||
}
|
||||
|
||||
/** @type {number|undefined} */
|
||||
this.strokeWidth = options.strokeWidth;
|
||||
if (goog.isDef(this.strokeWidth)) {
|
||||
@@ -65,14 +80,14 @@ ol.style.ShapeLiteral = function(options) {
|
||||
this.strokeWidth, 'strokeWidth must be a number');
|
||||
}
|
||||
|
||||
goog.asserts.assert(
|
||||
goog.isDef(this.fillColor) ||
|
||||
(goog.isDef(this.strokeColor) && goog.isDef(this.strokeWidth)),
|
||||
'Either fillColor or strokeColor and strokeWidth must be set');
|
||||
|
||||
goog.asserts.assertNumber(options.opacity, 'opacity must be a number');
|
||||
/** @type {number} */
|
||||
this.opacity = options.opacity;
|
||||
// fill and/or stroke properties must be defined
|
||||
var fillDef = goog.isDef(this.fillColor) && goog.isDef(this.fillOpacity);
|
||||
var strokeDef = goog.isDef(this.strokeColor) &&
|
||||
goog.isDef(this.strokeOpacity) &&
|
||||
goog.isDef(this.strokeWidth);
|
||||
goog.asserts.assert(fillDef || strokeDef,
|
||||
'Either fillColor and fillOpacity or ' +
|
||||
'strokeColor and strokeOpacity and strokeWidth must be set');
|
||||
|
||||
};
|
||||
goog.inherits(ol.style.ShapeLiteral, ol.style.PointLiteral);
|
||||
@@ -85,9 +100,10 @@ ol.style.ShapeLiteral.prototype.equals = function(shapeLiteral) {
|
||||
return this.type == shapeLiteral.type &&
|
||||
this.size == shapeLiteral.size &&
|
||||
this.fillColor == shapeLiteral.fillColor &&
|
||||
this.fillOpacity == shapeLiteral.fillOpacity &&
|
||||
this.strokeColor == shapeLiteral.strokeColor &&
|
||||
this.strokeWidth == shapeLiteral.strokeWidth &&
|
||||
this.opacity == shapeLiteral.opacity;
|
||||
this.strokeOpacity == shapeLiteral.strokeOpacity &&
|
||||
this.strokeWidth == shapeLiteral.strokeWidth;
|
||||
};
|
||||
|
||||
|
||||
@@ -110,25 +126,58 @@ ol.style.Shape = function(options) {
|
||||
* @type {ol.expr.Expression}
|
||||
* @private
|
||||
*/
|
||||
this.size_ = !goog.isDef(options.size) ?
|
||||
this.size_ = !goog.isDefAndNotNull(options.size) ?
|
||||
new ol.expr.Literal(ol.style.ShapeDefaults.size) :
|
||||
(options.size instanceof ol.expr.Expression) ?
|
||||
options.size : new ol.expr.Literal(options.size);
|
||||
|
||||
// fill handling - if any fill property is supplied, use all defaults
|
||||
var fillColor = null,
|
||||
fillOpacity = null;
|
||||
|
||||
if (goog.isDefAndNotNull(options.fillColor) ||
|
||||
goog.isDefAndNotNull(options.fillOpacity)) {
|
||||
|
||||
if (goog.isDefAndNotNull(options.fillColor)) {
|
||||
fillColor = (options.fillColor instanceof ol.expr.Expression) ?
|
||||
options.fillColor :
|
||||
new ol.expr.Literal(options.fillColor);
|
||||
} else {
|
||||
fillColor = new ol.expr.Literal(
|
||||
/** @type {string} */ (ol.style.ShapeDefaults.fillColor));
|
||||
}
|
||||
|
||||
if (goog.isDefAndNotNull(options.fillOpacity)) {
|
||||
fillOpacity = (options.fillOpacity instanceof ol.expr.Expression) ?
|
||||
options.fillOpacity :
|
||||
new ol.expr.Literal(options.fillOpacity);
|
||||
} else {
|
||||
fillOpacity = new ol.expr.Literal(
|
||||
/** @type {number} */ (ol.style.ShapeDefaults.fillOpacity));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {ol.expr.Expression}
|
||||
* @private
|
||||
*/
|
||||
this.fillColor_ = !goog.isDefAndNotNull(options.fillColor) ?
|
||||
null :
|
||||
(options.fillColor instanceof ol.expr.Expression) ?
|
||||
options.fillColor : new ol.expr.Literal(options.fillColor);
|
||||
this.fillColor_ = fillColor;
|
||||
|
||||
/**
|
||||
* @type {ol.expr.Expression}
|
||||
* @private
|
||||
*/
|
||||
this.fillOpacity_ = fillOpacity;
|
||||
|
||||
|
||||
// stroke handling - if any stroke property is supplied, use defaults
|
||||
var strokeColor = null,
|
||||
strokeOpacity = null,
|
||||
strokeWidth = null;
|
||||
|
||||
if (goog.isDefAndNotNull(options.strokeColor) ||
|
||||
goog.isDefAndNotNull(options.strokeOpacity) ||
|
||||
goog.isDefAndNotNull(options.strokeWidth)) {
|
||||
|
||||
if (goog.isDefAndNotNull(options.strokeColor)) {
|
||||
@@ -140,6 +189,15 @@ ol.style.Shape = function(options) {
|
||||
/** @type {string} */ (ol.style.ShapeDefaults.strokeColor));
|
||||
}
|
||||
|
||||
if (goog.isDefAndNotNull(options.strokeOpacity)) {
|
||||
strokeOpacity = (options.strokeOpacity instanceof ol.expr.Expression) ?
|
||||
options.strokeOpacity :
|
||||
new ol.expr.Literal(options.strokeOpacity);
|
||||
} else {
|
||||
strokeOpacity = new ol.expr.Literal(
|
||||
/** @type {number} */ (ol.style.ShapeDefaults.strokeOpacity));
|
||||
}
|
||||
|
||||
if (goog.isDefAndNotNull(options.strokeWidth)) {
|
||||
strokeWidth = (options.strokeWidth instanceof ol.expr.Expression) ?
|
||||
options.strokeWidth :
|
||||
@@ -161,21 +219,21 @@ ol.style.Shape = function(options) {
|
||||
* @type {ol.expr.Expression}
|
||||
* @private
|
||||
*/
|
||||
this.strokeWidth_ = strokeWidth;
|
||||
|
||||
// one of stroke or fill can be null, both null is user error
|
||||
goog.asserts.assert(!goog.isNull(this.fillColor_) ||
|
||||
!(goog.isNull(this.strokeColor_) && goog.isNull(this.strokeWidth_)),
|
||||
'Stroke or fill properties must be provided');
|
||||
this.strokeOpacity_ = strokeOpacity;
|
||||
|
||||
/**
|
||||
* @type {ol.expr.Expression}
|
||||
* @private
|
||||
*/
|
||||
this.opacity_ = !goog.isDef(options.opacity) ?
|
||||
new ol.expr.Literal(ol.style.ShapeDefaults.opacity) :
|
||||
(options.opacity instanceof ol.expr.Expression) ?
|
||||
options.opacity : new ol.expr.Literal(options.opacity);
|
||||
this.strokeWidth_ = strokeWidth;
|
||||
|
||||
// one of stroke or fill can be null, both null is user error
|
||||
var fill = !goog.isNull(this.fillColor_) && !goog.isNull(this.fillOpacity_);
|
||||
var stroke = !goog.isNull(this.strokeColor_) &&
|
||||
!goog.isNull(this.strokeOpacity_) &&
|
||||
!goog.isNull(this.strokeWidth_);
|
||||
goog.asserts.assert(fill || stroke,
|
||||
'Stroke or fill properties must be provided');
|
||||
|
||||
};
|
||||
|
||||
@@ -195,33 +253,45 @@ ol.style.Shape.prototype.createLiteral = function(opt_feature) {
|
||||
goog.asserts.assertString(fillColor, 'fillColor must be a string');
|
||||
}
|
||||
|
||||
var fillOpacity;
|
||||
if (!goog.isNull(this.fillOpacity_)) {
|
||||
fillOpacity = ol.expr.evaluateFeature(this.fillOpacity_, opt_feature);
|
||||
goog.asserts.assertNumber(fillOpacity, 'fillOpacity must be a number');
|
||||
}
|
||||
|
||||
var strokeColor;
|
||||
if (!goog.isNull(this.strokeColor_)) {
|
||||
strokeColor = ol.expr.evaluateFeature(this.strokeColor_, opt_feature);
|
||||
goog.asserts.assertString(strokeColor, 'strokeColor must be a string');
|
||||
}
|
||||
|
||||
var strokeOpacity;
|
||||
if (!goog.isNull(this.strokeOpacity_)) {
|
||||
strokeOpacity = ol.expr.evaluateFeature(this.strokeOpacity_, opt_feature);
|
||||
goog.asserts.assertNumber(strokeOpacity, 'strokeOpacity must be a number');
|
||||
}
|
||||
|
||||
var strokeWidth;
|
||||
if (!goog.isNull(this.strokeWidth_)) {
|
||||
strokeWidth = ol.expr.evaluateFeature(this.strokeWidth_, opt_feature);
|
||||
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
|
||||
}
|
||||
|
||||
goog.asserts.assert(
|
||||
goog.isDef(fillColor) ||
|
||||
(goog.isDef(strokeColor) && goog.isDef(strokeWidth)),
|
||||
'either fillColor or strokeColor and strokeWidth must be defined');
|
||||
var fill = goog.isDef(fillColor) && goog.isDef(fillOpacity);
|
||||
var stroke = goog.isDef(strokeColor) && goog.isDef(strokeOpacity) &&
|
||||
goog.isDef(strokeWidth);
|
||||
|
||||
var opacity = ol.expr.evaluateFeature(this.opacity_, opt_feature);
|
||||
goog.asserts.assertNumber(opacity, 'opacity must be a number');
|
||||
goog.asserts.assert(fill || stroke,
|
||||
'either fill or stroke properties must be defined');
|
||||
|
||||
return new ol.style.ShapeLiteral({
|
||||
type: this.type_,
|
||||
size: size,
|
||||
fillColor: fillColor,
|
||||
fillOpacity: fillOpacity,
|
||||
strokeColor: strokeColor,
|
||||
strokeWidth: strokeWidth,
|
||||
opacity: opacity
|
||||
strokeOpacity: strokeOpacity,
|
||||
strokeWidth: strokeWidth
|
||||
});
|
||||
};
|
||||
|
||||
@@ -236,11 +306,11 @@ ol.style.Shape.prototype.getFillColor = function() {
|
||||
|
||||
|
||||
/**
|
||||
* Get the opacity.
|
||||
* @return {ol.expr.Expression} Opacity.
|
||||
* Get the fill opacity.
|
||||
* @return {ol.expr.Expression} Fill opacity.
|
||||
*/
|
||||
ol.style.Shape.prototype.getOpacity = function() {
|
||||
return this.opacity_;
|
||||
ol.style.Shape.prototype.getFillOpacity = function() {
|
||||
return this.fillOpacity_;
|
||||
};
|
||||
|
||||
|
||||
@@ -262,6 +332,15 @@ ol.style.Shape.prototype.getStrokeColor = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the stroke opacity.
|
||||
* @return {ol.expr.Expression} Stroke opacity.
|
||||
*/
|
||||
ol.style.Shape.prototype.getStrokeOpacity = function() {
|
||||
return this.strokeOpacity_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the stroke width.
|
||||
* @return {ol.expr.Expression} Stroke width.
|
||||
@@ -291,12 +370,12 @@ ol.style.Shape.prototype.setFillColor = function(fillColor) {
|
||||
|
||||
|
||||
/**
|
||||
* Set the opacity.
|
||||
* @param {ol.expr.Expression} opacity Opacity.
|
||||
* Set the fill opacity.
|
||||
* @param {ol.expr.Expression} fillOpacity Fill opacity.
|
||||
*/
|
||||
ol.style.Shape.prototype.setOpacity = function(opacity) {
|
||||
goog.asserts.assertInstanceof(opacity, ol.expr.Expression);
|
||||
this.opacity_ = opacity;
|
||||
ol.style.Shape.prototype.setFillOpacity = function(fillOpacity) {
|
||||
goog.asserts.assertInstanceof(fillOpacity, ol.expr.Expression);
|
||||
this.fillOpacity_ = fillOpacity;
|
||||
};
|
||||
|
||||
|
||||
@@ -320,6 +399,16 @@ ol.style.Shape.prototype.setStrokeColor = function(strokeColor) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the stroke opacity.
|
||||
* @param {ol.expr.Expression} strokeOpacity Stroke opacity.
|
||||
*/
|
||||
ol.style.Shape.prototype.setStrokeOpacity = function(strokeOpacity) {
|
||||
goog.asserts.assertInstanceof(strokeOpacity, ol.expr.Expression);
|
||||
this.strokeOpacity_ = strokeOpacity;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the stroke width.
|
||||
* @param {ol.expr.Expression} strokeWidth Stroke width.
|
||||
@@ -346,7 +435,8 @@ ol.style.ShapeDefaults = new ol.style.ShapeLiteral({
|
||||
type: ol.style.ShapeType.CIRCLE,
|
||||
size: 5,
|
||||
fillColor: '#ffffff',
|
||||
fillOpacity: 0.4,
|
||||
strokeColor: '#696969',
|
||||
strokeWidth: 1.5,
|
||||
opacity: 0.75
|
||||
strokeOpacity: 0.8,
|
||||
strokeWidth: 1.5
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user