Handle all the calls to Base.read() differently, by checking result for null and bailing out if it is.

This commit is contained in:
Jürg Lehni 2011-03-08 17:17:36 +00:00
parent 261fa819d7
commit dd9340d522
6 changed files with 126 additions and 105 deletions

View file

@ -139,8 +139,7 @@ var Matrix = this.Matrix = Base.extend({
* @return {Matrix} This affine transform.
*/
translate: function(point) {
point = Point.read(arguments);
if (point) {
if (point = Point.read(arguments)) {
var x = point.x, y = point.y;
this._m02 += x * this._m00 + y * this._m01;
this._m12 += x * this._m10 + y * this._m11;
@ -418,8 +417,7 @@ var Matrix = this.Matrix = Base.extend({
* @return {Matrix} This affine transform.
*/
setToTranslation: function(delta) {
delta = Point.read(arguments);
if (delta) {
if (delta = Point.read(arguments)) {
this.set(1, 0, 0, 1, delta.x, delta.y);
}
return this;
@ -445,8 +443,7 @@ var Matrix = this.Matrix = Base.extend({
* @return {Matrix} This affine transform.
*/
setToRotation: function(angle, center) {
center = Point.read(arguments, 1);
if (center) {
if (center = Point.read(arguments, 1)) {
angle = angle * Math.PI / 180.0;
var x = center.x,
y = center.y,

View file

@ -77,38 +77,43 @@ var Point = this.Point = Base.extend({
return Point.create(this.x, this.y);
},
add: function() {
var point = Point.read(arguments);
return Point.create(this.x + point.x, this.y + point.y);
add: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x + point.x, this.y + point.y)
: null;
},
subtract: function() {
var point = Point.read(arguments);
return Point.create(this.x - point.x, this.y - point.y);
subtract: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x - point.x, this.y - point.y)
: null;
},
multiply: function() {
var point = Point.read(arguments);
return Point.create(this.x * point.x, this.y * point.y);
multiply: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x * point.x, this.y * point.y)
: null;
},
divide: function() {
var point = Point.read(arguments);
return Point.create(this.x / point.x, this.y / point.y);
divide: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x / point.x, this.y / point.y)
: null;
},
modulo: function() {
var point = Point.read(arguments);
return Point.create(this.x % point.x, this.y % point.y);
modulo: function(point) {
return (point = Point.read(arguments))
? Point.create(this.x % point.x, this.y % point.y)
: null;
},
negate: function() {
return Point.create(-this.x, -this.y);
},
equals: function() {
var point = Point.read(arguments);
return this.x == point.x && this.y == point.y;
equals: function(point) {
return (point = Point.read(arguments))
&& this.x == point.x && this.y == point.y;
},
transform: function(matrix) {
@ -131,15 +136,17 @@ var Point = this.Point = Base.extend({
* @param px
* @param py
*/
getDistance: function() {
var point = Point.read(arguments);
getDistance: function(point) {
if (!(point = Point.read(arguments)))
return null;
var px = point.x - this.x;
var py = point.y - this.y;
return Math.sqrt(px * px + py * py);
},
getDistanceSquared: function() {
var point = Point.read(arguments);
getDistanceSquared: function(point) {
if (!(point = Point.read(arguments)))
return null;
var px = point.x - this.x;
var py = point.y - this.y;
return px * px + py * py;
@ -152,7 +159,6 @@ var Point = this.Point = Base.extend({
* Setting the length changes the location but keeps the vector's angle.
*/
getLength: function() {
var point = Point.read(arguments);
return Math.sqrt(this.x * this.x + this.y * this.y);
},
@ -264,8 +270,9 @@ var Point = this.Point = Base.extend({
*
* @param point
*/
getDirectedAngle: function() {
var point = Point.read(arguments);
getDirectedAngle: function(point) {
if (!(point = Point.read(arguments)))
return null;
var angle = this.getAngle() - point.getAngle();
var bounds = 180;
if (angle < - bounds) {
@ -442,9 +449,10 @@ var Point = this.Point = Base.extend({
* @param point
* @return the dot product of the two points
*/
dot: function() {
var point = Point.read(arguments);
return this.x * point.x + this.y * point.y;
dot: function(point) {
return (point = Point.read(arguments))
? this.x * point.x + this.y * point.y
: null;
},
/**
@ -452,9 +460,10 @@ var Point = this.Point = Base.extend({
* @param point
* @return the cross product of the two points
*/
cross: function() {
var point = Point.read(arguments);
return this.x * point.y - this.y * point.x;
cross: function(point) {
return (point = Point.read(arguments))
? this.x * point.y - this.y * point.x
: null;
},
/**
@ -464,8 +473,9 @@ var Point = this.Point = Base.extend({
* @param point
* @return the project of the point on another point
*/
project: function() {
var point = Point.read(arguments);
project: function(point) {
if (!(point = Point.read(arguments)))
return null;
if (point.isZero()) {
return Point.create(0, 0);
} else {

View file

@ -74,8 +74,9 @@ var Rectangle = this.Rectangle = Base.extend({
return Point.create(this.x, this.y);
},
setPoint: function() {
var point = Point.read(arguments);
setPoint: function(point) {
if (!(point = Point.read(arguments)))
return this;
this.x = point.x;
this.y = point.y;
return this;
@ -85,8 +86,9 @@ var Rectangle = this.Rectangle = Base.extend({
return Size.create(this.width, this.height);
},
setSize: function() {
var size = Size.read(arguments);
setSize: function(size) {
if (!(size = Size.read(arguments)))
return this;
this.width = size.width;
this.height = size.height;
return this;
@ -153,18 +155,19 @@ var Rectangle = this.Rectangle = Base.extend({
return Point.create(this.getCenterX(), this.getCenterY());
},
setCenter: function() {
var pt = Point.read(arguments);
return this.setCenterX(pt.x).setCenterY(pt.y);
setCenter: function(point) {
if (!(point = Point.read(arguments)))
return this;
return this.setCenterX(point.x).setCenterY(point.y);
},
clone: function() {
return new Rectangle(this);
},
equals: function() {
var rect = Rectangle.read(arguments);
return this.x == rect.x && this.y == rect.y
equals: function(rect) {
rect = Rectangle.read(arguments);
return rect && this.x == rect.x && this.y == rect.y
&& this.width == rect.width && this.height == rect.height;
},
@ -185,35 +188,38 @@ var Rectangle = this.Rectangle = Base.extend({
}
},
intersects: function() {
var rect = Rectangle.read(arguments);
return rect.x + rect.width > this.x
intersects: function(rect) {
rect = Rectangle.read(arguments);
return rect && rect.x + rect.width > this.x
&& rect.y + rect.height > this.y
&& rect.x < this.x + this.width
&& rect.y < this.y + this.height;
},
intersect: function() {
var rect = Rectangle.read(arguments);
x1 = Math.max(this.x, rect.x),
intersect: function(rect) {
if (!(rect = Rectangle.read(arguments)))
return null;
var 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);
},
unite: function() {
var rect = Rectangle.read(arguments),
x1 = Math.min(this.x, rect.x),
unite: function(rect) {
if (!(rect = Rectangle.read(arguments)))
return null;
var 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);
},
include: function() {
var point = Point.read(arguments),
x1 = Math.min(this.x, point.x),
include: function(point) {
if (!(point = Point.read(arguments)))
return null;
var 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);
@ -235,14 +241,12 @@ var Rectangle = this.Rectangle = Base.extend({
}
}
}, new function() {
var keys = [
['Top', 'Left'], ['Top', 'Right'],
['Bottom', 'Left'], ['Bottom', 'Right'],
['Left', 'Center'], ['Top', 'Center'],
['Right', 'Center'], ['Bottom', 'Center']
];
return Base.each(keys,
return Base.each([
['Top', 'Left'], ['Top', 'Right'],
['Bottom', 'Left'], ['Bottom', 'Right'],
['Left', 'Center'], ['Top', 'Center'],
['Right', 'Center'], ['Bottom', 'Center']
],
function(parts, index) {
var key = parts.join('');
// find out if the first of the pair is an x or y property,
@ -260,9 +264,10 @@ var Rectangle = this.Rectangle = Base.extend({
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)[setY](pt.y); // Note: chaining here!
this['set' + key] = function(point) {
if (!(point = Point.read(arguments)))
return this;
return this[setX](point.x)[setY](point.y); // Note: call chaining!
};
}, { beans: true });
});

View file

@ -48,28 +48,34 @@ var Size = this.Size = Base.extend({
},
add: function() {
var size = Size.read(arguments);
return Size.create(this.width + size.width, this.height + size.height);
return (size = Size.read(arguments))
? Size.create(this.width + size.width, this.height + size.height)
: null;
},
subtract: function() {
var size = Size.read(arguments);
return Size.create(this.width - size.width, this.height - size.height);
return (size = Size.read(arguments))
? Size.create(this.width - size.width, this.height - size.height)
: null;
;
},
multiply: function() {
var size = Size.read(arguments);
return Size.create(this.width * size.width, this.height * size.height);
return (size = Size.read(arguments))
? Size.create(this.width * size.width, this.height * size.height)
: null;
},
divide: function() {
var size = Size.read(arguments);
return Size.create(this.width / size.width, this.height / size.height);
return (size = Size.read(arguments))
? Size.create(this.width / size.width, this.height / size.height)
: null;
},
modulo: function() {
var size = Size.read(arguments);
return Size.create(this.width % size.width, this.height % size.height);
return (size = Size.read(arguments))
? Size.create(this.width % size.width, this.height % size.height)
: null;
},
negate: function() {
@ -77,8 +83,8 @@ var Size = this.Size = Base.extend({
},
equals: function() {
var size = Size.read(arguments);
return this.width == size.width && this.height == size.height;
return (size = Size.read(arguments))
&& this.width == size.width && this.height == size.height;
},
isNaN: function() {
@ -101,11 +107,6 @@ var Size = this.Size = Base.extend({
return Size.create(Math.abs(this.width), Math.abs(this.height));
},
dot: function() {
var size = Size.read(arguments);
return this.width * size.width + this.height * size.height;
},
toString: function() {
return '{ x: ' + this.width + ', y: ' + this.height + ' }';
},

View file

@ -29,34 +29,42 @@ var GradientColor = this.GradientColor = Color.extend({
return this._origin;
},
setOrigin: function() {
this._origin = Point.read(arguments);
if (this._destination)
this._radius = this._destination.getDistance(this._origin);
setOrigin: function(origin) {
if (origin = Point.read(arguments)) {
this._origin = origin;
if (this._destination)
this._radius = this._destination.getDistance(this._origin);
}
return this;
},
getDestination: function() {
return this._destination;
},
setDestination: function() {
this._destination = Point.read(arguments);
this._radius = this._destination.getDistance(this._origin);
setDestination: function(destination) {
if (destination = Point.read(arguments)) {
this._destination = destination;
this._radius = this._destination.getDistance(this._origin);
}
return this;
},
getHilite: function() {
return this._hilite;
},
setHilite: function() {
var hilite = Point.read(arguments);
var vector = hilite.subtract(this._origin);
if (vector.getLength() > this._radius) {
this._hilite = this._origin.add(vector.normalize(
this._radius - 0.1));
} else {
this._hilite = hilite;
setHilite: function(hilite) {
if (hilite = Point.read(arguments)) {
var vector = hilite.subtract(this._origin);
if (vector.getLength() > this._radius) {
this._hilite = this._origin.add(vector.normalize(
this._radius - 0.1));
} else {
this._hilite = hilite;
}
}
return this;
},
getCanvasStyle: function(ctx) {

View file

@ -35,7 +35,7 @@ var GradientStop = this.GradientStop = Base.extend({
return this._color;
},
setColor: function() {
setColor: function(color) {
this._color = Color.read(arguments);
}
});