Modify GridLayer update logic to run smoother on zoom pinch & flyTo anims.

This commit is contained in:
Iván Sánchez Ortega 2015-09-09 14:03:12 +02:00
parent 944f2de3a8
commit 7098b04cd7
4 changed files with 20 additions and 19 deletions

View File

@ -52,9 +52,8 @@
var map = L.map('map').setView(dc, 10); var map = L.map('map').setView(dc, 10);
var tiles = L.tileLayer('http://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', { var positron = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: '<a href="https://www.mapbox.com/about/maps/">Terms and Feedback</a>', attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
id: 'examples.map-20v6611k'
}).addTo(map); }).addTo(map);
var marker1 = L.marker(kyiv).addTo(map), var marker1 = L.marker(kyiv).addTo(map),

View File

@ -183,7 +183,7 @@ L.GridLayer = L.Layer.extend({
} }
} }
if (willPrune) { this._pruneTiles(); } if (willPrune && !this._noPrune) { this._pruneTiles(); }
if (nextFrame) { if (nextFrame) {
L.Util.cancelAnimFrame(this._fadeFrame); L.Util.cancelAnimFrame(this._fadeFrame);
@ -334,8 +334,8 @@ L.GridLayer = L.Layer.extend({
}, },
_resetView: function (e) { _resetView: function (e) {
var pinch = e && e.pinch; var animating = e && (e.pinch || e.flyTo);
this._setView(this._map.getCenter(), this._map.getZoom(), pinch, pinch); this._setView(this._map.getCenter(), this._map.getZoom(), animating, animating);
}, },
_animateZoom: function (e) { _animateZoom: function (e) {
@ -351,9 +351,9 @@ L.GridLayer = L.Layer.extend({
tileZoom = Math.max(tileZoom, this.options.minZoom); tileZoom = Math.max(tileZoom, this.options.minZoom);
} }
var tileZoomChanged = (this._tileZoom !== tileZoom); var tileZoomChanged = (Math.abs(tileZoom - this._tileZoom) > 1 || this._tileZoom === undefined);
if (!noUpdate && tileZoomChanged) { if (!noUpdate || tileZoomChanged) {
if (this._abortLoading) { if (this._abortLoading) {
this._abortLoading(); this._abortLoading();
@ -363,13 +363,15 @@ L.GridLayer = L.Layer.extend({
this._updateLevels(); this._updateLevels();
this._resetGrid(); this._resetGrid();
if (!L.Browser.mobileWebkit) {
this._update(center, tileZoom); this._update(center, tileZoom);
}
if (!noPrune) { if (!noPrune) {
this._pruneTiles(); this._pruneTiles();
} }
// Flag to prevent _updateOpacity from pruning tiles during
// a zoom anim or a pinch gesture
this._noPrune = !!noPrune;
} }
this._setZoomTransforms(center, zoom); this._setZoomTransforms(center, zoom);
@ -417,8 +419,7 @@ L.GridLayer = L.Layer.extend({
_onMoveEnd: function () { _onMoveEnd: function () {
if (!this._map) { return; } if (!this._map) { return; }
this._update(); this._resetView();
this._pruneTiles();
}, },
_getTiledPixelBounds: function (center, zoom, tileZoom) { _getTiledPixelBounds: function (center, zoom, tileZoom) {
@ -432,12 +433,12 @@ L.GridLayer = L.Layer.extend({
}, },
_update: function (center, zoom) { _update: function (center, zoom) {
var map = this._map; var map = this._map;
if (!map) { return; } if (!map) { return; }
if (center === undefined) { center = map.getCenter(); } if (center === undefined) { center = map.getCenter(); }
if (zoom === undefined) { zoom = map.getZoom(); } if (zoom === undefined) { zoom = map.getZoom(); }
if (this._tileZoom === undefined) { return; }
var pixelBounds = this._getTiledPixelBounds(center, zoom, this._tileZoom); var pixelBounds = this._getTiledPixelBounds(center, zoom, this._tileZoom);
@ -449,7 +450,7 @@ L.GridLayer = L.Layer.extend({
this._tiles[key].current = false; this._tiles[key].current = false;
} }
if (Math.abs(zoom - this._tileZoom) > 1) { return; } if (Math.abs(zoom - this._tileZoom) > 1) { this._setView(center, zoom); }
// create a queue of coordinates to load tiles from // create a queue of coordinates to load tiles from
for (var j = tileRange.min.y; j <= tileRange.max.y; j++) { for (var j = tileRange.min.y; j <= tileRange.max.y; j++) {
@ -588,7 +589,7 @@ L.GridLayer = L.Layer.extend({
// we know that tile is async and will be ready later; otherwise // we know that tile is async and will be ready later; otherwise
if (this.createTile.length < 2) { if (this.createTile.length < 2) {
// mark tile as ready, but delay one frame for opacity animation to happen // mark tile as ready, but delay one frame for opacity animation to happen
setTimeout(L.bind(this._tileReady, this, coords, null, tile), 0); L.Util.requestAnimFrame(this._tileReady, this, coords, null, tile);
} }
// we prefer top/left over translate3d so that we don't create a HW-accelerated layer from each tile // we prefer top/left over translate3d so that we don't create a HW-accelerated layer from each tile

View File

@ -56,8 +56,8 @@ L.TileLayer = L.GridLayer.extend({
createTile: function (coords, done) { createTile: function (coords, done) {
var tile = document.createElement('img'); var tile = document.createElement('img');
tile.onload = L.bind(this._tileOnLoad, this, done, tile); L.DomEvent.on(tile, 'load', L.bind(this._tileOnLoad, this, done, tile));
tile.onerror = L.bind(this._tileOnError, this, done, tile); L.DomEvent.on(tile, 'error', L.bind(this._tileOnError, this, done, tile));
if (this.options.crossOrigin) { if (this.options.crossOrigin) {
tile.crossOrigin = ''; tile.crossOrigin = '';

View File

@ -52,7 +52,8 @@ L.Map.include({
this._move( this._move(
this.unproject(from.add(to.subtract(from).multiplyBy(u(s) / u1)), startZoom), this.unproject(from.add(to.subtract(from).multiplyBy(u(s) / u1)), startZoom),
this.getScaleZoom(w0 / w(s), startZoom)); this.getScaleZoom(w0 / w(s), startZoom),
{flyTo: true});
} else { } else {
this this