@@ -47,23 +47,11 @@ ol.source.ImageStatic = function(options) {
47
47
this . image_ = new ol . Image ( imageExtent , undefined , 1 , attributions ,
48
48
options . url , crossOrigin , imageLoadFunction ) ;
49
49
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 ;
67
55
68
56
goog . events . listen ( this . image_ , goog . events . EventType . CHANGE ,
69
57
this . handleImageChange , false , this ) ;
@@ -82,3 +70,35 @@ ol.source.ImageStatic.prototype.getImageInternal =
82
70
}
83
71
return null ;
84
72
} ;
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
+ } ;
0 commit comments