]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/src/util/Point.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / util / Point.js
CommitLineData
6527f429
DM
1/**\r
2 * Represents a 2D point with x and y properties, useful for comparison and instantiation\r
3 * from an event:\r
4 *\r
5 * var point = Ext.util.Point.fromEvent(e);\r
6 */\r
7Ext.define('Ext.util.Point', {\r
8 extend: 'Ext.util.Region',\r
9\r
10 radianToDegreeConstant: 180 / Math.PI,\r
11 \r
12 origin: { x: 0, y: 0 },\r
13\r
14 statics: {\r
15 /**\r
16 * Returns a new instance of {@link Ext.util.Point} based on the `pageX` / `pageY` values of the given event.\r
17 * @static\r
18 * @param {Event} e The event.\r
19 * @return {Ext.util.Point}\r
20 */\r
21 fromEvent: function(e) {\r
22 var changedTouches = e.changedTouches,\r
23 touch = (changedTouches && changedTouches.length > 0) ? changedTouches[0] : e;\r
24\r
25 return this.fromTouch(touch);\r
26 },\r
27\r
28 /**\r
29 * Returns a new instance of {@link Ext.util.Point} based on the `pageX` / `pageY` values of the given touch.\r
30 * @static\r
31 * @param {Event} touch\r
32 * @return {Ext.util.Point}\r
33 */\r
34 fromTouch: function(touch) {\r
35 return new this(touch.pageX, touch.pageY);\r
36 },\r
37\r
38 /**\r
39 * Returns a new point from an object that has `x` and `y` properties, if that object is not an instance\r
40 * of {@link Ext.util.Point}. Otherwise, returns the given point itself.\r
41 * @param {Object} object\r
42 * @return {Ext.util.Point}\r
43 */\r
44 from: function(object) {\r
45 if (!object) {\r
46 return new this(0, 0);\r
47 }\r
48\r
49 if (!(object instanceof this)) {\r
50 return new this(object.x, object.y);\r
51 }\r
52\r
53 return object;\r
54 }\r
55 },\r
56\r
57 /**\r
58 * Creates point on 2D plane.\r
59 * @param {Number} [x=0] X coordinate.\r
60 * @param {Number} [y=0] Y coordinate.\r
61 */\r
62 constructor: function(x, y) {\r
63 if (x == null) {\r
64 x = 0;\r
65 }\r
66\r
67 if (y == null) {\r
68 y = 0;\r
69 }\r
70\r
71 this.callParent([y, x, y, x]);\r
72 },\r
73\r
74 /**\r
75 * Copy a new instance of this point.\r
76 * @return {Ext.util.Point} The new point.\r
77 */\r
78 clone: function() {\r
79 return new this.self(this.x, this.y);\r
80 },\r
81\r
82 /**\r
83 * Clones this Point.\r
84 * @deprecated 2.0.0 Please use {@link #clone} instead.\r
85 * @return {Ext.util.Point} The new point.\r
86 */\r
87 copy: function() {\r
88 return this.clone.apply(this, arguments);\r
89 },\r
90\r
91 /**\r
92 * Copy the `x` and `y` values of another point / object to this point itself.\r
93 * @param {Ext.util.Point/Object} point.\r
94 * @return {Ext.util.Point} This point.\r
95 */\r
96 copyFrom: function(point) {\r
97 this.x = point.x;\r
98 this.y = point.y;\r
99\r
100 return this;\r
101 },\r
102\r
103 /**\r
104 * Returns a human-eye-friendly string that represents this point,\r
105 * useful for debugging.\r
106 * @return {String} For example `Point[12,8]`.\r
107 */\r
108 toString: function() {\r
109 return "Point[" + this.x + "," + this.y + "]";\r
110 },\r
111\r
112 /**\r
113 * Compare this point and another point.\r
114 * @param {Ext.util.Point/Object} point The point to compare with, either an instance\r
115 * of {@link Ext.util.Point} or an object with `x` and `y` properties.\r
116 * @return {Boolean} Returns whether they are equivalent.\r
117 */\r
118 equals: function(point) {\r
119 return (this.x === point.x && this.y === point.y);\r
120 },\r
121\r
122 /**\r
123 * Returns `true` if the passed point is within a certain distance of this point.\r
124 * @param {Ext.util.Point/Object} point The point to check with, either an instance\r
125 * of {@link Ext.util.Point} or an object with `x` and `y` properties.\r
126 * @param {Object/Number} threshold Can be either an object with `x` and `y` properties or a number.\r
127 * @return {Boolean}\r
128 */\r
129 isCloseTo: function(point, threshold) {\r
130 if (typeof threshold == 'number') {\r
131 return this.getDistanceTo(point) <= threshold;\r
132 }\r
133\r
134 var x = point.x,\r
135 y = point.y,\r
136 thresholdX = threshold.x,\r
137 thresholdY = threshold.y;\r
138\r
139 return (this.x <= x + thresholdX && this.x >= x - thresholdX &&\r
140 this.y <= y + thresholdY && this.y >= y - thresholdY);\r
141 },\r
142\r
143 /**\r
144 * Returns `true` if this point is close to another one.\r
145 * @deprecated 2.0.0 Please use {@link #isCloseTo} instead.\r
146 * @return {Boolean}\r
147 */\r
148 isWithin: function() {\r
149 return this.isCloseTo.apply(this, arguments);\r
150 },\r
151\r
152 /**\r
153 * Determins whether this Point contained by the passed Region, Component or element.\r
154 * @param {Ext.util.Region/Ext.Component/Ext.dom.Element/HTMLElement} region\r
155 * The rectangle to check that this Point is within.\r
156 * @return {Boolean}\r
157 */\r
158 isContainedBy: function(region) {\r
159 if (!(region instanceof Ext.util.Region)) {\r
160 region = Ext.get(region.el || region).getRegion();\r
161 }\r
162 return region.contains(this);\r
163 },\r
164\r
165 /**\r
166 * Compare this point with another point when the `x` and `y` values of both points are rounded. For example:\r
167 * [100.3,199.8] will equals to [100, 200].\r
168 * @param {Ext.util.Point/Object} point The point to compare with, either an instance\r
169 * of Ext.util.Point or an object with `x` and `y` properties.\r
170 * @return {Boolean}\r
171 */\r
172 roundedEquals: function(point) {\r
173 if (!point || typeof point !== 'object') {\r
174 point = this.origin;\r
175 }\r
176\r
177 return (Math.round(this.x) === Math.round(point.x) &&\r
178 Math.round(this.y) === Math.round(point.y));\r
179 },\r
180\r
181 getDistanceTo: function(point) {\r
182 if (!point || typeof point !== 'object') {\r
183 point = this.origin;\r
184 }\r
185\r
186 var deltaX = this.x - point.x,\r
187 deltaY = this.y - point.y;\r
188\r
189 return Math.sqrt(deltaX * deltaX + deltaY * deltaY);\r
190 },\r
191\r
192 getAngleTo: function(point) {\r
193 if (!point || typeof point !== 'object') {\r
194 point = this.origin;\r
195 }\r
196\r
197 var deltaX = this.x - point.x,\r
198 deltaY = this.y - point.y;\r
199\r
200 return Math.atan2(deltaY, deltaX) * this.radianToDegreeConstant;\r
201 }\r
202}, function() {\r
203 /**\r
204 * @method\r
205 * Alias for {@link #translateBy}\r
206 * @inheritdoc Ext.util.Region#translateBy\r
207 */\r
208 this.prototype.translate = this.prototype.translateBy;\r
209});\r