From a9b60057ea990c6c9ec0c63f28bc7c77327fa8a8 Mon Sep 17 00:00:00 2001 From: Antoine Vandecreme Date: Sun, 23 Oct 2016 22:25:16 +0200 Subject: [PATCH] Fix wrapping. --- src/tiledimage.js | 21 ++++++++++----------- src/tilesource.js | 11 +++++++++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/tiledimage.js b/src/tiledimage.js index 0d155325..992cc895 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -1058,16 +1058,12 @@ function updateLevel(tiledImage, haveDrawn, drawLevel, level, levelOpacity, resetCoverage(tiledImage.coverage, level); - if (tiledImage.wrapHorizontal) { - topLeftTile.x -= 1; // left invisible column (othervise we will have empty space after scroll at left) - } else { + if (!tiledImage.wrapHorizontal) { // Adjust for floating point error topLeftTile.x = Math.max(topLeftTile.x, 0); bottomRightTile.x = Math.min(bottomRightTile.x, numberOfTiles.x - 1); } - if (tiledImage.wrapVertical) { - topLeftTile.y -= 1; // top invisible row (othervise we will have empty space after scroll at top) - } else { + if (!tiledImage.wrapVertical) { // Adjust for floating point error topLeftTile.y = Math.max(topLeftTile.y, 0); bottomRightTile.y = Math.min(bottomRightTile.y, numberOfTiles.y - 1); @@ -1078,11 +1074,14 @@ function updateLevel(tiledImage, haveDrawn, drawLevel, level, levelOpacity, for (var x = topLeftTile.x; x <= bottomRightTile.x; x++) { for (var y = topLeftTile.y; y <= bottomRightTile.y; y++) { - var tileBounds = tiledImage.source.getTileBounds(level, x, y); - - if (drawArea.intersection(tileBounds) === null) { - // This tile is outside of the viewport, no need to draw it - continue; + // Optimisation disabled with wrapping because getTileBounds does not + // work correctly with x and y outside of the number of tiles + if (!tiledImage.wrapHorizontal && !tiledImage.wrapVertical) { + var tileBounds = tiledImage.source.getTileBounds(level, x, y); + if (drawArea.intersection(tileBounds) === null) { + // This tile is outside of the viewport, no need to draw it + continue; + } } best = updateTile( diff --git a/src/tilesource.js b/src/tilesource.js index fd0084c4..fa52f61e 100644 --- a/src/tilesource.js +++ b/src/tilesource.js @@ -346,10 +346,17 @@ $.TileSource.prototype = { */ getTileAtPoint: function(level, point) { var widthScaled = this.dimensions.x * this.getLevelScale(level); - var pixelX = point.x * widthScaled; - var pixelY = point.y * widthScaled; + var pixelX = $.positiveModulo(point.x, 1) * widthScaled; + var pixelY = $.positiveModulo(point.y, 1 / this.aspectRatio) * widthScaled; + var x = Math.floor(pixelX / this.getTileWidth()); var y = Math.floor(pixelY / this.getTileHeight()); + + // Fix for wrapping + var numTiles = this.getNumTiles(level); + x += numTiles.x * Math.floor(point.x); + y += numTiles.y * Math.floor(point.y * this.aspectRatio); + return new $.Point(x, y); },