From 7f02b63ccc0170ea6311ac64f01eee66399b7174 Mon Sep 17 00:00:00 2001 From: Brett Johnson Date: Wed, 6 Jan 2016 09:57:40 -0800 Subject: [PATCH] Overloading fill color (polygon or text) with CanvasPattern and CanvasGradient. --- externs/olx.js | 9 +++--- src/ol/colorlike.js | 41 +++++++++++++++++++++++++ src/ol/render/canvas/canvas.js | 2 +- src/ol/render/canvas/canvasimmediate.js | 5 +-- src/ol/render/canvas/canvasreplay.js | 17 +++++----- src/ol/style/circlestyle.js | 3 +- src/ol/style/fillstyle.js | 18 ++++++++--- src/ol/style/regularshapestyle.js | 3 +- 8 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 src/ol/colorlike.js diff --git a/externs/olx.js b/externs/olx.js index 66e5238e10..3e4d9eb180 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -6158,16 +6158,17 @@ olx.style.CircleOptions.prototype.atlasManager; /** - * @typedef {{color: (ol.Color|string|undefined)}} + * @typedef {{color: (ol.Color|ol.ColorLike|undefined)}} * @api */ olx.style.FillOptions; /** - * Color. See {@link ol.color} for possible formats. Default null; if null, - * the Canvas/renderer default black will be used. - * @type {ol.Color|string|undefined} + * A color, gradient or pattern. See {@link ol.color} + * and {@link ol.colorlike} for possible formats. Default null; + * if null, the Canvas/renderer default black will be used. + * @type {ol.Color|ol.ColorLike|undefined} * @api */ olx.style.FillOptions.prototype.color; diff --git a/src/ol/colorlike.js b/src/ol/colorlike.js new file mode 100644 index 0000000000..bbed7c7eab --- /dev/null +++ b/src/ol/colorlike.js @@ -0,0 +1,41 @@ +goog.provide('ol.ColorLike'); +goog.provide('ol.colorlike'); + +goog.require('ol.color'); + + +/** + * A type accepted by CanvasRenderingContext2D.fillStyle. + * Represents a color, pattern, or gradient. + * + * @typedef {string|CanvasPattern|CanvasGradient} + * @api + */ +ol.ColorLike; + + +/** + * @param {ol.Color|ol.ColorLike} color Color. + * @return {ol.ColorLike} The color as an ol.ColorLike + * @api + */ +ol.colorlike.asColorLike = function(color) { + if (ol.colorlike.isColorLike(color)) { + return /** @type {string|CanvasPattern|CanvasGradient} */ (color); + } else { + return ol.color.asString(/** @type {ol.Color} */ (color)); + } +}; + + +/** + * @param {?} color The value that is potentially an ol.ColorLike + * @return {boolean} Whether the color is an ol.ColorLike + */ +ol.colorlike.isColorLike = function(color) { + return ( + goog.isString(color) || + color instanceof CanvasPattern || + color instanceof CanvasGradient + ); +}; diff --git a/src/ol/render/canvas/canvas.js b/src/ol/render/canvas/canvas.js index 21e546d579..4028c72df3 100644 --- a/src/ol/render/canvas/canvas.js +++ b/src/ol/render/canvas/canvas.js @@ -2,7 +2,7 @@ goog.provide('ol.render.canvas'); /** - * @typedef {{fillStyle: string}} + * @typedef {{fillStyle: ol.ColorLike}} */ ol.render.canvas.FillState; diff --git a/src/ol/render/canvas/canvasimmediate.js b/src/ol/render/canvas/canvasimmediate.js index cabb1c0129..bf7b8de7c2 100644 --- a/src/ol/render/canvas/canvasimmediate.js +++ b/src/ol/render/canvas/canvasimmediate.js @@ -8,6 +8,7 @@ goog.require('goog.asserts'); goog.require('goog.vec.Mat4'); goog.require('ol.array'); goog.require('ol.color'); +goog.require('ol.colorlike'); goog.require('ol.extent'); goog.require('ol.geom.flat.transform'); goog.require('ol.has'); @@ -835,7 +836,7 @@ ol.render.canvas.Immediate.prototype.setFillStrokeStyle = function(fillStyle, st } else { var fillStyleColor = fillStyle.getColor(); this.fillState_ = { - fillStyle: ol.color.asString(fillStyleColor ? + fillStyle: ol.colorlike.asColorLike(fillStyleColor ? fillStyleColor : ol.render.canvas.defaultFillStyle) }; } @@ -919,7 +920,7 @@ ol.render.canvas.Immediate.prototype.setTextStyle = function(textStyle) { } else { var textFillStyleColor = textFillStyle.getColor(); this.textFillState_ = { - fillStyle: ol.color.asString(textFillStyleColor ? + fillStyle: ol.colorlike.asColorLike(textFillStyleColor ? textFillStyleColor : ol.render.canvas.defaultFillStyle) }; } diff --git a/src/ol/render/canvas/canvasreplay.js b/src/ol/render/canvas/canvasreplay.js index ebad793ceb..1a8e670989 100644 --- a/src/ol/render/canvas/canvasreplay.js +++ b/src/ol/render/canvas/canvasreplay.js @@ -13,6 +13,7 @@ goog.require('goog.vec.Mat4'); goog.require('ol'); goog.require('ol.array'); goog.require('ol.color'); +goog.require('ol.colorlike'); goog.require('ol.dom'); goog.require('ol.extent'); goog.require('ol.extent.Relationship'); @@ -497,9 +498,11 @@ ol.render.canvas.Replay.prototype.replay_ = function( ++i; break; case ol.render.canvas.Instruction.SET_FILL_STYLE: - goog.asserts.assert(typeof instruction[1] === 'string', - '2nd instruction should be a string'); - context.fillStyle = /** @type {string} */ (instruction[1]); + goog.asserts.assert( + ol.colorlike.isColorLike(instruction[1]), + '2nd instruction should be a string, ' + + 'CanvasPattern, or CanvasGradient'); + context.fillStyle = /** @type {ol.ColorLike} */ (instruction[1]); ++i; break; case ol.render.canvas.Instruction.SET_STROKE_STYLE: @@ -1180,14 +1183,14 @@ ol.render.canvas.PolygonReplay = function(tolerance, maxExtent, resolution) { /** * @private - * @type {{currentFillStyle: (string|undefined), + * @type {{currentFillStyle: (ol.ColorLike|undefined), * currentStrokeStyle: (string|undefined), * currentLineCap: (string|undefined), * currentLineDash: Array., * currentLineJoin: (string|undefined), * currentLineWidth: (number|undefined), * currentMiterLimit: (number|undefined), - * fillStyle: (string|undefined), + * fillStyle: (ol.ColorLike|undefined), * strokeStyle: (string|undefined), * lineCap: (string|undefined), * lineDash: Array., @@ -1435,7 +1438,7 @@ ol.render.canvas.PolygonReplay.prototype.setFillStrokeStyle = function(fillStyle var state = this.state_; if (fillStyle) { var fillStyleColor = fillStyle.getColor(); - state.fillStyle = ol.color.asString(fillStyleColor ? + state.fillStyle = ol.colorlike.asColorLike(fillStyleColor ? fillStyleColor : ol.render.canvas.defaultFillStyle); } else { state.fillStyle = undefined; @@ -1742,7 +1745,7 @@ ol.render.canvas.TextReplay.prototype.setTextStyle = function(textStyle) { this.textFillState_ = null; } else { var textFillStyleColor = textFillStyle.getColor(); - var fillStyle = ol.color.asString(textFillStyleColor ? + var fillStyle = ol.colorlike.asColorLike(textFillStyleColor ? textFillStyleColor : ol.render.canvas.defaultFillStyle); if (!this.textFillState_) { this.textFillState_ = { diff --git a/src/ol/style/circlestyle.js b/src/ol/style/circlestyle.js index baae435366..71846f2631 100644 --- a/src/ol/style/circlestyle.js +++ b/src/ol/style/circlestyle.js @@ -3,6 +3,7 @@ goog.provide('ol.style.Circle'); goog.require('goog.asserts'); goog.require('ol'); goog.require('ol.color'); +goog.require('ol.colorlike'); goog.require('ol.has'); goog.require('ol.render.canvas'); goog.require('ol.style.Fill'); @@ -340,7 +341,7 @@ ol.style.Circle.prototype.draw_ = function(renderOptions, context, x, y) { this.radius_, 0, 2 * Math.PI, true); if (this.fill_) { - context.fillStyle = ol.color.asString(this.fill_.getColor()); + context.fillStyle = ol.colorlike.asColorLike(this.fill_.getColor()); context.fill(); } if (this.stroke_) { diff --git a/src/ol/style/fillstyle.js b/src/ol/style/fillstyle.js index f57308ab17..f057e97e85 100644 --- a/src/ol/style/fillstyle.js +++ b/src/ol/style/fillstyle.js @@ -1,5 +1,6 @@ goog.provide('ol.style.Fill'); +goog.require('ol.ColorLike'); goog.require('ol.color'); @@ -17,7 +18,7 @@ ol.style.Fill = function(opt_options) { /** * @private - * @type {ol.Color|string} + * @type {ol.Color|ol.ColorLike} */ this.color_ = options.color !== undefined ? options.color : null; @@ -31,7 +32,7 @@ ol.style.Fill = function(opt_options) { /** * Get the fill color. - * @return {ol.Color|string} Color. + * @return {ol.Color|ol.ColorLike} Color. * @api */ ol.style.Fill.prototype.getColor = function() { @@ -42,7 +43,7 @@ ol.style.Fill.prototype.getColor = function() { /** * Set the color. * - * @param {ol.Color|string} color Color. + * @param {ol.Color|ol.ColorLike} color Color. * @api */ ol.style.Fill.prototype.setColor = function(color) { @@ -56,8 +57,15 @@ ol.style.Fill.prototype.setColor = function(color) { */ ol.style.Fill.prototype.getChecksum = function() { if (this.checksum_ === undefined) { - this.checksum_ = 'f' + (this.color_ ? - ol.color.asString(this.color_) : '-'); + if ( + this.color_ instanceof CanvasPattern || + this.color_ instanceof CanvasGradient + ) { + this.checksum_ = goog.getUid(this.color_).toString(); + } else { + this.checksum_ = 'f' + (this.color_ ? + ol.color.asString(this.color_) : '-'); + } } return this.checksum_; diff --git a/src/ol/style/regularshapestyle.js b/src/ol/style/regularshapestyle.js index b3766cd2c2..b59b452b2a 100644 --- a/src/ol/style/regularshapestyle.js +++ b/src/ol/style/regularshapestyle.js @@ -3,6 +3,7 @@ goog.provide('ol.style.RegularShape'); goog.require('goog.asserts'); goog.require('ol'); goog.require('ol.color'); +goog.require('ol.colorlike'); goog.require('ol.has'); goog.require('ol.render.canvas'); goog.require('ol.style.AtlasManager'); @@ -433,7 +434,7 @@ ol.style.RegularShape.prototype.draw_ = function(renderOptions, context, x, y) { } if (this.fill_) { - context.fillStyle = ol.color.asString(this.fill_.getColor()); + context.fillStyle = ol.colorlike.asColorLike(this.fill_.getColor()); context.fill(); } if (this.stroke_) {