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

View file

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

View file

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

View file

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

View file

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

View file

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