paper.js/src/path/Segment.js
2011-03-04 23:55:49 +00:00

115 lines
2.9 KiB
JavaScript

var Segment = this.Segment = Base.extend({
initialize: function() {
if (arguments.length == 0) {
this.point = new Point();
} else if (arguments.length == 1) {
if (arguments[0].point) {
var segment = arguments[0];
this.point = new Point(segment.point);
if (segment.handleIn)
this.handleIn = new Point(segment.handleIn);
if (segment.handleOut)
this.handleOut = new Point(segment.handleOut);
} else {
this.point = new Point(arguments[0]);
}
} else if (arguments.length < 6) {
if (arguments.length == 2 && !arguments[1].x) {
this.point = new Point(arguments[0], arguments[1]);
} else {
this.point = new Point(arguments[0]);
if (arguments[1])
this.handleIn = new Point(arguments[1]);
if (arguments[2])
this.handleOut = new Point(arguments[2]);
}
} else if (arguments.length == 6) {
this.point = new Point(arguments[0], arguments[1]);
this.handleIn = new Point(arguments[2], arguments[3]);
this.handleOut = new Point(arguments[4], arguments[5]);
}
if (!this.handleIn)
this.handleIn = new Point();
if (!this.handleOut)
this.handleOut = new Point();
},
getPoint: function() {
return this.point;
},
setPoint: function() {
var point = Point.read(arguments);
this.point = point;
},
getHandleIn: function() {
return this.handleIn;
},
setHandleIn: function() {
var point = Point.read(arguments);
this.handleIn = point;
},
getHandleOut: function() {
return this.handleOut;
},
setHandleOut: function() {
var point = Point.read(arguments);
this.handleOut = point;
this.corner = !handleIn.isParallel(handleOut);
},
getIndex: function() {
// TODO: Cache and update indices instead of searching?
return this.path ? this.path._segments.indexOf(this) : -1;
},
// TODO:
// getCurve: function() {
// if (this._segments && this._segments.path) {
// var curves = this._segments.path.getCurves();
// // The curves list handles closing curves, so the curves.size
// // is adjusted accordingly. just check to be in the boundaries here:
// return index < curves.length ? curves[index] : null;
// }
// },
getNext: function() {
return this.path && this.path._segments[this.getIndex() + 1] || null;
},
getPrevious: function() {
return this.path && this.path._segments[this.getIndex() - 1] || null;
},
// TODO:
// isSelected: function() {
//
// }
//
// setSelected: function(pt, selected)
reverse: function() {
return new Segment(this.point, this.handleOut, this.handleIn);
},
clone: function() {
return new Segment(this);
},
remove: function() {
if (this.path && this.path._segments)
return !!this.path._segments.splice(this.getIndex(), 1).length;
return false;
},
toString: function() {
return '{ point: ' + this.point
+ (this.handleIn ? ', handleIn: ' + this.handleIn : '')
+ (this.handleOut ? ', handleOut: ' + this.handleOut : '')
+ ' }';
}
});