mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Implement Curve#getOffsetAtTime()
This commit is contained in:
parent
b0d0e41ddc
commit
5854c25dd5
3 changed files with 22 additions and 6 deletions
|
@ -169,6 +169,8 @@ contribute to the code.
|
||||||
matrix (#972).
|
matrix (#972).
|
||||||
- Allow `Item#position` to be selected via `Item#position.selected` (#980).
|
- Allow `Item#position` to be selected via `Item#position.selected` (#980).
|
||||||
- Add `tolerance` argument to `Path#join(path, tolerance)`.
|
- Add `tolerance` argument to `Path#join(path, tolerance)`.
|
||||||
|
- Add `Curve#getOffsetAtTime(time)`, as the reverse of
|
||||||
|
`Curve#getTimeAt(offset)`.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- Fix calculations of `Item#strokeBounds` for all possible combinations of
|
- Fix calculations of `Item#strokeBounds` for all possible combinations of
|
||||||
|
|
|
@ -1050,6 +1050,17 @@ statics: /** @lends Curve */{
|
||||||
*/
|
*/
|
||||||
getParameterAt: '#getTimeAt',
|
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
|
* Returns the curve location of the specified point if it lies on the
|
||||||
* curve, `null` otherwise.
|
* curve, `null` otherwise.
|
||||||
|
|
|
@ -160,19 +160,22 @@ test('Curve#getTimeAt()', function() {
|
||||||
]).firstCurve;
|
]).firstCurve;
|
||||||
|
|
||||||
for (var f = 0; f <= 1; f += 0.1) {
|
for (var f = 0; f <= 1; f += 0.1) {
|
||||||
var o1 = curve.length * f;
|
var o1 = curve.length * f,
|
||||||
var o2 = -curve.length * (1 - f);
|
o2 = -curve.length * (1 - f),
|
||||||
|
t1 = curve.getTimeAt(o1),
|
||||||
|
t2 = curve.getTimeAt(o2);
|
||||||
var message = 'Curve-time parameter at offset ' + o1
|
var message = 'Curve-time parameter at offset ' + o1
|
||||||
+ ' should be the same value as at offset ' + o2;
|
+ ' should be the same value as at offset ' + o2;
|
||||||
equals(curve.getTimeAt(o1), curve.getTimeAt(o2), message,
|
equals(t1, t2, message, Numerical.CURVETIME_EPSILON);
|
||||||
Numerical.CURVETIME_EPSILON);
|
equals(function() { return curve.getOffsetAtTime(t1); }, o1);
|
||||||
|
equals(function() { return curve.getOffsetAtTime(t2); }, curve.length + o2);
|
||||||
// Legacy version:
|
// Legacy version:
|
||||||
equals(curve.getParameterAt(o1), curve.getParameterAt(o2),
|
equals(curve.getParameterAt(o1), curve.getParameterAt(o2),
|
||||||
'Legacy: ' + message, Numerical.CURVETIME_EPSILON);
|
'Legacy: ' + message, Numerical.CURVETIME_EPSILON);
|
||||||
|
// Test other methods with negatives offsets
|
||||||
equals(curve.getTangentAt(o1), curve.getTangentAt(o2),
|
equals(curve.getTangentAt(o1), curve.getTangentAt(o2),
|
||||||
'Tangent at offset ' + o1
|
'Tangent at offset ' + o1
|
||||||
+ ' should be the same value as at offset ' + o2,
|
+ ' should be the same value as at offset ' + o2);
|
||||||
Numerical.CURVETIME_EPSILON);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
equals(curve.getTimeAt(curve.length + 1), null,
|
equals(curve.getTimeAt(curve.length + 1), null,
|
||||||
|
|
Loading…
Reference in a new issue