Refactor and fix canvas tile layer renderer
This commit is contained in:
@@ -136,42 +136,50 @@ ol.renderer.canvas.TileLayer.prototype.renderFrame =
|
|||||||
|
|
||||||
var canvas, context;
|
var canvas, context;
|
||||||
if (goog.isNull(this.canvas_)) {
|
if (goog.isNull(this.canvas_)) {
|
||||||
|
goog.asserts.assert(goog.isNull(this.canvasSize_));
|
||||||
|
goog.asserts.assert(goog.isNull(this.context_));
|
||||||
|
goog.asserts.assert(goog.isNull(this.renderedCanvasTileRange_));
|
||||||
canvas = /** @type {HTMLCanvasElement} */
|
canvas = /** @type {HTMLCanvasElement} */
|
||||||
(goog.dom.createElement(goog.dom.TagName.CANVAS));
|
(goog.dom.createElement(goog.dom.TagName.CANVAS));
|
||||||
canvas.width = canvasWidth;
|
canvas.width = canvasWidth;
|
||||||
canvas.height = canvasHeight;
|
canvas.height = canvasHeight;
|
||||||
context = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
|
context = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
|
||||||
this.canvas_ = canvas;
|
this.canvas_ = canvas;
|
||||||
|
this.canvasSize_ = new ol.Size(canvasWidth, canvasHeight);
|
||||||
this.context_ = context;
|
this.context_ = context;
|
||||||
this.renderedCanvasTileRange_ = null;
|
|
||||||
} else {
|
} else {
|
||||||
|
goog.asserts.assert(!goog.isNull(this.canvasSize_));
|
||||||
|
goog.asserts.assert(!goog.isNull(this.context_));
|
||||||
canvas = this.canvas_;
|
canvas = this.canvas_;
|
||||||
context = this.context_;
|
context = this.context_;
|
||||||
if (this.canvasSize_.width < canvasWidth ||
|
if (this.canvasSize_.width < canvasWidth ||
|
||||||
this.canvasSize_.height < canvasHeight) {
|
this.canvasSize_.height < canvasHeight) {
|
||||||
|
// Canvas is too small, make it bigger
|
||||||
canvas.width = canvasWidth;
|
canvas.width = canvasWidth;
|
||||||
canvas.height = canvasHeight;
|
canvas.height = canvasHeight;
|
||||||
|
this.canvasSize_.width = canvasWidth;
|
||||||
|
this.canvasSize_.height = canvasHeight;
|
||||||
this.renderedCanvasTileRange_ = null;
|
this.renderedCanvasTileRange_ = null;
|
||||||
} else if (z != this.renderedCanvasZ_ ||
|
} else {
|
||||||
!this.renderedCanvasTileRange_.containsTileRange(tileRange)) {
|
canvasWidth = this.canvasSize_.width;
|
||||||
this.renderedCanvasTileRange_ = null;
|
canvasHeight = this.canvasSize_.height;
|
||||||
|
if (z != this.renderedCanvasZ_ ||
|
||||||
|
!this.renderedCanvasTileRange_.containsTileRange(tileRange)) {
|
||||||
|
this.renderedCanvasTileRange_ = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (goog.isNull(this.renderedCanvasTileRange_)) {
|
||||||
|
context.clearRect(0, 0, canvasWidth, canvasHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var canvasTileRange, canvasTileRangeWidth, minX, minY;
|
var canvasTileRange, canvasTileRangeWidth, minX, minY;
|
||||||
if (z != this.renderedCanvasZ_ ||
|
if (goog.isNull(this.renderedCanvasTileRange_)) {
|
||||||
goog.isNull(this.renderedCanvasTileRange_)) {
|
|
||||||
if (goog.isNull(this.canvasSize_)) {
|
|
||||||
this.canvasSize_ = new ol.Size(canvasWidth, canvasHeight);
|
|
||||||
} else {
|
|
||||||
this.canvasSize_.width = canvasWidth;
|
|
||||||
this.canvasSize_.height = canvasHeight;
|
|
||||||
}
|
|
||||||
canvasTileRangeWidth = canvasWidth / tileSize.width;
|
canvasTileRangeWidth = canvasWidth / tileSize.width;
|
||||||
var canvasTileRangeHeight = canvasHeight / tileSize.height;
|
var canvasTileRangeHeight = canvasHeight / tileSize.height;
|
||||||
minX = tileRange.minX +
|
minX = tileRange.minX -
|
||||||
Math.floor((canvasTileRangeWidth - tileRange.getWidth()) / 2);
|
Math.floor((canvasTileRangeWidth - tileRange.getWidth()) / 2);
|
||||||
minY = tileRange.minY +
|
minY = tileRange.minY -
|
||||||
Math.floor((canvasTileRangeHeight - tileRange.getHeight()) / 2);
|
Math.floor((canvasTileRangeHeight - tileRange.getHeight()) / 2);
|
||||||
this.renderedCanvasZ_ = z;
|
this.renderedCanvasZ_ = z;
|
||||||
this.renderedCanvasTileRange_ = new ol.TileRange(
|
this.renderedCanvasTileRange_ = new ol.TileRange(
|
||||||
@@ -185,7 +193,6 @@ ol.renderer.canvas.TileLayer.prototype.renderFrame =
|
|||||||
canvasTileRangeWidth = canvasTileRange.getWidth();
|
canvasTileRangeWidth = canvasTileRange.getWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
goog.asserts.assert(!goog.isNull(this.canvasSize_));
|
|
||||||
goog.asserts.assert(canvasTileRange.containsTileRange(tileRange));
|
goog.asserts.assert(canvasTileRange.containsTileRange(tileRange));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user