Point.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Represents a 2D point with x and y properties, useful for comparison and instantiation
  3. * from an event:
  4. *
  5. * var point = Ext.util.Point.fromEvent(e);
  6. *
  7. */
  8. Ext.define('Ext.util.Point', {
  9. /* Begin Definitions */
  10. extend: 'Ext.util.Region',
  11. statics: {
  12. /**
  13. * Returns a new instance of Ext.util.Point base on the pageX / pageY values of the given event
  14. * @static
  15. * @param {Event} e The event
  16. * @return {Ext.util.Point}
  17. */
  18. fromEvent: function(e) {
  19. e = (e.changedTouches && e.changedTouches.length > 0) ? e.changedTouches[0] : e;
  20. return new this(e.pageX, e.pageY);
  21. }
  22. },
  23. /* End Definitions */
  24. /**
  25. * Creates a point from two coordinates.
  26. * @param {Number} x X coordinate.
  27. * @param {Number} y Y coordinate.
  28. */
  29. constructor: function(x, y) {
  30. this.callParent([y, x, y, x]);
  31. },
  32. /**
  33. * Returns a human-eye-friendly string that represents this point,
  34. * useful for debugging
  35. * @return {String}
  36. */
  37. toString: function() {
  38. return "Point[" + this.x + "," + this.y + "]";
  39. },
  40. /**
  41. * Compare this point and another point
  42. * @param {Ext.util.Point/Object} The point to compare with, either an instance
  43. * of Ext.util.Point or an object with left and top properties
  44. * @return {Boolean} Returns whether they are equivalent
  45. */
  46. equals: function(p) {
  47. return (this.x == p.x && this.y == p.y);
  48. },
  49. /**
  50. * Whether the given point is not away from this point within the given threshold amount.
  51. * @param {Ext.util.Point/Object} p The point to check with, either an instance
  52. * of Ext.util.Point or an object with left and top properties
  53. * @param {Object/Number} threshold Can be either an object with x and y properties or a number
  54. * @return {Boolean}
  55. */
  56. isWithin: function(p, threshold) {
  57. if (!Ext.isObject(threshold)) {
  58. threshold = {
  59. x: threshold,
  60. y: threshold
  61. };
  62. }
  63. return (this.x <= p.x + threshold.x && this.x >= p.x - threshold.x &&
  64. this.y <= p.y + threshold.y && this.y >= p.y - threshold.y);
  65. },
  66. /**
  67. * Compare this point with another point when the x and y values of both points are rounded. E.g:
  68. * [100.3,199.8] will equals to [100, 200]
  69. * @param {Ext.util.Point/Object} p The point to compare with, either an instance
  70. * of Ext.util.Point or an object with x and y properties
  71. * @return {Boolean}
  72. */
  73. roundedEquals: function(p) {
  74. return (Math.round(this.x) == Math.round(p.x) && Math.round(this.y) == Math.round(p.y));
  75. }
  76. }, function() {
  77. /**
  78. * @method
  79. * Alias for {@link #translateBy}
  80. * @inheritdoc Ext.util.Region#translateBy
  81. */
  82. this.prototype.translate = Ext.util.Region.prototype.translateBy;
  83. });