From 69bf9254a5df0f9e8b83885340dd4dc75743602b Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 4 Aug 2016 11:27:36 -0600 Subject: [PATCH] Correctly set default anchor for collections --- src/ol/geom/geometrycollection.js | 6 +++- test/spec/ol/geom/geometrycollection.test.js | 37 ++++++++++++++++++++ test/spec/ol/geom/multipolygon.test.js | 31 ++++++++++++++++ test/spec/ol/geom/polygon.test.js | 31 ++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/ol/geom/geometrycollection.js b/src/ol/geom/geometrycollection.js index 0c6a6d52ce..774ff75978 100644 --- a/src/ol/geom/geometrycollection.js +++ b/src/ol/geom/geometrycollection.js @@ -249,9 +249,13 @@ ol.geom.GeometryCollection.prototype.rotate = function(angle, anchor) { * @api */ ol.geom.GeometryCollection.prototype.scale = function(sx, opt_sy, opt_anchor) { + var anchor = opt_anchor; + if (!anchor) { + anchor = ol.extent.getCenter(this.getExtent()); + } var geometries = this.geometries_; for (var i = 0, ii = geometries.length; i < ii; ++i) { - geometries[i].scale(sx, opt_sy, opt_anchor); + geometries[i].scale(sx, opt_sy, anchor); } this.changed(); }; diff --git a/test/spec/ol/geom/geometrycollection.test.js b/test/spec/ol/geom/geometrycollection.test.js index b2b3b45bdd..6fc5816d1e 100644 --- a/test/spec/ol/geom/geometrycollection.test.js +++ b/test/spec/ol/geom/geometrycollection.test.js @@ -170,6 +170,43 @@ describe('ol.geom.GeometryCollection', function() { }); + describe('#scale()', function() { + + it('scales a collection', function() { + var geom = new ol.geom.GeometryCollection([ + new ol.geom.Point([-1, -2]), + new ol.geom.LineString([[0, 0], [1, 2]]) + ]); + geom.scale(10); + var geometries = geom.getGeometries(); + expect(geometries[0].getCoordinates()).to.eql([-10, -20]); + expect(geometries[1].getCoordinates()).to.eql([[0, 0], [10, 20]]); + }); + + it('accepts sx and sy', function() { + var geom = new ol.geom.GeometryCollection([ + new ol.geom.Point([-1, -2]), + new ol.geom.LineString([[0, 0], [1, 2]]) + ]); + geom.scale(2, 3); + var geometries = geom.getGeometries(); + expect(geometries[0].getCoordinates()).to.eql([-2, -6]); + expect(geometries[1].getCoordinates()).to.eql([[0, 0], [2, 6]]); + }); + + it('accepts an anchor', function() { + var geom = new ol.geom.GeometryCollection([ + new ol.geom.Point([-1, -2]), + new ol.geom.LineString([[0, 0], [1, 2]]) + ]); + geom.scale(10, 15, [-1, -2]); + var geometries = geom.getGeometries(); + expect(geometries[0].getCoordinates()).to.eql([-1, -2]); + expect(geometries[1].getCoordinates()).to.eql([[9, 28], [19, 58]]); + }); + + }); + describe('#transform()', function() { var line, multi, point; diff --git a/test/spec/ol/geom/multipolygon.test.js b/test/spec/ol/geom/multipolygon.test.js index aa91d56db8..2189cb4326 100644 --- a/test/spec/ol/geom/multipolygon.test.js +++ b/test/spec/ol/geom/multipolygon.test.js @@ -55,6 +55,37 @@ describe('ol.geom.MultiPolygon', function() { }); + describe('#scale()', function() { + + it('scales a multi-polygon', function() { + var geom = new ol.geom.MultiPolygon([[ + [[-1, -2], [1, -2], [1, 2], [-1, 2], [-1, -2]] + ]]); + geom.scale(10); + var coordinates = geom.getCoordinates(); + expect(coordinates).to.eql([[[[-10, -20], [10, -20], [10, 20], [-10, 20], [-10, -20]]]]); + }); + + it('accepts sx and sy', function() { + var geom = new ol.geom.MultiPolygon([[ + [[-1, -2], [1, -2], [1, 2], [-1, 2], [-1, -2]] + ]]); + geom.scale(2, 3); + var coordinates = geom.getCoordinates(); + expect(coordinates).to.eql([[[[-2, -6], [2, -6], [2, 6], [-2, 6], [-2, -6]]]]); + }); + + it('accepts an anchor', function() { + var geom = new ol.geom.MultiPolygon([[ + [[-1, -2], [1, -2], [1, 2], [-1, 2], [-1, -2]] + ]]); + geom.scale(3, 2, [-1, -2]); + var coordinates = geom.getCoordinates(); + expect(coordinates).to.eql([[[[-1, -2], [5, -2], [5, 6], [-1, 6], [-1, -2]]]]); + }); + + }); + describe('with a simple MultiPolygon', function() { var multiPolygon; diff --git a/test/spec/ol/geom/polygon.test.js b/test/spec/ol/geom/polygon.test.js index a067a4c097..4756814038 100644 --- a/test/spec/ol/geom/polygon.test.js +++ b/test/spec/ol/geom/polygon.test.js @@ -505,6 +505,37 @@ describe('ol.geom.Polygon', function() { }); }); + describe('#scale()', function() { + + it('scales a polygon', function() { + var geom = new ol.geom.Polygon([ + [[-1, -2], [1, -2], [1, 2], [-1, 2], [-1, -2]] + ]); + geom.scale(10); + var coordinates = geom.getCoordinates(); + expect(coordinates).to.eql([[[-10, -20], [10, -20], [10, 20], [-10, 20], [-10, -20]]]); + }); + + it('accepts sx and sy', function() { + var geom = new ol.geom.Polygon([ + [[-1, -2], [1, -2], [1, 2], [-1, 2], [-1, -2]] + ]); + geom.scale(2, 3); + var coordinates = geom.getCoordinates(); + expect(coordinates).to.eql([[[-2, -6], [2, -6], [2, 6], [-2, 6], [-2, -6]]]); + }); + + it('accepts an anchor', function() { + var geom = new ol.geom.Polygon([ + [[-1, -2], [1, -2], [1, 2], [-1, 2], [-1, -2]] + ]); + geom.scale(3, 2, [-1, -2]); + var coordinates = geom.getCoordinates(); + expect(coordinates).to.eql([[[-1, -2], [5, -2], [5, 6], [-1, 6], [-1, -2]]]); + }); + + }); + describe('ol.geom.Polygon.fromExtent', function() { it('creates the correct polygon', function() { var extent = [1, 2, 3, 5];