paper.js/src/basic/Size.js

122 lines
2.8 KiB
JavaScript
Raw Normal View History

2011-02-07 13:28:09 -05:00
var Size = Base.extend({
initialize: function() {
if (arguments.length == 2) {
2011-02-07 13:28:09 -05:00
this.width = arguments[0];
this.height = arguments[1];
} else if (arguments.length == 1) {
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;
} else if (Array.isArray(arg)) {
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
set: function(width, height) {
this.width = width;
this.height = height;
},
2011-02-07 13:28:09 -05:00
add: function() {
var size = Size.read(arguments);
return Size.create(this.width + size.width, this.height + size.height);
2011-02-07 13:28:09 -05:00
},
subtract: function() {
var size = Size.read(arguments);
return Size.create(this.width - size.width, this.height - size.height);
2011-02-07 13:28:09 -05:00
},
multiply: function() {
var size = Size.read(arguments);
return Size.create(this.width * size.width, this.height * size.height);
2011-02-07 13:28:09 -05:00
},
divide: function() {
var size = Size.read(arguments);
return Size.create(this.width / size.width, this.height / size.height);
2011-02-07 13:28:09 -05:00
},
modulo: function() {
var size = Size.read(arguments);
return Size.create(this.width % size.width, this.height % size.height);
2011-02-07 13:28:09 -05:00
},
negate: function() {
return Size.create(-this.width, -this.height);
2011-02-07 13:28:09 -05:00
},
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 Size.create(Math.round(this.width), Math.round(this.height));
2011-02-07 13:28:09 -05:00
},
ceil: function() {
return Size.create(Math.ceil(this.width), Math.ceil(this.height));
2011-02-07 13:28:09 -05:00
},
floor: function() {
return Size.create(Math.floor(this.width), Math.floor(this.height));
2011-02-07 13:28:09 -05:00
},
abs: function() {
return Size.create(Math.abs(this.width), Math.abs(this.height));
2011-02-07 13:28:09 -05:00
},
dot: function(Size) {
return this.width * size.width + this.height * size.height;
},
toString: function() {
return '{ x: ' + this.width + ', y: ' + this.height + ' }';
},
statics: {
// See Point.create()
create: function(width, height) {
var size = new Size(Size.dont);
size.width = width;
size.height = height;
return size;
},
2011-02-07 13:28:09 -05:00
min: function(Size1, Size2) {
return Size.create(
2011-02-07 13:28:09 -05:00
Math.min(Size1.width, Size2.width),
Math.min(Size1.height, Size2.height));
},
max: function(Size1, Size2) {
return Size.create(
2011-02-07 13:28:09 -05:00
Math.max(Size1.width, Size2.width),
Math.max(Size1.height, Size2.height));
},
random: function() {
return Size.create(Math.random(), Math.random());
2011-02-07 13:28:09 -05:00
}
}
});