Merge pull request #4426 from ahocevar/nicer-staticimage
Improve ol.source.StaticImage
This commit is contained in:
@@ -47,23 +47,11 @@ ol.source.ImageStatic = function(options) {
|
||||
this.image_ = new ol.Image(imageExtent, undefined, 1, attributions,
|
||||
options.url, crossOrigin, imageLoadFunction);
|
||||
|
||||
goog.events.listen(this.image_, goog.events.EventType.CHANGE, function() {
|
||||
if (this.image_.getState() == ol.ImageState.LOADED) {
|
||||
var image = this.image_.getImage();
|
||||
var resolution = ol.extent.getHeight(imageExtent) / image.height;
|
||||
var pxWidth = Math.ceil(ol.extent.getWidth(imageExtent) / resolution);
|
||||
var pxHeight = Math.ceil(ol.extent.getHeight(imageExtent) / resolution);
|
||||
if (pxWidth !== image.width || pxHeight !== image.height) {
|
||||
var canvas = /** @type {HTMLCanvasElement} */
|
||||
(document.createElement('canvas'));
|
||||
canvas.width = pxWidth;
|
||||
canvas.height = pxHeight;
|
||||
var context = canvas.getContext('2d');
|
||||
context.drawImage(image, 0, 0, canvas.width, canvas.height);
|
||||
this.image_.setImage(canvas);
|
||||
}
|
||||
}
|
||||
}, false, this);
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.Size}
|
||||
*/
|
||||
this.imageSize_ = options.imageSize ? options.imageSize : null;
|
||||
|
||||
goog.events.listen(this.image_, goog.events.EventType.CHANGE,
|
||||
this.handleImageChange, false, this);
|
||||
@@ -82,3 +70,35 @@ ol.source.ImageStatic.prototype.getImageInternal =
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.source.ImageStatic.prototype.handleImageChange = function(evt) {
|
||||
if (this.image_.getState() == ol.ImageState.LOADED) {
|
||||
var imageExtent = this.image_.getExtent();
|
||||
var image = this.image_.getImage();
|
||||
var imageWidth, imageHeight;
|
||||
if (this.imageSize_) {
|
||||
imageWidth = this.imageSize_[0];
|
||||
imageHeight = this.imageSize_[1];
|
||||
} else {
|
||||
imageWidth = image.width;
|
||||
imageHeight = image.height;
|
||||
}
|
||||
var resolution = ol.extent.getHeight(imageExtent) / imageHeight;
|
||||
var targetWidth = Math.ceil(ol.extent.getWidth(imageExtent) / resolution);
|
||||
if (targetWidth != imageWidth) {
|
||||
var canvas = /** @type {HTMLCanvasElement} */
|
||||
(document.createElement('canvas'));
|
||||
canvas.width = targetWidth;
|
||||
canvas.height = /** @type {number} */ (imageHeight);
|
||||
var context = canvas.getContext('2d');
|
||||
context.drawImage(image, 0, 0, imageWidth, imageHeight,
|
||||
0, 0, canvas.width, canvas.height);
|
||||
this.image_.setImage(canvas);
|
||||
}
|
||||
}
|
||||
goog.base(this, 'handleImageChange', evt);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user