You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rect.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Vec2 from "./Vec2.js";
  2. export default class Rect {
  3. constructor(pos = new Vec2(), size = new Vec2()) {
  4. this.pos = pos;
  5. this.size = size;
  6. }
  7. draw(ctx) {
  8. ctx.moveTo(this.pixelLeft, this.pixelTop);
  9. ctx.lineTo(this.pixelRight, this.pixelTop);
  10. ctx.lineTo(this.pixelRight, this.pixelBottom);
  11. ctx.lineTo(this.pixelLeft, this.pixelBottom);
  12. ctx.closePath();
  13. ctx.stroke();
  14. }
  15. intersects(other) {
  16. return (
  17. (this.left < other.right && this.right > other.left) &&
  18. (this.top < other.bottom && this.bottom > other.top));
  19. }
  20. intersectSide(other) {
  21. let mx = this.midX;
  22. let my = this.midY;
  23. let diffleft = mx - other.pos.x;
  24. let diffright = mx - (other.pos.x + other.size.x);
  25. let difftop = my - (other.pos.y + other.size.y);
  26. if (diffleft <= 0)
  27. return "left";
  28. else if (diffright >= 0)
  29. return "right";
  30. else if (difftop >= 0)
  31. return "bottom";
  32. else
  33. return "top";
  34. }
  35. contains(other) {
  36. return (
  37. (this.left <= other.left && this.right >= other.right) &&
  38. (this.top <= other.top && this.bottom >= other.bottom));
  39. }
  40. get midX() {
  41. return this.pos.x + (this.size.x / 2);
  42. }
  43. get midY() {
  44. return this.pos.y + (this.size.y / 2);
  45. }
  46. get top() {
  47. return this.pos.y;
  48. }
  49. set top(n) {
  50. this.pos.y = n;
  51. }
  52. get pixelTop() {
  53. return this.pos.pixelY;
  54. }
  55. set pixelTop(y) {
  56. this.pos.pixelY = y;
  57. }
  58. resizeTopTo(y) {
  59. let diff = y - this.top;
  60. this.top = y;
  61. this.size.y += diff;
  62. }
  63. pixelResizeTopTo(y) {
  64. let diff = y - this.pixelTop;
  65. this.pixelTop = y;
  66. this.size.pixelY += diff;
  67. }
  68. get bottom() {
  69. return this.pos.y + this.size.y;
  70. }
  71. set bottom(y) {
  72. this.pos.y = y - this.size.y;
  73. }
  74. get pixelBottom() {
  75. return this.pos.pixelY + this.size.pixelY;
  76. }
  77. set pixelBottom(y) {
  78. this.pos.pixelY = y + this.size.pixelY;
  79. }
  80. resizeBottomTo(y) {
  81. let diff = y - this.bottom;
  82. this.size.y += diff;
  83. }
  84. pixelResizeBottomTo(y) {
  85. let diff = y - this.pixelBottom;
  86. this.size.pixelY += diff;
  87. }
  88. get left() {
  89. return this.pos.x;
  90. }
  91. set left(x) {
  92. this.pos.x = x;
  93. }
  94. get pixelLeft() {
  95. return this.pos.pixelX;
  96. }
  97. set pixelLeft(x) {
  98. this.pos.pixelX = x;
  99. }
  100. resizeLeftTo(x) {
  101. let diff = x - this.left;
  102. this.left = x;
  103. this.size.x += diff;
  104. }
  105. pixelResizeLeftTo(x) {
  106. let diff = x - this.pixelLeft;
  107. this.pixelLeft = x;
  108. this.size.pixelX += diff;
  109. }
  110. get right() {
  111. return this.pos.x + this.size.x;
  112. }
  113. set right(n) {
  114. this.pos.x = n - this.size.x;
  115. }
  116. get pixelRight() {
  117. return this.pos.pixelX + this.size.pixelX;
  118. }
  119. set pixelRight(x) {
  120. this.pos.pixelX = x + this.size.pixelX;
  121. }
  122. resizeRightTo(x) {
  123. let diff = x - this.right;
  124. this.size.x += diff;
  125. }
  126. pixelResizeRightTo(x) {
  127. let diff = x - this.pixelRight;
  128. this.size.pixelX += diff;
  129. }
  130. }