Merge pull request #1931 from russelldavis/fix-invalidate-size

Fix rounding issues in Map::invalidateSize
This commit is contained in:
Vladimir Agafonkin 2013-08-06 02:43:40 -07:00
commit e18b7eded6
2 changed files with 58 additions and 1 deletions

View File

@ -387,4 +387,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

@ -262,7 +262,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; }