coordinates.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**********************************************************
  2. Very minorly modified from the example by Tim Taylor
  3. http://tool-man.org/examples/sorting.html
  4. Added Coordinate.prototype.inside( northwest, southeast );
  5. **********************************************************/
  6. var Coordinates = {
  7. ORIGIN : new Coordinate(0, 0),
  8. northwestPosition : function(element) {
  9. var x = parseInt(element.style.left);
  10. var y = parseInt(element.style.top);
  11. return new Coordinate(isNaN(x) ? 0 : x, isNaN(y) ? 0 : y);
  12. },
  13. southeastPosition : function(element) {
  14. return Coordinates.northwestPosition(element).plus(
  15. new Coordinate(element.offsetWidth, element.offsetHeight));
  16. },
  17. northwestOffset : function(element, isRecursive) {
  18. var offset = new Coordinate(element.offsetLeft, element.offsetTop);
  19. if (!isRecursive) return offset;
  20. var parent = element.offsetParent;
  21. while (parent) {
  22. offset = offset.plus(
  23. new Coordinate(parent.offsetLeft, parent.offsetTop));
  24. parent = parent.offsetParent;
  25. }
  26. return offset;
  27. },
  28. southeastOffset : function(element, isRecursive) {
  29. return Coordinates.northwestOffset(element, isRecursive).plus(
  30. new Coordinate(element.offsetWidth, element.offsetHeight));
  31. },
  32. fixEvent : function(event) {
  33. event.windowCoordinate = new Coordinate(event.clientX, event.clientY);
  34. }
  35. };
  36. function Coordinate(x, y) {
  37. this.x = x;
  38. this.y = y;
  39. }
  40. Coordinate.prototype.toString = function() {
  41. return "(" + this.x + "," + this.y + ")";
  42. }
  43. Coordinate.prototype.plus = function(that) {
  44. return new Coordinate(this.x + that.x, this.y + that.y);
  45. }
  46. Coordinate.prototype.minus = function(that) {
  47. return new Coordinate(this.x - that.x, this.y - that.y);
  48. }
  49. Coordinate.prototype.distance = function(that) {
  50. var deltaX = this.x - that.x;
  51. var deltaY = this.y - that.y;
  52. return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
  53. }
  54. Coordinate.prototype.max = function(that) {
  55. var x = Math.max(this.x, that.x);
  56. var y = Math.max(this.y, that.y);
  57. return new Coordinate(x, y);
  58. }
  59. Coordinate.prototype.constrain = function(min, max) {
  60. if (min.x > max.x || min.y > max.y) return this;
  61. var x = this.x;
  62. var y = this.y;
  63. if (min.x != null) x = Math.max(x, min.x);
  64. if (max.x != null) x = Math.min(x, max.x);
  65. if (min.y != null) y = Math.max(y, min.y);
  66. if (max.y != null) y = Math.min(y, max.y);
  67. return new Coordinate(x, y);
  68. }
  69. Coordinate.prototype.reposition = function(element) {
  70. element.style["top"] = this.y + "px";
  71. element.style["left"] = this.x + "px";
  72. }
  73. Coordinate.prototype.equals = function(that) {
  74. if (this == that) return true;
  75. if (!that || that == null) return false;
  76. return this.x == that.x && this.y == that.y;
  77. }
  78. // returns true of this point is inside specified box
  79. Coordinate.prototype.inside = function(northwest, southeast) {
  80. if ((this.x >= northwest.x) && (this.x <= southeast.x) &&
  81. (this.y >= northwest.y) && (this.y <= southeast.y)) {
  82. return true;
  83. }
  84. return false;
  85. }