paper.js/src/basic/Rectangle.js

295 lines
6.6 KiB
JavaScript
Raw Normal View History

2011-02-07 13:28:09 -05:00
Rectangle = Base.extend({
2011-02-11 08:40:36 -05:00
beans: true,
2011-02-13 13:52:17 -05:00
2011-02-07 13:28:09 -05:00
initialize: function() {
if (arguments.length == 1) {
2011-02-07 13:28:09 -05:00
var rect = arguments[0];
this.x = rect.x;
this.y = rect.y;
this.width = rect.width;
this.height = rect.height;
} else if (arguments.length == 2) {
if (arguments[1].x !== undefined) {
2011-02-07 13:28:09 -05:00
// new Rectangle(point1, point2)
var point1 = new Point(arguments[0]);
var point2 = new Point(arguments[1]);
this.x = point1.x;
this.y = point1.y;
this.width = point2.x - point1.x;
this.height = point2.y - point1.y;
if (this.width < 0) {
2011-02-07 13:28:09 -05:00
this.x = point2.x;
this.width = -this.width;
}
if (this.height < 0) {
2011-02-07 13:28:09 -05:00
this.y = point2.y;
this.height = -this.height;
}
} else {
// new Rectangle(point, size)
var point = new Point(arguments[0]);
var size = new Size(arguments[1]);
this.x = point.x;
this.y = point.y;
this.width = size.width;
this.height = size.height;
}
} else if (arguments.length == 4) {
2011-02-07 13:28:09 -05:00
// new Rectangle(x, y, width, height)
this.x = arguments[0];
this.y = arguments[1];
this.width = arguments[2];
this.height = arguments[3];
} else {
// new Rectangle()
this.x = this.y = this.width = this.height = 0;
}
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getPoint: function() {
return new Point(this.x, this.y);
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setPoint: function() {
var point = Point.read(arguments);
this.x = point.x;
this.y = point.y;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getSize: function() {
return new Size(this.width, this.height);
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setSize: function() {
var size = Size.read(arguments);
this.width = size.width;
this.height = size.height;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getLeft: function() {
return this.x;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setLeft: function(left) {
// right should not move
this.width -= left - this.x;
this.x = left;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getTop: function() {
return this.y;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setTop: function(top) {
this.height -= top - this.y;
this.y = top;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getRight: function() {
return this.x + this.width;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setRight: function(right) {
this.width = right - this.x;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getBottom: function() {
return this.y + this.height;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setBottom: function(bottom) {
this.height = bottom - this.y;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getCenterX: function() {
return this.x + this.width * 0.5;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setCenterX: function(x) {
this.x = x - this.width * 0.5;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getCenterY: function() {
return this.y + this.height * 0.5;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setCenterY: function(y) {
this.y = y - this.height * 0.5;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getCenter: function() {
return new Point(this.x + this.width * 0.5, this.y + this.height * 0.5);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setCenter: function() {
var center = Point.read(arguments);
this.x = center.x - this.width * 0.5;
this.y = center.y - this.height * 0.5;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getTopLeft: function() {
2011-02-11 08:40:36 -05:00
return new Point(this.left, this.top);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setTopLeft: function() {
var topLeft = Point.read(arguments);
2011-02-11 08:40:36 -05:00
this.left = topLeft.x;
this.top = topLeft.y;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getTopRight: function() {
2011-02-11 08:40:36 -05:00
return new Point(this.right, this.top);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setTopRight: function() {
var topRight = Point.read(arguments);
2011-02-11 08:40:36 -05:00
this.right = topRight.x;
this.top = topRight.y;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getBottomLeft: function() {
2011-02-11 08:40:36 -05:00
return new Point(this.left, this.bottom);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setBottomLeft: function() {
var bottomLeft = Point.read(arguments);
2011-02-11 08:40:36 -05:00
this.left = bottomLeft.x;
this.bottom = bottomLeft.y;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getBottomRight: function() {
2011-02-11 08:40:36 -05:00
return new Point(this.right, this.bottom);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setBottomRight: function() {
var bottomRight = Point.read(arguments);
2011-02-11 08:40:36 -05:00
this.bottom = bottomRight.y;
this.right = bottomRight.x;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getLeftCenter: function() {
2011-02-11 08:40:36 -05:00
return new Point(this.left, this.centerY);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setLeftCenter: function() {
var leftCenter = Point.read(arguments);
2011-02-11 08:40:36 -05:00
this.left = leftCenter.x;
this.centerY = leftCenter.y;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getTopCenter: function() {
2011-02-11 08:40:36 -05:00
return new Point(this.centerX, this.top);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setTopCenter: function() {
var topCenter = Point.read(arguments);
2011-02-11 08:40:36 -05:00
this.centerX = topCenter.x;
this.top = topCenter.y;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getRightCenter: function() {
2011-02-11 08:40:36 -05:00
return new Point(this.right, this.centerY);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setRightCenter: function() {
var rightCenter = Point.read(arguments);
2011-02-11 08:40:36 -05:00
this.right = rightCenter.x;
this.centerY = rightCenter.y;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getBottomCenter: function() {
2011-02-11 08:40:36 -05:00
return new Point(this.centerX, this.bottom);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
setBottomCenter: function() {
var bottomCenter = Point.read(arguments);
2011-02-11 08:40:36 -05:00
this.bottom = bottomCenter.y;
this.centerX = bottomCenter.x;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
clone: function() {
return new Rectangle(this);
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
equals: function() {
var rect = Rectangle.read(arguments);
return this.x == rect.x && this.y == rect.y
&& this.width == rect.width && this.height == rect.height;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
isEmpty: function() {
return this.width == 0 || this.height == 0;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
contains: function(rect) {
if (rect.width !== undefined) {
2011-02-07 13:28:09 -05:00
return rect.x >= this.x && rect.y >= this.y
&& rect.x + rect.width <= this.x + this.width
&& rect.y + rect.height <= this.y + this.height;
} else {
var point = Point.read(arguments);
return point.x >= this.x && point.y >= this.y
&& point.x <= this.x + this.width
&& point.y <= this.y + this.height;
}
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
intersects: function() {
var rect = Rectangle.read(arguments);
return rect.x + rect.width > this.x
&& rect.y + rect.height > this.y
&& rect.x < this.x + this.width
&& rect.y < this.y + this.height;
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
intersect: function() {
var rect = Rectangle.read(arguments);
var x1 = Math.max(this.x, rect.x);
var y1 = Math.max(this.y, rect.y);
var x2 = Math.min(this.x + this.width, rect.x + rect.width);
var y2 = Math.min(this.y + this.height, rect.y + rect.height);
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
unite: function() {
var rect = Rectangle.read(arguments);
var x1 = Math.min(this.x, rect.x);
var y1 = Math.min(this.y, rect.y);
var x2 = Math.max(this.x + this.width, rect.x + rect.width);
var y2 = Math.max(this.y + this.height, rect.y + rect.height);
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
include: function() {
var point = Point.read(arguments);
var x1 = Math.min(this.x, point.x);
var y1 = Math.min(this.y, point.y);
var x2 = Math.max(this.x + this.width, point.x);
var y2 = Math.max(this.y + this.height, point.y);
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
toString: function() {
return '{ x: ' + this.x
+ ', y: ' + this.y
+ ', width: ' + this.width
+ ', height: ' + this.height
+ ' }';
},
2011-02-13 14:21:56 -05:00
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 Rectangle) {
return args[index];
} else if (length != 0) {
2011-02-07 13:28:09 -05:00
var rect = new Rectangle();
rect.initialize.apply(rect, index > 0
? Array.prototype.slice.call(args, index) : args);
2011-02-07 13:28:09 -05:00
return rect;
}
return null;
2011-02-07 13:28:09 -05:00
}
}
});