Add a snapToZero rotation constraint
This commit is contained in:
@@ -37,3 +37,23 @@ ol.RotationConstraint.createSnapToN = function(n) {
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number=} opt_tolerance Tolerance.
|
||||
* @return {ol.RotationConstraintType} Rotation constraint.
|
||||
*/
|
||||
ol.RotationConstraint.createSnapToZero = function(opt_tolerance) {
|
||||
var tolerance = opt_tolerance || 0.1;
|
||||
return function(rotation, delta) {
|
||||
if (goog.isDef(rotation)) {
|
||||
if (Math.abs(rotation + delta) <= tolerance) {
|
||||
return 0;
|
||||
} else {
|
||||
return rotation + delta;
|
||||
}
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -387,6 +387,6 @@ ol.View2D.createConstraints_ = function(view2DOptions) {
|
||||
zoomFactor, maxResolution, numZoomLevels - 1);
|
||||
}
|
||||
// FIXME rotation constraint is not configurable at the moment
|
||||
var rotationConstraint = ol.RotationConstraint.none;
|
||||
var rotationConstraint = ol.RotationConstraint.createSnapToZero();
|
||||
return new ol.Constraints(resolutionConstraint, rotationConstraint);
|
||||
};
|
||||
|
||||
36
test/spec/ol/rotationconstraint.test.js
Normal file
36
test/spec/ol/rotationconstraint.test.js
Normal file
@@ -0,0 +1,36 @@
|
||||
goog.provide('ol.test.RotationConstraint');
|
||||
|
||||
describe('ol.RotationConstraint', function() {
|
||||
|
||||
describe('SnapToZero', function() {
|
||||
|
||||
it('returns expected rotation value', function() {
|
||||
var rotationConstraint = ol.RotationConstraint.createSnapToZero(0.3);
|
||||
|
||||
expect(rotationConstraint(0.1, 0)).toEqual(0);
|
||||
expect(rotationConstraint(0.2, 0)).toEqual(0);
|
||||
expect(rotationConstraint(0.3, 0)).toEqual(0);
|
||||
expect(rotationConstraint(0.4, 0)).toEqual(0.4);
|
||||
|
||||
expect(rotationConstraint(-0.1, 0)).toEqual(0);
|
||||
expect(rotationConstraint(-0.2, 0)).toEqual(0);
|
||||
expect(rotationConstraint(-0.3, 0)).toEqual(0);
|
||||
expect(rotationConstraint(-0.4, 0)).toEqual(-0.4);
|
||||
|
||||
expect(rotationConstraint(1, -0.9)).toEqual(0);
|
||||
expect(rotationConstraint(1, -0.8)).toEqual(0);
|
||||
// floating-point arithmetic
|
||||
expect(rotationConstraint(1, -0.7)).not.toEqual(0);
|
||||
expect(rotationConstraint(1, -0.6)).toEqual(0.4);
|
||||
|
||||
expect(rotationConstraint(-1, 0.9)).toEqual(0);
|
||||
expect(rotationConstraint(-1, 0.8)).toEqual(0);
|
||||
// floating-point arithmetic
|
||||
expect(rotationConstraint(-1, 0.7)).not.toEqual(0);
|
||||
expect(rotationConstraint(-1, 0.6)).toEqual(-0.4);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
goog.require('ol.RotationConstraint');
|
||||
@@ -49,6 +49,16 @@ describe('ol.View2D', function() {
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('create rotation constraint', function() {
|
||||
it('gives a correct rotation constraint function', function() {
|
||||
var options = {};
|
||||
var fn = ol.View2D.createConstraints_(options).rotation;
|
||||
expect(fn(0.01, 0)).toEqual(0);
|
||||
expect(fn(0.15, 0)).toEqual(0.15);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user