Add L.transformation factory, allow creation from array (#5282)

* added transformation getter

* fixed lint, added editorconfig

* added CRS.Simple transformation values again

* removed editorconfig
This commit is contained in:
Markus 2017-01-30 09:43:50 +01:00 committed by Per Liedman
parent 60d1f78977
commit 3ac37c29a4
6 changed files with 39 additions and 7 deletions

View File

@ -28,4 +28,14 @@ describe("Transformation", function () {
expect(t.untransform(new L.Point(12, 64))).to.eql(new L.Point(10, 20));
});
});
describe('#constructor', function () {
it("allows an array property for a", function () {
var t2 = L.transformation([1, 2, 3, 4]);
expect(t._a).to.eql(t2._a);
expect(t._b).to.eql(t2._b);
expect(t._c).to.eql(t2._c);
expect(t._d).to.eql(t2._d);
});
});
});

View File

@ -11,6 +11,6 @@ L.CRS.EPSG3395 = L.extend({}, L.CRS.Earth, {
transformation: (function () {
var scale = 0.5 / (Math.PI * L.Projection.Mercator.R);
return new L.Transformation(scale, 0.5, -scale, 0.5);
return L.transformation(scale, 0.5, -scale, 0.5);
}())
});

View File

@ -13,7 +13,7 @@ L.CRS.EPSG3857 = L.extend({}, L.CRS.Earth, {
transformation: (function () {
var scale = 0.5 / (Math.PI * L.Projection.SphericalMercator.R);
return new L.Transformation(scale, 0.5, -scale, 0.5);
return L.transformation(scale, 0.5, -scale, 0.5);
}())
});

View File

@ -14,5 +14,5 @@
L.CRS.EPSG4326 = L.extend({}, L.CRS.Earth, {
code: 'EPSG:4326',
projection: L.Projection.LonLat,
transformation: new L.Transformation(1 / 180, 1, -1 / 180, 0.5)
transformation: L.transformation(1 / 180, 1, -1 / 180, 0.5)
});

View File

@ -10,7 +10,7 @@
L.CRS.Simple = L.extend({}, L.CRS, {
projection: L.Projection.LonLat,
transformation: new L.Transformation(1, 0, -1, 0),
transformation: L.transformation(1, 0, -1, 0),
scale: function (zoom) {
return Math.pow(2, zoom);

View File

@ -9,7 +9,7 @@
* @example
*
* ```js
* var transformation = new L.Transformation(2, 5, -1, 10),
* var transformation = L.transformation(2, 5, -1, 10),
* p = L.point(1, 2),
* p2 = transformation.transform(p), // L.point(7, 8)
* p3 = transformation.untransform(p2); // L.point(1, 2)
@ -20,6 +20,14 @@
// factory new L.Transformation(a: Number, b: Number, c: Number, d: Number)
// Creates a `Transformation` object with the given coefficients.
L.Transformation = function (a, b, c, d) {
if (L.Util.isArray(a)) {
// use array properties
this._a = a[0];
this._b = a[1];
this._c = a[2];
this._d = a[3];
return;
}
this._a = a;
this._b = b;
this._c = c;
@ -48,7 +56,21 @@ L.Transformation.prototype = {
untransform: function (point, scale) {
scale = scale || 1;
return new L.Point(
(point.x / scale - this._b) / this._a,
(point.y / scale - this._d) / this._c);
(point.x / scale - this._b) / this._a,
(point.y / scale - this._d) / this._c);
}
};
// factory L.transformation(a: Number, b: Number, c: Number, d: Number)
// @factory L.transformation(a: Number, b: Number, c: Number, d: Number)
// Instantiates a Transformation object with the given coefficients.
// @alternative
// @factory L.transformation(coefficients: Array): Transformation
// Expects an coeficients array of the form
// `[a: Number, b: Number, c: Number, d: Number]`.
L.transformation = function (a, b, c, d) {
return new L.Transformation(a, b, c, d);
};