Merge pull request #6493 from ahocevar/vectortile-cleanup

Vector tile cleanup
This commit is contained in:
Andreas Hocevar
2017-02-15 14:21:44 +01:00
committed by GitHub
5 changed files with 39 additions and 40 deletions

View File

@@ -71,7 +71,7 @@ ol.layer.VectorTile.prototype.createRenderer = function(mapRenderer) {
* @api
*/
ol.layer.VectorTile.prototype.getPreload = function() {
return /** @type {number} */ (this.get(ol.layer.VectorTile.Property_.PRELOAD));
return /** @type {number} */ (this.get(ol.layer.TileProperty.PRELOAD));
};
@@ -91,7 +91,7 @@ ol.layer.VectorTile.prototype.getRenderMode = function() {
*/
ol.layer.VectorTile.prototype.getUseInterimTilesOnError = function() {
return /** @type {boolean} */ (
this.get(ol.layer.VectorTile.Property_.USE_INTERIM_TILES_ON_ERROR));
this.get(ol.layer.TileProperty.USE_INTERIM_TILES_ON_ERROR));
};
@@ -116,13 +116,3 @@ ol.layer.VectorTile.prototype.setUseInterimTilesOnError = function(useInterimTil
this.set(
ol.layer.TileProperty.USE_INTERIM_TILES_ON_ERROR, useInterimTilesOnError);
};
/**
* @enum {string}
* @private
*/
ol.layer.VectorTile.Property_ = {
PRELOAD: 'preload',
USE_INTERIM_TILES_ON_ERROR: 'useInterimTilesOnError'
};

View File

@@ -29,12 +29,6 @@ ol.renderer.canvas.IntermediateCanvas = function(layer) {
*/
this.hitCanvasContext_ = null;
/**
* @protected
* @type {number}
*/
this.renderedResolution;
};
ol.inherits(ol.renderer.canvas.IntermediateCanvas, ol.renderer.canvas.Layer);

View File

@@ -26,7 +26,7 @@ ol.renderer.canvas.TileLayer = function(tileLayer) {
* @protected
* @type {CanvasRenderingContext2D}
*/
this.context = ol.dom.createCanvasContext2D();
this.context = this.context === null ? null : ol.dom.createCanvasContext2D();
/**
* @private
@@ -178,19 +178,20 @@ ol.renderer.canvas.TileLayer.prototype.prepareFrame = function(frameState, layer
this.renderedRevision != sourceRevision) ||
oversampling != this.oversampling_) {
var tilePixelSize = tileSource.getTilePixelSize(z, pixelRatio, projection);
var width = Math.round(tileRange.getWidth() * tilePixelSize[0] / oversampling);
var height = Math.round(tileRange.getHeight() * tilePixelSize[1] / oversampling);
var context = this.context;
var canvas = context.canvas;
var opaque = tileSource.getOpaque(projection);
if (canvas.width != width || canvas.height != height) {
this.oversampling_ = oversampling;
canvas.width = width;
canvas.height = height;
} else {
context.clearRect(0, 0, width, height);
oversampling = this.oversampling_;
if (context) {
var tilePixelSize = tileSource.getTilePixelSize(z, pixelRatio, projection);
var width = Math.round(tileRange.getWidth() * tilePixelSize[0] / oversampling);
var height = Math.round(tileRange.getHeight() * tilePixelSize[1] / oversampling);
var canvas = context.canvas;
if (canvas.width != width || canvas.height != height) {
this.oversampling_ = oversampling;
canvas.width = width;
canvas.height = height;
} else {
context.clearRect(0, 0, width, height);
oversampling = this.oversampling_;
}
}
this.renderedTiles.length = 0;
@@ -213,9 +214,6 @@ ol.renderer.canvas.TileLayer.prototype.prepareFrame = function(frameState, layer
y = (imageExtent[3] - tileExtent[3]) / tileResolution * tilePixelRatio / oversampling;
w = currentTilePixelSize[0] * currentScale / oversampling;
h = currentTilePixelSize[1] * currentScale / oversampling;
if (!opaque) {
context.clearRect(x, y, w, h);
}
this.drawTileImage(tile, frameState, layerState, x, y, w, h, tileGutter);
this.renderedTiles.push(tile);
}
@@ -261,6 +259,9 @@ ol.renderer.canvas.TileLayer.prototype.prepareFrame = function(frameState, layer
* @param {number} gutter Tile gutter.
*/
ol.renderer.canvas.TileLayer.prototype.drawTileImage = function(tile, frameState, layerState, x, y, w, h, gutter) {
if (!this.getLayer().getSource().getOpaque(frameState.viewState.projection)) {
this.context.clearRect(x, y, w, h);
}
var image = tile.getImage();
if (image) {
this.context.drawImage(image, gutter, gutter,
@@ -273,7 +274,8 @@ ol.renderer.canvas.TileLayer.prototype.drawTileImage = function(tile, frameState
* @inheritDoc
*/
ol.renderer.canvas.TileLayer.prototype.getImage = function() {
return this.context.canvas;
var context = this.context;
return context ? context.canvas : null;
};

View File

@@ -1,6 +1,7 @@
goog.provide('ol.renderer.canvas.VectorTileLayer');
goog.require('ol');
goog.require('ol.dom');
goog.require('ol.extent');
goog.require('ol.proj');
goog.require('ol.proj.Units');
@@ -22,6 +23,8 @@ goog.require('ol.transform');
*/
ol.renderer.canvas.VectorTileLayer = function(layer) {
this.context = null;
ol.renderer.canvas.TileLayer.call(this, layer);
/**
@@ -74,9 +77,17 @@ ol.renderer.canvas.VectorTileLayer.VECTOR_REPLAYS = {
* @inheritDoc
*/
ol.renderer.canvas.VectorTileLayer.prototype.prepareFrame = function(frameState, layerState) {
var layerRevision = this.getLayer().getRevision();
var layer = this.getLayer();
var layerRevision = layer.getRevision();
if (this.renderedLayerRevision_ != layerRevision) {
this.renderedTiles.length = 0;
var renderMode = layer.getRenderMode();
if (!this.context && renderMode != ol.layer.VectorTileRenderType.VECTOR) {
this.context = ol.dom.createCanvasContext2D();
}
if (this.context && renderMode == ol.layer.VectorTileRenderType.VECTOR) {
this.context = null;
}
}
this.renderedLayerRevision_ = layerRevision;
return ol.renderer.canvas.TileLayer.prototype.prepareFrame.apply(this, arguments);
@@ -184,11 +195,10 @@ ol.renderer.canvas.VectorTileLayer.prototype.drawTileImage = function(
tile, frameState, layerState, x, y, w, h, gutter) {
var vectorTile = /** @type {ol.VectorTile} */ (tile);
this.createReplayGroup_(vectorTile, frameState);
var layer = this.getLayer();
if (layer.getRenderMode() != ol.layer.VectorTileRenderType.VECTOR) {
if (this.context) {
this.renderTileImage_(vectorTile, frameState, layerState);
ol.renderer.canvas.TileLayer.prototype.drawTileImage.apply(this, arguments);
}
ol.renderer.canvas.TileLayer.prototype.drawTileImage.apply(this, arguments);
};

View File

@@ -24,7 +24,7 @@ ol.VectorTile = function(tileCoord, state, src, format, tileLoadFunction) {
* @private
* @type {CanvasRenderingContext2D}
*/
this.context_ = ol.dom.createCanvasContext2D();
this.context_ = null;
/**
* @private
@@ -83,6 +83,9 @@ ol.inherits(ol.VectorTile, ol.Tile);
* @return {CanvasRenderingContext2D} The rendering context.
*/
ol.VectorTile.prototype.getContext = function() {
if (!this.context_) {
this.context_ = ol.dom.createCanvasContext2D();
}
return this.context_;
};