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.
|
|
|
|
*/
|
|
|
|
|
2011-03-04 08:34:31 -05:00
|
|
|
var Size = this.Size = Base.extend({
|
2011-03-06 07:15:15 -05:00
|
|
|
initialize: function(arg0, arg1) {
|
2011-02-13 11:26:24 -05:00
|
|
|
if (arguments.length == 2) {
|
2011-03-06 07:15:15 -05:00
|
|
|
this.width = arg0;
|
|
|
|
this.height = arg1;
|
2011-02-13 11:26:24 -05:00
|
|
|
} else if (arguments.length == 1) {
|
2011-03-06 07:15:15 -05:00
|
|
|
if (arg0 == null) {
|
2011-02-14 05:32:42 -05:00
|
|
|
this.width = this.height = 0;
|
2011-03-06 07:15:15 -05:00
|
|
|
} else if (arg0.width !== undefined) {
|
|
|
|
this.width = arg0.width;
|
|
|
|
this.height = arg0.height;
|
|
|
|
} else if (arg0.x !== undefined) {
|
|
|
|
this.width = arg0.x;
|
|
|
|
this.height = arg0.y;
|
|
|
|
} else if (Array.isArray(arg0)) {
|
|
|
|
this.width = arg0[0];
|
|
|
|
this.height = arg0.length > 1 ? arg0[1] : arg0[0];
|
|
|
|
} else if (typeof arg0 === 'number') {
|
|
|
|
this.width = this.height = arg0;
|
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-28 14:14:10 -05:00
|
|
|
set: function(width, height) {
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2011-03-05 15:59:06 -05:00
|
|
|
return this;
|
2011-02-28 14:14:10 -05:00
|
|
|
},
|
|
|
|
|
2011-02-07 13:28:09 -05:00
|
|
|
add: function() {
|
|
|
|
var size = Size.read(arguments);
|
2011-03-03 12:06:35 -05:00
|
|
|
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);
|
2011-03-03 12:06:35 -05:00
|
|
|
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);
|
2011-03-03 12:06:35 -05:00
|
|
|
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);
|
2011-03-03 12:06:35 -05:00
|
|
|
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);
|
2011-03-03 12:06:35 -05:00
|
|
|
return Size.create(this.width % size.width, this.height % size.height);
|
2011-02-07 13:28:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
negate: function() {
|
2011-03-03 12:06:35 -05:00
|
|
|
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() {
|
2011-03-03 12:06:35 -05:00
|
|
|
return Size.create(Math.round(this.width), Math.round(this.height));
|
2011-02-07 13:28:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
ceil: function() {
|
2011-03-03 12:06:35 -05:00
|
|
|
return Size.create(Math.ceil(this.width), Math.ceil(this.height));
|
2011-02-07 13:28:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
floor: function() {
|
2011-03-03 12:06:35 -05:00
|
|
|
return Size.create(Math.floor(this.width), Math.floor(this.height));
|
2011-02-07 13:28:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
abs: function() {
|
2011-03-03 12:06:35 -05:00
|
|
|
return Size.create(Math.abs(this.width), Math.abs(this.height));
|
2011-02-07 13:28:09 -05:00
|
|
|
},
|
|
|
|
|
2011-03-07 12:49:43 -05:00
|
|
|
dot: function() {
|
|
|
|
var size = Size.read(arguments);
|
2011-02-07 13:28:09 -05:00
|
|
|
return this.width * size.width + this.height * size.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return '{ x: ' + this.width + ', y: ' + this.height + ' }';
|
|
|
|
},
|
|
|
|
|
|
|
|
statics: {
|
2011-03-03 12:06:35 -05:00
|
|
|
// See Point.create()
|
|
|
|
create: function(width, height) {
|
2011-03-06 16:02:57 -05:00
|
|
|
return new Size(Size.dont).set(width, height);
|
2011-03-03 12:06:35 -05:00
|
|
|
},
|
|
|
|
|
2011-02-07 13:28:09 -05:00
|
|
|
min: function(Size1, Size2) {
|
2011-03-03 12:06:35 -05:00
|
|
|
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) {
|
2011-03-03 12:06:35 -05:00
|
|
|
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() {
|
2011-03-03 12:06:35 -05:00
|
|
|
return Size.create(Math.random(), Math.random());
|
2011-02-07 13:28:09 -05:00
|
|
|
}
|
|
|
|
}
|
2011-02-13 11:26:24 -05:00
|
|
|
});
|