# all files / ol/source/ imagestaticsource.js

97.62% Statements 41/42
78.57% Branches 11/14
100% Functions 3/3
97.62% Lines 41/42

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    991×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    30×

    30×

    30×

    30×

    30×

    30×

    30×

    1×

    1×
    78×
    78×

    1×
    54×
    27×
    27×
    27×
    27×
    1×
    1×

    26×
    26×

    27×
    27×
    27×
    2×

    2×
    2×
    2×
    2×

    2×

    54×

     goog.provide('ol.source.ImageStatic');

    goog.require('ol.events');
    goog.require('ol.events.EventType');
    goog.require('ol.Image');
    goog.require('ol.ImageLoadFunctionType');
    goog.require('ol.ImageState');
    goog.require('ol.extent');
    goog.require('ol.proj');
    goog.require('ol.source.Image');

    /**
     * @classdesc
     * A layer source for displaying a single, static image.
     *
     * @constructor
     * @extends {ol.source.Image}
     * @param {olx.source.ImageStaticOptions} options Options.
     * @api stable
     */
    ol.source.ImageStatic = function(options) {
      var imageExtent = options.imageExtent;

      var crossOrigin = options.crossOrigin !== undefined ?
          options.crossOrigin : null;

      var /** @type {ol.ImageLoadFunctionType} */ imageLoadFunction =
          options.imageLoadFunction !== undefined ?
          options.imageLoadFunction : ol.source.Image.defaultImageLoadFunction;

      goog.base(this, {
        attributions: options.attributions,
        logo: options.logo,
        projection: ol.proj.get(options.projection)
      });

      /**
       * @private
       * @type {ol.Image}
       */
      this.image_ = new ol.Image(imageExtent, undefined, 1, this.getAttributions(),
          options.url, crossOrigin, imageLoadFunction);

      /**
       * @private
       * @type {ol.Size}
       */
      this.imageSize_ = options.imageSize ? options.imageSize : null;

      ol.events.listen(this.image_, ol.events.EventType.CHANGE,
          this.handleImageChange, this);

    };
    goog.inherits(ol.source.ImageStatic, ol.source.Image);

    /**
     * @inheritDoc
     */
    ol.source.ImageStatic.prototype.getImageInternal = function(extent, resolution, pixelRatio, projection) {
      Eif (ol.extent.intersects(extent, this.image_.getExtent())) {
        return this.image_;
      }
      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);
    };