]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/util/Point.js
2 * Represents a 2D point with x and y properties, useful for comparison and instantiation
5 * var point = Ext.util.Point.fromEvent(e);
7 Ext
.define('Ext.util.Point', {
9 radianToDegreeConstant
: 180 / Math
.PI
,
13 * Returns a new instance of {@link Ext.util.Point} based on the `pageX` / `pageY` values of the given event.
15 * @param {Event} e The event.
16 * @return {Ext.util.Point}
18 fromEvent: function(e
) {
19 var changedTouches
= e
.changedTouches
,
20 touch
= (changedTouches
&& changedTouches
.length
> 0) ? changedTouches
[0] : e
;
22 return this.fromTouch(touch
);
26 * Returns a new instance of {@link Ext.util.Point} based on the `pageX` / `pageY` values of the given touch.
28 * @param {Event} touch
29 * @return {Ext.util.Point}
31 fromTouch: function(touch
) {
32 return new this(touch
.pageX
, touch
.pageY
);
36 * Returns a new point from an object that has `x` and `y` properties, if that object is not an instance
37 * of {@link Ext.util.Point}. Otherwise, returns the given point itself.
38 * @param {Object} object
39 * @return {Ext.util.Point}
41 from: function(object
) {
43 return new this(0, 0);
46 if (!(object
instanceof this)) {
47 return new this(object
.x
, object
.y
);
55 * Creates point on 2D plane.
56 * @param {Number} [x=0] X coordinate.
57 * @param {Number} [y=0] Y coordinate.
59 constructor: function(x
, y
) {
60 if (typeof x
== 'undefined') {
64 if (typeof y
== 'undefined') {
75 * Copy a new instance of this point.
76 * @return {Ext.util.Point} The new point.
79 return new this.self(this.x
, this.y
);
84 * @deprecated 2.0.0 Please use {@link #clone} instead.
85 * @return {Ext.util.Point} The new point.
88 return this.clone
.apply(this, arguments
);
92 * Copy the `x` and `y` values of another point / object to this point itself.
93 * @param {Ext.util.Point/Object} point.
94 * @return {Ext.util.Point} This point.
96 copyFrom: function(point
) {
104 * Returns a human-eye-friendly string that represents this point,
105 * useful for debugging.
106 * @return {String} For example `Point[12,8]`.
108 toString: function() {
109 return "Point[" + this.x
+ "," + this.y
+ "]";
113 * Compare this point and another point.
114 * @param {Ext.util.Point/Object} point The point to compare with, either an instance
115 * of {@link Ext.util.Point} or an object with `x` and `y` properties.
116 * @return {Boolean} Returns whether they are equivalent.
118 equals: function(point
) {
119 return (this.x
=== point
.x
&& this.y
=== point
.y
);
123 * Whether the given point is not away from this point within the given threshold amount.
124 * @param {Ext.util.Point/Object} point The point to check with, either an instance
125 * of {@link Ext.util.Point} or an object with `x` and `y` properties.
126 * @param {Object/Number} threshold Can be either an object with `x` and `y` properties or a number.
129 isCloseTo: function(point
, threshold
) {
130 if (typeof threshold
== 'number') {
131 threshold
= {x
: threshold
};
132 threshold
.y
= threshold
.x
;
137 thresholdX
= threshold
.x
,
138 thresholdY
= threshold
.y
;
140 return (this.x
<= x
+ thresholdX
&& this.x
>= x
- thresholdX
&&
141 this.y
<= y
+ thresholdY
&& this.y
>= y
- thresholdY
);
145 * Returns `true` if this point is close to another one.
146 * @deprecated 2.0.0 Please use {@link #isCloseTo} instead.
149 isWithin: function() {
150 return this.isCloseTo
.apply(this, arguments
);
154 * Translate this point by the given amounts.
155 * @param {Number} x Amount to translate in the x-axis.
156 * @param {Number} y Amount to translate in the y-axis.
159 translate: function(x
, y
) {
167 * Compare this point with another point when the `x` and `y` values of both points are rounded. For example:
168 * [100.3,199.8] will equals to [100, 200].
169 * @param {Ext.util.Point/Object} point The point to compare with, either an instance
170 * of Ext.util.Point or an object with `x` and `y` properties.
173 roundedEquals: function(point
) {
174 if (typeof point
!= 'object') {
175 point
= { x
: 0, y
: 0};
178 return (Math
.round(this.x
) === Math
.round(point
.x
) &&
179 Math
.round(this.y
) === Math
.round(point
.y
));
182 getDistanceTo: function(point
) {
183 if (typeof point
!= 'object') {
184 point
= { x
: 0, y
: 0};
187 var deltaX
= this.x
- point
.x
,
188 deltaY
= this.y
- point
.y
;
190 return Math
.sqrt(deltaX
* deltaX
+ deltaY
* deltaY
);
193 getAngleTo: function(point
) {
194 if (typeof point
!= 'object') {
195 point
= { x
: 0, y
: 0};
198 var deltaX
= this.x
- point
.x
,
199 deltaY
= this.y
- point
.y
;
201 return Math
.atan2(deltaY
, deltaX
) * this.radianToDegreeConstant
;