2011-03-06 19:50:44 -05: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.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-02-17 15:04:02 -05:00
|
|
|
Path.inject({ statics: new function() {
|
2011-02-17 09:55:26 -05:00
|
|
|
var kappa = 2 / 3 * (Math.sqrt(2) - 1);
|
|
|
|
|
|
|
|
var ovalSegments = [
|
|
|
|
new Segment([0, 0.5], [0, kappa ], [0, -kappa]),
|
|
|
|
new Segment([0.5, 0], [-kappa, 0], [kappa, 0 ]),
|
|
|
|
new Segment([1, 0.5], [0, -kappa], [0, kappa ]),
|
|
|
|
new Segment([0.5, 1], [kappa, 0 ], [-kappa, 0])
|
|
|
|
];
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-17 15:04:02 -05:00
|
|
|
return {
|
2011-02-17 09:55:26 -05:00
|
|
|
Line: function() {
|
2011-03-13 13:31:00 -04:00
|
|
|
var step = Math.floor(arguments.length / 2);
|
|
|
|
return new Path(
|
|
|
|
Segment.read(arguments, 0, step),
|
|
|
|
Segment.read(arguments, step, step)
|
|
|
|
);
|
2011-02-17 09:55:26 -05:00
|
|
|
},
|
|
|
|
|
2011-03-08 12:20:30 -05:00
|
|
|
Rectangle: function(rect) {
|
2011-03-13 13:31:00 -04:00
|
|
|
rect = Rectangle.read(arguments);
|
2011-03-06 13:46:28 -05:00
|
|
|
var path = new Path(),
|
|
|
|
corners = ['getBottomLeft', 'getTopLeft', 'getTopRight',
|
|
|
|
'getBottomRight'];
|
2011-02-17 09:55:26 -05:00
|
|
|
for (var i = 0; i < 4; i++) {
|
2011-03-08 12:20:30 -05:00
|
|
|
path.add(rect[corners[i]]());
|
2011-02-17 09:55:26 -05:00
|
|
|
}
|
2011-03-06 13:46:28 -05:00
|
|
|
path.closed = true;
|
2011-02-17 09:55:26 -05:00
|
|
|
return path;
|
|
|
|
},
|
|
|
|
|
2011-03-08 12:20:30 -05:00
|
|
|
RoundRectangle: function(rect, size) {
|
2011-02-17 09:55:26 -05:00
|
|
|
if (arguments.length == 2) {
|
2011-03-06 13:46:28 -05:00
|
|
|
rect = Rectangle.read(arguments, 0, 1);
|
|
|
|
size = Size.read(arguments, 1, 1);
|
|
|
|
} else if (arguments.length == 6) {
|
|
|
|
rect = Rectangle.read(arguments, 0, 4);
|
|
|
|
size = Size.read(arguments, 4, 2);
|
2011-02-17 09:55:26 -05:00
|
|
|
}
|
2011-03-04 20:26:12 -05:00
|
|
|
size = Size.min(size, rect.getSize().divide(2));
|
2011-03-08 12:20:30 -05:00
|
|
|
var path = new Path(),
|
|
|
|
uSize = size.multiply(kappa * 2),
|
2011-03-06 13:46:28 -05:00
|
|
|
|
|
|
|
bl = rect.getBottomLeft(),
|
|
|
|
tl = rect.getTopLeft(),
|
|
|
|
tr = rect.getTopRight(),
|
|
|
|
br = rect.getBottomRight();
|
2011-02-17 09:55:26 -05:00
|
|
|
|
|
|
|
path.add(bl.add(size.width, 0), null, [-uSize.width, 0]);
|
|
|
|
path.add(bl.subtract(0, size.height), [0, uSize.height], null);
|
|
|
|
|
|
|
|
path.add(tl.add(0, size.height), null, [0, -uSize.height]);
|
|
|
|
path.add(tl.add(size.width, 0), [-uSize.width, 0], null);
|
|
|
|
|
|
|
|
path.add(tr.subtract(size.width, 0), null, [uSize.width, 0]);
|
|
|
|
path.add(tr.add(0, size.height), [0, -uSize.height], null);
|
|
|
|
|
|
|
|
path.add(br.subtract(0, size.height), null, [0, uSize.height]);
|
|
|
|
path.add(br.subtract(size.width, 0), [uSize.width, 0], null);
|
|
|
|
|
|
|
|
path.closed = true;
|
|
|
|
return path;
|
|
|
|
},
|
|
|
|
|
2011-03-08 12:20:30 -05:00
|
|
|
Oval: function(rect) {
|
2011-03-13 13:31:00 -04:00
|
|
|
rect = Rectangle.read(arguments);
|
2011-03-06 13:46:28 -05:00
|
|
|
var path = new Path(),
|
|
|
|
topLeft = rect.getTopLeft(),
|
|
|
|
size = new Size(rect.width, rect.height);
|
2011-02-17 09:55:26 -05:00
|
|
|
for (var i = 0; i < 4; i++) {
|
|
|
|
var segment = ovalSegments[i];
|
2011-03-03 07:51:47 -05:00
|
|
|
path._add(new Segment(
|
2011-03-06 05:57:14 -05:00
|
|
|
segment._point.multiply(size).add(topLeft),
|
|
|
|
segment._handleIn.multiply(size),
|
|
|
|
segment._handleOut.multiply(size)
|
2011-02-17 09:55:26 -05:00
|
|
|
));
|
|
|
|
}
|
|
|
|
path.closed = true;
|
|
|
|
return path;
|
|
|
|
},
|
|
|
|
|
2011-03-08 12:20:30 -05:00
|
|
|
Circle: function(center, radius) {
|
2011-02-17 09:55:26 -05:00
|
|
|
if (arguments.length == 3) {
|
2011-03-08 12:20:30 -05:00
|
|
|
center = Point.read(arguments, 0, 2);
|
2011-02-17 09:55:26 -05:00
|
|
|
radius = arguments[2];
|
|
|
|
} else {
|
2011-03-08 12:20:30 -05:00
|
|
|
center = Point.read(arguments, 0, 1);
|
2011-02-17 09:55:26 -05:00
|
|
|
}
|
|
|
|
return Path.Oval(new Rectangle(center.subtract(radius),
|
|
|
|
new Size(radius * 2, radius * 2)));
|
|
|
|
},
|
|
|
|
|
|
|
|
Arc: function(from, through, to) {
|
|
|
|
var path = new Path();
|
|
|
|
path.moveTo(from);
|
|
|
|
path.arcTo(through, to);
|
|
|
|
return path;
|
2011-02-26 13:19:02 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-26 13:19:02 -05:00
|
|
|
RegularPolygon: function(center, numSides, radius) {
|
2011-03-13 13:31:00 -04:00
|
|
|
center = Point.read(arguments, 0);
|
2011-03-06 13:46:28 -05:00
|
|
|
var path = new Path(),
|
|
|
|
three = !(numSides % 3),
|
|
|
|
vector = new Point(0, three ? -radius : radius),
|
|
|
|
offset = three ? -1 : 0.5;
|
2011-02-28 12:30:08 -05:00
|
|
|
for (var i = 0; i < numSides; i++) {
|
2011-02-26 13:21:52 -05:00
|
|
|
var angle = (360 / numSides) * (i + offset);
|
2011-02-26 13:19:02 -05:00
|
|
|
path.add(center.add(vector.rotate(angle)));
|
|
|
|
}
|
|
|
|
path.closed = true;
|
|
|
|
return path;
|
2011-04-12 08:18:00 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
Star: function(center, numPoints, radius1, radius2) {
|
|
|
|
center = Point.read(arguments, 0);
|
|
|
|
numPoints *= 2;
|
|
|
|
var inner = Math.min(radius1, radius2),
|
|
|
|
outer = Math.max(radius1, radius2),
|
|
|
|
angle = 360 / numPoints,
|
|
|
|
outerFirst = radius1 < radius2,
|
|
|
|
path = new Path();
|
|
|
|
for (var i = 0; i < numPoints; i++) {
|
2011-04-12 08:20:56 -04:00
|
|
|
path.add(center.add({
|
|
|
|
angle: -90 + angle * i,
|
|
|
|
length: i % 2 == outerFirst ? outer : inner
|
|
|
|
}));
|
2011-04-12 08:18:00 -04:00
|
|
|
}
|
|
|
|
path.closed = true;
|
|
|
|
return path;
|
2011-02-17 09:55:26 -05:00
|
|
|
}
|
|
|
|
};
|
2011-02-17 15:04:02 -05:00
|
|
|
}});
|