forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurveLocation.js
More file actions
268 lines (252 loc) · 7.16 KB
/
Copy pathCurveLocation.js
File metadata and controls
268 lines (252 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name CurveLocation
*
* @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
* {@link Path#curves} array is also provided.
*
* The class is in use in many places, such as
* {@link Path#getLocationAt(offset, isParameter)},
* {@link Path#getLocationOf(point)},
* {@link Path#getNearestLocation(point),
* {@link PathItem#getIntersections(path)},
* etc.
*/
var CurveLocation = Base.extend(/** @lends CurveLocation# */{
_class: 'CurveLocation',
// DOCS: CurveLocation class description: add these back when the mentioned
// functioned have been added: {@link Path#split(location)}
/**
* Creates a new CurveLocation object.
*
* @param {Curve} curve
* @param {Number} parameter
* @param {Point} point
*/
initialize: function CurveLocation(curve, parameter, point, _curve2,
_parameter2, _point2, _distance) {
// Define this CurveLocation's unique id.
this._id = CurveLocation._id = (CurveLocation._id || 0) + 1;
this._curve = curve;
// Also store references to segment1 and segment2, in case path
// splitting / dividing is going to happen, in which case the segments
// can be used to determine the new curves, see #getCurve(true)
this._segment1 = curve._segment1;
this._segment2 = curve._segment2;
this._parameter = parameter;
this._point = point;
this._curve2 = _curve2;
this._parameter2 = _parameter2;
this._point2 = _point2;
this._distance = _distance;
},
/**
* The segment of the curve which is closer to the described location.
*
* @type Segment
* @bean
*/
getSegment: function(/* preferFirst */) {
if (!this._segment) {
var curve = this.getCurve(),
parameter = this.getParameter();
if (parameter === 1) {
this._segment = curve._segment2;
} else if (parameter === 0 || arguments[0]) {
this._segment = curve._segment1;
} else if (parameter == null) {
return null;
} else {
// Determine the closest segment by comparing curve lengths
this._segment = curve.getLength(0, parameter)
< curve.getLength(parameter, 1)
? curve._segment1
: curve._segment2;
}
}
return this._segment;
},
/**
* The curve by which the location is defined.
*
* @type Curve
* @bean
*/
getCurve: function(/* uncached */) {
if (!this._curve || arguments[0]) {
// If we're asked to get the curve uncached, access current curve
// objects through segment1 / segment2. Since path splitting or
// dividing might have happened in the meantime, try segment1's
// curve, and see if _point lies on it still, otherwise assume it's
// the curve before segment2.
this._curve = this._segment1.getCurve();
if (this._curve.getParameterOf(this._point) == null)
this._curve = this._segment2.getPrevious().getCurve();
}
return this._curve;
},
/**
* The curve location on the intersecting curve, if this location is the
* result of a call to {@link PathItem#getIntersections(path)} /
* {@link Curve#getIntersections(curve)}.
*
* @type CurveLocation
* @bean
*/
getIntersection: function() {
var intersection = this._intersection;
if (!intersection && this._curve2) {
var param = this._parameter2;
// If we have the parameter on the other curve use that for
// intersection rather than the point.
this._intersection = intersection = new CurveLocation(
this._curve2, param, this._point2 || this._point, this);
intersection._intersection = this;
}
return intersection;
},
/**
* The path this curve belongs to, if any.
*
* @type Item
* @bean
*/
getPath: function() {
var curve = this.getCurve();
return curve && curve._path;
},
/**
* The index of the curve within the {@link Path#curves} list, if the
* curve is part of a {@link Path} item.
*
* @type Index
* @bean
*/
getIndex: function() {
var curve = this.getCurve();
return curve && curve.getIndex();
},
/**
* The length of the path from its beginning up to the location described
* by this object.
*
* @type Number
* @bean
*/
getOffset: function() {
var path = this.getPath();
return path && path._getOffset(this);
},
/**
* The length of the curve from its beginning up to the location described
* by this object.
*
* @type Number
* @bean
*/
getCurveOffset: function() {
var curve = this.getCurve(),
parameter = this.getParameter();
return parameter != null && curve && curve.getLength(0, parameter);
},
/**
* 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).
*
* @type Number
* @bean
*/
getParameter: function(/* uncached */) {
if ((this._parameter == null || arguments[0]) && this._point) {
var curve = this.getCurve(arguments[0] && this._point);
this._parameter = curve && curve.getParameterOf(this._point);
}
return this._parameter;
},
/**
* The point which is defined by the {@link #curve} and
* {@link #parameter}.
*
* @type Point
* @bean
*/
getPoint: function(/* uncached */) {
if ((!this._point || arguments[0]) && this._parameter != null) {
var curve = this.getCurve();
this._point = curve && curve.getPointAt(this._parameter, true);
}
return this._point;
},
/**
* The tangential vector to the {@link #curve} at the given location.
*
* @type Point
* @bean
*/
getTangent: function() {
var parameter = this.getParameter(),
curve = this.getCurve();
return parameter != null && curve && curve.getTangentAt(parameter, true);
},
/**
* The normal vector to the {@link #curve} at the given location.
*
* @type Point
* @bean
*/
getNormal: function() {
var parameter = this.getParameter(),
curve = this.getCurve();
return parameter != null && curve && curve.getNormalAt(parameter, true);
},
/**
* The distance from the queried point to the returned location.
*
* @type Number
* @bean
*/
getDistance: function() {
return this._distance;
},
divide: function() {
var curve = this.getCurve(true);
return curve && curve.divide(this.getParameter(true), true);
},
split: function() {
var curve = this.getCurve(true);
return curve && curve.split(this.getParameter(true), true);
},
/**
* @return {String} a string representation of the curve location
*/
toString: function() {
var parts = [],
point = this.getPoint(),
f = Formatter.instance;
if (point)
parts.push('point: ' + point);
var index = this.getIndex();
if (index != null)
parts.push('index: ' + index);
var parameter = this.getParameter();
if (parameter != null)
parts.push('parameter: ' + f.number(parameter));
if (this._distance != null)
parts.push('distance: ' + f.number(this._distance));
return '{ ' + parts.join(', ') + ' }';
}
});