2011-04-11 13:27:11 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CurveLocation = Base.extend({
|
2011-05-23 11:39:26 -04:00
|
|
|
/** @lends CurveLocation# */
|
|
|
|
|
2011-04-11 13:27:11 -04:00
|
|
|
beans: true,
|
|
|
|
|
2011-05-27 14:06:57 -04:00
|
|
|
// DOCS: CurveLocation class description: add this comment back when the
|
|
|
|
// mentioned functioned have been added:
|
|
|
|
// The class is in use in many places, such as {@link Path#getLocationAt(offset)},
|
|
|
|
// {@link Path#getLength(CurveLocation)}, {@link Path#getPoint(length)},
|
|
|
|
// {@link Path#split(CurveLocation)},
|
|
|
|
// {@link PathItem#getIntersections(PathItem)}, etc.
|
2011-05-23 11:39:26 -04:00
|
|
|
/**
|
|
|
|
* Creates a new CurveLocation object.
|
|
|
|
*
|
|
|
|
* @name CurveLocation
|
|
|
|
* @constructor
|
|
|
|
* @param {Curve} curve
|
|
|
|
* @param {number} parameter
|
|
|
|
* @param {Point} point
|
|
|
|
*
|
2011-05-27 14:06:57 -04:00
|
|
|
* @class CurveLocation objects describe a location on {@link Curve}
|
|
|
|
* objects, as defined by the curve {@link #parameter}, a value between
|
|
|
|
* {@code 0} (beginning of the curve) and {@code 1} (end of the curve). If
|
|
|
|
* the curve is part of a {@link Path} item, its {@link #index} inside the
|
2011-05-23 11:39:26 -04:00
|
|
|
* {@link Path#curves} array is also provided.
|
|
|
|
*/
|
2011-04-11 13:27:11 -04:00
|
|
|
initialize: function(curve, parameter, point) {
|
|
|
|
this._curve = curve;
|
|
|
|
this._parameter = parameter;
|
|
|
|
this._point = point;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The segment of the curve which is closer to the described location.
|
2011-05-23 11:39:26 -04:00
|
|
|
*
|
|
|
|
* @type Segment
|
|
|
|
* @bean
|
2011-04-11 13:27:11 -04:00
|
|
|
*/
|
|
|
|
getSegment: function() {
|
|
|
|
if (!this._segment) {
|
|
|
|
var parameter = this.getParameter();
|
|
|
|
if (parameter == 0) {
|
2011-04-27 15:48:41 -04:00
|
|
|
this._segment = curve._segment1;
|
2011-04-11 13:27:11 -04:00
|
|
|
} else if (parameter == 1) {
|
2011-04-27 15:48:41 -04:00
|
|
|
this._segment = curve._segment2;
|
2011-05-01 08:16:25 -04:00
|
|
|
} else if (parameter == null) {
|
2011-04-11 13:27:11 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
// Determine the closest segment by comparing curve lengths
|
2011-04-27 15:52:24 -04:00
|
|
|
this._segment = curve.getLength(0, parameter)
|
|
|
|
< curve.getLength(parameter, 1)
|
|
|
|
? curve._segment1
|
|
|
|
: curve._segment2;
|
2011-04-11 13:27:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._segment;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The curve by which the location is defined.
|
2011-05-23 11:39:26 -04:00
|
|
|
*
|
|
|
|
* @type Curve
|
|
|
|
* @bean
|
2011-04-11 13:27:11 -04:00
|
|
|
*/
|
|
|
|
getCurve: function() {
|
|
|
|
return this._curve;
|
|
|
|
},
|
|
|
|
|
2011-04-26 12:49:54 -04:00
|
|
|
/**
|
2011-04-27 14:22:57 -04:00
|
|
|
* The item this curve belongs to, if any.
|
2011-05-23 11:39:26 -04:00
|
|
|
*
|
|
|
|
* @type Item
|
|
|
|
* @bean
|
2011-04-26 12:49:54 -04:00
|
|
|
*/
|
2011-04-27 14:22:57 -04:00
|
|
|
getItem: function() {
|
2011-04-27 15:48:41 -04:00
|
|
|
return this._curve && this._curve._path;
|
2011-04-26 12:49:54 -04:00
|
|
|
},
|
|
|
|
|
2011-04-11 13:27:11 -04:00
|
|
|
/**
|
2011-05-27 14:06:57 -04:00
|
|
|
* The index of the curve within the {@link Path#curves} list, if the
|
2011-04-11 13:27:11 -04:00
|
|
|
* curve is part of a {@link Path} item.
|
2011-05-23 11:39:26 -04:00
|
|
|
*
|
|
|
|
* @type Index
|
|
|
|
* @bean
|
2011-04-11 13:27:11 -04:00
|
|
|
*/
|
|
|
|
getIndex: function() {
|
2011-04-26 12:49:54 -04:00
|
|
|
return this._curve && this._curve.getIndex();
|
2011-04-11 13:27:11 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The length of the path from its beginning up to the location described
|
|
|
|
* by this object.
|
2011-05-23 11:39:26 -04:00
|
|
|
*
|
|
|
|
* @type number
|
|
|
|
* @bean
|
2011-04-11 13:27:11 -04:00
|
|
|
*/
|
2011-04-27 15:08:57 -04:00
|
|
|
getOffset: function() {
|
2011-04-27 15:48:41 -04:00
|
|
|
var path = this._curve && this._curve._path;
|
2011-04-27 15:08:57 -04:00
|
|
|
return path && path._getOffset(this);
|
2011-04-11 13:27:11 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The length of the curve from its beginning up to the location described
|
|
|
|
* by this object.
|
2011-05-23 11:39:26 -04:00
|
|
|
*
|
|
|
|
* @type number
|
|
|
|
* @bean
|
2011-04-11 13:27:11 -04:00
|
|
|
*/
|
2011-04-27 15:08:57 -04:00
|
|
|
getCurveOffset: function() {
|
|
|
|
var parameter = this._curve && this.getParameter();
|
|
|
|
return parameter != null ? this._curve.getLength(0, parameter) : null;
|
2011-04-11 13:27:11 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The curve parameter, as used by various bezier curve calculations. It is
|
|
|
|
* value between {@code 0} (beginning of the curve) and {@code 1} (end of
|
|
|
|
* the curve).
|
2011-05-23 11:39:26 -04:00
|
|
|
*
|
|
|
|
* @type number
|
|
|
|
* @bean
|
2011-04-11 13:27:11 -04:00
|
|
|
*/
|
|
|
|
getParameter: function() {
|
2011-04-27 14:24:40 -04:00
|
|
|
if (this._parameter == null && this._point)
|
2011-05-01 08:16:25 -04:00
|
|
|
this._parameter = this._curve.getParameter(this._point);
|
2011-04-26 12:49:54 -04:00
|
|
|
return this._parameter;
|
2011-04-11 13:27:11 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-05-23 11:39:26 -04:00
|
|
|
* The point which is defined by the {@link #curve} and
|
|
|
|
* {@link #parameter}.
|
|
|
|
*
|
|
|
|
* @type Point
|
|
|
|
* @bean
|
2011-04-11 13:27:11 -04:00
|
|
|
*/
|
|
|
|
getPoint: function() {
|
2011-04-27 14:24:40 -04:00
|
|
|
if (!this._point && this._curve) {
|
|
|
|
var parameter = this.getParameter();
|
|
|
|
if (parameter != null)
|
|
|
|
this._point = this._curve.getPoint(parameter);
|
|
|
|
}
|
2011-04-11 13:27:11 -04:00
|
|
|
return this._point;
|
|
|
|
},
|
|
|
|
|
2011-04-27 14:26:03 -04:00
|
|
|
/**
|
2011-05-23 11:39:26 -04:00
|
|
|
* The tangential vector to the {@link #curve} at the given location.
|
|
|
|
*
|
|
|
|
* @type Point
|
|
|
|
* @bean
|
2011-04-27 14:26:03 -04:00
|
|
|
*/
|
|
|
|
getTangent: function() {
|
|
|
|
var parameter = this.getParameter();
|
|
|
|
return parameter != null && this._curve
|
|
|
|
&& this._curve.getTangent(parameter);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-05-23 11:39:26 -04:00
|
|
|
* The normal vector to the {@link #curve} at the given location.
|
|
|
|
*
|
|
|
|
* @type Point
|
|
|
|
* @bean
|
2011-04-27 14:26:03 -04:00
|
|
|
*/
|
|
|
|
getNormal: function() {
|
|
|
|
var parameter = this.getParameter();
|
|
|
|
return parameter != null && this._curve
|
|
|
|
&& this._curve.getNormal(parameter);
|
|
|
|
},
|
|
|
|
|
2011-05-23 11:39:26 -04:00
|
|
|
/**
|
|
|
|
* @return {string} A string representation of the curve location.
|
|
|
|
*/
|
2011-04-11 13:27:11 -04:00
|
|
|
toString: function() {
|
2011-05-04 14:42:50 -04:00
|
|
|
var parts = [],
|
|
|
|
point = this.getPoint();
|
2011-04-27 14:24:40 -04:00
|
|
|
if (point)
|
|
|
|
parts.push('point: ' + point);
|
2011-04-11 13:27:11 -04:00
|
|
|
var index = this.getIndex();
|
2011-05-01 08:18:36 -04:00
|
|
|
if (index != null)
|
2011-04-26 12:49:54 -04:00
|
|
|
parts.push('index: ' + index);
|
2011-04-11 13:27:11 -04:00
|
|
|
var parameter = this.getParameter();
|
2011-05-01 08:18:36 -04:00
|
|
|
if (parameter != null)
|
2011-05-04 14:42:50 -04:00
|
|
|
parts.push('parameter: ' + Base.formatNumber(parameter));
|
2011-05-02 06:23:42 -04:00
|
|
|
return '{ ' + parts.join(', ') + ' }';
|
2011-04-11 13:27:11 -04:00
|
|
|
}
|
2011-04-11 13:33:34 -04:00
|
|
|
});
|