paper.js/src/basic/Rectangle.js

266 lines
6.3 KiB
JavaScript
Raw Normal View History

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.
* http://scriptographer.org/
*
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* All rights reserved. See LICENSE file for details.
*/
var Rectangle = this.Rectangle = Base.extend({
2011-02-11 08:40:36 -05:00
beans: true,
2011-02-13 13:52:17 -05:00
initialize: function(arg0, arg1, arg2, arg3) {
if (arguments.length == 1) {
// Use 0 as defaults, in case we're reading from a Point or Size
this.x = arg0.x || 0;
this.y = arg0.y || 0;
this.width = arg0.width || 0;
this.height = arg0.height || 0;
} else if (arguments.length == 2) {
if (arg1.x !== undefined) {
2011-02-07 13:28:09 -05:00
// new Rectangle(point1, point2)
var point1 = Point.read(arguments, 0, 1);
var point2 = Point.read(arguments, 1, 1);
2011-02-07 13:28:09 -05:00
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 = Point.read(arguments, 0, 1);
var size = Size.read(arguments, 1, 1);
2011-02-07 13:28:09 -05:00
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 = arg0;
this.y = arg1;
this.width = arg2;
this.height = arg3;
2011-02-07 13:28:09 -05:00
} else {
// new Rectangle()
this.x = this.y = this.width = this.height = 0;
}
},
2011-02-13 14:21:56 -05:00
set: function(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
2011-03-05 15:57:45 -05:00
return this;
},
2011-02-07 13:28:09 -05:00
getPoint: function() {
return Point.create(this.x, this.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
setPoint: function() {
var point = Point.read(arguments);
this.x = point.x;
this.y = point.y;
2011-03-05 15:57:45 -05:00
return this;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getSize: function() {
return Size.create(this.width, this.height);
2011-02-07 13:28:09 -05:00
},
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-03-05 15:57:45 -05:00
return this;
2011-02-07 13:28:09 -05:00
},
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-03-05 15:57:45 -05:00
return this;
2011-02-07 13:28:09 -05:00
},
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-03-05 15:57:45 -05:00
return this;
2011-02-07 13:28:09 -05:00
},
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-03-05 15:57:45 -05:00
return this;
2011-02-07 13:28:09 -05:00
},
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-03-05 15:57:45 -05:00
return this;
2011-02-07 13:28:09 -05:00
},
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-03-05 15:57:45 -05:00
return this;
2011-02-07 13:28:09 -05:00
},
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-03-05 15:57:45 -05:00
return this;
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
getCenter: function() {
2011-03-04 20:59:15 -05:00
return Point.create(this.getCenterX(), this.getCenterY());
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() {
2011-03-05 15:57:45 -05:00
var pt = Point.read(arguments);
return this.setCenterX(pt.x).setCenterY(pt.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
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);
x1 = Math.max(this.x, rect.x),
y1 = Math.max(this.y, rect.y),
x2 = Math.min(this.x + this.width, rect.x + rect.width),
y2 = Math.min(this.y + this.height, rect.y + rect.height);
return Rectangle.create(x1, y1, x2 - x1, y2 - y1);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
unite: function() {
var rect = Rectangle.read(arguments),
x1 = Math.min(this.x, rect.x),
y1 = Math.min(this.y, rect.y),
x2 = Math.max(this.x + this.width, rect.x + rect.width),
y2 = Math.max(this.y + this.height, rect.y + rect.height);
return Rectangle.create(x1, y1, x2 - x1, y2 - y1);
2011-02-07 13:28:09 -05:00
},
2011-02-13 14:21:56 -05:00
2011-02-07 13:28:09 -05:00
include: function() {
var point = Point.read(arguments),
x1 = Math.min(this.x, point.x),
y1 = Math.min(this.y, point.y),
x2 = Math.max(this.x + this.width, point.x),
y2 = Math.max(this.y + this.height, point.y);
return Rectangle.create(x1, y1, x2 - x1, y2 - y1);
2011-02-07 13:28:09 -05:00
},
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
+ ' }';
},
statics: {
// See Point.create()
create: function(x, y, width, height) {
return new Rectangle(Rectangle.dont).set(x, y, width, height);
}
2011-02-07 13:28:09 -05:00
}
}, new function() {
var keys = [
['Top', 'Left'], ['Top', 'Right'],
['Bottom', 'Left'], ['Bottom', 'Right'],
['Left', 'Center'], ['Top', 'Center'],
['Right', 'Center'], ['Bottom', 'Center']
];
return Base.each(keys,
function(parts, index) {
var key = parts.join('');
// find out if the first of the pair is an x or y property,
// by checking the first character for [R]ight or [L]eft;
var xFirst = /^[RL]/.test(key);
// Rename Center to CenterX or CenterY:
if (index >= 4) parts[1] += xFirst ? 'Y' : 'X';
var xIndex = xFirst ? 0 : 1,
yIndex = xFirst ? 1 : 0,
getX = 'get' + parts[xIndex],
getY = 'get' + parts[yIndex],
setX = 'set' + parts[xIndex],
setY = 'set' + parts[yIndex];
this['get' + key] = function() {
return Point.create(this[getX](), this[getY]());
};
this['set' + key] = function(value) {
var pt = Point.read(arguments);
return this[setX](pt.x) // Note: chaining here!
[setY](pt.y);
};
}, { beans: true });
});