paper.js/src/path/Path.js

132 lines
3.7 KiB
JavaScript
Raw Normal View History

2011-02-07 13:28:09 -05:00
Path = PathItem.extend({
initialize: function() {
this.base.apply(this, arguments);
},
statics: {
Line: Base.extend({
2011-02-07 13:28:09 -05:00
initialize: function() {
var path = new Path();
if (arguments.length == 2) {
path.addSegment(new Segment(arguments[0]));
path.addSegment(new Segment(arguments[1]));
} else if (arguments.length == 4) {
path.addSegment(Segment.read(arguments[0], arguments[1]));
path.addSegment(Segment.read(arguments[2], arguments[3]));
2011-02-07 13:28:09 -05:00
}
return path;
2011-02-07 13:28:09 -05:00
}
}),
Rectangle: Base.extend({
2011-02-07 13:28:09 -05:00
initialize: function() {
var path = new Path();
path.closed = true;
2011-02-07 13:28:09 -05:00
var rectangle = Rectangle.read(arguments);
var corners = ['bottomLeft', 'topLeft', 'topRight', 'bottomRight'];
for (var i = 0; i < 4; i++) {
path.add(rectangle[corners[i]]);
2011-02-07 13:28:09 -05:00
}
return path;
2011-02-07 13:28:09 -05:00
}
}),
RoundRectangle: Base.extend(new function() {
2011-02-07 13:28:09 -05:00
var u = 4 / 3 * (Math.sqrt(2) - 1);
return {
initialize: function() {
var path = new Path();
2011-02-07 13:28:09 -05:00
var rect, size;
if (arguments.length == 2) {
2011-02-07 13:28:09 -05:00
rect = new Rectangle(arguments[0]);
size = new Size(arguments[1]);
} else {
rect = new Rectangle(arguments[0], arguments[1],
arguments[2], arguments[3]);
size = new Size(arguments[4], arguments[5]);
}
size = Size.min(size, rect.size.divide(2));
2011-02-07 13:28:09 -05:00
uSize = size.multiply(u);
var bl = rect.bottomLeft;
path.add(bl.add(size.width, 0), null, [-uSize.width, 0]);
path.add(bl.subtract(0, size.height), [0, uSize.height], null);
2011-02-07 13:28:09 -05:00
var tl = rect.topLeft;
path.add(tl.add(0, size.height), null, [0, -uSize.height]);
path.add(tl.add(size.width, 0), [-uSize.width, 0], null);
2011-02-07 13:28:09 -05:00
var tr = rect.topRight;
path.add(tr.subtract(size.width, 0), null, [uSize.width, 0]);
path.add(tr.add(0, size.height), [0, -uSize.height], null);
2011-02-07 13:28:09 -05:00
var br = rect.bottomRight;
path.add(br.subtract(0, size.height), null, [0, uSize.height]);
path.add(br.subtract(size.width, 0), [uSize.width, 0], null);
2011-02-07 13:28:09 -05:00
path.closed = true;
return path;
2011-02-07 13:28:09 -05:00
}
}
}),
Oval: Base.extend(new function() {
2011-02-07 13:28:09 -05:00
var u = 2 / 3 * (Math.sqrt(2) - 1);
var segments = [
{ handleOut: [0, -u], handleIn: [0, u], point: [ 0, 0.5] },
{ handleOut: [u, 0], handleIn: [-u, 0], point: [ 0.5, 0] },
{ handleOut: [0, u], handleIn: [0, -u], point: [ 1, 0.5] },
{ handleOut: [-u, 0], handleIn: [u, 0], point: [0.5, 1] }
];
return {
initialize: function() {
var path = new Path();
2011-02-07 13:28:09 -05:00
var rect = Rectangle.read(arguments);
var topLeft = rect.topLeft;
2011-02-07 13:28:09 -05:00
var size = new Size(rect.width, rect.height);
for (var i = 0; i < 4; i++) {
2011-02-07 13:28:09 -05:00
var segment = Segment.read([segments[i]]);
segment.handleIn = segment.handleIn.multiply(size);
segment.handleOut = segment.handleOut.multiply(size);
segment.point = segment.point.multiply(size).add(topLeft);
path._segments.push(segment);
2011-02-07 13:28:09 -05:00
}
path.closed = true;
return path;
2011-02-07 13:28:09 -05:00
}
}
}),
Circle: Base.extend({
2011-02-07 13:28:09 -05:00
initialize: function() {
var path = new Path();
2011-02-07 13:28:09 -05:00
var center, radius;
if (arguments.length == 3) {
2011-02-07 13:28:09 -05:00
center = new Point(arguments[0], arguments[1]);
radius = arguments[2];
} else {
center = new Point(arguments[0]);
radius = arguments[1];
}
var left = center.subtract(radius, 0);
path.moveTo(left);
path.arcTo(center.add(radius, 0), true);
path.arcTo(left, true);
var last = path._segments.pop();
path._segments[0].handleIn = last.handleIn;
path.closed = true;
return path;
2011-02-07 13:28:09 -05:00
}
}),
Arc: PathItem.extend({
initialize: function(from, through, to) {
var path = new Path();
path.moveTo(from);
path.arcTo(through, to);
return path;
2011-02-07 13:28:09 -05:00
}
})
}
});