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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. if (this.midY < other.top)
  22. return "top";
  23. else if (this.midY > other.bottom)
  24. return "bottom";
  25. else if (this.midX < other.left)
  26. return "left";
  27. else
  28. return "right";
  29. }
  30. contains(other) {
  31. return (
  32. (this.left <= other.left && this.right >= other.right) &&
  33. (this.top <= other.top && this.bottom >= other.bottom));
  34. }
  35. get midX() {
  36. return this.pos.x + (this.size.x / 2);
  37. }
  38. get midY() {
  39. return this.pos.y + (this.size.y / 2);
  40. }
  41. get top() {
  42. return this.pos.y;
  43. }
  44. set top(n) {
  45. this.pos.y = n;
  46. }
  47. get pixelTop() {
  48. return this.pos.pixelY;
  49. }
  50. set pixelTop(y) {
  51. this.pos.pixelY = y;
  52. }
  53. resizeTopTo(y) {
  54. let diff = y - this.top;
  55. this.top = y;
  56. this.size.y += diff;
  57. }
  58. pixelResizeTopTo(y) {
  59. let diff = y - this.pixelTop;
  60. this.pixelTop = y;
  61. this.size.pixelY += diff;
  62. }
  63. get bottom() {
  64. return this.pos.y + this.size.y;
  65. }
  66. set bottom(y) {
  67. this.pos.y = y - this.size.y;
  68. }
  69. get pixelBottom() {
  70. return this.pos.pixelY + this.size.pixelY;
  71. }
  72. set pixelBottom(y) {
  73. this.pos.pixelY = y + this.size.pixelY;
  74. }
  75. resizeBottomTo(y) {
  76. let diff = y - this.bottom;
  77. this.size.y += diff;
  78. }
  79. pixelResizeBottomTo(y) {
  80. let diff = y - this.pixelBottom;
  81. this.size.pixelY += diff;
  82. }
  83. get left() {
  84. return this.pos.x;
  85. }
  86. set left(x) {
  87. this.pos.x = x;
  88. }
  89. get pixelLeft() {
  90. return this.pos.pixelX;
  91. }
  92. set pixelLeft(x) {
  93. this.pos.pixelX = x;
  94. }
  95. resizeLeftTo(x) {
  96. let diff = x - this.left;
  97. this.left = x;
  98. this.size.x += diff;
  99. }
  100. pixelResizeLeftTo(x) {
  101. let diff = x - this.pixelLeft;
  102. this.pixelLeft = x;
  103. this.size.pixelX += diff;
  104. }
  105. get right() {
  106. return this.pos.x + this.size.x;
  107. }
  108. set right(n) {
  109. this.pos.x = n - this.size.x;
  110. }
  111. get pixelRight() {
  112. return this.pos.pixelX + this.size.pixelX;
  113. }
  114. set pixelRight(x) {
  115. this.pos.pixelX = x + this.size.pixelX;
  116. }
  117. resizeRightTo(x) {
  118. let diff = x - this.right;
  119. this.size.x += diff;
  120. }
  121. pixelResizeRightTo(x) {
  122. let diff = x - this.pixelRight;
  123. this.size.pixelX += diff;
  124. }
  125. }