From 339c7c5c23375c5b7dfe9134870dfb4e3eab8682 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Tue, 15 Mar 2016 11:19:57 +0100 Subject: [PATCH] Use imgSize when provided --- externs/olx.js | 4 +++- src/ol/style/iconstyle.js | 7 ++++--- test/spec/ol/style/iconstyle.test.js | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index ea94ca87eb..316c709dac 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -6487,7 +6487,9 @@ olx.style.IconOptions.prototype.size; /** - * Image size in pixel. Only required if `img` is set and `src` is not. + * Image size in pixels. Only required if `img` is set and `src` is not, and for + * SVG images in Internet Explorer 11. The provided `imgSize` needs to match + * the actual size of the image. * @type {ol.Size|undefined} * @api */ diff --git a/src/ol/style/iconstyle.js b/src/ol/style/iconstyle.js index 7ef132e639..9f2ff99468 100644 --- a/src/ol/style/iconstyle.js +++ b/src/ol/style/iconstyle.js @@ -106,9 +106,6 @@ ol.style.Icon = function(opt_options) { goog.asserts.assert(!(src !== undefined && image), 'image and src can not provided at the same time'); - goog.asserts.assert( - src === undefined || (src !== undefined && !imgSize), - 'imgSize should not be set when src is provided'); goog.asserts.assert( !image || (image && imgSize), 'imgSize must be set when image is provided'); @@ -513,6 +510,10 @@ ol.style.IconImage_.prototype.handleImageError_ = function() { */ ol.style.IconImage_.prototype.handleImageLoad_ = function() { this.imageState_ = ol.style.ImageState.LOADED; + if (this.size_) { + this.image_.width = this.size_[0]; + this.image_.height = this.size_[1]; + } this.size_ = [this.image_.width, this.image_.height]; this.unlistenImage_(); this.determineTainting_(); diff --git a/test/spec/ol/style/iconstyle.test.js b/test/spec/ol/style/iconstyle.test.js index f46bb337e1..b477d76dbf 100644 --- a/test/spec/ol/style/iconstyle.test.js +++ b/test/spec/ol/style/iconstyle.test.js @@ -8,6 +8,8 @@ goog.require('ol.style.IconOrigin'); describe('ol.style.Icon', function() { var size = [36, 48]; + var src = 'data:image/gif;base64,' + + 'R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=' describe('constructor', function() { @@ -21,6 +23,19 @@ describe('ol.style.Icon', function() { canvas, goog.getUid(canvas), size, '').getImage()).to.eql(canvas); }); + it('imgSize overrides img.width and img.height', function(done) { + var style = new ol.style.Icon({ + src: src, + imgSize: size + }); + var iconImage = style.iconImage_; + iconImage.addEventListener('change', function() { + expect([iconImage.image_.width, iconImage.image_.height]).to.eql(size); + done(); + }); + style.load(); + }); + }); describe('#getAnchor', function() {