diff --git a/CHANGELOG.md b/CHANGELOG.md index 5150ef52..7e64c933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -169,6 +169,8 @@ contribute to the code. matrix (#972). - Allow `Item#position` to be selected via `Item#position.selected` (#980). - Add `tolerance` argument to `Path#join(path, tolerance)`. +- Add `Curve#getOffsetAtTime(time)`, as the reverse of + `Curve#getTimeAt(offset)`. ### Fixed - Fix calculations of `Item#strokeBounds` for all possible combinations of diff --git a/src/path/Curve.js b/src/path/Curve.js index e5ef3718..ae7286f3 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -1050,6 +1050,17 @@ statics: /** @lends Curve */{ */ getParameterAt: '#getTimeAt', + /** + * Calculates the curve offset at the specified curve-time parameter on + * the curve. + * + * @param {Number} time the curve-time parameter on the curve + * @return {Number} the curve offset at the specified the location + */ + getOffsetAtTime: function(t) { + return this.getPartLength(0, t); + }, + /** * Returns the curve location of the specified point if it lies on the * curve, `null` otherwise. diff --git a/test/tests/Curve.js b/test/tests/Curve.js index 899736fb..c2fe6d78 100644 --- a/test/tests/Curve.js +++ b/test/tests/Curve.js @@ -160,19 +160,22 @@ test('Curve#getTimeAt()', function() { ]).firstCurve; for (var f = 0; f <= 1; f += 0.1) { - var o1 = curve.length * f; - var o2 = -curve.length * (1 - f); + var o1 = curve.length * f, + o2 = -curve.length * (1 - f), + t1 = curve.getTimeAt(o1), + t2 = curve.getTimeAt(o2); var message = 'Curve-time parameter at offset ' + o1 + ' should be the same value as at offset ' + o2; - equals(curve.getTimeAt(o1), curve.getTimeAt(o2), message, - Numerical.CURVETIME_EPSILON); + equals(t1, t2, message, Numerical.CURVETIME_EPSILON); + equals(function() { return curve.getOffsetAtTime(t1); }, o1); + equals(function() { return curve.getOffsetAtTime(t2); }, curve.length + o2); // Legacy version: equals(curve.getParameterAt(o1), curve.getParameterAt(o2), 'Legacy: ' + message, Numerical.CURVETIME_EPSILON); + // Test other methods with negatives offsets equals(curve.getTangentAt(o1), curve.getTangentAt(o2), 'Tangent at offset ' + o1 - + ' should be the same value as at offset ' + o2, - Numerical.CURVETIME_EPSILON); + + ' should be the same value as at offset ' + o2); } equals(curve.getTimeAt(curve.length + 1), null,