# all files / ol/renderer/canvas/ canvasimagelayerrenderer.js

26.6% Statements 25/94
0% Branches 0/31
14.29% Functions 1/7
26.6% Lines 25/94

    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
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    2161×

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

    1×

    3×

    3×

    3×

    3×

    3×

    1×

    1×

    1×

    1×

    1×

    1×

     goog.provide('ol.renderer.canvas.ImageLayer');

    goog.require('goog.asserts');
    goog.require('goog.functions');
    goog.require('goog.vec.Mat4');
    goog.require('ol.ImageBase');
    goog.require('ol.ViewHint');
    goog.require('ol.dom');
    goog.require('ol.extent');
    goog.require('ol.layer.Image');
    goog.require('ol.proj');
    goog.require('ol.renderer.canvas.Layer');
    goog.require('ol.source.ImageVector');
    goog.require('ol.vec.Mat4');

    /**
     * @constructor
     * @extends {ol.renderer.canvas.Layer}
     * @param {ol.layer.Image} imageLayer Single image layer.
     */
    ol.renderer.canvas.ImageLayer = function(imageLayer) {

      goog.base(this, imageLayer);

      /**
       * @private
       * @type {?ol.ImageBase}
       */
      this.image_ = null;

      /**
       * @private
       * @type {!goog.vec.Mat4.Number}
       */
      this.imageTransform_ = goog.vec.Mat4.createNumber();

      /**
       * @private
       * @type {?goog.vec.Mat4.Number}
       */
      this.imageTransformInv_ = null;

      /**
       * @private
       * @type {CanvasRenderingContext2D}
       */
      this.hitCanvasContext_ = null;

    };
    goog.inherits(ol.renderer.canvas.ImageLayer, ol.renderer.canvas.Layer);

    /**
     * @inheritDoc
     */
    ol.renderer.canvas.ImageLayer.prototype.forEachFeatureAtCoordinate =
        function(coordinate, frameState, callback, thisArg) {
      var layer = this.getLayer();
      var source = layer.getSource();
      var resolution = frameState.viewState.resolution;
      var rotation = frameState.viewState.rotation;
      var skippedFeatureUids = frameState.skippedFeatureUids;
      return source.forEachFeatureAtCoordinate(
          coordinate, resolution, rotation, skippedFeatureUids,
          /**
           * @param {ol.Feature|ol.render.Feature} feature Feature.
           * @return {?} Callback result.
           */
          function(feature) {
            return callback.call(thisArg, feature, layer);
          });
    };

    /**
     * @inheritDoc
     */
    ol.renderer.canvas.ImageLayer.prototype.forEachLayerAtPixel =
        function(pixel, frameState, callback, thisArg) {
      if (!this.getImage()) {
        return undefined;
      }

      if (this.getLayer().getSource() instanceof ol.source.ImageVector) {
        // for ImageVector sources use the original hit-detection logic,
        // so that for example also transparent polygons are detected
        var coordinate = pixel.slice();
        ol.vec.Mat4.multVec2(
            frameState.pixelToCoordinateMatrix, coordinate, coordinate);
        var hasFeature = this.forEachFeatureAtCoordinate(
            coordinate, frameState, goog.functions.TRUE, this);

        if (hasFeature) {
          return callback.call(thisArg, this.getLayer());
        } else {
          return undefined;
        }
      } else {
        // for all other image sources directly check the image
        if (!this.imageTransformInv_) {
          this.imageTransformInv_ = goog.vec.Mat4.createNumber();
          goog.vec.Mat4.invert(this.imageTransform_, this.imageTransformInv_);
        }

        var pixelOnCanvas =
            this.getPixelOnCanvas(pixel, this.imageTransformInv_);

        if (!this.hitCanvasContext_) {
          this.hitCanvasContext_ = ol.dom.createCanvasContext2D(1, 1);
        }

        this.hitCanvasContext_.clearRect(0, 0, 1, 1);
        this.hitCanvasContext_.drawImage(
            this.getImage(), pixelOnCanvas[0], pixelOnCanvas[1], 1, 1, 0, 0, 1, 1);

        var imageData = this.hitCanvasContext_.getImageData(0, 0, 1, 1).data;
        if (imageData[3] > 0) {
          return callback.call(thisArg, this.getLayer());
        } else {
          return undefined;
        }
      }
    };

    /**
     * @inheritDoc
     */
    ol.renderer.canvas.ImageLayer.prototype.getImage = function() {
      return !this.image_ ? null : this.image_.getImage();
    };

    /**
     * @inheritDoc
     */
    ol.renderer.canvas.ImageLayer.prototype.getImageTransform = function() {
      return this.imageTransform_;
    };

    /**
     * @inheritDoc
     */
    ol.renderer.canvas.ImageLayer.prototype.prepareFrame =
        function(frameState, layerState) {

      var pixelRatio = frameState.pixelRatio;
      var viewState = frameState.viewState;
      var viewCenter = viewState.center;
      var viewResolution = viewState.resolution;
      var viewRotation = viewState.rotation;

      var image;
      var imageLayer = this.getLayer();
      goog.asserts.assertInstanceof(imageLayer, ol.layer.Image,
          'layer is an instance of ol.layer.Image');
      var imageSource = imageLayer.getSource();

      var hints = frameState.viewHints;

      var renderedExtent = frameState.extent;
      if (layerState.extent !== undefined) {
        renderedExtent = ol.extent.getIntersection(
            renderedExtent, layerState.extent);
      }

      if (!hints[ol.ViewHint.ANIMATING] && !hints[ol.ViewHint.INTERACTING] &&
          !ol.extent.isEmpty(renderedExtent)) {
        var projection = viewState.projection;
        if (!ol.ENABLE_RASTER_REPROJECTION) {
          var sourceProjection = imageSource.getProjection();
          if (sourceProjection) {
            goog.asserts.assert(ol.proj.equivalent(projection, sourceProjection),
                'projection and sourceProjection are equivalent');
            projection = sourceProjection;
          }
        }
        image = imageSource.getImage(
            renderedExtent, viewResolution, pixelRatio, projection);
        if (image) {
          var loaded = this.loadImage(image);
          if (loaded) {
            this.image_ = image;
          }
        }
      }

      if (this.image_) {
        image = this.image_;
        var imageExtent = image.getExtent();
        var imageResolution = image.getResolution();
        var imagePixelRatio = image.getPixelRatio();
        var xImageResolution = imageResolution[0];
        var yImageResolution = imageResolution[1];
        var xScale = pixelRatio * xImageResolution /
            (viewResolution * imagePixelRatio);
        var yScale = pixelRatio * yImageResolution /
            (viewResolution * imagePixelRatio);
        ol.vec.Mat4.makeTransform2D(this.imageTransform_,
            pixelRatio * frameState.size[0] / 2,
            pixelRatio * frameState.size[1] / 2,
            xScale, yScale,
            viewRotation,
            imagePixelRatio * (imageExtent[0] - viewCenter[0]) / xImageResolution,
            imagePixelRatio * (viewCenter[1] - imageExtent[3]) / yImageResolution);
        this.imageTransformInv_ = null;
        this.updateAttributions(frameState.attributions, image.getAttributions());
        this.updateLogos(frameState, imageSource);
      }

      return true;
    };