Skip to content

Commit fbba528

Browse files
committed
Improve ol.source.StaticImage
Brings back the imgSize property, and puts all image load handler code in a single listener.
1 parent 5e0b0e6 commit fbba528

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

externs/olx.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4936,6 +4936,7 @@ olx.source.StamenOptions.prototype.url;
49364936
* crossOrigin: (null|string|undefined),
49374937
* imageExtent: (ol.Extent),
49384938
* imageLoadFunction: (ol.ImageLoadFunctionType|undefined),
4939+
* imageSize: (ol.Size|undefined),
49394940
* logo: (string|olx.LogoOptions|undefined),
49404941
* projection: ol.proj.ProjectionLike,
49414942
* url: string}}
@@ -4997,6 +4998,15 @@ olx.source.ImageStaticOptions.prototype.logo;
49974998
olx.source.ImageStaticOptions.prototype.projection;
49984999

49995000

5001+
/**
5002+
* Size of the image in pixels. Usually the image size is auto-detected, so this
5003+
* only needs to be set if auto-detection fails for some reason.
5004+
* @type {ol.Size|undefined}
5005+
* @api stable
5006+
*/
5007+
olx.source.ImageStaticOptions.prototype.imageSize;
5008+
5009+
50005010
/**
50015011
* Image URL.
50025012
* @type {string}

src/ol/source/imagestaticsource.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,11 @@ ol.source.ImageStatic = function(options) {
4747
this.image_ = new ol.Image(imageExtent, undefined, 1, attributions,
4848
options.url, crossOrigin, imageLoadFunction);
4949

50-
goog.events.listen(this.image_, goog.events.EventType.CHANGE, function() {
51-
if (this.image_.getState() == ol.ImageState.LOADED) {
52-
var image = this.image_.getImage();
53-
var resolution = ol.extent.getHeight(imageExtent) / image.height;
54-
var pxWidth = Math.ceil(ol.extent.getWidth(imageExtent) / resolution);
55-
var pxHeight = Math.ceil(ol.extent.getHeight(imageExtent) / resolution);
56-
if (pxWidth !== image.width || pxHeight !== image.height) {
57-
var canvas = /** @type {HTMLCanvasElement} */
58-
(document.createElement('canvas'));
59-
canvas.width = pxWidth;
60-
canvas.height = pxHeight;
61-
var context = canvas.getContext('2d');
62-
context.drawImage(image, 0, 0, canvas.width, canvas.height);
63-
this.image_.setImage(canvas);
64-
}
65-
}
66-
}, false, this);
50+
/**
51+
* @private
52+
* @type {ol.Size}
53+
*/
54+
this.imageSize_ = options.imageSize ? options.imageSize : null;
6755

6856
goog.events.listen(this.image_, goog.events.EventType.CHANGE,
6957
this.handleImageChange, false, this);
@@ -82,3 +70,35 @@ ol.source.ImageStatic.prototype.getImageInternal =
8270
}
8371
return null;
8472
};
73+
74+
75+
/**
76+
* @inheritDoc
77+
*/
78+
ol.source.ImageStatic.prototype.handleImageChange = function(evt) {
79+
if (this.image_.getState() == ol.ImageState.LOADED) {
80+
var imageExtent = this.image_.getExtent();
81+
var image = this.image_.getImage();
82+
var imageWidth, imageHeight;
83+
if (this.imageSize_) {
84+
imageWidth = this.imageSize_[0];
85+
imageHeight = this.imageSize_[1];
86+
} else {
87+
imageWidth = image.width;
88+
imageHeight = image.height;
89+
}
90+
var resolution = ol.extent.getHeight(imageExtent) / imageHeight;
91+
var targetWidth = Math.ceil(ol.extent.getWidth(imageExtent) / resolution);
92+
if (targetWidth !== imageWidth) {
93+
var canvas = /** @type {HTMLCanvasElement} */
94+
(document.createElement('canvas'));
95+
canvas.width = targetWidth;
96+
canvas.height = imageHeight;
97+
var context = canvas.getContext('2d');
98+
context.drawImage(image, 0, 0, imageWidth, imageHeight,
99+
0, 0, canvas.width, canvas.height);
100+
this.image_.setImage(canvas);
101+
}
102+
}
103+
goog.base(this, 'handleImageChange', evt);
104+
};

test/spec/ol/source/imagestaticsource.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ describe('ol.source.ImageStatic', function() {
3333
image.load();
3434
});
3535

36+
it('respects imageSize', function(done) {
37+
var source = new ol.source.ImageStatic({
38+
url: 'spec/ol/source/images/12-655-1583.png',
39+
imageExtent: [
40+
-13629027.891360067, 4539747.983913189,
41+
-13619243.951739565, 4559315.863154193],
42+
imageSize: [256, 512],
43+
projection: projection
44+
});
45+
46+
source.on('imageloadend', function(event) {
47+
expect(image.getImage().width).to.be(256);
48+
expect(image.getImage().height).to.be(256);
49+
done();
50+
});
51+
52+
var image = source.getImage(extent, resolution, pixelRatio, projection);
53+
image.load();
54+
});
55+
3656
it('triggers image load events', function(done) {
3757
var source = new ol.source.ImageStatic({
3858
url: 'spec/ol/source/images/12-655-1583.png',

0 commit comments

Comments
 (0)