Correctly set default anchor for collections

This commit is contained in:
Tim Schaub
2016-08-04 11:27:36 -06:00
parent 795cee876e
commit 69bf9254a5
4 changed files with 104 additions and 1 deletions

View File

@@ -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();
};

View File

@@ -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;

View File

@@ -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;

View File

@@ -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];