1 (function(global) { 2 3 "use strict"; 4 5 /* Adaptation of work of Kevin Lindsey (kevin@kevlindev.com) */ 6 7 var fabric = global.fabric || (global.fabric = { }); 8 9 if (fabric.Point) { 10 fabric.warn('fabric.Point is already defined'); 11 return; 12 } 13 14 fabric.Point = Point; 15 16 /** 17 * @name Point 18 * @memberOf fabric 19 * @constructor 20 * @param {Number} x 21 * @param {Number} y 22 * @return {fabric.Point} thisArg 23 */ 24 function Point(x, y) { 25 if (arguments.length > 0) { 26 this.init(x, y); 27 } 28 } 29 30 Point.prototype = /** @scope fabric.Point.prototype */ { 31 32 constructor: Point, 33 34 /** 35 * @method init 36 * @param {Number} x 37 * @param {Number} y 38 */ 39 init: function (x, y) { 40 this.x = x; 41 this.y = y; 42 }, 43 44 /** 45 * @method add 46 * @param {fabric.Point} that 47 * @return {fabric.Point} new Point instance with added values 48 */ 49 add: function (that) { 50 return new Point(this.x + that.x, this.y + that.y); 51 }, 52 53 /** 54 * @method addEquals 55 * @param {fabric.Point} that 56 * @return {fabric.Point} thisArg 57 */ 58 addEquals: function (that) { 59 this.x += that.x; 60 this.y += that.y; 61 return this; 62 }, 63 64 /** 65 * @method scalarAdd 66 * @param {Number} scalar 67 * @return {fabric.Point} new Point with added value 68 */ 69 scalarAdd: function (scalar) { 70 return new Point(this.x + scalar, this.y + scalar); 71 }, 72 73 /** 74 * @method scalarAddEquals 75 * @param {Number} scalar 76 * @param {fabric.Point} thisArg 77 */ 78 scalarAddEquals: function (scalar) { 79 this.x += scalar; 80 this.y += scalar; 81 return this; 82 }, 83 84 /** 85 * @method subtract 86 * @param {fabric.Point} that 87 * @return {fabric.Point} new Point object with subtracted values 88 */ 89 subtract: function (that) { 90 return new Point(this.x - that.x, this.y - that.y); 91 }, 92 93 /** 94 * @method subtractEquals 95 * @param {fabric.Point} that 96 * @return {fabric.Point} thisArg 97 */ 98 subtractEquals: function (that) { 99 this.x -= that.x; 100 this.y -= that.y; 101 return this; 102 }, 103 104 scalarSubtract: function (scalar) { 105 return new Point(this.x - scalar, this.y - scalar); 106 }, 107 108 scalarSubtractEquals: function (scalar) { 109 this.x -= scalar; 110 this.y -= scalar; 111 return this; 112 }, 113 114 multiply: function (scalar) { 115 return new Point(this.x * scalar, this.y * scalar); 116 }, 117 118 multiplyEquals: function (scalar) { 119 this.x *= scalar; 120 this.y *= scalar; 121 return this; 122 }, 123 124 divide: function (scalar) { 125 return new Point(this.x / scalar, this.y / scalar); 126 }, 127 128 divideEquals: function (scalar) { 129 this.x /= scalar; 130 this.y /= scalar; 131 return this; 132 }, 133 134 eq: function (that) { 135 return (this.x == that.x && this.y == that.y); 136 }, 137 138 lt: function (that) { 139 return (this.x < that.x && this.y < that.y); 140 }, 141 142 lte: function (that) { 143 return (this.x <= that.x && this.y <= that.y); 144 }, 145 146 gt: function (that) { 147 return (this.x > that.x && this.y > that.y); 148 }, 149 150 gte: function (that) { 151 return (this.x >= that.x && this.y >= that.y); 152 }, 153 154 lerp: function (that, t) { 155 return new Point(this.x + (that.x - this.x) * t, this.y + (that.y - this.y) * t); 156 }, 157 158 distanceFrom: function (that) { 159 var dx = this.x - that.x, 160 dy = this.y - that.y; 161 return Math.sqrt(dx * dx + dy * dy); 162 }, 163 164 min: function (that) { 165 return new Point(Math.min(this.x, that.x), Math.min(this.y, that.y)); 166 }, 167 168 max: function (that) { 169 return new Point(Math.max(this.x, that.x), Math.max(this.y, that.y)); 170 }, 171 172 toString: function () { 173 return this.x + "," + this.y; 174 }, 175 176 setXY: function (x, y) { 177 this.x = x; 178 this.y = y; 179 }, 180 181 setFromPoint: function (that) { 182 this.x = that.x; 183 this.y = that.y; 184 }, 185 186 swap: function (that) { 187 var x = this.x, 188 y = this.y; 189 this.x = that.x; 190 this.y = that.y; 191 that.x = x; 192 that.y = y; 193 } 194 }; 195 196 })(this);