Skip to content

Commit

Permalink
fix tilelayer animation
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhenn committed Aug 4, 2017
1 parent 234b0ac commit 8fb92d2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
14 changes: 14 additions & 0 deletions debug/tilelayer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<meta charset="UTF-8"/>
<script type="text/javascript" src="maptalks.js"></script>
<title>TileLayer Tests</title>
<!-- <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script> -->
<style>
html,
body {
Expand Down Expand Up @@ -127,8 +129,20 @@
duration : 4000
})
});


function createMap(div, options, cb) {

/*var map = L.map(div).setView([51.505, -0.09], 13);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.marker([51.5, -0.09]).addTo(map)
.bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
.openPopup();*/

const opts = maptalks.Util.extend({
center: center,
zoom: 14,
Expand Down
23 changes: 8 additions & 15 deletions src/map/Map.Anim.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,8 @@ Map.include({
}
}
});
if (props['zoom'] || !this.isTransforming() && (props['pitch'] || props['bearing'])) {
// reset map container if will zoom or rotate
// force tilelayer to reset, to fix the incorrect offset of tiles if animation starts without pitch and bearing.
this._zoom(this.getZoom());
renderer.callInNextFrame(() => {
this._startAnim(props, zoomOrigin);
});
} else {
this._startAnim(props, zoomOrigin);
}

this._startAnim(props, zoomOrigin);

return this;
},
Expand All @@ -143,6 +135,11 @@ Map.include({

_endAnim(props, zoomOrigin, options) {
delete this._animRotating;
if (this._animPlayer) {
const evtType = this._animPlayer._interupted ? 'animateinterupted' : 'animateend';
delete this._animPlayer;
this._fireEvent(evtType);
}
if (props['center']) {
this.onMoveEnd();
}
Expand All @@ -153,10 +150,6 @@ Map.include({
this.onZooming(props['zoom'][1], zoomOrigin);
}
}
if (this._animPlayer) {
this._fireEvent(this._animPlayer._interupted ? 'animateinterupted' : 'animateend');
delete this._animPlayer;
}
},

_startAnim(props, zoomOrigin) {
Expand All @@ -172,8 +165,8 @@ Map.include({
if (props['pitch'] || props['bearing']) {
this._animRotating = true;
}
this._animPlayer.play();
this._fireEvent('animatestart');
this._animPlayer.play();
},

_stopAnim() {
Expand Down
19 changes: 9 additions & 10 deletions src/map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,15 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
/**
* Sets zoom of the map
* @param {Number} zoom
* @param {Object} [options=null] options
* @param {Boolean} [options.animation=true] whether zoom is animation, true by default
* @returns {Map} this
*/
setZoom(zoom) {
setZoom(zoom, options = { 'animation' : true }) {
if (isNaN(zoom) || isNil(zoom)) {
return this;
}
if (this._loaded && this.options['zoomAnimation']) {
if (this._loaded && this.options['zoomAnimation'] && options['animation']) {
this._zoomAnimation(zoom);
} else {
this._zoom(zoom);
Expand Down Expand Up @@ -666,7 +668,7 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
* @return {Map} this
*/
zoomOut() {
return this.setZoom(this.getZoom() + 1);
return this.setZoom(this.getZoom() - 1);
}

/**
Expand Down Expand Up @@ -694,10 +696,7 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
setCenterAndZoom(center, zoom) {
if (!isNil(zoom) && this._zoomLevel !== zoom) {
this.setCenter(center);
const a = this.options['zoomAnimation'];
this.config('zoomAnimation', false);
this.setZoom(zoom);
this.config('zoomAnimation', a);
this.setZoom(zoom, { animation : false });
} else {
this.setCenter(center);
}
Expand Down Expand Up @@ -1260,9 +1259,9 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
const hided = (watched['width'] === 0 || watched['height'] === 0 || oldWidth === 0 || oldHeight === 0);

if (justStart || hided) {
this._eventSuppressed = true;
this._noEvent = true;
this.setCenter(center);
this._eventSuppressed = false;
delete this._noEvent;
}
/**
* resize event when map container's size changes
Expand Down Expand Up @@ -1610,7 +1609,7 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {


_fireEvent(eventName, param) {
if (this._eventSuppressed) {
if (this._noEvent) {
return;
}
//fire internal events at first
Expand Down
4 changes: 1 addition & 3 deletions src/map/handler/Map.ScrollWheelZoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class MapScrollWheelZoomHandler extends Handler {
map.onZoomStart(null, origin);
this._origin = origin;
this._delta = levelValue;
this._startTime = Date.now();
}
const duration = 180;
map.animateTo({
Expand All @@ -45,7 +44,7 @@ class MapScrollWheelZoomHandler extends Handler {
'duration' : duration,
'wheelZoom' : true,
'onFinish' : () => {
if (this._requesting < 3 || this._requesting >= 10) {
if (this._requesting === 0) {
map.animateTo({
'zoom' : nextZoom,
'around' : this._origin
Expand All @@ -57,7 +56,6 @@ class MapScrollWheelZoomHandler extends Handler {
});
delete this._origin;
delete this._delta;
delete this._timeout;
this._requesting = 0;
} else if (!isNil(this._requesting)) {
delete this._zooming;
Expand Down
11 changes: 5 additions & 6 deletions src/renderer/layer/tilelayer/TileLayerDomRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export default class TileLayerDomRenderer extends Class {
if (this._endZoom < this._startZoom && next === tileZoom) {
this._abortLoading(false);
this._renderTiles();
this._pruneLevels();
this._curAnimZoom = this._tileZoom;

const nextZoom = this._curAnimZoom + gap * s;
Expand All @@ -128,7 +129,7 @@ export default class TileLayerDomRenderer extends Class {
this._preloadTiles(nextGrid.tiles.slice(0, curTilesCount));
}
} else {
this._updateContainer();
this._drawOnZooming();
}
} else if (map.isRotating()) {
this._drawOnDragRotating();
Expand Down Expand Up @@ -362,15 +363,13 @@ export default class TileLayerDomRenderer extends Class {
}
const mapOffset = map.getViewPoint().round();
let tileOffset;
if (map.isZooming()) {
if (map.isZooming() && !map.isMoving()) {
// when map is zooming, mapOffset is fixed when zoom starts
// should multiply with zoom fraction if zoom start from a fractional zoom
const startFraction = map.getResolution(tileZoom) / map.getResolution(this._startZoom);
tileOffset = mapOffset.multi(1 / startFraction);
// centerOffset = centerOffset.multi(1 / startFraction);
} else {
tileOffset = mapOffset.multi(1 / fraction);
// centerOffset = centerOffset.multi(1 / fraction);
}
if (centerOffset) {
tileOffset._add(centerOffset);
Expand Down Expand Up @@ -692,7 +691,7 @@ export default class TileLayerDomRenderer extends Class {
container.style.cssText = POSITION0;

const tileContainer = createEl('div');
tileContainer.style.cssText = POSITION0 + ';will-change:transform';
tileContainer.style.cssText = POSITION0;// + ';will-change:transform';
container.appendChild(tileContainer);
container.tile = tileContainer;
this._container.appendChild(container);
Expand Down Expand Up @@ -775,7 +774,7 @@ export default class TileLayerDomRenderer extends Class {
if (!this.getMap() || !this._levelContainers) {
return;
}
if (!this._zoomParam && this._tileZoom !== this._endZoom) {
if (!this._zoomParam && this._tileZoom !== this.layer._getTileZoom()) {
// zoom without animation
this._removeTileContainer(this._tileZoom);
}
Expand Down

0 comments on commit 8fb92d2

Please sign in to comment.