Skip to content

Commit

Permalink
Ensure images always fade in completely, fix broken "raster-fade-dura…
Browse files Browse the repository at this point in the history
…tion" property (#3532)

* Ensure images always fade in completely, remove "raster-fade-duration"

* Restore "raster-fade-duration" functionality

* Remove unnecessary whitespace change
  • Loading branch information
lucaswoj authored Nov 3, 2016
1 parent 19cd42f commit cd7f64f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
2 changes: 2 additions & 0 deletions js/render/draw_raster.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function drawRasterTile(painter, sourceCache, layer, coord) {
const tile = sourceCache.getTile(coord);
const posMatrix = painter.transform.calculatePosMatrix(coord, sourceCache.getSource().maxzoom);

tile.setAnimationLoop(painter.style.animationLoop, layer.paint['raster-fade-duration']);

const program = painter.useProgram('raster');
gl.uniformMatrix4fv(program.u_matrix, false, posMatrix);

Expand Down
2 changes: 0 additions & 2 deletions js/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ class RasterTileSource extends Evented {
}
gl.generateMipmap(gl.TEXTURE_2D);

this.map.animationLoop.set(this.map.style.rasterFadeDuration);

tile.state = 'loaded';

callback(null);
Expand Down
7 changes: 3 additions & 4 deletions js/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SourceCache extends Evented {
if (this._sourceLoaded && event.dataType === 'source') {
this.reload();
if (this.transform) {
this.update(this.transform, this.map && this.map.style.rasterFadeDuration);
this.update(this.transform);
}
}
});
Expand Down Expand Up @@ -278,7 +278,7 @@ class SourceCache extends Evented {
* are inside the viewport.
* @private
*/
update(transform, fadeDuration) {
update(transform) {
if (!this._sourceLoaded) { return; }
let i;
let coord;
Expand All @@ -295,7 +295,6 @@ class SourceCache extends Evented {
// the most ideal tile for the current viewport. This may include tiles like
// parent or child tiles that are *already* loaded.
const retain = {};
const now = new Date().getTime();

// Covered is a list of retained tiles who's areas are full covered by other,
// better, retained tiles. They are not drawn separately.
Expand Down Expand Up @@ -339,7 +338,7 @@ class SourceCache extends Evented {
const id = ids[k];
coord = TileCoord.fromID(id);
tile = this._tiles[id];
if (tile && tile.timeAdded > now - (fadeDuration || 0)) {
if (tile && tile.animationLoopEndTime >= Date.now()) {
// This tile is still fading in. Find tiles to cross-fade with it.
if (this.findLoadedChildren(coord, maxCoveringZoom, retain)) {
retain[id] = true;
Expand Down
8 changes: 8 additions & 0 deletions js/source/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class Tile {
this.state = 'loading';
}

setAnimationLoop(animationLoop, t) {
this.animationLoopEndTime = t + Date.now();
if (this.animationLoopId !== undefined) {
animationLoop.cancel(this.animationLoopId);
}
this.animationLoopId = animationLoop.set(t);
}

/**
* Given a data object with a 'buffers' property, load it into
* this tile's elementGroups and buffers properties and set loaded
Expand Down
1 change: 0 additions & 1 deletion js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ class Style extends Evented {

this._updateZoomHistory(z);

this.rasterFadeDuration = 300;
for (const layerId in this._layers) {
const layer = this._layers[layerId];

Expand Down
16 changes: 11 additions & 5 deletions test/js/source/source_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const test = require('mapbox-gl-js-test').test;
const SourceCache = require('../../../js/source/source_cache');
const AnimationLoop = require('../../../js/style/animation_loop');
const Source = require('../../../js/source/source');
const TileCoord = require('../../../js/source/tile_coord');
const Transform = require('../../../js/geo/transform');
Expand Down Expand Up @@ -378,17 +379,19 @@ test('SourceCache#update', (t) => {
const transform = new Transform();
transform.resize(511, 511);
transform.zoom = 2;
const animationLoop = new AnimationLoop();

const sourceCache = createSourceCache({
loadTile: function(tile, callback) {
tile.timeAdded = Infinity;
tile.state = 'loaded';
tile.setAnimationLoop(animationLoop, 100);
callback();
}
});

sourceCache.on('source.load', () => {
sourceCache.update(transform, 100);
sourceCache.update(transform);
t.deepEqual(sourceCache.getIds(), [
new TileCoord(2, 1, 1).id,
new TileCoord(2, 2, 1).id,
Expand All @@ -397,7 +400,7 @@ test('SourceCache#update', (t) => {
]);

transform.zoom = 0;
sourceCache.update(transform, 100);
sourceCache.update(transform);

t.deepEqual(sourceCache.getRenderableIds().length, 5);
t.end();
Expand All @@ -409,22 +412,25 @@ test('SourceCache#update', (t) => {
transform.resize(511, 511);
transform.zoom = 0;

const animationLoop = new AnimationLoop();

const sourceCache = createSourceCache({
loadTile: function(tile, callback) {
tile.timeAdded = Infinity;
tile.state = 'loaded';
tile.setAnimationLoop(animationLoop, 100);
callback();
}
});

sourceCache.on('source.load', () => {
sourceCache.update(transform, 100);
sourceCache.update(transform);

transform.zoom = 2;
sourceCache.update(transform, 100);
sourceCache.update(transform);

transform.zoom = 1;
sourceCache.update(transform, 100);
sourceCache.update(transform);

t.equal(sourceCache._coveredTiles[(new TileCoord(0, 0, 0).id)], true);
t.end();
Expand Down

0 comments on commit cd7f64f

Please sign in to comment.