paper.js/src/path/Segment.js

138 lines
3.4 KiB
JavaScript
Raw Normal View History

2011-02-07 13:28:09 -05:00
Segment = Base.extend({
initialize: function() {
if (arguments.length == 0) {
2011-02-07 13:28:09 -05:00
this.point = new Point();
} else if (arguments.length == 1) {
if (arguments[0].point) {
2011-02-07 13:28:09 -05:00
var segment = arguments[0];
this.point = new Point(segment.point);
if (segment.handleIn)
2011-02-07 13:28:09 -05:00
this.handleIn = new Point(segment.handleIn);
if (segment.handleOut)
2011-02-07 13:28:09 -05:00
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) {
2011-02-07 13:28:09 -05:00
this.point = new Point(arguments[0], arguments[1]);
} else {
this.point = new Point(arguments[0]);
if (arguments[1])
2011-02-07 13:28:09 -05:00
this.handleIn = new Point(arguments[1]);
if (arguments[2])
2011-02-07 13:28:09 -05:00
this.handleOut = new Point(arguments[2]);
}
} else if (arguments.length == 6) {
2011-02-07 13:28:09 -05:00
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();
2011-02-07 13:28:09 -05:00
},
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._segments.indexOf(this);
2011-02-07 13:28:09 -05:00
},
getPath: function() {
2011-02-11 08:40:36 -05:00
return this._path;
2011-02-07 13:28:09 -05:00
},
// TODO:
2011-02-07 13:28:09 -05:00
// getCurve: function() {
// if (this._segments && this._segments.path) {
// var curves = this._segments.path.getCurves();
2011-02-07 13:28:09 -05:00
// // 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() {
2011-02-11 08:40:36 -05:00
var index = this.index;
return this.path && index < this.path._segments.length - 1
? this.path._segments[index + 1] : null;
2011-02-07 13:28:09 -05:00
},
getPrevious: function() {
return this.path != null && index > 0
? this.path._segments[this.index - 1] : null;
2011-02-07 13:28:09 -05:00
},
// TODO:
2011-02-07 13:28:09 -05:00
// 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.index, 1);
2011-02-07 13:28:09 -05:00
return false;
},
toString: function() {
return '{ point: ' + this.point
+ (this.handleIn ? ', handleIn '+ this.handleIn : '')
+ (this.handleOut ? ', handleOut ' + this.handleOut : '')
2011-02-07 13:28:09 -05:00
+ ' }';
},
statics: {
read: function(args, index) {
var index = index || 0, length = args.length - index;
if (length == 1 && args[index] instanceof Segment) {
return args[index];
} else if (length != 0) {
var segment = new Segment(Segment.dont);
segment.initialize.apply(segment, index > 0
? Array.prototype.slice.call(args, index) : args);
2011-02-07 13:28:09 -05:00
return segment;
}
return null;
2011-02-07 13:28:09 -05:00
}
}
});