# all files / ol/source/ imagecanvas.js

28.57% Statements 8/28
0% Branches 0/11
0% Functions 0/2
28.57% Lines 8/28

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

    1×
    1×
    1×
    1×

    1×

    1×

    1×

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

    goog.require('ol');
    goog.require('ol.ImageCanvas');
    goog.require('ol.extent');
    goog.require('ol.source.Image');

    /**
     * @classdesc
     * Base class for image sources where a canvas element is the image.
     *
     * @constructor
     * @extends {ol.source.Image}
     * @param {olx.source.ImageCanvasOptions} options Constructor options.
     * @api
     */
    ol.source.ImageCanvas = function(options) {

      ol.source.Image.call(this, {
        attributions: options.attributions,
        logo: options.logo,
        projection: options.projection,
        resolutions: options.resolutions,
        state: options.state
      });

      /**
       * @private
       * @type {ol.CanvasFunctionType}
       */
      this.canvasFunction_ = options.canvasFunction;

      /**
       * @private
       * @type {ol.ImageCanvas}
       */
      this.canvas_ = null;

      /**
       * @private
       * @type {number}
       */
      this.renderedRevision_ = 0;

      /**
       * @private
       * @type {number}
       */
      this.ratio_ = options.ratio !== undefined ?
        options.ratio : 1.5;

    };
    ol.inherits(ol.source.ImageCanvas, ol.source.Image);

    /**
     * @inheritDoc
     */
    ol.source.ImageCanvas.prototype.getImageInternal = function(extent, resolution, pixelRatio, projection) {
      resolution = this.findNearestResolution(resolution);

      var canvas = this.canvas_;
      if (canvas &&
          this.renderedRevision_ == this.getRevision() &&
          canvas.getResolution() == resolution &&
          canvas.getPixelRatio() == pixelRatio &&
          ol.extent.containsExtent(canvas.getExtent(), extent)) {
        return canvas;
      }

      extent = extent.slice();
      ol.extent.scaleFromCenter(extent, this.ratio_);
      var width = ol.extent.getWidth(extent) / resolution;
      var height = ol.extent.getHeight(extent) / resolution;
      var size = [width * pixelRatio, height * pixelRatio];

      var canvasElement = this.canvasFunction_(
          extent, resolution, pixelRatio, size, projection);
      if (canvasElement) {
        canvas = new ol.ImageCanvas(extent, resolution, pixelRatio,
            this.getAttributions(), canvasElement);
      }
      this.canvas_ = canvas;
      this.renderedRevision_ = this.getRevision();

      return canvas;
    };