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

92.98% Statements 53/57
71.43% Branches 15/21
100% Functions 6/6
92.98% Lines 53/57

    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
    1441×

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

    1×

    47×

    47×

    47×

    1×

    1×
    195×

    1×
    11×

    1×
    54×

    1×
    54×

    1×

    114×
    114×
    114×
    114×
    114×

    114×
    114×
    114×

    114×

    114×
    114×

    114×

    114×
    114×

    114×

    114×
    80×
    80×
    54×

    114×
    54×
    54×
    54×
    54×
    54×

    54×

    54×

    54×
    54×
    54×

    114×

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

    goog.require('ol');
    goog.require('ol.LayerType');
    goog.require('ol.ViewHint');
    goog.require('ol.extent');
    goog.require('ol.renderer.Type');
    goog.require('ol.renderer.canvas.IntermediateCanvas');
    goog.require('ol.transform');

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

      ol.renderer.canvas.IntermediateCanvas.call(this, imageLayer);

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

      /**
       * @private
       * @type {ol.Transform}
       */
      this.imageTransform_ = ol.transform.create();

    };
    ol.inherits(ol.renderer.canvas.ImageLayer, ol.renderer.canvas.IntermediateCanvas);

    /**
     * Determine if this renderer handles the provided layer.
     * @param {ol.renderer.Type} type The renderer type.
     * @param {ol.layer.Layer} layer The candidate layer.
     * @return {boolean} The renderer can render the layer.
     */
    ol.renderer.canvas.ImageLayer['handles'] = function(type, layer) {
      return type === ol.renderer.Type.CANVAS && layer.getType() === ol.LayerType.IMAGE;
    };

    /**
     * Create a layer renderer.
     * @param {ol.renderer.Map} mapRenderer The map renderer.
     * @param {ol.layer.Layer} layer The layer to be rendererd.
     * @return {ol.renderer.canvas.ImageLayer} The layer renderer.
     */
    ol.renderer.canvas.ImageLayer['create'] = function(mapRenderer, layer) {
      return new ol.renderer.canvas.ImageLayer(/** @type {ol.layer.Image} */ (layer));
    };

    /**
     * @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 size = frameState.size;
      var viewState = frameState.viewState;
      var viewCenter = viewState.center;
      var viewResolution = viewState.resolution;

      var image;
      var imageLayer = /** @type {ol.layer.Image} */ (this.getLayer());
      var imageSource = imageLayer.getSource();

      var hints = frameState.viewHints;

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

      Eif (!hints[ol.ViewHint.ANIMATING] && !hints[ol.ViewHint.INTERACTING] &&
          !ol.extent.isEmpty(renderedExtent)) {
        var projection = viewState.projection;
        Iif (!ol.ENABLE_RASTER_REPROJECTION) {
          var sourceProjection = imageSource.getProjection();
          if (sourceProjection) {
            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 scale = pixelRatio * imageResolution /
            (viewResolution * imagePixelRatio);
        var transform = ol.transform.compose(this.imageTransform_,
            pixelRatio * size[0] / 2, pixelRatio * size[1] / 2,
            scale, scale,
            0,
            imagePixelRatio * (imageExtent[0] - viewCenter[0]) / imageResolution,
            imagePixelRatio * (viewCenter[1] - imageExtent[3]) / imageResolution);
        ol.transform.compose(this.coordinateToCanvasPixelTransform,
            pixelRatio * size[0] / 2 - transform[4], pixelRatio * size[1] / 2 - transform[5],
            pixelRatio / viewResolution, -pixelRatio / viewResolution,
            0,
            -viewCenter[0], -viewCenter[1]);

        this.updateAttributions(frameState.attributions, image.getAttributions());
        this.updateLogos(frameState, imageSource);
        this.renderedResolution = viewResolution * pixelRatio / imagePixelRatio;
      }

      return !!this.image_;
    };