2010-10-22 02:54:00 +00:00
|
|
|
(function(global) {
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2014-02-16 21:36:03 +00:00
|
|
|
'use strict';
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-07-10 01:25:28 +00:00
|
|
|
/* Adaptation of work of Kevin Lindsey (kevin@kevlindev.com) */
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
var fabric = global.fabric || (global.fabric = { });
|
2010-06-10 15:37:51 +00:00
|
|
|
|
2012-10-14 00:53:12 +00:00
|
|
|
if (fabric.Point) {
|
2010-10-11 18:45:06 +00:00
|
|
|
fabric.warn('fabric.Point is already defined');
|
2010-06-10 15:37:51 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2010-10-14 21:42:39 +00:00
|
|
|
|
|
|
|
|
fabric.Point = Point;
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Point class
|
2013-04-25 18:21:32 +00:00
|
|
|
* @class fabric.Point
|
2013-09-06 17:12:57 +00:00
|
|
|
* @memberOf fabric
|
2010-10-14 21:42:39 +00:00
|
|
|
* @constructor
|
|
|
|
|
* @param {Number} x
|
|
|
|
|
* @param {Number} y
|
|
|
|
|
* @return {fabric.Point} thisArg
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
function Point(x, y) {
|
2013-06-06 00:49:42 +00:00
|
|
|
this.x = x;
|
|
|
|
|
this.y = y;
|
2010-06-10 15:37:51 +00:00
|
|
|
}
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2013-04-24 16:58:04 +00:00
|
|
|
Point.prototype = /** @lends fabric.Point.prototype */ {
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-06-10 15:37:51 +00:00
|
|
|
constructor: Point,
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Adds another point to this one and returns another one
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {fabric.Point} new Point instance with added values
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
add: function (that) {
|
|
|
|
|
return new Point(this.x + that.x, this.y + that.y);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Adds another point to this one
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {fabric.Point} thisArg
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
addEquals: function (that) {
|
|
|
|
|
this.x += that.x;
|
|
|
|
|
this.y += that.y;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Adds value to this point and returns a new one
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Number} scalar
|
|
|
|
|
* @return {fabric.Point} new Point with added value
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
scalarAdd: function (scalar) {
|
|
|
|
|
return new Point(this.x + scalar, this.y + scalar);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Adds value to this point
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Number} scalar
|
|
|
|
|
* @param {fabric.Point} thisArg
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
scalarAddEquals: function (scalar) {
|
|
|
|
|
this.x += scalar;
|
|
|
|
|
this.y += scalar;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Subtracts another point from this point and returns a new one
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {fabric.Point} new Point object with subtracted values
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
subtract: function (that) {
|
|
|
|
|
return new Point(this.x - that.x, this.y - that.y);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Subtracts another point from this point
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {fabric.Point} thisArg
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
subtractEquals: function (that) {
|
|
|
|
|
this.x -= that.x;
|
|
|
|
|
this.y -= that.y;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Subtracts value from this point and returns a new one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {Number} scalar
|
|
|
|
|
* @return {fabric.Point}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
scalarSubtract: function (scalar) {
|
|
|
|
|
return new Point(this.x - scalar, this.y - scalar);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Subtracts value from this point
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {Number} scalar
|
|
|
|
|
* @return {fabric.Point} thisArg
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
scalarSubtractEquals: function (scalar) {
|
|
|
|
|
this.x -= scalar;
|
|
|
|
|
this.y -= scalar;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Miltiplies this point by a value and returns a new one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {Number} scalar
|
|
|
|
|
* @return {fabric.Point}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
multiply: function (scalar) {
|
|
|
|
|
return new Point(this.x * scalar, this.y * scalar);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Miltiplies this point by a value
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {Number} scalar
|
|
|
|
|
* @return {fabric.Point} thisArg
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
multiplyEquals: function (scalar) {
|
|
|
|
|
this.x *= scalar;
|
|
|
|
|
this.y *= scalar;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Divides this point by a value and returns a new one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {Number} scalar
|
|
|
|
|
* @return {fabric.Point}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
divide: function (scalar) {
|
|
|
|
|
return new Point(this.x / scalar, this.y / scalar);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Divides this point by a value
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {Number} scalar
|
|
|
|
|
* @return {fabric.Point} thisArg
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
divideEquals: function (scalar) {
|
|
|
|
|
this.x /= scalar;
|
|
|
|
|
this.y /= scalar;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Returns true if this point is equal to another one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {Boolean}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
eq: function (that) {
|
2012-10-14 00:53:12 +00:00
|
|
|
return (this.x === that.x && this.y === that.y);
|
2010-06-10 15:37:51 +00:00
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Returns true if this point is less than another one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {Boolean}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
lt: function (that) {
|
|
|
|
|
return (this.x < that.x && this.y < that.y);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Returns true if this point is less than or equal to another one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {Boolean}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
lte: function (that) {
|
|
|
|
|
return (this.x <= that.x && this.y <= that.y);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-18 17:12:08 +00:00
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
* Returns true if this point is greater another one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {Boolean}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
gt: function (that) {
|
|
|
|
|
return (this.x > that.x && this.y > that.y);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Returns true if this point is greater than or equal to another one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {Boolean}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
gte: function (that) {
|
|
|
|
|
return (this.x >= that.x && this.y >= that.y);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Returns new point which is the result of linear interpolation with this one and another one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @param {Number} t
|
|
|
|
|
* @return {fabric.Point}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
lerp: function (that, t) {
|
|
|
|
|
return new Point(this.x + (that.x - this.x) * t, this.y + (that.y - this.y) * t);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-15 16:05:23 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Returns distance from this point and another one
|
2012-12-15 16:05:23 +00:00
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {Number}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
distanceFrom: function (that) {
|
2010-06-10 15:49:13 +00:00
|
|
|
var dx = this.x - that.x,
|
|
|
|
|
dy = this.y - that.y;
|
2010-06-10 15:37:51 +00:00
|
|
|
return Math.sqrt(dx * dx + dy * dy);
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-11-24 19:12:33 +00:00
|
|
|
/**
|
2012-12-16 19:44:26 +00:00
|
|
|
* Returns the point between this point and another one
|
|
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {fabric.Point}
|
2012-12-15 16:05:23 +00:00
|
|
|
*/
|
2012-11-24 19:12:33 +00:00
|
|
|
midPointFrom: function (that) {
|
|
|
|
|
return new Point(this.x + (that.x - this.x)/2, this.y + (that.y - this.y)/2);
|
|
|
|
|
},
|
|
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a new point which is the min of this and another one
|
|
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {fabric.Point}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
min: function (that) {
|
|
|
|
|
return new Point(Math.min(this.x, that.x), Math.min(this.y, that.y));
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a new point which is the max of this and another one
|
|
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
* @return {fabric.Point}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
max: function (that) {
|
|
|
|
|
return new Point(Math.max(this.x, that.x), Math.max(this.y, that.y));
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* Returns string representation of this point
|
|
|
|
|
* @return {String}
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
toString: function () {
|
2014-02-16 21:36:03 +00:00
|
|
|
return this.x + ',' + this.y;
|
2010-06-10 15:37:51 +00:00
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* Sets x/y of this point
|
|
|
|
|
* @param {Number} x
|
|
|
|
|
* @return {Number} y
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
setXY: function (x, y) {
|
|
|
|
|
this.x = x;
|
|
|
|
|
this.y = y;
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* Sets x/y of this point from another point
|
|
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
setFromPoint: function (that) {
|
|
|
|
|
this.x = that.x;
|
|
|
|
|
this.y = that.y;
|
|
|
|
|
},
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* Swaps x/y of this point and another point
|
|
|
|
|
* @param {fabric.Point} that
|
|
|
|
|
*/
|
2010-06-10 15:37:51 +00:00
|
|
|
swap: function (that) {
|
2010-06-10 15:49:13 +00:00
|
|
|
var x = this.x,
|
|
|
|
|
y = this.y;
|
2010-06-10 15:37:51 +00:00
|
|
|
this.x = that.x;
|
|
|
|
|
this.y = that.y;
|
|
|
|
|
that.x = x;
|
|
|
|
|
that.y = y;
|
|
|
|
|
}
|
|
|
|
|
};
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2012-11-24 19:12:33 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|