diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/src/math/Matrix.js b/src/math/Matrix.js new file mode 100644 index 0000000..d717b58 --- /dev/null +++ b/src/math/Matrix.js @@ -0,0 +1,246 @@ +var Point = require('./Point'); + +/** + * The Matrix class is now an object, which makes it a lot faster, + * here is a representation of it : + * | a | b | tx| + * | c | d | ty| + * | 0 | 0 | 1 | + * + * @class + * @namespace PIXI + */ +function Matrix() { + /** + * @member {number} + * @default 1 + */ + this.a = 1; + + /** + * @member {number} + * @default 0 + */ + this.b = 0; + + /** + * @member {number} + * @default 0 + */ + this.c = 0; + + /** + * @member {number} + * @default 1 + */ + this.d = 1; + + /** + * @member {number} + * @default 0 + */ + this.tx = 0; + + /** + * @member {number} + * @default 0 + */ + this.ty = 0; +}; + +Matrix.prototype.constructor = Matrix; +module.exports = Matrix; + +/** + * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: + * + * a = array[0] + * b = array[1] + * c = array[3] + * d = array[4] + * tx = array[2] + * ty = array[5] + * + * @param array {number[]} The array that the matrix will be populated from. + */ +Matrix.prototype.fromArray = function (array) { + this.a = array[0]; + this.b = array[1]; + this.c = array[3]; + this.d = array[4]; + this.tx = array[2]; + this.ty = array[5]; +}; + +/** + * Creates an array from the current Matrix object. + * + * @param transpose {boolean} Whether we need to transpose the matrix or not + * @return {number[]} the newly created array which contains the matrix + */ +Matrix.prototype.toArray = function (transpose) { + if (!this.array) { + this.array = new Float32Array(9); + } + + var array = this.array; + + if (transpose) { + array[0] = this.a; + array[1] = this.b; + array[2] = 0; + array[3] = this.c; + array[4] = this.d; + array[5] = 0; + array[6] = this.tx; + array[7] = this.ty; + array[8] = 1; + } + else { + array[0] = this.a; + array[1] = this.c; + array[2] = this.tx; + array[3] = this.b; + array[4] = this.d; + array[5] = this.ty; + array[6] = 0; + array[7] = 0; + array[8] = 1; + } + + return array; +}; + +/** + * Get a new position with the current transformation applied. + * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, transformed through this matrix + */ +Matrix.prototype.apply = function (pos, newPos) { + newPos = newPos || new Point(); + + newPos.x = this.a * pos.x + this.c * pos.y + this.tx; + newPos.y = this.b * pos.x + this.d * pos.y + this.ty; + + return newPos; +}; + +/** + * Get a new position with the inverse of the current transformation applied. + * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, inverse-transformed through this matrix + */ +Matrix.prototype.applyInverse = function (pos, newPos) { + newPos = newPos || new Point(); + + var id = 1 / (this.a * this.d + this.c * -this.b); + + newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; + newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; + + return newPos; +}; + +/** + * Translates the matrix on the x and y. + * + * @param {number} x + * @param {number} y + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.translate = function (x, y) { + this.tx += x; + this.ty += y; + + return this; +}; + +/** + * Applies a scale transformation to the matrix. + * + * @param {number} x The amount to scale horizontally + * @param {number} y The amount to scale vertically + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.scale = function (x, y) { + this.a *= x; + this.d *= y; + this.c *= x; + this.b *= y; + this.tx *= x; + this.ty *= y; + + return this; +}; + + +/** + * Applies a rotation transformation to the matrix. + * + * @param {number} angle - The angle in radians. + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.rotate = function (angle) { + var cos = Math.cos( angle ); + var sin = Math.sin( angle ); + + var a1 = this.a; + var c1 = this.c; + var tx1 = this.tx; + + this.a = a1 * cos-this.b * sin; + this.b = a1 * sin+this.b * cos; + this.c = c1 * cos-this.d * sin; + this.d = c1 * sin+this.d * cos; + this.tx = tx1 * cos - this.ty * sin; + this.ty = tx1 * sin + this.ty * cos; + + return this; +}; + +/** + * Appends the given Matrix to this Matrix. + * + * @param {Matrix} matrix + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.append = function (matrix) { + var a1 = this.a; + var b1 = this.b; + var c1 = this.c; + var d1 = this.d; + + this.a = matrix.a * a1 + matrix.b * c1; + this.b = matrix.a * b1 + matrix.b * d1; + this.c = matrix.c * a1 + matrix.d * c1; + this.d = matrix.c * b1 + matrix.d * d1; + + this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; + this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; + + return this; +}; + +/** + * Resets this Matix to an identity (default) matrix. + * + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.identity = function () { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + + return this; +}; + +identityMatrix = new Matrix(); diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/src/math/Matrix.js b/src/math/Matrix.js new file mode 100644 index 0000000..d717b58 --- /dev/null +++ b/src/math/Matrix.js @@ -0,0 +1,246 @@ +var Point = require('./Point'); + +/** + * The Matrix class is now an object, which makes it a lot faster, + * here is a representation of it : + * | a | b | tx| + * | c | d | ty| + * | 0 | 0 | 1 | + * + * @class + * @namespace PIXI + */ +function Matrix() { + /** + * @member {number} + * @default 1 + */ + this.a = 1; + + /** + * @member {number} + * @default 0 + */ + this.b = 0; + + /** + * @member {number} + * @default 0 + */ + this.c = 0; + + /** + * @member {number} + * @default 1 + */ + this.d = 1; + + /** + * @member {number} + * @default 0 + */ + this.tx = 0; + + /** + * @member {number} + * @default 0 + */ + this.ty = 0; +}; + +Matrix.prototype.constructor = Matrix; +module.exports = Matrix; + +/** + * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: + * + * a = array[0] + * b = array[1] + * c = array[3] + * d = array[4] + * tx = array[2] + * ty = array[5] + * + * @param array {number[]} The array that the matrix will be populated from. + */ +Matrix.prototype.fromArray = function (array) { + this.a = array[0]; + this.b = array[1]; + this.c = array[3]; + this.d = array[4]; + this.tx = array[2]; + this.ty = array[5]; +}; + +/** + * Creates an array from the current Matrix object. + * + * @param transpose {boolean} Whether we need to transpose the matrix or not + * @return {number[]} the newly created array which contains the matrix + */ +Matrix.prototype.toArray = function (transpose) { + if (!this.array) { + this.array = new Float32Array(9); + } + + var array = this.array; + + if (transpose) { + array[0] = this.a; + array[1] = this.b; + array[2] = 0; + array[3] = this.c; + array[4] = this.d; + array[5] = 0; + array[6] = this.tx; + array[7] = this.ty; + array[8] = 1; + } + else { + array[0] = this.a; + array[1] = this.c; + array[2] = this.tx; + array[3] = this.b; + array[4] = this.d; + array[5] = this.ty; + array[6] = 0; + array[7] = 0; + array[8] = 1; + } + + return array; +}; + +/** + * Get a new position with the current transformation applied. + * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, transformed through this matrix + */ +Matrix.prototype.apply = function (pos, newPos) { + newPos = newPos || new Point(); + + newPos.x = this.a * pos.x + this.c * pos.y + this.tx; + newPos.y = this.b * pos.x + this.d * pos.y + this.ty; + + return newPos; +}; + +/** + * Get a new position with the inverse of the current transformation applied. + * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, inverse-transformed through this matrix + */ +Matrix.prototype.applyInverse = function (pos, newPos) { + newPos = newPos || new Point(); + + var id = 1 / (this.a * this.d + this.c * -this.b); + + newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; + newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; + + return newPos; +}; + +/** + * Translates the matrix on the x and y. + * + * @param {number} x + * @param {number} y + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.translate = function (x, y) { + this.tx += x; + this.ty += y; + + return this; +}; + +/** + * Applies a scale transformation to the matrix. + * + * @param {number} x The amount to scale horizontally + * @param {number} y The amount to scale vertically + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.scale = function (x, y) { + this.a *= x; + this.d *= y; + this.c *= x; + this.b *= y; + this.tx *= x; + this.ty *= y; + + return this; +}; + + +/** + * Applies a rotation transformation to the matrix. + * + * @param {number} angle - The angle in radians. + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.rotate = function (angle) { + var cos = Math.cos( angle ); + var sin = Math.sin( angle ); + + var a1 = this.a; + var c1 = this.c; + var tx1 = this.tx; + + this.a = a1 * cos-this.b * sin; + this.b = a1 * sin+this.b * cos; + this.c = c1 * cos-this.d * sin; + this.d = c1 * sin+this.d * cos; + this.tx = tx1 * cos - this.ty * sin; + this.ty = tx1 * sin + this.ty * cos; + + return this; +}; + +/** + * Appends the given Matrix to this Matrix. + * + * @param {Matrix} matrix + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.append = function (matrix) { + var a1 = this.a; + var b1 = this.b; + var c1 = this.c; + var d1 = this.d; + + this.a = matrix.a * a1 + matrix.b * c1; + this.b = matrix.a * b1 + matrix.b * d1; + this.c = matrix.c * a1 + matrix.d * c1; + this.d = matrix.c * b1 + matrix.d * d1; + + this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; + this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; + + return this; +}; + +/** + * Resets this Matix to an identity (default) matrix. + * + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.identity = function () { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + + return this; +}; + +identityMatrix = new Matrix(); diff --git a/src/math/Point.js b/src/math/Point.js new file mode 100644 index 0000000..420e5bc --- /dev/null +++ b/src/math/Point.js @@ -0,0 +1,46 @@ +/** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. + * + * @class + * @namespace PIXI + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +function Point(x, y) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; +}; + +Point.prototype.constructor = Point; +module.exports = Point; + +/** + * Creates a clone of this point + * + * @return {Point} a copy of the point + */ +Point.prototype.clone = function () { + return new Point(this.x, this.y); +}; + +/** + * Sets the point to a new x and y position. + * If y is omitted, both x and y will be set to x. + * + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +Point.prototype.set = function (x, y) { + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ) ; +}; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/src/math/Matrix.js b/src/math/Matrix.js new file mode 100644 index 0000000..d717b58 --- /dev/null +++ b/src/math/Matrix.js @@ -0,0 +1,246 @@ +var Point = require('./Point'); + +/** + * The Matrix class is now an object, which makes it a lot faster, + * here is a representation of it : + * | a | b | tx| + * | c | d | ty| + * | 0 | 0 | 1 | + * + * @class + * @namespace PIXI + */ +function Matrix() { + /** + * @member {number} + * @default 1 + */ + this.a = 1; + + /** + * @member {number} + * @default 0 + */ + this.b = 0; + + /** + * @member {number} + * @default 0 + */ + this.c = 0; + + /** + * @member {number} + * @default 1 + */ + this.d = 1; + + /** + * @member {number} + * @default 0 + */ + this.tx = 0; + + /** + * @member {number} + * @default 0 + */ + this.ty = 0; +}; + +Matrix.prototype.constructor = Matrix; +module.exports = Matrix; + +/** + * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: + * + * a = array[0] + * b = array[1] + * c = array[3] + * d = array[4] + * tx = array[2] + * ty = array[5] + * + * @param array {number[]} The array that the matrix will be populated from. + */ +Matrix.prototype.fromArray = function (array) { + this.a = array[0]; + this.b = array[1]; + this.c = array[3]; + this.d = array[4]; + this.tx = array[2]; + this.ty = array[5]; +}; + +/** + * Creates an array from the current Matrix object. + * + * @param transpose {boolean} Whether we need to transpose the matrix or not + * @return {number[]} the newly created array which contains the matrix + */ +Matrix.prototype.toArray = function (transpose) { + if (!this.array) { + this.array = new Float32Array(9); + } + + var array = this.array; + + if (transpose) { + array[0] = this.a; + array[1] = this.b; + array[2] = 0; + array[3] = this.c; + array[4] = this.d; + array[5] = 0; + array[6] = this.tx; + array[7] = this.ty; + array[8] = 1; + } + else { + array[0] = this.a; + array[1] = this.c; + array[2] = this.tx; + array[3] = this.b; + array[4] = this.d; + array[5] = this.ty; + array[6] = 0; + array[7] = 0; + array[8] = 1; + } + + return array; +}; + +/** + * Get a new position with the current transformation applied. + * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, transformed through this matrix + */ +Matrix.prototype.apply = function (pos, newPos) { + newPos = newPos || new Point(); + + newPos.x = this.a * pos.x + this.c * pos.y + this.tx; + newPos.y = this.b * pos.x + this.d * pos.y + this.ty; + + return newPos; +}; + +/** + * Get a new position with the inverse of the current transformation applied. + * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, inverse-transformed through this matrix + */ +Matrix.prototype.applyInverse = function (pos, newPos) { + newPos = newPos || new Point(); + + var id = 1 / (this.a * this.d + this.c * -this.b); + + newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; + newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; + + return newPos; +}; + +/** + * Translates the matrix on the x and y. + * + * @param {number} x + * @param {number} y + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.translate = function (x, y) { + this.tx += x; + this.ty += y; + + return this; +}; + +/** + * Applies a scale transformation to the matrix. + * + * @param {number} x The amount to scale horizontally + * @param {number} y The amount to scale vertically + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.scale = function (x, y) { + this.a *= x; + this.d *= y; + this.c *= x; + this.b *= y; + this.tx *= x; + this.ty *= y; + + return this; +}; + + +/** + * Applies a rotation transformation to the matrix. + * + * @param {number} angle - The angle in radians. + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.rotate = function (angle) { + var cos = Math.cos( angle ); + var sin = Math.sin( angle ); + + var a1 = this.a; + var c1 = this.c; + var tx1 = this.tx; + + this.a = a1 * cos-this.b * sin; + this.b = a1 * sin+this.b * cos; + this.c = c1 * cos-this.d * sin; + this.d = c1 * sin+this.d * cos; + this.tx = tx1 * cos - this.ty * sin; + this.ty = tx1 * sin + this.ty * cos; + + return this; +}; + +/** + * Appends the given Matrix to this Matrix. + * + * @param {Matrix} matrix + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.append = function (matrix) { + var a1 = this.a; + var b1 = this.b; + var c1 = this.c; + var d1 = this.d; + + this.a = matrix.a * a1 + matrix.b * c1; + this.b = matrix.a * b1 + matrix.b * d1; + this.c = matrix.c * a1 + matrix.d * c1; + this.d = matrix.c * b1 + matrix.d * d1; + + this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; + this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; + + return this; +}; + +/** + * Resets this Matix to an identity (default) matrix. + * + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.identity = function () { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + + return this; +}; + +identityMatrix = new Matrix(); diff --git a/src/math/Point.js b/src/math/Point.js new file mode 100644 index 0000000..420e5bc --- /dev/null +++ b/src/math/Point.js @@ -0,0 +1,46 @@ +/** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. + * + * @class + * @namespace PIXI + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +function Point(x, y) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; +}; + +Point.prototype.constructor = Point; +module.exports = Point; + +/** + * Creates a clone of this point + * + * @return {Point} a copy of the point + */ +Point.prototype.clone = function () { + return new Point(this.x, this.y); +}; + +/** + * Sets the point to a new x and y position. + * If y is omitted, both x and y will be set to x. + * + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +Point.prototype.set = function (x, y) { + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ) ; +}; diff --git a/src/math/index.js b/src/math/index.js index 071c583..cc176e9 100644 --- a/src/math/index.js +++ b/src/math/index.js @@ -23,7 +23,12 @@ */ DEG_TO_RAD: Math.PI / 180, - Point: require('./Point'), - Matrix: require('./Matrix'), - Rectangle: require('./Rectangle') + Point: require('./Point'), + Matrix: require('./Matrix'), + + Circle: require('./shapes/Circle'), + Ellipse: require('./shapes/Ellipse'), + Polygon: require('./shapes/Polygon'), + Rectangle: require('./shapes/Rectangle'), + RoundedRectangle: require('./shapes/RoundedRectangle') }; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/src/math/Matrix.js b/src/math/Matrix.js new file mode 100644 index 0000000..d717b58 --- /dev/null +++ b/src/math/Matrix.js @@ -0,0 +1,246 @@ +var Point = require('./Point'); + +/** + * The Matrix class is now an object, which makes it a lot faster, + * here is a representation of it : + * | a | b | tx| + * | c | d | ty| + * | 0 | 0 | 1 | + * + * @class + * @namespace PIXI + */ +function Matrix() { + /** + * @member {number} + * @default 1 + */ + this.a = 1; + + /** + * @member {number} + * @default 0 + */ + this.b = 0; + + /** + * @member {number} + * @default 0 + */ + this.c = 0; + + /** + * @member {number} + * @default 1 + */ + this.d = 1; + + /** + * @member {number} + * @default 0 + */ + this.tx = 0; + + /** + * @member {number} + * @default 0 + */ + this.ty = 0; +}; + +Matrix.prototype.constructor = Matrix; +module.exports = Matrix; + +/** + * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: + * + * a = array[0] + * b = array[1] + * c = array[3] + * d = array[4] + * tx = array[2] + * ty = array[5] + * + * @param array {number[]} The array that the matrix will be populated from. + */ +Matrix.prototype.fromArray = function (array) { + this.a = array[0]; + this.b = array[1]; + this.c = array[3]; + this.d = array[4]; + this.tx = array[2]; + this.ty = array[5]; +}; + +/** + * Creates an array from the current Matrix object. + * + * @param transpose {boolean} Whether we need to transpose the matrix or not + * @return {number[]} the newly created array which contains the matrix + */ +Matrix.prototype.toArray = function (transpose) { + if (!this.array) { + this.array = new Float32Array(9); + } + + var array = this.array; + + if (transpose) { + array[0] = this.a; + array[1] = this.b; + array[2] = 0; + array[3] = this.c; + array[4] = this.d; + array[5] = 0; + array[6] = this.tx; + array[7] = this.ty; + array[8] = 1; + } + else { + array[0] = this.a; + array[1] = this.c; + array[2] = this.tx; + array[3] = this.b; + array[4] = this.d; + array[5] = this.ty; + array[6] = 0; + array[7] = 0; + array[8] = 1; + } + + return array; +}; + +/** + * Get a new position with the current transformation applied. + * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, transformed through this matrix + */ +Matrix.prototype.apply = function (pos, newPos) { + newPos = newPos || new Point(); + + newPos.x = this.a * pos.x + this.c * pos.y + this.tx; + newPos.y = this.b * pos.x + this.d * pos.y + this.ty; + + return newPos; +}; + +/** + * Get a new position with the inverse of the current transformation applied. + * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, inverse-transformed through this matrix + */ +Matrix.prototype.applyInverse = function (pos, newPos) { + newPos = newPos || new Point(); + + var id = 1 / (this.a * this.d + this.c * -this.b); + + newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; + newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; + + return newPos; +}; + +/** + * Translates the matrix on the x and y. + * + * @param {number} x + * @param {number} y + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.translate = function (x, y) { + this.tx += x; + this.ty += y; + + return this; +}; + +/** + * Applies a scale transformation to the matrix. + * + * @param {number} x The amount to scale horizontally + * @param {number} y The amount to scale vertically + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.scale = function (x, y) { + this.a *= x; + this.d *= y; + this.c *= x; + this.b *= y; + this.tx *= x; + this.ty *= y; + + return this; +}; + + +/** + * Applies a rotation transformation to the matrix. + * + * @param {number} angle - The angle in radians. + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.rotate = function (angle) { + var cos = Math.cos( angle ); + var sin = Math.sin( angle ); + + var a1 = this.a; + var c1 = this.c; + var tx1 = this.tx; + + this.a = a1 * cos-this.b * sin; + this.b = a1 * sin+this.b * cos; + this.c = c1 * cos-this.d * sin; + this.d = c1 * sin+this.d * cos; + this.tx = tx1 * cos - this.ty * sin; + this.ty = tx1 * sin + this.ty * cos; + + return this; +}; + +/** + * Appends the given Matrix to this Matrix. + * + * @param {Matrix} matrix + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.append = function (matrix) { + var a1 = this.a; + var b1 = this.b; + var c1 = this.c; + var d1 = this.d; + + this.a = matrix.a * a1 + matrix.b * c1; + this.b = matrix.a * b1 + matrix.b * d1; + this.c = matrix.c * a1 + matrix.d * c1; + this.d = matrix.c * b1 + matrix.d * d1; + + this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; + this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; + + return this; +}; + +/** + * Resets this Matix to an identity (default) matrix. + * + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.identity = function () { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + + return this; +}; + +identityMatrix = new Matrix(); diff --git a/src/math/Point.js b/src/math/Point.js new file mode 100644 index 0000000..420e5bc --- /dev/null +++ b/src/math/Point.js @@ -0,0 +1,46 @@ +/** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. + * + * @class + * @namespace PIXI + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +function Point(x, y) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; +}; + +Point.prototype.constructor = Point; +module.exports = Point; + +/** + * Creates a clone of this point + * + * @return {Point} a copy of the point + */ +Point.prototype.clone = function () { + return new Point(this.x, this.y); +}; + +/** + * Sets the point to a new x and y position. + * If y is omitted, both x and y will be set to x. + * + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +Point.prototype.set = function (x, y) { + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ) ; +}; diff --git a/src/math/index.js b/src/math/index.js index 071c583..cc176e9 100644 --- a/src/math/index.js +++ b/src/math/index.js @@ -23,7 +23,12 @@ */ DEG_TO_RAD: Math.PI / 180, - Point: require('./Point'), - Matrix: require('./Matrix'), - Rectangle: require('./Rectangle') + Point: require('./Point'), + Matrix: require('./Matrix'), + + Circle: require('./shapes/Circle'), + Ellipse: require('./shapes/Ellipse'), + Polygon: require('./shapes/Polygon'), + Rectangle: require('./shapes/Rectangle'), + RoundedRectangle: require('./shapes/RoundedRectangle') }; diff --git a/src/math/shapes/Circle.js b/src/math/shapes/Circle.js new file mode 100644 index 0000000..208b30f --- /dev/null +++ b/src/math/shapes/Circle.js @@ -0,0 +1,79 @@ +/** + * The Circle object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of this circle + * @param y {number} The Y coordinate of the center of this circle + * @param radius {number} The radius of the circle + */ +function Circle(x, y, radius) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.radius = radius || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.CIRC in this case + * + * @member {number} + */ +}; + +Circle.prototype.constructor = Circle; +module.exports = Circle; + +/** + * Creates a clone of this Circle instance + * + * @method clone + * @return {Circle} a copy of the Circle + */ +Circle.prototype.clone = function () { + return new Circle(this.x, this.y, this.radius); +}; + +/** + * Checks whether the x and y coordinates given are contained within this circle + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this Circle + */ +Circle.prototype.contains = function (x, y) { + if (this.radius <= 0) + return false; + + var dx = (this.x - x), + dy = (this.y - y), + r2 = this.radius * this.radius; + + dx *= dx; + dy *= dy; + + return (dx + dy <= r2); +}; + +/** +* Returns the framing rectangle of the circle as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Circle.prototype.getBounds = function () { + return new Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); +}; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/src/math/Matrix.js b/src/math/Matrix.js new file mode 100644 index 0000000..d717b58 --- /dev/null +++ b/src/math/Matrix.js @@ -0,0 +1,246 @@ +var Point = require('./Point'); + +/** + * The Matrix class is now an object, which makes it a lot faster, + * here is a representation of it : + * | a | b | tx| + * | c | d | ty| + * | 0 | 0 | 1 | + * + * @class + * @namespace PIXI + */ +function Matrix() { + /** + * @member {number} + * @default 1 + */ + this.a = 1; + + /** + * @member {number} + * @default 0 + */ + this.b = 0; + + /** + * @member {number} + * @default 0 + */ + this.c = 0; + + /** + * @member {number} + * @default 1 + */ + this.d = 1; + + /** + * @member {number} + * @default 0 + */ + this.tx = 0; + + /** + * @member {number} + * @default 0 + */ + this.ty = 0; +}; + +Matrix.prototype.constructor = Matrix; +module.exports = Matrix; + +/** + * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: + * + * a = array[0] + * b = array[1] + * c = array[3] + * d = array[4] + * tx = array[2] + * ty = array[5] + * + * @param array {number[]} The array that the matrix will be populated from. + */ +Matrix.prototype.fromArray = function (array) { + this.a = array[0]; + this.b = array[1]; + this.c = array[3]; + this.d = array[4]; + this.tx = array[2]; + this.ty = array[5]; +}; + +/** + * Creates an array from the current Matrix object. + * + * @param transpose {boolean} Whether we need to transpose the matrix or not + * @return {number[]} the newly created array which contains the matrix + */ +Matrix.prototype.toArray = function (transpose) { + if (!this.array) { + this.array = new Float32Array(9); + } + + var array = this.array; + + if (transpose) { + array[0] = this.a; + array[1] = this.b; + array[2] = 0; + array[3] = this.c; + array[4] = this.d; + array[5] = 0; + array[6] = this.tx; + array[7] = this.ty; + array[8] = 1; + } + else { + array[0] = this.a; + array[1] = this.c; + array[2] = this.tx; + array[3] = this.b; + array[4] = this.d; + array[5] = this.ty; + array[6] = 0; + array[7] = 0; + array[8] = 1; + } + + return array; +}; + +/** + * Get a new position with the current transformation applied. + * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, transformed through this matrix + */ +Matrix.prototype.apply = function (pos, newPos) { + newPos = newPos || new Point(); + + newPos.x = this.a * pos.x + this.c * pos.y + this.tx; + newPos.y = this.b * pos.x + this.d * pos.y + this.ty; + + return newPos; +}; + +/** + * Get a new position with the inverse of the current transformation applied. + * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, inverse-transformed through this matrix + */ +Matrix.prototype.applyInverse = function (pos, newPos) { + newPos = newPos || new Point(); + + var id = 1 / (this.a * this.d + this.c * -this.b); + + newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; + newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; + + return newPos; +}; + +/** + * Translates the matrix on the x and y. + * + * @param {number} x + * @param {number} y + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.translate = function (x, y) { + this.tx += x; + this.ty += y; + + return this; +}; + +/** + * Applies a scale transformation to the matrix. + * + * @param {number} x The amount to scale horizontally + * @param {number} y The amount to scale vertically + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.scale = function (x, y) { + this.a *= x; + this.d *= y; + this.c *= x; + this.b *= y; + this.tx *= x; + this.ty *= y; + + return this; +}; + + +/** + * Applies a rotation transformation to the matrix. + * + * @param {number} angle - The angle in radians. + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.rotate = function (angle) { + var cos = Math.cos( angle ); + var sin = Math.sin( angle ); + + var a1 = this.a; + var c1 = this.c; + var tx1 = this.tx; + + this.a = a1 * cos-this.b * sin; + this.b = a1 * sin+this.b * cos; + this.c = c1 * cos-this.d * sin; + this.d = c1 * sin+this.d * cos; + this.tx = tx1 * cos - this.ty * sin; + this.ty = tx1 * sin + this.ty * cos; + + return this; +}; + +/** + * Appends the given Matrix to this Matrix. + * + * @param {Matrix} matrix + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.append = function (matrix) { + var a1 = this.a; + var b1 = this.b; + var c1 = this.c; + var d1 = this.d; + + this.a = matrix.a * a1 + matrix.b * c1; + this.b = matrix.a * b1 + matrix.b * d1; + this.c = matrix.c * a1 + matrix.d * c1; + this.d = matrix.c * b1 + matrix.d * d1; + + this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; + this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; + + return this; +}; + +/** + * Resets this Matix to an identity (default) matrix. + * + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.identity = function () { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + + return this; +}; + +identityMatrix = new Matrix(); diff --git a/src/math/Point.js b/src/math/Point.js new file mode 100644 index 0000000..420e5bc --- /dev/null +++ b/src/math/Point.js @@ -0,0 +1,46 @@ +/** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. + * + * @class + * @namespace PIXI + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +function Point(x, y) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; +}; + +Point.prototype.constructor = Point; +module.exports = Point; + +/** + * Creates a clone of this point + * + * @return {Point} a copy of the point + */ +Point.prototype.clone = function () { + return new Point(this.x, this.y); +}; + +/** + * Sets the point to a new x and y position. + * If y is omitted, both x and y will be set to x. + * + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +Point.prototype.set = function (x, y) { + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ) ; +}; diff --git a/src/math/index.js b/src/math/index.js index 071c583..cc176e9 100644 --- a/src/math/index.js +++ b/src/math/index.js @@ -23,7 +23,12 @@ */ DEG_TO_RAD: Math.PI / 180, - Point: require('./Point'), - Matrix: require('./Matrix'), - Rectangle: require('./Rectangle') + Point: require('./Point'), + Matrix: require('./Matrix'), + + Circle: require('./shapes/Circle'), + Ellipse: require('./shapes/Ellipse'), + Polygon: require('./shapes/Polygon'), + Rectangle: require('./shapes/Rectangle'), + RoundedRectangle: require('./shapes/RoundedRectangle') }; diff --git a/src/math/shapes/Circle.js b/src/math/shapes/Circle.js new file mode 100644 index 0000000..208b30f --- /dev/null +++ b/src/math/shapes/Circle.js @@ -0,0 +1,79 @@ +/** + * The Circle object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of this circle + * @param y {number} The Y coordinate of the center of this circle + * @param radius {number} The radius of the circle + */ +function Circle(x, y, radius) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.radius = radius || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.CIRC in this case + * + * @member {number} + */ +}; + +Circle.prototype.constructor = Circle; +module.exports = Circle; + +/** + * Creates a clone of this Circle instance + * + * @method clone + * @return {Circle} a copy of the Circle + */ +Circle.prototype.clone = function () { + return new Circle(this.x, this.y, this.radius); +}; + +/** + * Checks whether the x and y coordinates given are contained within this circle + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this Circle + */ +Circle.prototype.contains = function (x, y) { + if (this.radius <= 0) + return false; + + var dx = (this.x - x), + dy = (this.y - y), + r2 = this.radius * this.radius; + + dx *= dx; + dy *= dy; + + return (dx + dy <= r2); +}; + +/** +* Returns the framing rectangle of the circle as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Circle.prototype.getBounds = function () { + return new Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); +}; diff --git a/src/math/shapes/Ellipse.js b/src/math/shapes/Ellipse.js new file mode 100644 index 0000000..20d2539 --- /dev/null +++ b/src/math/shapes/Ellipse.js @@ -0,0 +1,86 @@ +/** + * The Ellipse object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of the ellipse + * @param y {number} The Y coordinate of the center of the ellipse + * @param width {number} The half width of this ellipse + * @param height {number} The half height of this ellipse + */ +function Ellipse(x, y, width, height) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.width = width || 0; + + /** + * @member {number} + * @default 0 + */ + this.height = height || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.ELIP in this case + * + * @member {number} + */ +}; + +Ellipse.prototype.constructor = Ellipse; +module.exports = Ellipse; + +/** + * Creates a clone of this Ellipse instance + * + * @method clone + * @return {Ellipse} a copy of the ellipse + */ +Ellipse.prototype.clone = function () { + return new Ellipse(this.x, this.y, this.width, this.height); +}; + +/** + * Checks whether the x and y coordinates given are contained within this ellipse + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coords are within this ellipse + */ +Ellipse.prototype.contains = function (x, y) { + if (this.width <= 0 || this.height <= 0) + return false; + + //normalize the coords to an ellipse with center 0,0 + var normx = ((x - this.x) / this.width), + normy = ((y - this.y) / this.height); + + normx *= normx; + normy *= normy; + + return (normx + normy <= 1); +}; + +/** +* Returns the framing rectangle of the ellipse as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Ellipse.prototype.getBounds = function () { + return new Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); +}; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/src/math/Matrix.js b/src/math/Matrix.js new file mode 100644 index 0000000..d717b58 --- /dev/null +++ b/src/math/Matrix.js @@ -0,0 +1,246 @@ +var Point = require('./Point'); + +/** + * The Matrix class is now an object, which makes it a lot faster, + * here is a representation of it : + * | a | b | tx| + * | c | d | ty| + * | 0 | 0 | 1 | + * + * @class + * @namespace PIXI + */ +function Matrix() { + /** + * @member {number} + * @default 1 + */ + this.a = 1; + + /** + * @member {number} + * @default 0 + */ + this.b = 0; + + /** + * @member {number} + * @default 0 + */ + this.c = 0; + + /** + * @member {number} + * @default 1 + */ + this.d = 1; + + /** + * @member {number} + * @default 0 + */ + this.tx = 0; + + /** + * @member {number} + * @default 0 + */ + this.ty = 0; +}; + +Matrix.prototype.constructor = Matrix; +module.exports = Matrix; + +/** + * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: + * + * a = array[0] + * b = array[1] + * c = array[3] + * d = array[4] + * tx = array[2] + * ty = array[5] + * + * @param array {number[]} The array that the matrix will be populated from. + */ +Matrix.prototype.fromArray = function (array) { + this.a = array[0]; + this.b = array[1]; + this.c = array[3]; + this.d = array[4]; + this.tx = array[2]; + this.ty = array[5]; +}; + +/** + * Creates an array from the current Matrix object. + * + * @param transpose {boolean} Whether we need to transpose the matrix or not + * @return {number[]} the newly created array which contains the matrix + */ +Matrix.prototype.toArray = function (transpose) { + if (!this.array) { + this.array = new Float32Array(9); + } + + var array = this.array; + + if (transpose) { + array[0] = this.a; + array[1] = this.b; + array[2] = 0; + array[3] = this.c; + array[4] = this.d; + array[5] = 0; + array[6] = this.tx; + array[7] = this.ty; + array[8] = 1; + } + else { + array[0] = this.a; + array[1] = this.c; + array[2] = this.tx; + array[3] = this.b; + array[4] = this.d; + array[5] = this.ty; + array[6] = 0; + array[7] = 0; + array[8] = 1; + } + + return array; +}; + +/** + * Get a new position with the current transformation applied. + * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, transformed through this matrix + */ +Matrix.prototype.apply = function (pos, newPos) { + newPos = newPos || new Point(); + + newPos.x = this.a * pos.x + this.c * pos.y + this.tx; + newPos.y = this.b * pos.x + this.d * pos.y + this.ty; + + return newPos; +}; + +/** + * Get a new position with the inverse of the current transformation applied. + * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, inverse-transformed through this matrix + */ +Matrix.prototype.applyInverse = function (pos, newPos) { + newPos = newPos || new Point(); + + var id = 1 / (this.a * this.d + this.c * -this.b); + + newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; + newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; + + return newPos; +}; + +/** + * Translates the matrix on the x and y. + * + * @param {number} x + * @param {number} y + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.translate = function (x, y) { + this.tx += x; + this.ty += y; + + return this; +}; + +/** + * Applies a scale transformation to the matrix. + * + * @param {number} x The amount to scale horizontally + * @param {number} y The amount to scale vertically + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.scale = function (x, y) { + this.a *= x; + this.d *= y; + this.c *= x; + this.b *= y; + this.tx *= x; + this.ty *= y; + + return this; +}; + + +/** + * Applies a rotation transformation to the matrix. + * + * @param {number} angle - The angle in radians. + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.rotate = function (angle) { + var cos = Math.cos( angle ); + var sin = Math.sin( angle ); + + var a1 = this.a; + var c1 = this.c; + var tx1 = this.tx; + + this.a = a1 * cos-this.b * sin; + this.b = a1 * sin+this.b * cos; + this.c = c1 * cos-this.d * sin; + this.d = c1 * sin+this.d * cos; + this.tx = tx1 * cos - this.ty * sin; + this.ty = tx1 * sin + this.ty * cos; + + return this; +}; + +/** + * Appends the given Matrix to this Matrix. + * + * @param {Matrix} matrix + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.append = function (matrix) { + var a1 = this.a; + var b1 = this.b; + var c1 = this.c; + var d1 = this.d; + + this.a = matrix.a * a1 + matrix.b * c1; + this.b = matrix.a * b1 + matrix.b * d1; + this.c = matrix.c * a1 + matrix.d * c1; + this.d = matrix.c * b1 + matrix.d * d1; + + this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; + this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; + + return this; +}; + +/** + * Resets this Matix to an identity (default) matrix. + * + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.identity = function () { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + + return this; +}; + +identityMatrix = new Matrix(); diff --git a/src/math/Point.js b/src/math/Point.js new file mode 100644 index 0000000..420e5bc --- /dev/null +++ b/src/math/Point.js @@ -0,0 +1,46 @@ +/** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. + * + * @class + * @namespace PIXI + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +function Point(x, y) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; +}; + +Point.prototype.constructor = Point; +module.exports = Point; + +/** + * Creates a clone of this point + * + * @return {Point} a copy of the point + */ +Point.prototype.clone = function () { + return new Point(this.x, this.y); +}; + +/** + * Sets the point to a new x and y position. + * If y is omitted, both x and y will be set to x. + * + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +Point.prototype.set = function (x, y) { + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ) ; +}; diff --git a/src/math/index.js b/src/math/index.js index 071c583..cc176e9 100644 --- a/src/math/index.js +++ b/src/math/index.js @@ -23,7 +23,12 @@ */ DEG_TO_RAD: Math.PI / 180, - Point: require('./Point'), - Matrix: require('./Matrix'), - Rectangle: require('./Rectangle') + Point: require('./Point'), + Matrix: require('./Matrix'), + + Circle: require('./shapes/Circle'), + Ellipse: require('./shapes/Ellipse'), + Polygon: require('./shapes/Polygon'), + Rectangle: require('./shapes/Rectangle'), + RoundedRectangle: require('./shapes/RoundedRectangle') }; diff --git a/src/math/shapes/Circle.js b/src/math/shapes/Circle.js new file mode 100644 index 0000000..208b30f --- /dev/null +++ b/src/math/shapes/Circle.js @@ -0,0 +1,79 @@ +/** + * The Circle object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of this circle + * @param y {number} The Y coordinate of the center of this circle + * @param radius {number} The radius of the circle + */ +function Circle(x, y, radius) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.radius = radius || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.CIRC in this case + * + * @member {number} + */ +}; + +Circle.prototype.constructor = Circle; +module.exports = Circle; + +/** + * Creates a clone of this Circle instance + * + * @method clone + * @return {Circle} a copy of the Circle + */ +Circle.prototype.clone = function () { + return new Circle(this.x, this.y, this.radius); +}; + +/** + * Checks whether the x and y coordinates given are contained within this circle + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this Circle + */ +Circle.prototype.contains = function (x, y) { + if (this.radius <= 0) + return false; + + var dx = (this.x - x), + dy = (this.y - y), + r2 = this.radius * this.radius; + + dx *= dx; + dy *= dy; + + return (dx + dy <= r2); +}; + +/** +* Returns the framing rectangle of the circle as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Circle.prototype.getBounds = function () { + return new Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); +}; diff --git a/src/math/shapes/Ellipse.js b/src/math/shapes/Ellipse.js new file mode 100644 index 0000000..20d2539 --- /dev/null +++ b/src/math/shapes/Ellipse.js @@ -0,0 +1,86 @@ +/** + * The Ellipse object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of the ellipse + * @param y {number} The Y coordinate of the center of the ellipse + * @param width {number} The half width of this ellipse + * @param height {number} The half height of this ellipse + */ +function Ellipse(x, y, width, height) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.width = width || 0; + + /** + * @member {number} + * @default 0 + */ + this.height = height || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.ELIP in this case + * + * @member {number} + */ +}; + +Ellipse.prototype.constructor = Ellipse; +module.exports = Ellipse; + +/** + * Creates a clone of this Ellipse instance + * + * @method clone + * @return {Ellipse} a copy of the ellipse + */ +Ellipse.prototype.clone = function () { + return new Ellipse(this.x, this.y, this.width, this.height); +}; + +/** + * Checks whether the x and y coordinates given are contained within this ellipse + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coords are within this ellipse + */ +Ellipse.prototype.contains = function (x, y) { + if (this.width <= 0 || this.height <= 0) + return false; + + //normalize the coords to an ellipse with center 0,0 + var normx = ((x - this.x) / this.width), + normy = ((y - this.y) / this.height); + + normx *= normx; + normy *= normy; + + return (normx + normy <= 1); +}; + +/** +* Returns the framing rectangle of the ellipse as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Ellipse.prototype.getBounds = function () { + return new Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); +}; diff --git a/src/math/shapes/Polygon.js b/src/math/shapes/Polygon.js new file mode 100644 index 0000000..b2c3483 --- /dev/null +++ b/src/math/shapes/Polygon.js @@ -0,0 +1,75 @@ +var Point = require('../Point'); + +/** + * @class + * @namespace PIXI + * @param points* {Point[]|number[]|Point...|number...} This can be an array of Points that form the polygon, + * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be + * all the points of the polygon e.g. `new Polygon(new Point(), new Point(), ...)`, or the + * arguments passed can be flat x,y values e.g. `new Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are + * Numbers. + */ +function Polygon(points) { + //if points isn't an array, use arguments as the array + if (!(points instanceof Array)) { + points = Array.prototype.slice.call(arguments); + } + + //if this is a flat array of numbers, convert it to points + if (points[0] instanceof Point) { + var p = []; + for (var i = 0, il = points.length; i < il; i++) { + p.push(points[i].x, points[i].y); + } + + points = p; + } + + this.closed = true; + + /** + * An array of the points of this polygon + * + * @member {Point[]} + */ + this.points = points; +}; + +Polygon.prototype.constructor = Polygon; +module.exports = Polygon; + +/** + * Creates a clone of this polygon + * + * @return {Polygon} a copy of the polygon + */ +Polygon.prototype.clone = function () { + return new Polygon(this.points.slice()); +}; + +/** + * Checks whether the x and y coordinates passed to this function are contained within this polygon + * + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this polygon + */ +Polygon.prototype.contains = function (x, y) { + var inside = false; + + // use some raycasting to test hits + // https://github.com/substack/point-in-polygon/blob/master/index.js + var length = this.points.length / 2; + + for (var i = 0, j = length - 1; i < length; j = i++) { + var xi = this.points[i * 2], yi = this.points[i * 2 + 1], + xj = this.points[j * 2], yj = this.points[j * 2 + 1], + intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); + + if (intersect) { + inside = !inside; + } + } + + return inside; +}; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/src/math/Matrix.js b/src/math/Matrix.js new file mode 100644 index 0000000..d717b58 --- /dev/null +++ b/src/math/Matrix.js @@ -0,0 +1,246 @@ +var Point = require('./Point'); + +/** + * The Matrix class is now an object, which makes it a lot faster, + * here is a representation of it : + * | a | b | tx| + * | c | d | ty| + * | 0 | 0 | 1 | + * + * @class + * @namespace PIXI + */ +function Matrix() { + /** + * @member {number} + * @default 1 + */ + this.a = 1; + + /** + * @member {number} + * @default 0 + */ + this.b = 0; + + /** + * @member {number} + * @default 0 + */ + this.c = 0; + + /** + * @member {number} + * @default 1 + */ + this.d = 1; + + /** + * @member {number} + * @default 0 + */ + this.tx = 0; + + /** + * @member {number} + * @default 0 + */ + this.ty = 0; +}; + +Matrix.prototype.constructor = Matrix; +module.exports = Matrix; + +/** + * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: + * + * a = array[0] + * b = array[1] + * c = array[3] + * d = array[4] + * tx = array[2] + * ty = array[5] + * + * @param array {number[]} The array that the matrix will be populated from. + */ +Matrix.prototype.fromArray = function (array) { + this.a = array[0]; + this.b = array[1]; + this.c = array[3]; + this.d = array[4]; + this.tx = array[2]; + this.ty = array[5]; +}; + +/** + * Creates an array from the current Matrix object. + * + * @param transpose {boolean} Whether we need to transpose the matrix or not + * @return {number[]} the newly created array which contains the matrix + */ +Matrix.prototype.toArray = function (transpose) { + if (!this.array) { + this.array = new Float32Array(9); + } + + var array = this.array; + + if (transpose) { + array[0] = this.a; + array[1] = this.b; + array[2] = 0; + array[3] = this.c; + array[4] = this.d; + array[5] = 0; + array[6] = this.tx; + array[7] = this.ty; + array[8] = 1; + } + else { + array[0] = this.a; + array[1] = this.c; + array[2] = this.tx; + array[3] = this.b; + array[4] = this.d; + array[5] = this.ty; + array[6] = 0; + array[7] = 0; + array[8] = 1; + } + + return array; +}; + +/** + * Get a new position with the current transformation applied. + * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, transformed through this matrix + */ +Matrix.prototype.apply = function (pos, newPos) { + newPos = newPos || new Point(); + + newPos.x = this.a * pos.x + this.c * pos.y + this.tx; + newPos.y = this.b * pos.x + this.d * pos.y + this.ty; + + return newPos; +}; + +/** + * Get a new position with the inverse of the current transformation applied. + * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, inverse-transformed through this matrix + */ +Matrix.prototype.applyInverse = function (pos, newPos) { + newPos = newPos || new Point(); + + var id = 1 / (this.a * this.d + this.c * -this.b); + + newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; + newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; + + return newPos; +}; + +/** + * Translates the matrix on the x and y. + * + * @param {number} x + * @param {number} y + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.translate = function (x, y) { + this.tx += x; + this.ty += y; + + return this; +}; + +/** + * Applies a scale transformation to the matrix. + * + * @param {number} x The amount to scale horizontally + * @param {number} y The amount to scale vertically + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.scale = function (x, y) { + this.a *= x; + this.d *= y; + this.c *= x; + this.b *= y; + this.tx *= x; + this.ty *= y; + + return this; +}; + + +/** + * Applies a rotation transformation to the matrix. + * + * @param {number} angle - The angle in radians. + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.rotate = function (angle) { + var cos = Math.cos( angle ); + var sin = Math.sin( angle ); + + var a1 = this.a; + var c1 = this.c; + var tx1 = this.tx; + + this.a = a1 * cos-this.b * sin; + this.b = a1 * sin+this.b * cos; + this.c = c1 * cos-this.d * sin; + this.d = c1 * sin+this.d * cos; + this.tx = tx1 * cos - this.ty * sin; + this.ty = tx1 * sin + this.ty * cos; + + return this; +}; + +/** + * Appends the given Matrix to this Matrix. + * + * @param {Matrix} matrix + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.append = function (matrix) { + var a1 = this.a; + var b1 = this.b; + var c1 = this.c; + var d1 = this.d; + + this.a = matrix.a * a1 + matrix.b * c1; + this.b = matrix.a * b1 + matrix.b * d1; + this.c = matrix.c * a1 + matrix.d * c1; + this.d = matrix.c * b1 + matrix.d * d1; + + this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; + this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; + + return this; +}; + +/** + * Resets this Matix to an identity (default) matrix. + * + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.identity = function () { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + + return this; +}; + +identityMatrix = new Matrix(); diff --git a/src/math/Point.js b/src/math/Point.js new file mode 100644 index 0000000..420e5bc --- /dev/null +++ b/src/math/Point.js @@ -0,0 +1,46 @@ +/** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. + * + * @class + * @namespace PIXI + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +function Point(x, y) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; +}; + +Point.prototype.constructor = Point; +module.exports = Point; + +/** + * Creates a clone of this point + * + * @return {Point} a copy of the point + */ +Point.prototype.clone = function () { + return new Point(this.x, this.y); +}; + +/** + * Sets the point to a new x and y position. + * If y is omitted, both x and y will be set to x. + * + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +Point.prototype.set = function (x, y) { + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ) ; +}; diff --git a/src/math/index.js b/src/math/index.js index 071c583..cc176e9 100644 --- a/src/math/index.js +++ b/src/math/index.js @@ -23,7 +23,12 @@ */ DEG_TO_RAD: Math.PI / 180, - Point: require('./Point'), - Matrix: require('./Matrix'), - Rectangle: require('./Rectangle') + Point: require('./Point'), + Matrix: require('./Matrix'), + + Circle: require('./shapes/Circle'), + Ellipse: require('./shapes/Ellipse'), + Polygon: require('./shapes/Polygon'), + Rectangle: require('./shapes/Rectangle'), + RoundedRectangle: require('./shapes/RoundedRectangle') }; diff --git a/src/math/shapes/Circle.js b/src/math/shapes/Circle.js new file mode 100644 index 0000000..208b30f --- /dev/null +++ b/src/math/shapes/Circle.js @@ -0,0 +1,79 @@ +/** + * The Circle object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of this circle + * @param y {number} The Y coordinate of the center of this circle + * @param radius {number} The radius of the circle + */ +function Circle(x, y, radius) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.radius = radius || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.CIRC in this case + * + * @member {number} + */ +}; + +Circle.prototype.constructor = Circle; +module.exports = Circle; + +/** + * Creates a clone of this Circle instance + * + * @method clone + * @return {Circle} a copy of the Circle + */ +Circle.prototype.clone = function () { + return new Circle(this.x, this.y, this.radius); +}; + +/** + * Checks whether the x and y coordinates given are contained within this circle + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this Circle + */ +Circle.prototype.contains = function (x, y) { + if (this.radius <= 0) + return false; + + var dx = (this.x - x), + dy = (this.y - y), + r2 = this.radius * this.radius; + + dx *= dx; + dy *= dy; + + return (dx + dy <= r2); +}; + +/** +* Returns the framing rectangle of the circle as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Circle.prototype.getBounds = function () { + return new Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); +}; diff --git a/src/math/shapes/Ellipse.js b/src/math/shapes/Ellipse.js new file mode 100644 index 0000000..20d2539 --- /dev/null +++ b/src/math/shapes/Ellipse.js @@ -0,0 +1,86 @@ +/** + * The Ellipse object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of the ellipse + * @param y {number} The Y coordinate of the center of the ellipse + * @param width {number} The half width of this ellipse + * @param height {number} The half height of this ellipse + */ +function Ellipse(x, y, width, height) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.width = width || 0; + + /** + * @member {number} + * @default 0 + */ + this.height = height || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.ELIP in this case + * + * @member {number} + */ +}; + +Ellipse.prototype.constructor = Ellipse; +module.exports = Ellipse; + +/** + * Creates a clone of this Ellipse instance + * + * @method clone + * @return {Ellipse} a copy of the ellipse + */ +Ellipse.prototype.clone = function () { + return new Ellipse(this.x, this.y, this.width, this.height); +}; + +/** + * Checks whether the x and y coordinates given are contained within this ellipse + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coords are within this ellipse + */ +Ellipse.prototype.contains = function (x, y) { + if (this.width <= 0 || this.height <= 0) + return false; + + //normalize the coords to an ellipse with center 0,0 + var normx = ((x - this.x) / this.width), + normy = ((y - this.y) / this.height); + + normx *= normx; + normy *= normy; + + return (normx + normy <= 1); +}; + +/** +* Returns the framing rectangle of the ellipse as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Ellipse.prototype.getBounds = function () { + return new Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); +}; diff --git a/src/math/shapes/Polygon.js b/src/math/shapes/Polygon.js new file mode 100644 index 0000000..b2c3483 --- /dev/null +++ b/src/math/shapes/Polygon.js @@ -0,0 +1,75 @@ +var Point = require('../Point'); + +/** + * @class + * @namespace PIXI + * @param points* {Point[]|number[]|Point...|number...} This can be an array of Points that form the polygon, + * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be + * all the points of the polygon e.g. `new Polygon(new Point(), new Point(), ...)`, or the + * arguments passed can be flat x,y values e.g. `new Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are + * Numbers. + */ +function Polygon(points) { + //if points isn't an array, use arguments as the array + if (!(points instanceof Array)) { + points = Array.prototype.slice.call(arguments); + } + + //if this is a flat array of numbers, convert it to points + if (points[0] instanceof Point) { + var p = []; + for (var i = 0, il = points.length; i < il; i++) { + p.push(points[i].x, points[i].y); + } + + points = p; + } + + this.closed = true; + + /** + * An array of the points of this polygon + * + * @member {Point[]} + */ + this.points = points; +}; + +Polygon.prototype.constructor = Polygon; +module.exports = Polygon; + +/** + * Creates a clone of this polygon + * + * @return {Polygon} a copy of the polygon + */ +Polygon.prototype.clone = function () { + return new Polygon(this.points.slice()); +}; + +/** + * Checks whether the x and y coordinates passed to this function are contained within this polygon + * + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this polygon + */ +Polygon.prototype.contains = function (x, y) { + var inside = false; + + // use some raycasting to test hits + // https://github.com/substack/point-in-polygon/blob/master/index.js + var length = this.points.length / 2; + + for (var i = 0, j = length - 1; i < length; j = i++) { + var xi = this.points[i * 2], yi = this.points[i * 2 + 1], + xj = this.points[j * 2], yj = this.points[j * 2 + 1], + intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); + + if (intersect) { + inside = !inside; + } + } + + return inside; +}; diff --git a/src/math/shapes/Rectangle.js b/src/math/shapes/Rectangle.js new file mode 100644 index 0000000..9c52db4 --- /dev/null +++ b/src/math/shapes/Rectangle.js @@ -0,0 +1,83 @@ +/** + * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the upper-left corner of the rectangle + * @param y {number} The Y coordinate of the upper-left corner of the rectangle + * @param width {number} The overall width of this rectangle + * @param height {number} The overall height of this rectangle + */ +function Rectangle(x, y, width, height) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.width = width || 0; + + /** + * @member {number} + * @default 0 + */ + this.height = height || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.RECT in this case + * + * @member {number} + */ +}; + +Rectangle.prototype.constructor = Rectangle; +module.exports = Rectangle; + +/** + * A constant empty rectangle. + * + * @static + * @constant + */ +Rectangle.EMPTY = new Rectangle(0, 0, 0, 0); + + +/** + * Creates a clone of this Rectangle + * + * @return {Rectangle} a copy of the rectangle + */ +Rectangle.prototype.clone = function () { + return new Rectangle(this.x, this.y, this.width, this.height); +}; + +/** + * Checks whether the x and y coordinates given are contained within this Rectangle + * + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this Rectangle + */ +Rectangle.prototype.contains = function (x, y) { + if (this.width <= 0 || this.height <= 0) { + return false; + } + + if (x >= this.x && x <= this.x + this.width) { + if (y >= this.y && y <= this.y + this.height) { + return true; + } + } + + return false; +}; diff --git a/README.md b/README.md index 38b3837..7c74a27 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - [x] `display/` - [ ] `extras/` - [x] `filters/` -- [ ] `geom/` (move to `math/`) +- [x] `geom/` (move to `math/`) - [ ] `loaders/` - [ ] `primitives/` - [ ] `renderers/` diff --git a/src/geom/Circle.js b/src/geom/Circle.js deleted file mode 100644 index 47288c6..0000000 --- a/src/geom/Circle.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Circle object can be used to specify a hit area for displayObjects - * - * @class Circle - * @constructor - * @param x {Number} The X coordinate of the center of this circle - * @param y {Number} The Y coordinate of the center of this circle - * @param radius {Number} The radius of the circle - */ -PIXI.Circle = function(x, y, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property radius - * @type Number - * @default 0 - */ - this.radius = radius || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.CIRC in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Circle instance - * - * @method clone - * @return {Circle} a copy of the Circle - */ -PIXI.Circle.prototype.clone = function() -{ - return new PIXI.Circle(this.x, this.y, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this circle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Circle - */ -PIXI.Circle.prototype.contains = function(x, y) -{ - if(this.radius <= 0) - return false; - - var dx = (this.x - x), - dy = (this.y - y), - r2 = this.radius * this.radius; - - dx *= dx; - dy *= dy; - - return (dx + dy <= r2); -}; - -/** -* Returns the framing rectangle of the circle as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Circle.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); -}; - -// constructor -PIXI.Circle.prototype.constructor = PIXI.Circle; diff --git a/src/geom/Ellipse.js b/src/geom/Ellipse.js deleted file mode 100644 index 2700a71..0000000 --- a/src/geom/Ellipse.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @author Chad Engler - */ - -/** - * The Ellipse object can be used to specify a hit area for displayObjects - * - * @class Ellipse - * @constructor - * @param x {Number} The X coordinate of the center of the ellipse - * @param y {Number} The Y coordinate of the center of the ellipse - * @param width {Number} The half width of this ellipse - * @param height {Number} The half height of this ellipse - */ -PIXI.Ellipse = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.ELIP in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Ellipse instance - * - * @method clone - * @return {Ellipse} a copy of the ellipse - */ -PIXI.Ellipse.prototype.clone = function() -{ - return new PIXI.Ellipse(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this ellipse - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coords are within this ellipse - */ -PIXI.Ellipse.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - //normalize the coords to an ellipse with center 0,0 - var normx = ((x - this.x) / this.width), - normy = ((y - this.y) / this.height); - - normx *= normx; - normy *= normy; - - return (normx + normy <= 1); -}; - -/** -* Returns the framing rectangle of the ellipse as a PIXI.Rectangle object -* -* @method getBounds -* @return {Rectangle} the framing rectangle -*/ -PIXI.Ellipse.prototype.getBounds = function() -{ - return new PIXI.Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); -}; - -// constructor -PIXI.Ellipse.prototype.constructor = PIXI.Ellipse; diff --git a/src/geom/Matrix.js b/src/geom/Matrix.js deleted file mode 100644 index b0f043f..0000000 --- a/src/geom/Matrix.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Matrix class is now an object, which makes it a lot faster, - * here is a representation of it : - * | a | b | tx| - * | c | d | ty| - * | 0 | 0 | 1 | - * - * @class Matrix - * @constructor - */ -PIXI.Matrix = function() -{ - /** - * @property a - * @type Number - * @default 1 - */ - this.a = 1; - - /** - * @property b - * @type Number - * @default 0 - */ - this.b = 0; - - /** - * @property c - * @type Number - * @default 0 - */ - this.c = 0; - - /** - * @property d - * @type Number - * @default 1 - */ - this.d = 1; - - /** - * @property tx - * @type Number - * @default 0 - */ - this.tx = 0; - - /** - * @property ty - * @type Number - * @default 0 - */ - this.ty = 0; -}; - -/** - * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: - * - * a = array[0] - * b = array[1] - * c = array[3] - * d = array[4] - * tx = array[2] - * ty = array[5] - * - * @method fromArray - * @param array {Array} The array that the matrix will be populated from. - */ -PIXI.Matrix.prototype.fromArray = function(array) -{ - this.a = array[0]; - this.b = array[1]; - this.c = array[3]; - this.d = array[4]; - this.tx = array[2]; - this.ty = array[5]; -}; - -/** - * Creates an array from the current Matrix object. - * - * @method toArray - * @param transpose {Boolean} Whether we need to transpose the matrix or not - * @return {Array} the newly created array which contains the matrix - */ -PIXI.Matrix.prototype.toArray = function(transpose) -{ - if(!this.array) this.array = new PIXI.Float32Array(9); - var array = this.array; - - if(transpose) - { - array[0] = this.a; - array[1] = this.b; - array[2] = 0; - array[3] = this.c; - array[4] = this.d; - array[5] = 0; - array[6] = this.tx; - array[7] = this.ty; - array[8] = 1; - } - else - { - array[0] = this.a; - array[1] = this.c; - array[2] = this.tx; - array[3] = this.b; - array[4] = this.d; - array[5] = this.ty; - array[6] = 0; - array[7] = 0; - array[8] = 1; - } - - return array; -}; - -/** - * Get a new position with the current transformation applied. - * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) - * - * @method apply - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, transformed through this matrix - */ -PIXI.Matrix.prototype.apply = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - newPos.x = this.a * pos.x + this.c * pos.y + this.tx; - newPos.y = this.b * pos.x + this.d * pos.y + this.ty; - - return newPos; -}; - -/** - * Get a new position with the inverse of the current transformation applied. - * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) - * - * @method applyInverse - * @param pos {Point} The origin - * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) - * @return {Point} The new point, inverse-transformed through this matrix - */ -PIXI.Matrix.prototype.applyInverse = function(pos, newPos) -{ - newPos = newPos || new PIXI.Point(); - - var id = 1 / (this.a * this.d + this.c * -this.b); - - newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; - newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; - - return newPos; -}; - -/** - * Translates the matrix on the x and y. - * - * @method translate - * @param {Number} x - * @param {Number} y - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.translate = function(x, y) -{ - this.tx += x; - this.ty += y; - - return this; -}; - -/** - * Applies a scale transformation to the matrix. - * - * @method scale - * @param {Number} x The amount to scale horizontally - * @param {Number} y The amount to scale vertically - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.scale = function(x, y) -{ - this.a *= x; - this.d *= y; - this.c *= x; - this.b *= y; - this.tx *= x; - this.ty *= y; - - return this; -}; - - -/** - * Applies a rotation transformation to the matrix. - * @method rotate - * @param {Number} angle The angle in radians. - * @return {Matrix} This matrix. Good for chaining method calls. - **/ -PIXI.Matrix.prototype.rotate = function(angle) -{ - var cos = Math.cos( angle ); - var sin = Math.sin( angle ); - - var a1 = this.a; - var c1 = this.c; - var tx1 = this.tx; - - this.a = a1 * cos-this.b * sin; - this.b = a1 * sin+this.b * cos; - this.c = c1 * cos-this.d * sin; - this.d = c1 * sin+this.d * cos; - this.tx = tx1 * cos - this.ty * sin; - this.ty = tx1 * sin + this.ty * cos; - - return this; -}; - -/** - * Appends the given Matrix to this Matrix. - * - * @method append - * @param {Matrix} matrix - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.append = function(matrix) -{ - var a1 = this.a; - var b1 = this.b; - var c1 = this.c; - var d1 = this.d; - - this.a = matrix.a * a1 + matrix.b * c1; - this.b = matrix.a * b1 + matrix.b * d1; - this.c = matrix.c * a1 + matrix.d * c1; - this.d = matrix.c * b1 + matrix.d * d1; - - this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; - this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; - - return this; -}; - -/** - * Resets this Matix to an identity (default) matrix. - * - * @method identity - * @return {Matrix} This matrix. Good for chaining method calls. - */ -PIXI.Matrix.prototype.identity = function() -{ - this.a = 1; - this.b = 0; - this.c = 0; - this.d = 1; - this.tx = 0; - this.ty = 0; - - return this; -}; - -PIXI.identityMatrix = new PIXI.Matrix(); diff --git a/src/geom/Point.js b/src/geom/Point.js deleted file mode 100644 index 2adcb13..0000000 --- a/src/geom/Point.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. - * - * @class Point - * @constructor - * @param x {Number} position of the point on the x axis - * @param y {Number} position of the point on the y axis - */ -PIXI.Point = function(x, y) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; -}; - -/** - * Creates a clone of this point - * - * @method clone - * @return {Point} a copy of the point - */ -PIXI.Point.prototype.clone = function() -{ - return new PIXI.Point(this.x, this.y); -}; - -/** - * Sets the point to a new x and y position. - * If y is omitted, both x and y will be set to x. - * - * @method set - * @param [x=0] {Number} position of the point on the x axis - * @param [y=0] {Number} position of the point on the y axis - */ -PIXI.Point.prototype.set = function(x, y) -{ - this.x = x || 0; - this.y = y || ( (y !== 0) ? this.x : 0 ) ; -}; - -// constructor -PIXI.Point.prototype.constructor = PIXI.Point; \ No newline at end of file diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js deleted file mode 100644 index 77f8f56..0000000 --- a/src/geom/Polygon.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * @author Adrien Brault - */ - -/** - * @class Polygon - * @constructor - * @param points* {Array(Point)|Array(Number)|Point...|Number...} This can be an array of Points that form the polygon, - * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be - * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the - * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are - * Numbers. - */ -PIXI.Polygon = function(points) -{ - //if points isn't an array, use arguments as the array - if(!(points instanceof Array))points = Array.prototype.slice.call(arguments); - - //if this is a flat array of numbers, convert it to points - if(points[0] instanceof PIXI.Point) - { - var p = []; - for(var i = 0, il = points.length; i < il; i++) - { - p.push(points[i].x, points[i].y); - } - - points = p; - } - - this.closed = true; - - /** - * An array of the points of this polygon - * @property points - * @type Array(Point)|Array(Number) - * - */ - this.points = points; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.POLY in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this polygon - * - * @method clone - * @return {Polygon} a copy of the polygon - */ -PIXI.Polygon.prototype.clone = function() -{ - var points = this.points.slice(); - return new PIXI.Polygon(points); -}; - -/** - * Checks whether the x and y coordinates passed to this function are contained within this polygon - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this polygon - */ -PIXI.Polygon.prototype.contains = function(x, y) -{ - var inside = false; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = this.points.length / 2; - - for(var i = 0, j = length - 1; i < length; j = i++) - { - var xi = this.points[i * 2], yi = this.points[i * 2 + 1], - xj = this.points[j * 2], yj = this.points[j * 2 + 1], - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if(intersect) inside = !inside; - } - - return inside; -}; - -// constructor -PIXI.Polygon.prototype.constructor = PIXI.Polygon; diff --git a/src/geom/Rectangle.js b/src/geom/Rectangle.js deleted file mode 100644 index 61985fa..0000000 --- a/src/geom/Rectangle.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class Rectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rectangle - * @param width {Number} The overall width of this rectangle - * @param height {Number} The overall height of this rectangle - */ -PIXI.Rectangle = function(x, y, width, height) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rectangle - * - * @method clone - * @return {Rectangle} a copy of the rectangle - */ -PIXI.Rectangle.prototype.clone = function() -{ - return new PIXI.Rectangle(this.x, this.y, this.width, this.height); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rectangle - */ -PIXI.Rectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.Rectangle.prototype.constructor = PIXI.Rectangle; - -PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0); \ No newline at end of file diff --git a/src/geom/RoundedRectangle.js b/src/geom/RoundedRectangle.js deleted file mode 100644 index 4f75723..0000000 --- a/src/geom/RoundedRectangle.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @author Mat Groves http://matgroves.com/ - */ - -/** - * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. - * - * @class RoundedRectangle - * @constructor - * @param x {Number} The X coordinate of the upper-left corner of the rounded rectangle - * @param y {Number} The Y coordinate of the upper-left corner of the rounded rectangle - * @param width {Number} The overall width of this rounded rectangle - * @param height {Number} The overall height of this rounded rectangle - * @param radius {Number} Controls the radius of the rounded corners - */ -PIXI.RoundedRectangle = function(x, y, width, height, radius) -{ - /** - * @property x - * @type Number - * @default 0 - */ - this.x = x || 0; - - /** - * @property y - * @type Number - * @default 0 - */ - this.y = y || 0; - - /** - * @property width - * @type Number - * @default 0 - */ - this.width = width || 0; - - /** - * @property height - * @type Number - * @default 0 - */ - this.height = height || 0; - - /** - * @property radius - * @type Number - * @default 20 - */ - this.radius = radius || 20; - - /** - * The type of the object, should be one of the Graphics type consts, PIXI.Graphics.RRECT in this case - * @property type - * @type Number - * @default 0 - */ -}; - -/** - * Creates a clone of this Rounded Rectangle - * - * @method clone - * @return {RoundedRectangle} a copy of the rounded rectangle - */ -PIXI.RoundedRectangle.prototype.clone = function() -{ - return new PIXI.RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); -}; - -/** - * Checks whether the x and y coordinates given are contained within this Rounded Rectangle - * - * @method contains - * @param x {Number} The X coordinate of the point to test - * @param y {Number} The Y coordinate of the point to test - * @return {Boolean} Whether the x/y coordinates are within this Rounded Rectangle - */ -PIXI.RoundedRectangle.prototype.contains = function(x, y) -{ - if(this.width <= 0 || this.height <= 0) - return false; - - var x1 = this.x; - if(x >= x1 && x <= x1 + this.width) - { - var y1 = this.y; - - if(y >= y1 && y <= y1 + this.height) - { - return true; - } - } - - return false; -}; - -// constructor -PIXI.RoundedRectangle.prototype.constructor = PIXI.RoundedRectangle; - diff --git a/src/math/Matrix.js b/src/math/Matrix.js new file mode 100644 index 0000000..d717b58 --- /dev/null +++ b/src/math/Matrix.js @@ -0,0 +1,246 @@ +var Point = require('./Point'); + +/** + * The Matrix class is now an object, which makes it a lot faster, + * here is a representation of it : + * | a | b | tx| + * | c | d | ty| + * | 0 | 0 | 1 | + * + * @class + * @namespace PIXI + */ +function Matrix() { + /** + * @member {number} + * @default 1 + */ + this.a = 1; + + /** + * @member {number} + * @default 0 + */ + this.b = 0; + + /** + * @member {number} + * @default 0 + */ + this.c = 0; + + /** + * @member {number} + * @default 1 + */ + this.d = 1; + + /** + * @member {number} + * @default 0 + */ + this.tx = 0; + + /** + * @member {number} + * @default 0 + */ + this.ty = 0; +}; + +Matrix.prototype.constructor = Matrix; +module.exports = Matrix; + +/** + * Creates a Matrix object based on the given array. The Element to Matrix mapping order is as follows: + * + * a = array[0] + * b = array[1] + * c = array[3] + * d = array[4] + * tx = array[2] + * ty = array[5] + * + * @param array {number[]} The array that the matrix will be populated from. + */ +Matrix.prototype.fromArray = function (array) { + this.a = array[0]; + this.b = array[1]; + this.c = array[3]; + this.d = array[4]; + this.tx = array[2]; + this.ty = array[5]; +}; + +/** + * Creates an array from the current Matrix object. + * + * @param transpose {boolean} Whether we need to transpose the matrix or not + * @return {number[]} the newly created array which contains the matrix + */ +Matrix.prototype.toArray = function (transpose) { + if (!this.array) { + this.array = new Float32Array(9); + } + + var array = this.array; + + if (transpose) { + array[0] = this.a; + array[1] = this.b; + array[2] = 0; + array[3] = this.c; + array[4] = this.d; + array[5] = 0; + array[6] = this.tx; + array[7] = this.ty; + array[8] = 1; + } + else { + array[0] = this.a; + array[1] = this.c; + array[2] = this.tx; + array[3] = this.b; + array[4] = this.d; + array[5] = this.ty; + array[6] = 0; + array[7] = 0; + array[8] = 1; + } + + return array; +}; + +/** + * Get a new position with the current transformation applied. + * Can be used to go from a child's coordinate space to the world coordinate space. (e.g. rendering) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, transformed through this matrix + */ +Matrix.prototype.apply = function (pos, newPos) { + newPos = newPos || new Point(); + + newPos.x = this.a * pos.x + this.c * pos.y + this.tx; + newPos.y = this.b * pos.x + this.d * pos.y + this.ty; + + return newPos; +}; + +/** + * Get a new position with the inverse of the current transformation applied. + * Can be used to go from the world coordinate space to a child's coordinate space. (e.g. input) + * + * @param pos {Point} The origin + * @param [newPos] {Point} The point that the new position is assigned to (allowed to be same as input) + * @return {Point} The new point, inverse-transformed through this matrix + */ +Matrix.prototype.applyInverse = function (pos, newPos) { + newPos = newPos || new Point(); + + var id = 1 / (this.a * this.d + this.c * -this.b); + + newPos.x = this.d * id * pos.x + -this.c * id * pos.y + (this.ty * this.c - this.tx * this.d) * id; + newPos.y = this.a * id * pos.y + -this.b * id * pos.x + (-this.ty * this.a + this.tx * this.b) * id; + + return newPos; +}; + +/** + * Translates the matrix on the x and y. + * + * @param {number} x + * @param {number} y + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.translate = function (x, y) { + this.tx += x; + this.ty += y; + + return this; +}; + +/** + * Applies a scale transformation to the matrix. + * + * @param {number} x The amount to scale horizontally + * @param {number} y The amount to scale vertically + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.scale = function (x, y) { + this.a *= x; + this.d *= y; + this.c *= x; + this.b *= y; + this.tx *= x; + this.ty *= y; + + return this; +}; + + +/** + * Applies a rotation transformation to the matrix. + * + * @param {number} angle - The angle in radians. + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.rotate = function (angle) { + var cos = Math.cos( angle ); + var sin = Math.sin( angle ); + + var a1 = this.a; + var c1 = this.c; + var tx1 = this.tx; + + this.a = a1 * cos-this.b * sin; + this.b = a1 * sin+this.b * cos; + this.c = c1 * cos-this.d * sin; + this.d = c1 * sin+this.d * cos; + this.tx = tx1 * cos - this.ty * sin; + this.ty = tx1 * sin + this.ty * cos; + + return this; +}; + +/** + * Appends the given Matrix to this Matrix. + * + * @param {Matrix} matrix + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.append = function (matrix) { + var a1 = this.a; + var b1 = this.b; + var c1 = this.c; + var d1 = this.d; + + this.a = matrix.a * a1 + matrix.b * c1; + this.b = matrix.a * b1 + matrix.b * d1; + this.c = matrix.c * a1 + matrix.d * c1; + this.d = matrix.c * b1 + matrix.d * d1; + + this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx; + this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty; + + return this; +}; + +/** + * Resets this Matix to an identity (default) matrix. + * + * @return {Matrix} This matrix. Good for chaining method calls. + */ +Matrix.prototype.identity = function () { + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.tx = 0; + this.ty = 0; + + return this; +}; + +identityMatrix = new Matrix(); diff --git a/src/math/Point.js b/src/math/Point.js new file mode 100644 index 0000000..420e5bc --- /dev/null +++ b/src/math/Point.js @@ -0,0 +1,46 @@ +/** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. + * + * @class + * @namespace PIXI + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +function Point(x, y) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; +}; + +Point.prototype.constructor = Point; +module.exports = Point; + +/** + * Creates a clone of this point + * + * @return {Point} a copy of the point + */ +Point.prototype.clone = function () { + return new Point(this.x, this.y); +}; + +/** + * Sets the point to a new x and y position. + * If y is omitted, both x and y will be set to x. + * + * @param [x=0] {number} position of the point on the x axis + * @param [y=0] {number} position of the point on the y axis + */ +Point.prototype.set = function (x, y) { + this.x = x || 0; + this.y = y || ( (y !== 0) ? this.x : 0 ) ; +}; diff --git a/src/math/index.js b/src/math/index.js index 071c583..cc176e9 100644 --- a/src/math/index.js +++ b/src/math/index.js @@ -23,7 +23,12 @@ */ DEG_TO_RAD: Math.PI / 180, - Point: require('./Point'), - Matrix: require('./Matrix'), - Rectangle: require('./Rectangle') + Point: require('./Point'), + Matrix: require('./Matrix'), + + Circle: require('./shapes/Circle'), + Ellipse: require('./shapes/Ellipse'), + Polygon: require('./shapes/Polygon'), + Rectangle: require('./shapes/Rectangle'), + RoundedRectangle: require('./shapes/RoundedRectangle') }; diff --git a/src/math/shapes/Circle.js b/src/math/shapes/Circle.js new file mode 100644 index 0000000..208b30f --- /dev/null +++ b/src/math/shapes/Circle.js @@ -0,0 +1,79 @@ +/** + * The Circle object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of this circle + * @param y {number} The Y coordinate of the center of this circle + * @param radius {number} The radius of the circle + */ +function Circle(x, y, radius) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.radius = radius || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.CIRC in this case + * + * @member {number} + */ +}; + +Circle.prototype.constructor = Circle; +module.exports = Circle; + +/** + * Creates a clone of this Circle instance + * + * @method clone + * @return {Circle} a copy of the Circle + */ +Circle.prototype.clone = function () { + return new Circle(this.x, this.y, this.radius); +}; + +/** + * Checks whether the x and y coordinates given are contained within this circle + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this Circle + */ +Circle.prototype.contains = function (x, y) { + if (this.radius <= 0) + return false; + + var dx = (this.x - x), + dy = (this.y - y), + r2 = this.radius * this.radius; + + dx *= dx; + dy *= dy; + + return (dx + dy <= r2); +}; + +/** +* Returns the framing rectangle of the circle as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Circle.prototype.getBounds = function () { + return new Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2); +}; diff --git a/src/math/shapes/Ellipse.js b/src/math/shapes/Ellipse.js new file mode 100644 index 0000000..20d2539 --- /dev/null +++ b/src/math/shapes/Ellipse.js @@ -0,0 +1,86 @@ +/** + * The Ellipse object can be used to specify a hit area for displayObjects + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the center of the ellipse + * @param y {number} The Y coordinate of the center of the ellipse + * @param width {number} The half width of this ellipse + * @param height {number} The half height of this ellipse + */ +function Ellipse(x, y, width, height) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.width = width || 0; + + /** + * @member {number} + * @default 0 + */ + this.height = height || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.ELIP in this case + * + * @member {number} + */ +}; + +Ellipse.prototype.constructor = Ellipse; +module.exports = Ellipse; + +/** + * Creates a clone of this Ellipse instance + * + * @method clone + * @return {Ellipse} a copy of the ellipse + */ +Ellipse.prototype.clone = function () { + return new Ellipse(this.x, this.y, this.width, this.height); +}; + +/** + * Checks whether the x and y coordinates given are contained within this ellipse + * + * @method contains + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coords are within this ellipse + */ +Ellipse.prototype.contains = function (x, y) { + if (this.width <= 0 || this.height <= 0) + return false; + + //normalize the coords to an ellipse with center 0,0 + var normx = ((x - this.x) / this.width), + normy = ((y - this.y) / this.height); + + normx *= normx; + normy *= normy; + + return (normx + normy <= 1); +}; + +/** +* Returns the framing rectangle of the ellipse as a Rectangle object +* +* @method getBounds +* @return {Rectangle} the framing rectangle +*/ +Ellipse.prototype.getBounds = function () { + return new Rectangle(this.x - this.width, this.y - this.height, this.width, this.height); +}; diff --git a/src/math/shapes/Polygon.js b/src/math/shapes/Polygon.js new file mode 100644 index 0000000..b2c3483 --- /dev/null +++ b/src/math/shapes/Polygon.js @@ -0,0 +1,75 @@ +var Point = require('../Point'); + +/** + * @class + * @namespace PIXI + * @param points* {Point[]|number[]|Point...|number...} This can be an array of Points that form the polygon, + * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be + * all the points of the polygon e.g. `new Polygon(new Point(), new Point(), ...)`, or the + * arguments passed can be flat x,y values e.g. `new Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are + * Numbers. + */ +function Polygon(points) { + //if points isn't an array, use arguments as the array + if (!(points instanceof Array)) { + points = Array.prototype.slice.call(arguments); + } + + //if this is a flat array of numbers, convert it to points + if (points[0] instanceof Point) { + var p = []; + for (var i = 0, il = points.length; i < il; i++) { + p.push(points[i].x, points[i].y); + } + + points = p; + } + + this.closed = true; + + /** + * An array of the points of this polygon + * + * @member {Point[]} + */ + this.points = points; +}; + +Polygon.prototype.constructor = Polygon; +module.exports = Polygon; + +/** + * Creates a clone of this polygon + * + * @return {Polygon} a copy of the polygon + */ +Polygon.prototype.clone = function () { + return new Polygon(this.points.slice()); +}; + +/** + * Checks whether the x and y coordinates passed to this function are contained within this polygon + * + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this polygon + */ +Polygon.prototype.contains = function (x, y) { + var inside = false; + + // use some raycasting to test hits + // https://github.com/substack/point-in-polygon/blob/master/index.js + var length = this.points.length / 2; + + for (var i = 0, j = length - 1; i < length; j = i++) { + var xi = this.points[i * 2], yi = this.points[i * 2 + 1], + xj = this.points[j * 2], yj = this.points[j * 2 + 1], + intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); + + if (intersect) { + inside = !inside; + } + } + + return inside; +}; diff --git a/src/math/shapes/Rectangle.js b/src/math/shapes/Rectangle.js new file mode 100644 index 0000000..9c52db4 --- /dev/null +++ b/src/math/shapes/Rectangle.js @@ -0,0 +1,83 @@ +/** + * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height. + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the upper-left corner of the rectangle + * @param y {number} The Y coordinate of the upper-left corner of the rectangle + * @param width {number} The overall width of this rectangle + * @param height {number} The overall height of this rectangle + */ +function Rectangle(x, y, width, height) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.width = width || 0; + + /** + * @member {number} + * @default 0 + */ + this.height = height || 0; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.RECT in this case + * + * @member {number} + */ +}; + +Rectangle.prototype.constructor = Rectangle; +module.exports = Rectangle; + +/** + * A constant empty rectangle. + * + * @static + * @constant + */ +Rectangle.EMPTY = new Rectangle(0, 0, 0, 0); + + +/** + * Creates a clone of this Rectangle + * + * @return {Rectangle} a copy of the rectangle + */ +Rectangle.prototype.clone = function () { + return new Rectangle(this.x, this.y, this.width, this.height); +}; + +/** + * Checks whether the x and y coordinates given are contained within this Rectangle + * + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this Rectangle + */ +Rectangle.prototype.contains = function (x, y) { + if (this.width <= 0 || this.height <= 0) { + return false; + } + + if (x >= this.x && x <= this.x + this.width) { + if (y >= this.y && y <= this.y + this.height) { + return true; + } + } + + return false; +}; diff --git a/src/math/shapes/RoundedRectangle.js b/src/math/shapes/RoundedRectangle.js new file mode 100644 index 0000000..b592389 --- /dev/null +++ b/src/math/shapes/RoundedRectangle.js @@ -0,0 +1,80 @@ +/** + * The Rounded Rectangle object is an area defined by its position and has nice rounded corners, as indicated by its top-left corner point (x, y) and by its width and its height. + * + * @class + * @namespace PIXI + * @param x {number} The X coordinate of the upper-left corner of the rounded rectangle + * @param y {number} The Y coordinate of the upper-left corner of the rounded rectangle + * @param width {number} The overall width of this rounded rectangle + * @param height {number} The overall height of this rounded rectangle + * @param radius {number} Controls the radius of the rounded corners + */ +function RoundedRectangle(x, y, width, height, radius) { + /** + * @member {number} + * @default 0 + */ + this.x = x || 0; + + /** + * @member {number} + * @default 0 + */ + this.y = y || 0; + + /** + * @member {number} + * @default 0 + */ + this.width = width || 0; + + /** + * @member {number} + * @default 0 + */ + this.height = height || 0; + + /** + * @member {number} + * @default 20 + */ + this.radius = radius || 20; + + /** + * The type of the object, should be one of the Graphics type consts, Graphics.RRECT in this case + * + * @member {number} + */ +}; + +RoundedRectangle.prototype.constructor = RoundedRectangle; +module.exports = RoundedRectangle; + +/** + * Creates a clone of this Rounded Rectangle + * + * @return {RoundedRectangle} a copy of the rounded rectangle + */ +RoundedRectangle.prototype.clone = function () { + return new RoundedRectangle(this.x, this.y, this.width, this.height, this.radius); +}; + +/** + * Checks whether the x and y coordinates given are contained within this Rounded Rectangle + * + * @param x {number} The X coordinate of the point to test + * @param y {number} The Y coordinate of the point to test + * @return {boolean} Whether the x/y coordinates are within this Rounded Rectangle + */ +RoundedRectangle.prototype.contains = function (x, y) { + if (this.width <= 0 || this.height <= 0) + return false; + + if (x >= this.x && x <= this.x + this.width) { + if (y >= this.y && y <= this.y + this.height) { + return true; + } + } + + return false; +};