Merge pull request #2891 from ahocevar/imageloadfunction
Introduce a load function for image sources
This commit is contained in:
@@ -3863,6 +3863,7 @@ olx.source.IGCOptions.prototype.urls;
|
|||||||
* projection: ol.proj.ProjectionLike,
|
* projection: ol.proj.ProjectionLike,
|
||||||
* ratio: (number|undefined),
|
* ratio: (number|undefined),
|
||||||
* resolutions: (Array.<number>|undefined),
|
* resolutions: (Array.<number>|undefined),
|
||||||
|
* imageLoadFunction: (ol.ImageLoadFunctionType|undefined),
|
||||||
* params: (Object|undefined)}}
|
* params: (Object|undefined)}}
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -3935,6 +3936,15 @@ olx.source.ImageMapGuideOptions.prototype.ratio;
|
|||||||
olx.source.ImageMapGuideOptions.prototype.resolutions;
|
olx.source.ImageMapGuideOptions.prototype.resolutions;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional function to load an image given a URL.
|
||||||
|
* @type {ol.TileLoadFunctionType|undefined}
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
olx.source.ImageMapGuideOptions.prototype.imageLoadFunction;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Additional parameters.
|
* Additional parameters.
|
||||||
* @type {Object|undefined}
|
* @type {Object|undefined}
|
||||||
@@ -4374,6 +4384,7 @@ olx.source.ImageVectorOptions.prototype.style;
|
|||||||
* hidpi: (boolean|undefined),
|
* hidpi: (boolean|undefined),
|
||||||
* serverType: (ol.source.wms.ServerType|string|undefined),
|
* serverType: (ol.source.wms.ServerType|string|undefined),
|
||||||
* logo: (string|olx.LogoOptions|undefined),
|
* logo: (string|olx.LogoOptions|undefined),
|
||||||
|
* imageLoadFunction: (ol.ImageLoadFunctionType|undefined),
|
||||||
* params: Object.<string,*>,
|
* params: Object.<string,*>,
|
||||||
* projection: ol.proj.ProjectionLike,
|
* projection: ol.proj.ProjectionLike,
|
||||||
* ratio: (number|undefined),
|
* ratio: (number|undefined),
|
||||||
@@ -4418,6 +4429,14 @@ olx.source.ImageWMSOptions.prototype.hidpi;
|
|||||||
olx.source.ImageWMSOptions.prototype.serverType;
|
olx.source.ImageWMSOptions.prototype.serverType;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional function to load an image given a URL.
|
||||||
|
* @type {ol.TileLoadFunctionType|undefined}
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
olx.source.ImageWMSOptions.prototype.imageLoadFunction;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logo.
|
* Logo.
|
||||||
* @type {string|olx.LogoOptions|undefined}
|
* @type {string|olx.LogoOptions|undefined}
|
||||||
@@ -4534,6 +4553,7 @@ olx.source.StamenOptions.prototype.url;
|
|||||||
* crossOrigin: (null|string|undefined),
|
* crossOrigin: (null|string|undefined),
|
||||||
* imageExtent: (ol.Extent),
|
* imageExtent: (ol.Extent),
|
||||||
* imageSize: (ol.Size|undefined),
|
* imageSize: (ol.Size|undefined),
|
||||||
|
* imageLoadFunction: (ol.ImageLoadFunctionType|undefined),
|
||||||
* logo: (string|olx.LogoOptions|undefined),
|
* logo: (string|olx.LogoOptions|undefined),
|
||||||
* projection: ol.proj.ProjectionLike,
|
* projection: ol.proj.ProjectionLike,
|
||||||
* url: string}}
|
* url: string}}
|
||||||
@@ -4574,6 +4594,14 @@ olx.source.ImageStaticOptions.prototype.imageExtent;
|
|||||||
olx.source.ImageStaticOptions.prototype.imageSize;
|
olx.source.ImageStaticOptions.prototype.imageSize;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional function to load an image given a URL.
|
||||||
|
* @type {ol.TileLoadFunctionType|undefined}
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
olx.source.ImageStaticOptions.prototype.imageLoadFunction;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logo.
|
* Logo.
|
||||||
* @type {string|olx.LogoOptions|undefined}
|
* @type {string|olx.LogoOptions|undefined}
|
||||||
|
|||||||
@@ -20,9 +20,10 @@ goog.require('ol.extent');
|
|||||||
* @param {Array.<ol.Attribution>} attributions Attributions.
|
* @param {Array.<ol.Attribution>} attributions Attributions.
|
||||||
* @param {string} src Image source URI.
|
* @param {string} src Image source URI.
|
||||||
* @param {?string} crossOrigin Cross origin.
|
* @param {?string} crossOrigin Cross origin.
|
||||||
|
* @param {ol.ImageLoadFunctionType} imageLoadFunction Image load function.
|
||||||
*/
|
*/
|
||||||
ol.Image =
|
ol.Image = function(extent, resolution, pixelRatio, attributions, src,
|
||||||
function(extent, resolution, pixelRatio, attributions, src, crossOrigin) {
|
crossOrigin, imageLoadFunction) {
|
||||||
|
|
||||||
goog.base(this, extent, resolution, pixelRatio, ol.ImageState.IDLE,
|
goog.base(this, extent, resolution, pixelRatio, ol.ImageState.IDLE,
|
||||||
attributions);
|
attributions);
|
||||||
@@ -59,6 +60,13 @@ ol.Image =
|
|||||||
* @type {ol.ImageState}
|
* @type {ol.ImageState}
|
||||||
*/
|
*/
|
||||||
this.state = ol.ImageState.IDLE;
|
this.state = ol.ImageState.IDLE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {ol.ImageLoadFunctionType}
|
||||||
|
*/
|
||||||
|
this.imageLoadFunction_ = imageLoadFunction;
|
||||||
|
|
||||||
};
|
};
|
||||||
goog.inherits(ol.Image, ol.ImageBase);
|
goog.inherits(ol.Image, ol.ImageBase);
|
||||||
|
|
||||||
@@ -66,8 +74,9 @@ goog.inherits(ol.Image, ol.ImageBase);
|
|||||||
/**
|
/**
|
||||||
* @param {Object=} opt_context Object.
|
* @param {Object=} opt_context Object.
|
||||||
* @return {HTMLCanvasElement|Image|HTMLVideoElement} Image.
|
* @return {HTMLCanvasElement|Image|HTMLVideoElement} Image.
|
||||||
|
* @api
|
||||||
*/
|
*/
|
||||||
ol.Image.prototype.getImageElement = function(opt_context) {
|
ol.Image.prototype.getImage = function(opt_context) {
|
||||||
if (goog.isDef(opt_context)) {
|
if (goog.isDef(opt_context)) {
|
||||||
var image;
|
var image;
|
||||||
var key = goog.getUid(opt_context);
|
var key = goog.getUid(opt_context);
|
||||||
@@ -126,7 +135,7 @@ ol.Image.prototype.load = function() {
|
|||||||
goog.events.listenOnce(this.image_, goog.events.EventType.LOAD,
|
goog.events.listenOnce(this.image_, goog.events.EventType.LOAD,
|
||||||
this.handleImageLoad_, false, this)
|
this.handleImageLoad_, false, this)
|
||||||
];
|
];
|
||||||
this.image_.src = this.src_;
|
this.imageLoadFunction_(this, this.src_);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ ol.ImageBase.prototype.getExtent = function() {
|
|||||||
* @param {Object=} opt_context Object.
|
* @param {Object=} opt_context Object.
|
||||||
* @return {HTMLCanvasElement|Image|HTMLVideoElement} Image.
|
* @return {HTMLCanvasElement|Image|HTMLVideoElement} Image.
|
||||||
*/
|
*/
|
||||||
ol.ImageBase.prototype.getImageElement = goog.abstractMethod;
|
ol.ImageBase.prototype.getImage = goog.abstractMethod;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -33,6 +33,6 @@ goog.inherits(ol.ImageCanvas, ol.ImageBase);
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.ImageCanvas.prototype.getImageElement = function(opt_context) {
|
ol.ImageCanvas.prototype.getImage = function(opt_context) {
|
||||||
return this.canvas_;
|
return this.canvas_;
|
||||||
};
|
};
|
||||||
|
|||||||
21
src/ol/imageloadfunction.js
Normal file
21
src/ol/imageloadfunction.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
goog.provide('ol.ImageLoadFunctionType');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function that takes an {@link ol.Image} for the image and a `{string}` for
|
||||||
|
* the src as arguments. It is supposed to make it so the underlying image
|
||||||
|
* {@link ol.Image#getImage} is assigned the content specified by the src. If
|
||||||
|
* not specified, the default is
|
||||||
|
*
|
||||||
|
* function(image, src) {
|
||||||
|
* image.getImage().src = src;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* Providing a custom `imageLoadFunction` can be useful to load images with
|
||||||
|
* post requests or - in general - through XHR requests, where the src of the
|
||||||
|
* image element would be set to a data URI when the content is loaded.
|
||||||
|
*
|
||||||
|
* @typedef {function(ol.Image, string)}
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
ol.ImageLoadFunctionType;
|
||||||
@@ -69,7 +69,7 @@ ol.renderer.canvas.ImageLayer.prototype.forEachFeatureAtPixel =
|
|||||||
*/
|
*/
|
||||||
ol.renderer.canvas.ImageLayer.prototype.getImage = function() {
|
ol.renderer.canvas.ImageLayer.prototype.getImage = function() {
|
||||||
return goog.isNull(this.image_) ?
|
return goog.isNull(this.image_) ?
|
||||||
null : this.image_.getImageElement();
|
null : this.image_.getImage();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ ol.renderer.dom.ImageLayer.prototype.prepareFrame =
|
|||||||
(imageExtent[0] - viewCenter[0]) / imageResolution,
|
(imageExtent[0] - viewCenter[0]) / imageResolution,
|
||||||
(viewCenter[1] - imageExtent[3]) / imageResolution);
|
(viewCenter[1] - imageExtent[3]) / imageResolution);
|
||||||
if (image != this.image_) {
|
if (image != this.image_) {
|
||||||
var imageElement = image.getImageElement(this);
|
var imageElement = image.getImage(this);
|
||||||
// Bootstrap sets the style max-width: 100% for all images, which breaks
|
// Bootstrap sets the style max-width: 100% for all images, which breaks
|
||||||
// prevents the image from being displayed in FireFox. Workaround by
|
// prevents the image from being displayed in FireFox. Workaround by
|
||||||
// overriding the max-width style.
|
// overriding the max-width style.
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ ol.renderer.webgl.ImageLayer.prototype.createTexture_ = function(image) {
|
|||||||
// http://www.khronos.org/webgl/wiki/WebGL_and_OpenGL_Differences#Non-Power_of_Two_Texture_Support
|
// http://www.khronos.org/webgl/wiki/WebGL_and_OpenGL_Differences#Non-Power_of_Two_Texture_Support
|
||||||
// http://learningwebgl.com/blog/?p=2101
|
// http://learningwebgl.com/blog/?p=2101
|
||||||
|
|
||||||
var imageElement = image.getImageElement();
|
var imageElement = image.getImage();
|
||||||
var gl = this.getWebGLMapRenderer().getGL();
|
var gl = this.getWebGLMapRenderer().getGL();
|
||||||
|
|
||||||
var texture = gl.createTexture();
|
var texture = gl.createTexture();
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ goog.provide('ol.source.ImageMapGuide');
|
|||||||
goog.require('goog.object');
|
goog.require('goog.object');
|
||||||
goog.require('goog.uri.utils');
|
goog.require('goog.uri.utils');
|
||||||
goog.require('ol.Image');
|
goog.require('ol.Image');
|
||||||
|
goog.require('ol.ImageLoadFunctionType');
|
||||||
goog.require('ol.ImageUrlFunction');
|
goog.require('ol.ImageUrlFunction');
|
||||||
goog.require('ol.extent');
|
goog.require('ol.extent');
|
||||||
goog.require('ol.source.Image');
|
goog.require('ol.source.Image');
|
||||||
@@ -59,6 +60,13 @@ ol.source.ImageMapGuide = function(options) {
|
|||||||
*/
|
*/
|
||||||
this.imageUrlFunction_ = imageUrlFunction;
|
this.imageUrlFunction_ = imageUrlFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {ol.ImageLoadFunctionType}
|
||||||
|
*/
|
||||||
|
this.imageLoadFunction_ = goog.isDef(options.imageLoadFunction) ?
|
||||||
|
options.imageLoadFunction : ol.source.Image.defaultImageLoadFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {boolean}
|
* @type {boolean}
|
||||||
@@ -140,7 +148,8 @@ ol.source.ImageMapGuide.prototype.getImage =
|
|||||||
var imageUrl = this.imageUrlFunction_(extent, size, projection);
|
var imageUrl = this.imageUrlFunction_(extent, size, projection);
|
||||||
if (goog.isDef(imageUrl)) {
|
if (goog.isDef(imageUrl)) {
|
||||||
image = new ol.Image(extent, resolution, pixelRatio,
|
image = new ol.Image(extent, resolution, pixelRatio,
|
||||||
this.getAttributions(), imageUrl, this.crossOrigin_);
|
this.getAttributions(), imageUrl, this.crossOrigin_,
|
||||||
|
this.imageLoadFunction_);
|
||||||
} else {
|
} else {
|
||||||
image = null;
|
image = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,3 +88,14 @@ ol.source.Image.prototype.findNearestResolution =
|
|||||||
* @return {ol.ImageBase} Single image.
|
* @return {ol.ImageBase} Single image.
|
||||||
*/
|
*/
|
||||||
ol.source.Image.prototype.getImage = goog.abstractMethod;
|
ol.source.Image.prototype.getImage = goog.abstractMethod;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default image load function for image sources that use ol.Image image
|
||||||
|
* instances.
|
||||||
|
* @param {ol.Image} image Image.
|
||||||
|
* @param {string} src Source.
|
||||||
|
*/
|
||||||
|
ol.source.Image.defaultImageLoadFunction = function(image, src) {
|
||||||
|
image.getImage().src = src;
|
||||||
|
};
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ ol.source.ImageStatic = function(options) {
|
|||||||
var crossOrigin = goog.isDef(options.crossOrigin) ?
|
var crossOrigin = goog.isDef(options.crossOrigin) ?
|
||||||
options.crossOrigin : null;
|
options.crossOrigin : null;
|
||||||
|
|
||||||
|
var imageLoadFunction = goog.isDef(options.imageLoadFunction) ?
|
||||||
|
options.imageLoadFunction : ol.source.Image.defaultImageLoadFunction;
|
||||||
|
|
||||||
goog.base(this, {
|
goog.base(this, {
|
||||||
attributions: attributions,
|
attributions: attributions,
|
||||||
logo: options.logo,
|
logo: options.logo,
|
||||||
@@ -46,7 +49,7 @@ ol.source.ImageStatic = function(options) {
|
|||||||
* @type {ol.Image}
|
* @type {ol.Image}
|
||||||
*/
|
*/
|
||||||
this.image_ = new ol.Image(imageExtent, resolution, 1, attributions,
|
this.image_ = new ol.Image(imageExtent, resolution, 1, attributions,
|
||||||
options.url, crossOrigin);
|
options.url, crossOrigin, imageLoadFunction);
|
||||||
|
|
||||||
};
|
};
|
||||||
goog.inherits(ol.source.ImageStatic, ol.source.Image);
|
goog.inherits(ol.source.ImageStatic, ol.source.Image);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ goog.require('goog.string');
|
|||||||
goog.require('goog.uri.utils');
|
goog.require('goog.uri.utils');
|
||||||
goog.require('ol');
|
goog.require('ol');
|
||||||
goog.require('ol.Image');
|
goog.require('ol.Image');
|
||||||
|
goog.require('ol.ImageLoadFunctionType');
|
||||||
goog.require('ol.extent');
|
goog.require('ol.extent');
|
||||||
goog.require('ol.proj');
|
goog.require('ol.proj');
|
||||||
goog.require('ol.source.Image');
|
goog.require('ol.source.Image');
|
||||||
@@ -49,6 +50,13 @@ ol.source.ImageWMS = function(opt_options) {
|
|||||||
*/
|
*/
|
||||||
this.url_ = options.url;
|
this.url_ = options.url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {ol.ImageLoadFunctionType}
|
||||||
|
*/
|
||||||
|
this.imageLoadFunction_ = goog.isDef(options.imageLoadFunction) ?
|
||||||
|
options.imageLoadFunction : ol.source.Image.defaultImageLoadFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {Object}
|
* @type {Object}
|
||||||
@@ -235,7 +243,7 @@ ol.source.ImageWMS.prototype.getImage =
|
|||||||
projection, params);
|
projection, params);
|
||||||
|
|
||||||
this.image_ = new ol.Image(extent, resolution, pixelRatio,
|
this.image_ = new ol.Image(extent, resolution, pixelRatio,
|
||||||
this.getAttributions(), url, this.crossOrigin_);
|
this.getAttributions(), url, this.crossOrigin_, this.imageLoadFunction_);
|
||||||
|
|
||||||
this.renderedRevision_ = this.getRevision();
|
this.renderedRevision_ = this.getRevision();
|
||||||
|
|
||||||
|
|||||||
@@ -134,6 +134,16 @@ describe('ol.source.ImageWMS', function() {
|
|||||||
expect(queryData.get('DPI')).to.be('180');
|
expect(queryData.get('DPI')).to.be('180');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('creates an image with a custom imageLoadFunction', function() {
|
||||||
|
var imageLoadFunction = sinon.spy();
|
||||||
|
options.imageLoadFunction = imageLoadFunction;
|
||||||
|
var source = new ol.source.ImageWMS(options);
|
||||||
|
var image = source.getImage(extent, resolution, pixelRatio, projection);
|
||||||
|
image.load();
|
||||||
|
expect(imageLoadFunction).to.be.called();
|
||||||
|
expect(imageLoadFunction.calledWith(image, image.src_)).to.be(true);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#getGetFeatureInfo', function() {
|
describe('#getGetFeatureInfo', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user