diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index 497b911024..203a9f185d 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -608,11 +608,14 @@ * @property {number|ol.expr.Expression|undefined} size Size in pixels. * @property {string|ol.expr.Expression|undefined} fillColor Fill color as * hex color code. + * @property {number|ol.expr.Expression|undefined} fillOpacity Fill opacity + * (0-1). * @property {string|ol.expr.Expression|undefined} strokeColor Stroke * color as hex color code. + * @property {number|ol.expr.Expression|undefined} strokeOpacity Stroke opacity + * (0-1). * @property {number|ol.expr.Expression|undefined} strokeWidth Stroke * width in pixels. - * @property {number|ol.expr.Expression|undefined} opacity Opacity (0-1). */ /** diff --git a/src/ol/renderer/canvas/canvasvectorrenderer.js b/src/ol/renderer/canvas/canvasvectorrenderer.js index 9bf610e33a..a3dd4ce0fd 100644 --- a/src/ol/renderer/canvas/canvasvectorrenderer.js +++ b/src/ol/renderer/canvas/canvasvectorrenderer.js @@ -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; diff --git a/src/ol/style/shape.js b/src/ol/style/shape.js index 4df16825d6..dc9b1ead07 100644 --- a/src/ol/style/shape.js +++ b/src/ol/style/shape.js @@ -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 }); diff --git a/test/spec/ol/style/shape.test.js b/test/spec/ol/style/shape.test.js index be1fd83e8a..8c0c396e1c 100644 --- a/test/spec/ol/style/shape.test.js +++ b/test/spec/ol/style/shape.test.js @@ -9,28 +9,81 @@ describe('ol.style.ShapeLiteral', function() { type: ol.style.ShapeType.CIRCLE, size: 4, fillColor: '#BADA55', + fillOpacity: 0.9, strokeColor: '#013', - strokeWidth: 3, - opacity: 1 + strokeOpacity: 0.8, + strokeWidth: 3 }); var equalLiteral = new ol.style.ShapeLiteral({ type: ol.style.ShapeType.CIRCLE, size: 4, fillColor: '#BADA55', + fillOpacity: 0.9, strokeColor: '#013', - strokeWidth: 3, - opacity: 1 + strokeOpacity: 0.8, + strokeWidth: 3 }); - var differentLiteral = new ol.style.ShapeLiteral({ + var differentSize = new ol.style.ShapeLiteral({ + type: ol.style.ShapeType.CIRCLE, + size: 5, + fillColor: '#BADA55', + fillOpacity: 0.9, + strokeColor: '#013', + strokeOpacity: 0.8, + strokeWidth: 3 + }); + var differentFillColor = new ol.style.ShapeLiteral({ type: ol.style.ShapeType.CIRCLE, size: 4, - fillColor: '#013', + fillColor: '#ffffff', + fillOpacity: 0.9, strokeColor: '#013', - strokeWidth: 3, - opacity: 1 + strokeOpacity: 0.8, + strokeWidth: 3 + }); + var differentFillOpacity = new ol.style.ShapeLiteral({ + type: ol.style.ShapeType.CIRCLE, + size: 4, + fillColor: '#BADA55', + fillOpacity: 0.8, + strokeColor: '#013', + strokeOpacity: 0.8, + strokeWidth: 3 + }); + var differentStrokeColor = new ol.style.ShapeLiteral({ + type: ol.style.ShapeType.CIRCLE, + size: 4, + fillColor: '#BADA55', + fillOpacity: 0.9, + strokeColor: '#ffffff', + strokeOpacity: 0.8, + strokeWidth: 3 + }); + var differentStrokeOpacity = new ol.style.ShapeLiteral({ + type: ol.style.ShapeType.CIRCLE, + size: 4, + fillColor: '#BADA55', + fillOpacity: 0.9, + strokeColor: '#013', + strokeOpacity: 0.7, + strokeWidth: 3 + }); + var differentStrokeWidth = new ol.style.ShapeLiteral({ + type: ol.style.ShapeType.CIRCLE, + size: 4, + fillColor: '#BADA55', + fillOpacity: 0.9, + strokeColor: '#013', + strokeOpacity: 0.8, + strokeWidth: 4 }); expect(literal.equals(equalLiteral)).to.be(true); - expect(literal.equals(differentLiteral)).to.be(false); + expect(literal.equals(differentSize)).to.be(false); + expect(literal.equals(differentFillColor)).to.be(false); + expect(literal.equals(differentFillOpacity)).to.be(false); + expect(literal.equals(differentStrokeColor)).to.be(false); + expect(literal.equals(differentStrokeOpacity)).to.be(false); + expect(literal.equals(differentStrokeWidth)).to.be(false); }); }); @@ -64,7 +117,7 @@ describe('ol.style.Shape', function() { it('evaluates expressions with the given feature', function() { var symbolizer = new ol.style.Shape({ size: ol.expr.parse('sizeAttr'), - opacity: ol.expr.parse('opacityAttr'), + fillOpacity: ol.expr.parse('opacityAttr'), fillColor: '#BADA55' }); @@ -76,45 +129,27 @@ describe('ol.style.Shape', function() { var literal = symbolizer.createLiteral(feature); expect(literal).to.be.a(ol.style.ShapeLiteral); expect(literal.size).to.be(42); - expect(literal.opacity).to.be(0.4); + expect(literal.fillOpacity).to.be(0.4); }); it('can be called without a feature', function() { var symbolizer = new ol.style.Shape({ size: 10, - opacity: 1, fillColor: '#BADA55', strokeColor: '#013', + strokeOpacity: 1, strokeWidth: 2 }); var literal = symbolizer.createLiteral(); expect(literal).to.be.a(ol.style.ShapeLiteral); expect(literal.size).to.be(10); - expect(literal.opacity).to.be(1); expect(literal.fillColor).to.be('#BADA55'); expect(literal.strokeColor).to.be('#013'); + expect(literal.strokeOpacity).to.be(1); expect(literal.strokeWidth).to.be(2); }); - it('applies default type if none provided', function() { - var symbolizer = new ol.style.Shape({ - size: ol.expr.parse('sizeAttr'), - opacity: ol.expr.parse('opacityAttr'), - fillColor: '#BADA55' - }); - - var feature = new ol.Feature({ - sizeAttr: 42, - opacityAttr: 0.4 - }); - - var literal = symbolizer.createLiteral(feature); - expect(literal).to.be.a(ol.style.ShapeLiteral); - expect(literal.size).to.be(42); - expect(literal.opacity).to.be(0.4); - }); - }); describe('#getFillColor()', function() { @@ -131,6 +166,30 @@ describe('ol.style.Shape', function() { }); + describe('#getFillOpacity()', function() { + + it('returns the fill opacity', function() { + var symbolizer = new ol.style.Shape({ + fillOpacity: 0.123 + }); + + var opacity = symbolizer.getFillOpacity(); + expect(opacity).to.be.a(ol.expr.Literal); + expect(opacity.getValue()).to.be(0.123); + }); + + it('returns the default if none provided', function() { + var symbolizer = new ol.style.Shape({ + fillColor: '#ffffff' + }); + + var opacity = symbolizer.getFillOpacity(); + expect(opacity).to.be.a(ol.expr.Literal); + expect(opacity.getValue()).to.be(0.4); + }); + + }); + describe('#getStrokeColor()', function() { it('returns the stroke color', function() { @@ -145,6 +204,31 @@ describe('ol.style.Shape', function() { }); + describe('#getStrokeOpacity()', function() { + + it('returns the stroke opacity', function() { + var symbolizer = new ol.style.Shape({ + strokeWidth: 1, + strokeOpacity: 0.123 + }); + + var opacity = symbolizer.getStrokeOpacity(); + expect(opacity).to.be.a(ol.expr.Literal); + expect(opacity.getValue()).to.be(0.123); + }); + + it('returns the default if none provided', function() { + var symbolizer = new ol.style.Shape({ + strokeWidth: 1 + }); + + var opacity = symbolizer.getStrokeOpacity(); + expect(opacity).to.be.a(ol.expr.Literal); + expect(opacity.getValue()).to.be(0.8); + }); + + }); + describe('#getStrokeWidth()', function() { it('returns the stroke width', function() { @@ -159,21 +243,6 @@ describe('ol.style.Shape', function() { }); - describe('#getOpacity()', function() { - - it('returns the stroke opacity', function() { - var symbolizer = new ol.style.Shape({ - strokeWidth: 1, - opacity: 0.123 - }); - - var opacity = symbolizer.getOpacity(); - expect(opacity).to.be.a(ol.expr.Literal); - expect(opacity.getValue()).to.be(0.123); - }); - - }); - describe('#getType()', function() { it('returns the shape type', function() { @@ -216,6 +285,34 @@ describe('ol.style.Shape', function() { }); + describe('#setFillOpacity()', function() { + + it('sets the fill opacity', function() { + var symbolizer = new ol.style.Shape({ + fillOpacity: 0.5 + }); + + symbolizer.setFillOpacity(new ol.expr.Literal(0.6)); + + var fillOpacity = symbolizer.getFillOpacity(); + expect(fillOpacity).to.be.a(ol.expr.Literal); + expect(fillOpacity.getValue()).to.be(0.6); + }); + + it('throws when not provided an expression', function() { + var symbolizer = new ol.style.Shape({ + fillOpacity: 0.5 + }); + + expect(function() { + symbolizer.setFillOpacity(0.4); + }).throwException(function(err) { + expect(err).to.be.a(goog.asserts.AssertionError); + }); + }); + + }); + describe('#setStrokeColor()', function() { it('sets the stroke color', function() { @@ -244,6 +341,35 @@ describe('ol.style.Shape', function() { }); + describe('#setStrokeOpacity()', function() { + + it('sets the stroke opacity', function() { + var symbolizer = new ol.style.Shape({ + strokeWidth: 1, + strokeOpacity: 0.123 + }); + symbolizer.setStrokeOpacity(new ol.expr.Literal(0.321)); + + var opacity = symbolizer.getStrokeOpacity(); + expect(opacity).to.be.a(ol.expr.Literal); + expect(opacity.getValue()).to.be(0.321); + }); + + it('throws when not provided an expression', function() { + var symbolizer = new ol.style.Shape({ + strokeWidth: 1, + strokeOpacity: 1 + }); + + expect(function() { + symbolizer.setStrokeOpacity(0.5); + }).throwException(function(err) { + expect(err).to.be.a(goog.asserts.AssertionError); + }); + }); + + }); + describe('#setStrokeWidth()', function() { it('sets the stroke width', function() { @@ -286,34 +412,6 @@ describe('ol.style.Shape', function() { }); - describe('#setOpacity()', function() { - - it('sets the stroke opacity', function() { - var symbolizer = new ol.style.Shape({ - strokeWidth: 1, - opacity: 0.123 - }); - symbolizer.setOpacity(new ol.expr.Literal(0.321)); - - var opacity = symbolizer.getOpacity(); - expect(opacity).to.be.a(ol.expr.Literal); - expect(opacity.getValue()).to.be(0.321); - }); - - it('throws when not provided an expression', function() { - var symbolizer = new ol.style.Shape({ - strokeWidth: 1, - opacity: 1 - }); - - expect(function() { - symbolizer.setOpacity(0.5); - }).throwException(function(err) { - expect(err).to.be.a(goog.asserts.AssertionError); - }); - }); - - }); }); goog.require('goog.asserts.AssertionError');