# all files / ol/ imagetile.js

38.89% Statements 21/54
0% Branches 0/14
11.11% Functions 1/9
38.89% Lines 21/54

    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
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    1611×

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

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×
    1×
    1×
    1×
    1×

     goog.provide('ol.ImageTile');

    goog.require('ol');
    goog.require('ol.Tile');
    goog.require('ol.TileState');
    goog.require('ol.dom');
    goog.require('ol.events');
    goog.require('ol.events.EventType');

    /**
     * @constructor
     * @extends {ol.Tile}
     * @param {ol.TileCoord} tileCoord Tile coordinate.
     * @param {ol.TileState} state State.
     * @param {string} src Image source URI.
     * @param {?string} crossOrigin Cross origin.
     * @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function.
     */
    ol.ImageTile = function(tileCoord, state, src, crossOrigin, tileLoadFunction) {

      ol.Tile.call(this, tileCoord, state);

      /**
       * Image URI
       *
       * @private
       * @type {string}
       */
      this.src_ = src;

      /**
       * @private
       * @type {Image|HTMLCanvasElement}
       */
      this.image_ = new Image();
      if (crossOrigin !== null) {
        this.image_.crossOrigin = crossOrigin;
      }

      /**
       * @private
       * @type {Array.<ol.EventsKey>}
       */
      this.imageListenerKeys_ = null;

      /**
       * @private
       * @type {ol.TileLoadFunctionType}
       */
      this.tileLoadFunction_ = tileLoadFunction;

    };
    ol.inherits(ol.ImageTile, ol.Tile);

    /**
     * @inheritDoc
     */
    ol.ImageTile.prototype.disposeInternal = function() {
      if (this.state == ol.TileState.LOADING) {
        this.unlistenImage_();
        this.image_.src = ol.ImageTile.blankImageUrl;
      }
      if (this.interimTile) {
        this.interimTile.dispose();
      }
      this.state = ol.TileState.ABORT;
      this.changed();
      ol.Tile.prototype.disposeInternal.call(this);
    };

    /**
     * Get the HTML image element for this tile (may be a Canvas, Image, or Video).
     * @return {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} Image.
     * @api
     */
    ol.ImageTile.prototype.getImage = function() {
      return this.image_;
    };

    /**
     * @inheritDoc
     */
    ol.ImageTile.prototype.getKey = function() {
      return this.src_;
    };

    /**
     * Tracks loading or read errors.
     *
     * @private
     */
    ol.ImageTile.prototype.handleImageError_ = function() {
      this.state = ol.TileState.ERROR;
      this.unlistenImage_();
      this.image_.src = ol.ImageTile.blankImageUrl;
      this.changed();
    };

    /**
     * Tracks successful image load.
     *
     * @private
     */
    ol.ImageTile.prototype.handleImageLoad_ = function() {
      if (this.image_.naturalWidth && this.image_.naturalHeight) {
        this.state = ol.TileState.LOADED;
      } else {
        this.state = ol.TileState.EMPTY;
      }
      this.unlistenImage_();
      this.changed();
    };

    /**
     * @inheritDoc
     * @api
     */
    ol.ImageTile.prototype.load = function() {
      if (this.state == ol.TileState.IDLE || this.state == ol.TileState.ERROR) {
        this.state = ol.TileState.LOADING;
        this.changed();
        this.imageListenerKeys_ = [
          ol.events.listenOnce(this.image_, ol.events.EventType.ERROR,
              this.handleImageError_, this),
          ol.events.listenOnce(this.image_, ol.events.EventType.LOAD,
              this.handleImageLoad_, this)
        ];
        this.tileLoadFunction_(this, this.src_);
      }
    };

    /**
     * Discards event handlers which listen for load completion or errors.
     *
     * @private
     */
    ol.ImageTile.prototype.unlistenImage_ = function() {
      this.imageListenerKeys_.forEach(ol.events.unlistenByKey);
      this.imageListenerKeys_ = null;
    };

    /**
     * Data URI for a blank image.
     * @type {string}
     */
    ol.ImageTile.blankImageUrl = (function() {
      var ctx = ol.dom.createCanvasContext2D(1, 1);
      ctx.fillStyle = 'rgba(0,0,0,0)';
      ctx.fillRect(0, 0, 1, 1);
      return ctx.canvas.toDataURL('image/png');
    })();