Extract logic from getScaleZoom to CRS

Added tests for custom crs with zooms not power of two related
carto
javimolla 9 years ago
parent 1ef9e220e4
commit 76868ad3f4

@ -84,6 +84,9 @@
<!-- /map/handler -->
<script type="text/javascript" src="suites/map/handler/Map.DragSpec.js"></script>
<!-- /geo/crs -->
<script type="text/javascript" src="suites/geo/CRSSpec.js"></script>
<script>
(window.mochaPhantomJS || window.mocha).run();
</script>

@ -157,3 +157,43 @@ describe("CRS.Simple", function () {
});
});
});
describe("CRS", function () {
var crs = L.CRS;
describe("#zoom && #scale", function () {
it("convert zoom to scale and viceversa and return the same values", function () {
var zoom = 2.5;
var scale = crs.scale(zoom);
expect(crs.zoom(scale)).to.eql(zoom);
});
});
});
describe("CRS.ZoomNotPowerOfTwo", function () {
var crs = L.extend({}, L.CRS, {
scale: function (zoom) {
return 256 * Math.pow(1.5, zoom);
},
zoom: function (scale) {
return Math.log(scale / 256) / Math.log(1.5);
}
});
describe("#scale", function () {
it("of zoom levels are related by a power of 1.5", function () {
var zoom = 5;
var scale = crs.scale(zoom);
expect(crs.scale(zoom + 1)).to.eql(1.5 * scale);
expect(crs.zoom(1.5 * scale)).to.eql(zoom + 1);
});
});
describe("#zoom && #scale", function () {
it("convert zoom to scale and viceversa and return the same values", function () {
var zoom = 2;
var scale = crs.scale(zoom);
expect(crs.zoom(scale)).to.eql(zoom);
});
});
});

@ -746,5 +746,13 @@ describe("Map", function () {
});
});
describe('#getScaleZoom && #getZoomScale', function () {
it("convert zoom to scale and viceversa and return the same values", function () {
var toZoom = 6.25;
var fromZoom = 8.5;
var scale = map.getScaleZoom(toZoom, fromZoom);
expect(Math.round(map.getZoomScale(scale, fromZoom) * 100) / 100).to.eql(toZoom);
});
});
});

@ -33,6 +33,10 @@ L.CRS = {
scale: function (zoom) {
return 256 * Math.pow(2, zoom);
},
zoom: function (scale) {
return Math.log(scale / 256) / Math.LN2;
},
// returns the bounds of the world in projected coords if applicable
getProjectedBounds: function (zoom) {

@ -400,11 +400,11 @@ L.Map = L.Evented.extend({
},
getScaleZoom: function (scale, fromZoom) {
var crs = this.options.crs;
fromZoom = fromZoom === undefined ? this._zoom : fromZoom;
return fromZoom + (Math.log(scale) / Math.LN2);
return crs.zoom(scale * crs.scale(fromZoom));
},
// conversion methods
project: function (latlng, zoom) { // (LatLng[, Number]) -> Point

Loading…
Cancel
Save