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

71.15% Statements 74/104
44.44% Branches 8/18
80% Functions 8/10
71.15% Lines 74/104

    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
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    2781×

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

    1×

    76×

    76×

    1×

    1×

    2×

    2×
    2×

    2×
    2×
    2×

    2×

    2×
    2×

    2×
    2×
    2×
    2×
    2×
    2×

    2×

    2×

    2×

    1×

    138×
    138×

    1×

    69×

    1×

    69×

    1×

    1×

    1×

    1×

    67×
    67×
    67×

    1×

    1×

    1×

    1×

    1×

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

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

    1×

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

    goog.require('goog.array');
    goog.require('goog.asserts');
    goog.require('goog.vec.Mat4');
    goog.require('ol.dom');
    goog.require('ol.extent');
    goog.require('ol.layer.Layer');
    goog.require('ol.render.Event');
    goog.require('ol.render.EventType');
    goog.require('ol.render.canvas.Immediate');
    goog.require('ol.renderer.Layer');
    goog.require('ol.vec.Mat4');

    /**
     * @constructor
     * @extends {ol.renderer.Layer}
     * @param {ol.layer.Layer} layer Layer.
     */
    ol.renderer.canvas.Layer = function(layer) {

      goog.base(this, layer);

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

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

    /**
     * @param {olx.FrameState} frameState Frame state.
     * @param {ol.layer.LayerState} layerState Layer state.
     * @param {CanvasRenderingContext2D} context Context.
     */
    ol.renderer.canvas.Layer.prototype.composeFrame =
        function(frameState, layerState, context) {

      this.dispatchPreComposeEvent(context, frameState);

      var image = this.getImage();
      Eif (image) {

        // clipped rendering if layer extent is set
        var extent = layerState.extent;
        var clipped = extent !== undefined;
        Iif (clipped) {
          goog.asserts.assert(extent !== undefined,
              'layerState extent is defined');
          var pixelRatio = frameState.pixelRatio;
          var topLeft = ol.extent.getTopLeft(extent);
          var topRight = ol.extent.getTopRight(extent);
          var bottomRight = ol.extent.getBottomRight(extent);
          var bottomLeft = ol.extent.getBottomLeft(extent);

          ol.vec.Mat4.multVec2(frameState.coordinateToPixelMatrix,
              topLeft, topLeft);
          ol.vec.Mat4.multVec2(frameState.coordinateToPixelMatrix,
              topRight, topRight);
          ol.vec.Mat4.multVec2(frameState.coordinateToPixelMatrix,
              bottomRight, bottomRight);
          ol.vec.Mat4.multVec2(frameState.coordinateToPixelMatrix,
              bottomLeft, bottomLeft);

          context.save();
          context.beginPath();
          context.moveTo(topLeft[0] * pixelRatio, topLeft[1] * pixelRatio);
          context.lineTo(topRight[0] * pixelRatio, topRight[1] * pixelRatio);
          context.lineTo(bottomRight[0] * pixelRatio, bottomRight[1] * pixelRatio);
          context.lineTo(bottomLeft[0] * pixelRatio, bottomLeft[1] * pixelRatio);
          context.clip();
        }

        var imageTransform = this.getImageTransform();
        // for performance reasons, context.save / context.restore is not used
        // to save and restore the transformation matrix and the opacity.
        // see http://jsperf.com/context-save-restore-versus-variable
        var alpha = context.globalAlpha;
        context.globalAlpha = layerState.opacity;

        // for performance reasons, context.setTransform is only used
        // when the view is rotated. see http://jsperf.com/canvas-transform
        Eif (frameState.viewState.rotation === 0) {
          var dx = goog.vec.Mat4.getElement(imageTransform, 0, 3);
          var dy = goog.vec.Mat4.getElement(imageTransform, 1, 3);
          var dw = image.width * goog.vec.Mat4.getElement(imageTransform, 0, 0);
          var dh = image.height * goog.vec.Mat4.getElement(imageTransform, 1, 1);
          context.drawImage(image, 0, 0, +image.width, +image.height,
              Math.round(dx), Math.round(dy), Math.round(dw), Math.round(dh));
        } else {
          context.setTransform(
              goog.vec.Mat4.getElement(imageTransform, 0, 0),
              goog.vec.Mat4.getElement(imageTransform, 1, 0),
              goog.vec.Mat4.getElement(imageTransform, 0, 1),
              goog.vec.Mat4.getElement(imageTransform, 1, 1),
              goog.vec.Mat4.getElement(imageTransform, 0, 3),
              goog.vec.Mat4.getElement(imageTransform, 1, 3));
          context.drawImage(image, 0, 0);
          context.setTransform(1, 0, 0, 1, 0, 0);
        }
        context.globalAlpha = alpha;

        Iif (clipped) {
          context.restore();
        }
      }

      this.dispatchPostComposeEvent(context, frameState);

    };

    /**
     * @param {ol.render.EventType} type Event type.
     * @param {CanvasRenderingContext2D} context Context.
     * @param {olx.FrameState} frameState Frame state.
     * @param {goog.vec.Mat4.Number=} opt_transform Transform.
     * @private
     */
    ol.renderer.canvas.Layer.prototype.dispatchComposeEvent_ =
        function(type, context, frameState, opt_transform) {
      var layer = this.getLayer();
      Iif (layer.hasListener(type)) {
        var transform = opt_transform !== undefined ?
            opt_transform : this.getTransform(frameState, 0);
        var render = new ol.render.canvas.Immediate(
            context, frameState.pixelRatio, frameState.extent, transform,
            frameState.viewState.rotation);
        var composeEvent = new ol.render.Event(type, layer, render, frameState,
            context, null);
        layer.dispatchEvent(composeEvent);
        render.flush();
      }
    };

    /**
     * @param {CanvasRenderingContext2D} context Context.
     * @param {olx.FrameState} frameState Frame state.
     * @param {goog.vec.Mat4.Number=} opt_transform Transform.
     * @protected
     */
    ol.renderer.canvas.Layer.prototype.dispatchPostComposeEvent =
        function(context, frameState, opt_transform) {
      this.dispatchComposeEvent_(ol.render.EventType.POSTCOMPOSE, context,
          frameState, opt_transform);
    };

    /**
     * @param {CanvasRenderingContext2D} context Context.
     * @param {olx.FrameState} frameState Frame state.
     * @param {goog.vec.Mat4.Number=} opt_transform Transform.
     * @protected
     */
    ol.renderer.canvas.Layer.prototype.dispatchPreComposeEvent =
        function(context, frameState, opt_transform) {
      this.dispatchComposeEvent_(ol.render.EventType.PRECOMPOSE, context,
          frameState, opt_transform);
    };

    /**
     * @param {CanvasRenderingContext2D} context Context.
     * @param {olx.FrameState} frameState Frame state.
     * @param {goog.vec.Mat4.Number=} opt_transform Transform.
     * @protected
     */
    ol.renderer.canvas.Layer.prototype.dispatchRenderEvent =
        function(context, frameState, opt_transform) {
      this.dispatchComposeEvent_(ol.render.EventType.RENDER, context,
          frameState, opt_transform);
    };

    /**
     * @return {HTMLCanvasElement|HTMLVideoElement|Image} Canvas.
     */
    ol.renderer.canvas.Layer.prototype.getImage = goog.abstractMethod;

    /**
     * @return {!goog.vec.Mat4.Number} Image transform.
     */
    ol.renderer.canvas.Layer.prototype.getImageTransform = goog.abstractMethod;

    /**
     * @param {olx.FrameState} frameState Frame state.
     * @param {number} offsetX Offset on the x-axis in view coordinates.
     * @protected
     * @return {!goog.vec.Mat4.Number} Transform.
     */
    ol.renderer.canvas.Layer.prototype.getTransform =
        function(frameState, offsetX) {
      var viewState = frameState.viewState;
      var pixelRatio = frameState.pixelRatio;
      return ol.vec.Mat4.makeTransform2D(this.transform_,
          pixelRatio * frameState.size[0] / 2,
          pixelRatio * frameState.size[1] / 2,
          pixelRatio / viewState.resolution,
          -pixelRatio / viewState.resolution,
          -viewState.rotation,
          -viewState.center[0] + offsetX,
          -viewState.center[1]);
    };

    /**
     * @param {olx.FrameState} frameState Frame state.
     * @param {ol.layer.LayerState} layerState Layer state.
     * @return {boolean} whether composeFrame should be called.
     */
    ol.renderer.canvas.Layer.prototype.prepareFrame = goog.abstractMethod;

    /**
     * @param {ol.Pixel} pixelOnMap Pixel.
     * @param {goog.vec.Mat4.Number} imageTransformInv The transformation matrix
     *        to convert from a map pixel to a canvas pixel.
     * @return {ol.Pixel}
     * @protected
     */
    ol.renderer.canvas.Layer.prototype.getPixelOnCanvas =
        function(pixelOnMap, imageTransformInv) {
      var pixelOnCanvas = [0, 0];
      ol.vec.Mat4.multVec2(imageTransformInv, pixelOnMap, pixelOnCanvas);
      return pixelOnCanvas;
    };

    /**
     * @param {ol.Size} size Size.
     * @return {boolean} True when the canvas with the current size does not exceed
     *     the maximum dimensions.
     */
    ol.renderer.canvas.Layer.testCanvasSize = (function() {

      /**
       * @type {CanvasRenderingContext2D}
       */
      var context = null;

      /**
       * @type {ImageData}
       */
      var imageData = null;

      return function(size) {
        Eif (!context) {
          context = ol.dom.createCanvasContext2D(1, 1);
          imageData = context.createImageData(1, 1);
          var data = imageData.data;
          data[0] = 42;
          data[1] = 84;
          data[2] = 126;
          data[3] = 255;
        }
        var canvas = context.canvas;
        var good = size[0] <= canvas.width && size[1] <= canvas.height;
        Eif (!good) {
          canvas.width = size[0];
          canvas.height = size[1];
          var x = size[0] - 1;
          var y = size[1] - 1;
          context.putImageData(imageData, x, y);
          var result = context.getImageData(x, y, 1, 1);
          good = goog.array.equals(imageData.data, result.data);
        }
        return good;
      };
    })();