Merge pull request #7267 from tschaub/fade

Render tiles with an opacity transition
This commit is contained in:
Tim Schaub
2017-09-27 19:51:55 -07:00
committed by GitHub
28 changed files with 418 additions and 70 deletions

View File

@@ -16,10 +16,11 @@ goog.require('ol.events.EventType');
* @param {string} src Image source URI.
* @param {?string} crossOrigin Cross origin.
* @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function.
* @param {olx.TileOptions=} opt_options Tile options.
*/
ol.ImageTile = function(tileCoord, state, src, crossOrigin, tileLoadFunction) {
ol.ImageTile = function(tileCoord, state, src, crossOrigin, tileLoadFunction, opt_options) {
ol.Tile.call(this, tileCoord, state);
ol.Tile.call(this, tileCoord, state, opt_options);
/**
* Image URI

View File

@@ -1,5 +1,3 @@
// FIXME find correct globalCompositeOperation
goog.provide('ol.renderer.canvas.TileLayer');
goog.require('ol');
@@ -7,7 +5,6 @@ goog.require('ol.LayerType');
goog.require('ol.TileRange');
goog.require('ol.TileState');
goog.require('ol.ViewHint');
goog.require('ol.array');
goog.require('ol.dom');
goog.require('ol.extent');
goog.require('ol.renderer.Type');
@@ -181,23 +178,29 @@ ol.renderer.canvas.TileLayer.prototype.prepareFrame = function(frameState, layer
tile = tile.getInterimTile();
}
if (this.isDrawableTile_(tile)) {
var uid = ol.getUid(this);
if (tile.getState() == ol.TileState.LOADED) {
tilesToDrawByZ[z][tile.tileCoord.toString()] = tile;
if (!newTiles && this.renderedTiles.indexOf(tile) == -1) {
var inTransition = tile.inTransition(uid);
if (!newTiles && (inTransition || this.renderedTiles.indexOf(tile) === -1)) {
newTiles = true;
}
}
continue;
if (tile.getAlpha(uid, frameState.time) === 1) {
// don't look for alt tiles if alpha is 1
continue;
}
}
var fullyLoaded = tileGrid.forEachTileCoordParentTileRange(
tile.tileCoord, findLoadedTiles, null, tmpTileRange, tmpExtent);
if (!fullyLoaded) {
var childTileRange = tileGrid.getTileCoordChildTileRange(
tile.tileCoord, tmpTileRange, tmpExtent);
if (childTileRange) {
findLoadedTiles(z + 1, childTileRange);
}
var childTileRange = tileGrid.getTileCoordChildTileRange(
tile.tileCoord, tmpTileRange, tmpExtent);
var covered = false;
if (childTileRange) {
covered = findLoadedTiles(z + 1, childTileRange);
}
if (!covered) {
tileGrid.forEachTileCoordParentTileRange(
tile.tileCoord, findLoadedTiles, null, tmpTileRange, tmpExtent);
}
}
@@ -233,7 +236,15 @@ ol.renderer.canvas.TileLayer.prototype.prepareFrame = function(frameState, layer
this.renderedTiles.length = 0;
/** @type {Array.<number>} */
var zs = Object.keys(tilesToDrawByZ).map(Number);
zs.sort(ol.array.numberSafeCompareFunction);
zs.sort(function(a, b) {
if (a === z) {
return 1;
} else if (b === z) {
return -1;
} else {
return a > b ? 1 : a < b ? -1 : 0;
}
});
var currentResolution, currentScale, currentTilePixelSize, currentZ, i, ii;
var tileExtent, tileGutter, tilesToDraw, w, h;
for (i = 0, ii = zs.length; i < ii; ++i) {
@@ -250,7 +261,7 @@ 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;
this.drawTileImage(tile, frameState, layerState, x, y, w, h, tileGutter);
this.drawTileImage(tile, frameState, layerState, x, y, w, h, tileGutter, z === currentZ);
this.renderedTiles.push(tile);
}
}
@@ -293,15 +304,33 @@ ol.renderer.canvas.TileLayer.prototype.prepareFrame = function(frameState, layer
* @param {number} w Width of the tile.
* @param {number} h Height of the tile.
* @param {number} gutter Tile gutter.
* @param {boolean} transition Apply an alpha transition.
*/
ol.renderer.canvas.TileLayer.prototype.drawTileImage = function(tile, frameState, layerState, x, y, w, h, gutter) {
if (!this.getLayer().getSource().getOpaque(frameState.viewState.projection)) {
ol.renderer.canvas.TileLayer.prototype.drawTileImage = function(tile, frameState, layerState, x, y, w, h, gutter, transition) {
var image = tile.getImage(this.getLayer());
if (!image) {
return;
}
var uid = ol.getUid(this);
var alpha = transition ? tile.getAlpha(uid, frameState.time) : 1;
if (alpha === 1 && !this.getLayer().getSource().getOpaque(frameState.viewState.projection)) {
this.context.clearRect(x, y, w, h);
}
var image = tile.getImage(this.getLayer());
if (image) {
this.context.drawImage(image, gutter, gutter,
image.width - 2 * gutter, image.height - 2 * gutter, x, y, w, h);
var alphaChanged = alpha !== this.context.globalAlpha;
if (alphaChanged) {
this.context.save();
this.context.globalAlpha = alpha;
}
this.context.drawImage(image, gutter, gutter,
image.width - 2 * gutter, image.height - 2 * gutter, x, y, w, h);
if (alphaChanged) {
this.context.restore();
}
if (alpha !== 1) {
frameState.animate = true;
} else if (transition) {
tile.endTransition(uid);
}
};

View File

@@ -224,7 +224,7 @@ ol.renderer.canvas.VectorTileLayer.prototype.createReplayGroup_ = function(
* @inheritDoc
*/
ol.renderer.canvas.VectorTileLayer.prototype.drawTileImage = function(
tile, frameState, layerState, x, y, w, h, gutter) {
tile, frameState, layerState, x, y, w, h, gutter, transition) {
var vectorImageTile = /** @type {ol.VectorImageTile} */ (tile);
this.createReplayGroup_(vectorImageTile, frameState);
if (this.context) {

View File

@@ -34,8 +34,7 @@ goog.require('ol.reproj.Triangulation');
ol.reproj.Tile = function(sourceProj, sourceTileGrid,
targetProj, targetTileGrid, tileCoord, wrappedTileCoord,
pixelRatio, gutter, getTileFunction,
opt_errorThreshold,
opt_renderEdges) {
opt_errorThreshold, opt_renderEdges) {
ol.Tile.call(this, tileCoord, ol.TileState.IDLE);
/**

View File

@@ -38,7 +38,8 @@ ol.source.BingMaps = function(options) {
state: ol.source.State.LOADING,
tileLoadFunction: options.tileLoadFunction,
tilePixelRatio: this.hidpi_ ? 2 : 1,
wrapX: options.wrapX !== undefined ? options.wrapX : true
wrapX: options.wrapX !== undefined ? options.wrapX : true,
transition: options.transition
});
/**

View File

@@ -71,6 +71,12 @@ ol.source.Tile = function(options) {
*/
this.key_ = '';
/**
* @protected
* @type {olx.TileOptions}
*/
this.tileOptions = {transition: options.transition};
};
ol.inherits(ol.source.Tile, ol.source.Source);

View File

@@ -39,7 +39,8 @@ ol.source.TileArcGISRest = function(opt_options) {
tileLoadFunction: options.tileLoadFunction,
url: options.url,
urls: options.urls,
wrapX: options.wrapX !== undefined ? options.wrapX : true
wrapX: options.wrapX !== undefined ? options.wrapX : true,
transition: options.transition
});
/**

View File

@@ -39,7 +39,8 @@ ol.source.TileImage = function(options) {
tileUrlFunction: options.tileUrlFunction,
url: options.url,
urls: options.urls,
wrapX: options.wrapX
wrapX: options.wrapX,
transition: options.transition
});
/**
@@ -52,7 +53,7 @@ ol.source.TileImage = function(options) {
/**
* @protected
* @type {function(new: ol.ImageTile, ol.TileCoord, ol.TileState, string,
* ?string, ol.TileLoadFunctionType)}
* ?string, ol.TileLoadFunctionType, olx.TileOptions=)}
*/
this.tileClass = options.tileClass !== undefined ?
options.tileClass : ol.ImageTile;
@@ -222,7 +223,8 @@ ol.source.TileImage.prototype.createTile_ = function(z, x, y, pixelRatio, projec
tileUrl !== undefined ? ol.TileState.IDLE : ol.TileState.EMPTY,
tileUrl !== undefined ? tileUrl : '',
this.crossOrigin,
this.tileLoadFunction);
this.tileLoadFunction,
this.tileOptions);
tile.key = key;
ol.events.listen(tile, ol.events.EventType.CHANGE,
this.handleTileChange, this);

View File

@@ -43,7 +43,8 @@ ol.source.TileJSON = function(options) {
reprojectionErrorThreshold: options.reprojectionErrorThreshold,
state: ol.source.State.LOADING,
tileLoadFunction: options.tileLoadFunction,
wrapX: options.wrapX !== undefined ? options.wrapX : true
wrapX: options.wrapX !== undefined ? options.wrapX : true,
transition: options.transition
});
if (options.url) {

View File

@@ -47,7 +47,8 @@ ol.source.TileWMS = function(opt_options) {
tileLoadFunction: options.tileLoadFunction,
url: options.url,
urls: options.urls,
wrapX: options.wrapX !== undefined ? options.wrapX : true
wrapX: options.wrapX !== undefined ? options.wrapX : true,
transition: options.transition
});
/**

View File

@@ -29,7 +29,8 @@ ol.source.UrlTile = function(options) {
state: options.state,
tileGrid: options.tileGrid,
tilePixelRatio: options.tilePixelRatio,
wrapX: options.wrapX
wrapX: options.wrapX,
transition: options.transition
});
/**

View File

@@ -51,7 +51,8 @@ ol.source.VectorTile = function(options) {
tileUrlFunction: options.tileUrlFunction,
url: options.url,
urls: options.urls,
wrapX: options.wrapX === undefined ? true : options.wrapX
wrapX: options.wrapX === undefined ? true : options.wrapX,
transition: options.transition
});
/**
@@ -125,7 +126,8 @@ ol.source.VectorTile.prototype.getTile = function(z, x, y, pixelRatio, projectio
this.format_, this.tileLoadFunction, urlTileCoord, this.tileUrlFunction,
this.tileGrid, this.getTileGridForProjection(projection),
this.sourceTiles_, pixelRatio, projection, this.tileClass,
this.handleTileChange.bind(this));
this.handleTileChange.bind(this),
this.tileOptions);
this.tileCache.set(tileCoordKey, tile);
return tile;

View File

@@ -166,7 +166,8 @@ ol.source.WMTS = function(options) {
tilePixelRatio: options.tilePixelRatio,
tileUrlFunction: tileUrlFunction,
urls: urls,
wrapX: options.wrapX !== undefined ? options.wrapX : false
wrapX: options.wrapX !== undefined ? options.wrapX : false,
transition: options.transition
});
this.setKey(this.getKeyForDimensions_());

View File

@@ -54,7 +54,8 @@ ol.source.XYZ = function(opt_options) {
tileUrlFunction: options.tileUrlFunction,
url: options.url,
urls: options.urls,
wrapX: options.wrapX !== undefined ? options.wrapX : true
wrapX: options.wrapX !== undefined ? options.wrapX : true,
transition: options.transition
});
};

View File

@@ -139,7 +139,8 @@ ol.source.Zoomify = function(opt_options) {
reprojectionErrorThreshold: options.reprojectionErrorThreshold,
tileClass: ol.source.Zoomify.Tile_,
tileGrid: tileGrid,
tileUrlFunction: tileUrlFunction
tileUrlFunction: tileUrlFunction,
transition: options.transition
});
};
@@ -154,12 +155,13 @@ ol.inherits(ol.source.Zoomify, ol.source.TileImage);
* @param {string} src Image source URI.
* @param {?string} crossOrigin Cross origin.
* @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function.
* @param {olx.TileOptions=} opt_options Tile options.
* @private
*/
ol.source.Zoomify.Tile_ = function(
tileCoord, state, src, crossOrigin, tileLoadFunction) {
tileCoord, state, src, crossOrigin, tileLoadFunction, opt_options) {
ol.ImageTile.call(this, tileCoord, state, src, crossOrigin, tileLoadFunction);
ol.ImageTile.call(this, tileCoord, state, src, crossOrigin, tileLoadFunction, opt_options);
/**
* @private

View File

@@ -2,6 +2,7 @@ goog.provide('ol.Tile');
goog.require('ol');
goog.require('ol.TileState');
goog.require('ol.easing');
goog.require('ol.events.EventTarget');
goog.require('ol.events.EventType');
@@ -15,11 +16,13 @@ goog.require('ol.events.EventType');
* @extends {ol.events.EventTarget}
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {ol.TileState} state State.
* @param {olx.TileOptions=} opt_options Tile options.
*/
ol.Tile = function(tileCoord, state) {
ol.Tile = function(tileCoord, state, opt_options) {
ol.events.EventTarget.call(this);
var options = opt_options ? opt_options : {};
/**
* @type {ol.TileCoord}
*/
@@ -47,6 +50,20 @@ ol.Tile = function(tileCoord, state) {
*/
this.key = '';
/**
* The duration for the opacity transition.
* @type {number}
*/
this.transition_ = options.transition === undefined ?
250 : options.transition;
/**
* Lookup of start times for rendering transitions. If the start time is
* equal to -1, the transition is complete.
* @type {Object.<number, number>}
*/
this.transitionStarts_ = {};
};
ol.inherits(ol.Tile, ol.events.EventTarget);
@@ -161,3 +178,53 @@ ol.Tile.prototype.setState = function(state) {
* @api
*/
ol.Tile.prototype.load = function() {};
/**
* Get the alpha value for rendering.
* @param {number} id An id for the renderer.
* @param {number} time The render frame time.
* @return {number} A number between 0 and 1.
*/
ol.Tile.prototype.getAlpha = function(id, time) {
if (!this.transition_) {
return 1;
}
var start = this.transitionStarts_[id];
if (!start) {
start = time;
this.transitionStarts_[id] = start;
} else if (start === -1) {
return 1;
}
var delta = time - start + (1000 / 60); // avoid rendering at 0
if (delta >= this.transition_) {
return 1;
}
return ol.easing.easeIn(delta / this.transition_);
};
/**
* Determine if a tile is in an alpha transition. A tile is considered in
* transition if tile.getAlpha() has not yet been called or has been called
* and returned 1.
* @param {number} id An id for the renderer.
* @return {boolean} The tile is in transition.
*/
ol.Tile.prototype.inTransition = function(id) {
if (!this.transition_) {
return false;
}
return this.transitionStarts_[id] !== -1;
};
/**
* Mark a transition as complete.
* @param {number} id An id for the renderer.
*/
ol.Tile.prototype.endTransition = function(id) {
if (this.transition_) {
this.transitionStarts_[id] = -1;
}
};

View File

@@ -580,7 +580,8 @@ ol.SourceSourceOptions;
* projection: ol.ProjectionLike,
* state: (ol.source.State|undefined),
* tileGrid: (ol.tilegrid.TileGrid|undefined),
* wrapX: (boolean|undefined)}}
* wrapX: (boolean|undefined),
* transition: (number|undefined)}}
*/
ol.SourceTileOptions;
@@ -599,7 +600,8 @@ ol.SourceTileOptions;
* tileUrlFunction: (ol.TileUrlFunctionType|undefined),
* url: (string|undefined),
* urls: (Array.<string>|undefined),
* wrapX: (boolean|undefined)}}
* wrapX: (boolean|undefined),
* transition: (number|undefined)}}
*/
ol.SourceUrlTileOptions;

View File

@@ -30,12 +30,13 @@ goog.require('ol.featureloader');
* instantiate for source tiles.
* @param {function(this: ol.source.VectorTile, ol.events.Event)} handleTileChange
* Function to call when a source tile's state changes.
* @param {olx.TileOptions=} opt_options Tile options.
*/
ol.VectorImageTile = function(tileCoord, state, src, format, tileLoadFunction,
urlTileCoord, tileUrlFunction, sourceTileGrid, tileGrid, sourceTiles,
pixelRatio, projection, tileClass, handleTileChange) {
pixelRatio, projection, tileClass, handleTileChange, opt_options) {
ol.Tile.call(this, tileCoord, state);
ol.Tile.call(this, tileCoord, state, opt_options);
/**
* @private

View File

@@ -13,10 +13,11 @@ goog.require('ol.TileState');
* @param {string} src Data source url.
* @param {ol.format.Feature} format Feature format.
* @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function.
* @param {olx.TileOptions=} opt_options Tile options.
*/
ol.VectorTile = function(tileCoord, state, src, format, tileLoadFunction) {
ol.VectorTile = function(tileCoord, state, src, format, tileLoadFunction, opt_options) {
ol.Tile.call(this, tileCoord, state);
ol.Tile.call(this, tileCoord, state, opt_options);
/**
* @type {number}