Fix rounding issues in Map::invalidateSize

This commit is contained in:
Russell Davis 2013-08-01 23:09:49 -07:00
parent 16db077b2a
commit f568768142
2 changed files with 58 additions and 1 deletions

View File

@ -374,4 +374,59 @@ describe("Map", function () {
expect(spy.thisValues[0]).to.eql(map);
});
});
describe("#invalidateSize", function () {
var container,
orig_width = 100;
beforeEach(function () {
container = map.getContainer();
container.style.width = orig_width + "px";
document.body.appendChild(container);
map.setView([0, 0], 0);
map.invalidateSize({pan: false});
});
afterEach(function () {
document.body.removeChild(container);
});
it("pans by the right amount when growing in 1px increments", function () {
container.style.width = (orig_width + 1) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(1);
container.style.width = (orig_width + 2) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(1);
container.style.width = (orig_width + 3) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(2);
});
it("pans by the right amount when shrinking in 1px increments", function () {
container.style.width = (orig_width - 1) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(0);
container.style.width = (orig_width - 2) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(-1);
container.style.width = (orig_width - 3) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(-1);
});
it("pans back to the original position after growing by an odd size then returning to the original size", function () {
container.style.width = (orig_width + 5) + "px";
map.invalidateSize();
container.style.width = orig_width + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(0);
});
});
});

View File

@ -261,7 +261,9 @@ L.Map = L.Class.extend({
if (!this._loaded) { return this; }
var newSize = this.getSize(),
offset = oldSize.subtract(newSize).divideBy(2).round();
oldCenter = oldSize.divideBy(2).round(),
newCenter = newSize.divideBy(2).round(),
offset = oldCenter.subtract(newCenter);
if (!offset.x && !offset.y) { return this; }