/ext-4.0.7/src/util/Point.js

https://bitbucket.org/srogerf/javascript · JavaScript · 108 lines · 33 code · 11 blank · 64 comment · 10 complexity · a9b1a891b007a9500eda5f4cf288076a MD5 · raw file

  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * Represents a 2D point with x and y properties, useful for comparison and instantiation
  11. * from an event:
  12. *
  13. * var point = Ext.util.Point.fromEvent(e);
  14. *
  15. */
  16. Ext.define('Ext.util.Point', {
  17. /* Begin Definitions */
  18. extend: 'Ext.util.Region',
  19. statics: {
  20. /**
  21. * Returns a new instance of Ext.util.Point base on the pageX / pageY values of the given event
  22. * @static
  23. * @param {Event} e The event
  24. * @return {Ext.util.Point}
  25. */
  26. fromEvent: function(e) {
  27. e = (e.changedTouches && e.changedTouches.length > 0) ? e.changedTouches[0] : e;
  28. return new this(e.pageX, e.pageY);
  29. }
  30. },
  31. /* End Definitions */
  32. /**
  33. * Creates a point from two coordinates.
  34. * @param {Number} x X coordinate.
  35. * @param {Number} y Y coordinate.
  36. */
  37. constructor: function(x, y) {
  38. this.callParent([y, x, y, x]);
  39. },
  40. /**
  41. * Returns a human-eye-friendly string that represents this point,
  42. * useful for debugging
  43. * @return {String}
  44. */
  45. toString: function() {
  46. return "Point[" + this.x + "," + this.y + "]";
  47. },
  48. /**
  49. * Compare this point and another point
  50. * @param {Ext.util.Point/Object} The point to compare with, either an instance
  51. * of Ext.util.Point or an object with left and top properties
  52. * @return {Boolean} Returns whether they are equivalent
  53. */
  54. equals: function(p) {
  55. return (this.x == p.x && this.y == p.y);
  56. },
  57. /**
  58. * Whether the given point is not away from this point within the given threshold amount.
  59. * @param {Ext.util.Point/Object} p The point to check with, either an instance
  60. * of Ext.util.Point or an object with left and top properties
  61. * @param {Object/Number} threshold Can be either an object with x and y properties or a number
  62. * @return {Boolean}
  63. */
  64. isWithin: function(p, threshold) {
  65. if (!Ext.isObject(threshold)) {
  66. threshold = {
  67. x: threshold,
  68. y: threshold
  69. };
  70. }
  71. return (this.x <= p.x + threshold.x && this.x >= p.x - threshold.x &&
  72. this.y <= p.y + threshold.y && this.y >= p.y - threshold.y);
  73. },
  74. /**
  75. * Compare this point with another point when the x and y values of both points are rounded. E.g:
  76. * [100.3,199.8] will equals to [100, 200]
  77. * @param {Ext.util.Point/Object} p The point to compare with, either an instance
  78. * of Ext.util.Point or an object with x and y properties
  79. * @return {Boolean}
  80. */
  81. roundedEquals: function(p) {
  82. return (Math.round(this.x) == Math.round(p.x) && Math.round(this.y) == Math.round(p.y));
  83. }
  84. }, function() {
  85. /**
  86. * @method
  87. * Alias for {@link #translateBy}
  88. * @alias Ext.util.Region#translateBy
  89. */
  90. this.prototype.translate = Ext.util.Region.prototype.translateBy;
  91. });