Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mouse events on overzoomed canvas tiles #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Leaflet.Renderer.Canvas.Tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,23 @@ L.Canvas.Tile = L.Canvas.extend({
},

getOffset: function() {
return this._tileCoord.scaleBy(this._size).subtract(this._map.getPixelOrigin());
return this._tileCoord
.scaleBy(this._size)
.multiplyBy(this.getOverZoomFactor())
.subtract(this._map.getPixelOrigin())
.divideBy(this.getOverZoomFactor());
},

getOverZoomFactor: function() {
var numberOfZoomLevels = Math.max(0, this._map.getZoom() - this._tileCoord.z);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a case in leaflet fractional zooming where the tiles are "underzoomed".
So this._map.getZoom() is smaller than _tileCoord.z.
By Removing the "0"-Limit "underzoomed" cases will work as well.

Suggested change
var numberOfZoomLevels = Math.max(0, this._map.getZoom() - this._tileCoord.z);
var numberOfZoomLevels = this._map.getZoom() - this._tileCoord.z;

This Case happens when using zoomSnap of 0.25. Fractions < 0.5 take overzoom, Fractions >= 0.5 underzoom

return Math.pow(2, numberOfZoomLevels);
},

getPointFromMouseEvent: function(e) {
return this._map
.mouseEventToLayerPoint(e)
.divideBy(this.getOverZoomFactor())
.subtract(this.getOffset());
},

onAdd: L.Util.falseFn,
Expand All @@ -43,7 +59,7 @@ L.Canvas.Tile = L.Canvas.extend({
},

_onClick: function (e) {
var point = this._map.mouseEventToLayerPoint(e).subtract(this.getOffset()), layer, clickedLayer;
var point = this.getPointFromMouseEvent(e), layer, clickedLayer;

for (var id in this._layers) {
layer = this._layers[id];
Expand All @@ -60,7 +76,7 @@ L.Canvas.Tile = L.Canvas.extend({
_onMouseMove: function (e) {
if (!this._map || this._map.dragging.moving() || this._map._animatingZoom) { return; }

var point = this._map.mouseEventToLayerPoint(e).subtract(this.getOffset());
var point = this.getPointFromMouseEvent(e);
this._handleMouseHover(e, point);
},

Expand Down