2011-02-07 13:28:09 -05:00
|
|
|
var Size = Base.extend({
|
|
|
|
initialize: function() {
|
2011-02-13 11:26:24 -05:00
|
|
|
if (arguments.length == 2) {
|
2011-02-07 13:28:09 -05:00
|
|
|
this.width = arguments[0];
|
|
|
|
this.height = arguments[1];
|
2011-02-13 11:26:24 -05:00
|
|
|
} else if (arguments.length == 1) {
|
2011-02-14 05:32:42 -05:00
|
|
|
var arg = arguments[0];
|
|
|
|
if (arg == null) {
|
|
|
|
this.width = this.height = 0;
|
|
|
|
} else if (arg.width !== undefined) {
|
|
|
|
this.width = arg.width;
|
|
|
|
this.height = arg.height;
|
|
|
|
} else if (arg.x !== undefined) {
|
|
|
|
this.width = arg.x;
|
|
|
|
this.height = arg.y;
|
2011-02-15 18:15:13 -05:00
|
|
|
} else if (Array.isArray(arg)) {
|
2011-02-14 05:32:42 -05:00
|
|
|
this.width = arg[0];
|
|
|
|
this.height = arg.length > 1 ? arg[1] : arg[0];
|
|
|
|
} else if (typeof arg === 'number') {
|
|
|
|
this.width = this.height = arg;
|
2011-02-07 13:28:09 -05:00
|
|
|
} else {
|
|
|
|
this.width = this.height = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2011-02-13 14:21:56 -05:00
|
|
|
this.width = this.height = 0;
|
2011-02-07 13:28:09 -05:00
|
|
|
}
|
|
|
|
},
|
2011-02-13 14:21:56 -05:00
|
|
|
|
2011-02-07 13:28:09 -05:00
|
|
|
add: function() {
|
|
|
|
var size = Size.read(arguments);
|
|
|
|
return new Size(this.width + size.width, this.height + size.height);
|
|
|
|
},
|
|
|
|
|
|
|
|
subtract: function() {
|
|
|
|
var size = Size.read(arguments);
|
|
|
|
return new Size(this.width - size.width, this.height - size.height);
|
|
|
|
},
|
|
|
|
|
|
|
|
multiply: function() {
|
|
|
|
var size = Size.read(arguments);
|
|
|
|
return new Size(this.width * size.width, this.height * size.height);
|
|
|
|
},
|
|
|
|
|
|
|
|
divide: function() {
|
|
|
|
var size = Size.read(arguments);
|
|
|
|
return new Size(this.width / size.width, this.height / size.height);
|
|
|
|
},
|
|
|
|
|
|
|
|
modulo: function() {
|
|
|
|
var size = Size.read(arguments);
|
|
|
|
return new Size(this.width % size.width, this.height % size.height);
|
|
|
|
},
|
|
|
|
|
|
|
|
negate: function() {
|
|
|
|
return new Size(-this.width, -this.height);
|
|
|
|
},
|
|
|
|
|
|
|
|
equals: function() {
|
|
|
|
var size = Size.read(arguments);
|
|
|
|
return this.width == size.width && this.height == size.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
isNaN: function() {
|
|
|
|
return isNaN(this.width) || isNaN(this.height);
|
|
|
|
},
|
|
|
|
|
|
|
|
round: function() {
|
|
|
|
return new Size(Math.round(this.width), Math.round(this.height));
|
|
|
|
},
|
|
|
|
|
|
|
|
ceil: function() {
|
|
|
|
return new Size(Math.ceil(this.width), Math.ceil(this.height));
|
|
|
|
},
|
|
|
|
|
|
|
|
floor: function() {
|
|
|
|
return new Size(Math.floor(this.width), Math.floor(this.height));
|
|
|
|
},
|
|
|
|
|
|
|
|
abs: function() {
|
|
|
|
return new Size(Math.abs(this.width), Math.abs(this.height));
|
|
|
|
},
|
|
|
|
|
|
|
|
dot: function(Size) {
|
|
|
|
return this.width * size.width + this.height * size.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return '{ x: ' + this.width + ', y: ' + this.height + ' }';
|
|
|
|
},
|
|
|
|
|
|
|
|
statics: {
|
2011-02-13 13:15:55 -05:00
|
|
|
read: function(args, index) {
|
|
|
|
var index = index || 0, length = args.length - index;
|
|
|
|
if (length == 1 && args[index] instanceof Size) {
|
|
|
|
return args[index];
|
|
|
|
} else if (length != 0) {
|
2011-02-19 19:45:53 -05:00
|
|
|
var size = new Size(Size.dont);
|
2011-02-13 13:15:55 -05:00
|
|
|
size.initialize.apply(size, index > 0
|
|
|
|
? Array.prototype.slice.call(args, index) : args);
|
2011-02-07 13:28:09 -05:00
|
|
|
return size;
|
|
|
|
}
|
2011-02-13 13:15:55 -05:00
|
|
|
return null;
|
2011-02-07 13:28:09 -05:00
|
|
|
},
|
2011-02-13 11:05:19 -05:00
|
|
|
|
2011-02-07 13:28:09 -05:00
|
|
|
min: function(Size1, Size2) {
|
|
|
|
return new Size(
|
|
|
|
Math.min(Size1.width, Size2.width),
|
|
|
|
Math.min(Size1.height, Size2.height));
|
|
|
|
},
|
|
|
|
|
|
|
|
max: function(Size1, Size2) {
|
|
|
|
return new Size(
|
|
|
|
Math.max(Size1.width, Size2.width),
|
|
|
|
Math.max(Size1.height, Size2.height));
|
|
|
|
},
|
|
|
|
|
|
|
|
random: function() {
|
|
|
|
return new Size(Math.random(), Math.random());
|
|
|
|
}
|
|
|
|
}
|
2011-02-13 11:26:24 -05:00
|
|
|
});
|